Metric Calculations

Recommended Posts

Hello,

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

Error:

Total Travel:

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,

Norman

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

abs(\$cmfo.runningDelta()).runningSum(\$d)

Edited by norman47
• 1
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
\$reversals.toStep()```

And here's some of the outputs.

Andrew

• 2
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,

Norman

Edited by norman47
• 2
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.

Thanks,

Andrew

• 3
Share on other sites

Hey Andrew,

Thanks for the quick response. That makes sense and the added max duration made it work properly now. I appreciate all the help!

Cheers,

Norman

Share on other sites

Awesome. Happy to hear!

• 1