Jump to content

Metric Calculations

Recommended Posts


I am looking to calculate some metrics on a loop - error, total control output travel, and number of reversals/changes.

Error: image.png.a10ceefea7d6e82afed480314699a031.png

Total Travel: image.png.df78bd122d3b33394285c04cf7d0ca50.png

The error needs to sum up the difference between two values over a time period and divide it by the total number of values used in that time period. 

The total travel would sum up all the movements of a number over the day (for eg if a value went from 10->15->20 total travel would be 10).

Number of reversals/changes is as stated how many times a signal changes direction/value.

I noticed that for some signals, the total count can be different depending on how often the value changes. Is there a simple way to pull the value of a number at a specific time stamp? And then to continue on, to do that over a capsule period ie calculating average over a 1 day timeframe?

Thank you,



Edit 1: I think I solved the total travel. I used a combination of running delta and running sum bounded within a capsule


Edited by norman47
  • Like 1
Link to comment
Share on other sites

If you'd like to make the number of samples consistent over time, I'd use the resample() function in a formula. $signal.resample(1min) If you pass resample() a period such as 1min, the result is a signal that will have a sample every 1 minute, regardless of the input sample frequency.

To compute the error, I would create a new signal which is the difference between PV and SP. Then create a periodic condition for which you'd like to sum up this value and use the 'Signal from Condition' tool to sum the new signal over the given condition. To count the number of values in a period, use the 'Signal from Condition' tool to count the number of samples in your signal using whatever periodic condition you'd like as a bounding condition. 

For the number of reversals, I'd first use .runningDelta() to create a running delta signal of the signal you'd like to count the number of reversals for. I'd then create a condition using the 'Value Search' tool for anytime the delta signal is greater than or equal to zero, and another condition for anytime the delta signal is less than zero. Then, using the same method as above, create a periodic signal over which you'd like to calculate the number of reversals and use the 'Signal from Condition' tools to count the number of capsules within a given period. The total number of reversals should equal the number of >= 0 capsules plus number of <0 capsules minus 1.

You could do this all in one formula if you'd like.

$delta = $cv.runningDelta()

//Conditions when delta >= 0 & < 0
$gte = $delta >= 0
$lt = $delta < 0

//Condition when to total the number of reversals
$period = periods(10min)

//Signal for total number of $gte/$lt conditions
$gteCount = $gte.aggregate(count(), $period, startKey())
$ltCount = $lt.aggregate(count(), $period, startKey())

//Signal for total number of reversals
$reversals = $gteCount + $ltCount - 1		

//Output the reversals signal with Step interpolation

And here's some of the outputs.







  • Like 2
Link to comment
Share on other sites

Hey Andrew,

Thank you very much for the response! I did end up computing the error in the way you described, breaking it down step by step.

Your reversal count also works really well. I just have one question - I tried to replace the $period with a daily periodic condition I already made that starts at 6AM, but then the formula does not run and says it is unbound. Is there a reason why trying to replace $period in the formula with an already made periodic condition breaks it?

Edit: In addition, would that formula not also count anytime the signal still goes up? Say it goes from 5 -> 10 -> 15 -> 10. in that sequence, there is only 1 reversal, but I think the formula above would count a total of 3.

Thank you,


Edited by norman47
  • Like 2
Link to comment
Share on other sites

I believe the unbounded error is because the original code creates conditions for >=0 and <0 with no maximum duration. You can change $gte and $lte to have max durations and this may fix the issue.

$gte = ($delta >= 0).setMaximumDuration(7d)
$lt = ($delta < 0).setMaximumDuration(7d)

In your example, the reversal count will not count 3, it will count one. Let's transform your example as runningDelta() would and assume the previous value was also 5. 

5 -> 5 -> 10 -> 15 -> 10 becomes

   0 ->  5 ->  5 ->  -5

Now let's identify the gte and lt conditions. We would see an $gte {0 -> 5 -> 5->}, and an $lt at {-> -5}. Use our formula and the result would be 1 reversal. 

However this does bring up a potential issue. If the example had another 10 at the end, the delta samples would become  0 ->  5 ->  5 ->  -5 -> 0 and that would create an additional $gte capsule at the very end, resulting in 2 reversals. You could fix this by change the $gte to only greater than instead of greater than or equal zero.



  • Like 3
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...