• # Standard Deviation Calculation - Meta Data Point Error

Recently I successfully implemented the basic statistical calculations (min/max/average) by creating a Meta data source, scripting the data-point and then publishing via Modbus.

I have had some difficulty putting together a script for calculating the standard deviation. The system returns a black question mark diamond icon as a data point place holder, and continues to fill the historical values table unlike when there is a bad/no data source. I am not sure whether the error icon is indicative of NaN, out of range, etc (new to mango).

I tested the script incrementally and only towards the end of the calculation does this error occur, maybe a problem with the division & square root being to small?

If anyone could provide clarification on this error icon, or point me towards the right documentation section that would be very helpful!

Thanks!

Script is below:

``````var average     = p6167.past(MINUTE, 5).average; //Get Average
var valueArray  = p6167.last(300);              //Get Last 300 Points(5 min)
var Power_StdDev = 0;                           //reset std devation

for (var i=0; i<300; i++){                  //loop through points
var value_i = valueArray.get(i).value;	//get array(i) value
var DiffSquared = (value_i - average)^2;    //take the diff & square
var sumDiffSquared = DiffSquared + sumDiffSquared;       //sum of squares
var sumDiffSQuotient = sumDiffSquared/299;                    // divide by n-1
Power_StdDev = Math.sqrt(sumDiffSQuotient);                  // set std to calculated value
}
return Power_StdDev;                   // return to numeric meta data point
``````
• Your power calculation is incorrect at first glance, use Math.pow(num,exp).
The ^ I believe is treated as a bitwise exclusive OR.
Also did the mango JavaScript provide any info as there is statistical analysis available.
Look at the help icon and have a glance there.

Also:

``````var sumDiffSQuotient = sumDiffSquared/299;
Power_StdDev = Math.sqrt(sumDiffSQuotient);                  // set std to calculated value

``````

Take these outside of the loop!

``````Var sumDiffSquared=0;
for...
{..}
var sumDiffSQuotient = sumDiffSquared/299;
Power_StdDev = Math.sqrt(sumDiffSQuotient);                  // set std to calculated value

``````

You reset their values every time your for loop runs, you may as well wait till after the loop since you're not adding those calculations inside the loop and only run them once.

Fox

• Good catches, Fox!

Hi HSAcontrols, welcome to the forum!

I think Fox may have set you on the right path for where the calculation may be going wrong. The only think I have to add is that the black question mark diamond is indeed a NaN value being rendered. I'd perhaps have to see a screenshot to be sure, but that's what it sounds like.

I tested the script incrementally and only towards the end of the calculation does this error occur, maybe a problem with the division & square root being to small?

NaN is a very infectious value. If one of the point values you're looping over is NaN, then doing any mathematical operation on it or with it will also produce NaN. You could check with an `if(!isNaN(value_i)) {}` around the calculations for DiffSquared and sumDiffSquared

• Thanks Phil, just thought I could give my ten cents whilst out and about.

• Thanks guys, appreciate the help. Managed to get it all working, the error handling was definitely a good call.

Posts 5Views 609