• Recent
    • Tags
    • Popular
    • Register
    • Login

    Please Note This forum exists for community support for the Mango product family and the Radix IoT Platform. Although Radix IoT employees participate in this forum from time to time, there is no guarantee of a response to anything posted here, nor can Radix IoT, LLC guarantee the accuracy of any information expressed or conveyed. Specific project questions from customers with active support contracts are asked to send requests to support@radixiot.com.

    Radix IoT Website Mango 3 Documentation Website Mango 4 Documentation Website Mango 5 Documentation Website

    Standard Deviation Calculation - Meta Data Point Error

    Scripting general Discussion
    statistics calculations meta datapoint error
    3
    5
    1.7k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • H
      HSAcontrols
      last edited by

      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
      
      1 Reply Last reply Reply Quote 0
      • MattFoxM
        MattFox
        last edited by MattFox

        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

        Do not follow where the path may lead; go instead where there is no path.
        And leave a trail - Muriel Strode

        1 Reply Last reply Reply Quote 1
        • phildunlapP
          phildunlap
          last edited by

          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

          1 Reply Last reply Reply Quote 1
          • MattFoxM
            MattFox
            last edited by

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

            Do not follow where the path may lead; go instead where there is no path.
            And leave a trail - Muriel Strode

            1 Reply Last reply Reply Quote 0
            • H
              HSAcontrols
              last edited by

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

              1 Reply Last reply Reply Quote 0
              • First post
                Last post