-
Notifications
You must be signed in to change notification settings - Fork 8.1k
First pass at adding std deviation to measure-object #4969
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
54a29cd
06dfb2f
eb541c5
ca4b539
806f3a7
c95b829
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,7 +35,7 @@ public sealed class GenericMeasureInfo : MeasureInfo | |
| /// </summary> | ||
| public GenericMeasureInfo() | ||
| { | ||
| Average = Sum = Maximum = Minimum = null; | ||
| Average = Sum = Maximum = Minimum = StdDeviation = null; | ||
| } | ||
|
|
||
| /// <summary> | ||
|
|
@@ -72,6 +72,13 @@ public GenericMeasureInfo() | |
| /// | ||
| /// </summary> | ||
| public double? Minimum { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// | ||
| /// The Standard Deviation of property values | ||
| /// | ||
| /// </summary> | ||
| public double? StdDeviation { get; set; } | ||
| } | ||
|
|
||
| /// <summary> | ||
|
|
@@ -91,7 +98,7 @@ public sealed class GenericObjectMeasureInfo : MeasureInfo | |
| /// </summary> | ||
| public GenericObjectMeasureInfo() | ||
| { | ||
| Average = Sum = null; | ||
| Average = Sum = StdDeviation = null; | ||
| Maximum = Minimum = null; | ||
| } | ||
|
|
||
|
|
@@ -129,6 +136,13 @@ public GenericObjectMeasureInfo() | |
| /// | ||
| /// </summary> | ||
| public object Minimum { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// | ||
| /// The Standard Deviation of property values | ||
| /// | ||
| /// </summary> | ||
| public double? StdDeviation { get; set; } | ||
| } | ||
|
|
||
|
|
||
|
|
@@ -227,6 +241,8 @@ private class Statistics | |
|
|
||
| // Generic/Numeric statistics | ||
| internal double sum = 0.0; | ||
| internal double stdDeviation = 0.0; | ||
| internal List<double> stdDeviationNumbers = new List<double>(); | ||
| internal object max = null; | ||
| internal object min = null; | ||
|
|
||
|
|
@@ -265,6 +281,27 @@ public MeasureObjectCommand() | |
|
|
||
| #endregion Common parameters in both sets | ||
|
|
||
| /// <summary> | ||
| /// Set to true if Standard Deviation is to be returned | ||
| /// </summary> | ||
| /// <value></value> | ||
| [Parameter(ParameterSetName = GenericParameterSet)] | ||
| public SwitchParameter StdDeviation | ||
| { | ||
| get | ||
| { | ||
| return _measureStdDeviation; | ||
| } | ||
| set | ||
| { | ||
| _measureStdDeviation = value; | ||
| if(value == true) | ||
| _measureAverage = true; | ||
| } | ||
| } | ||
|
|
||
| private bool _measureStdDeviation; | ||
|
|
||
| /// <summary> | ||
| /// Set to true is Sum is to be returned | ||
| /// </summary> | ||
|
|
@@ -713,6 +750,8 @@ private void AnalyzeNumber(double numValue, Statistics stat) | |
| { | ||
| if (_measureSum || _measureAverage) | ||
| stat.sum += numValue; | ||
| if (_measureStdDeviation) | ||
| stat.stdDeviationNumbers.Add(numValue); | ||
| } | ||
|
|
||
| /// <summary> | ||
|
|
@@ -793,15 +832,41 @@ private MeasureInfo CreateGenericMeasureInfo(Statistics stat, bool shouldUseGene | |
| { | ||
| double? sum = null; | ||
| double? average = null; | ||
| double? stdDeviation = null; | ||
| object max = null; | ||
| object min = null; | ||
|
|
||
| if (!_nonNumericError) | ||
| { | ||
| if (_measureSum) | ||
| sum = stat.sum; | ||
|
|
||
| if (_measureAverage && stat.count > 0) | ||
| average = stat.sum / stat.count; | ||
|
|
||
| if(_measureStdDeviation && !_measureAverage) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it must be Diagnostic.Assert().
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With different parameter sets, this code is not needed. |
||
| ErrorRecord errorRecord = new ErrorRecord( | ||
| PSTraceSource.NewArgumentException("Average"), | ||
| "AverageSwitchNotSet", | ||
| ErrorCategory.InvalidArgument, | ||
| null); | ||
|
|
||
| errorRecord.ErrorDetails = new ErrorDetails(this, "MeasureObjectStrings", "AverageSwitchNotSet", "Average"); | ||
| WriteError(errorRecord); | ||
| } | ||
|
|
||
| if (_measureStdDeviation && _measureAverage && stat.count > 0) | ||
| { | ||
| var sumOfDerivation = 0.0; | ||
|
|
||
| foreach (double n in stat.stdDeviationNumbers) | ||
| { | ||
| var m = n - (double)average; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we move the cast out of foreach ? |
||
| sumOfDerivation += m * m; | ||
| } | ||
|
|
||
| stdDeviation = Math.Round(Math.Sqrt(sumOfDerivation / (stat.stdDeviationNumbers.Count - 1)), 4); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please clarify why we round? I believe it must be a double.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I rounded for no particular reason. I can remove it. |
||
| } | ||
| } | ||
|
|
||
| if (_measureMax) | ||
|
|
@@ -838,6 +903,7 @@ private MeasureInfo CreateGenericMeasureInfo(Statistics stat, bool shouldUseGene | |
| gmi.Count = stat.count; | ||
| gmi.Sum = sum; | ||
| gmi.Average = average; | ||
| gmi.StdDeviation = stdDeviation; | ||
| if (null != max) | ||
| { | ||
| gmi.Maximum = (double)max; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
stddeviation requires _measureAverage, so I think we should set _measureAverage = true. If _measureAverage = false, perhaps we should error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense. Where should it throw, in
CreateGenericMeasureInfo?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I think that's the right place.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder why we request Average parameter if we can set _measureAverage = true here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thinking about this, it seems that
-Average:$false -StdDeviationshould not be a valid combination and perhaps a better way to solve this is to make them different parameter sets. The problem here is that depending on which one is set first, you may get unintended behavior-StdDeviation -Average:$falsesince the code here overwrites_measureAverage.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need new flag to process a sum. We can set it in BeginProcessing.