How does one calculate the rate of change (derivative) of streaming data?
Solution 1
If you want something more sophisticated that smooths the data, you should look into a a digital filter algorithm. It's not hard to implement if you can cut through the engineering jargon. The classic method is Savitzky-Golay
If you have the last n
samples stored in an array y
and each sample is equally spaced in time, then you can calculate the derivative using something like this:
deriv = 0
coefficient = (1,-8,0,8,-1)
N = 5 # points
h = 1 # second
for i range(0,N):
deriv += y[i] * coefficient[i]
deriv /= (12 * h)
This example happens to be a N=5 filter of "3/4 (cubic/quartic)" filter. The bigger N
, the more points it is averaging and the smoother it will be, but also the latency will be higher. You'll have to wait N/2 points to get the derivative at time "now".
For more coefficients, look here at the Appendix
https://en.wikipedia.org/wiki/Savitzky%E2%80%93Golay_filter
Solution 2
You need both the data value V
and the corresponding time T
, at least for the latest data point and the one before that. The rate of change can then be approximated with Eulers backward formula, which translates into
dvdt = (V_now - V_a_moment_ago) / (T_now - T_a_moment_ago);
in C#.
Solution 3
Rate of change is calculated as follows
- Calculate a delta such as "price minus - price 20 days ago"
- Calculate rate of change such as "delta / price 99 days ago"
makerofthings7
Updated on July 03, 2022Comments
-
makerofthings7 almost 2 years
I have a stream of data that trends over time. How do I determine the rate of change using C#?
It's been a long time since calculus class, but now is the first time I actually need it (in 15 years). Now when I search for the term 'derivatives' I get financial stuff, and other math things I don't think I really need.
Mind pointing me in the right direction?
-
Tomas Aschan over 13 yearsI don't like using a capital T for time (i'd rather have it lowercase), but it looked even worse with the "sub"-notes... Also, note that this is not the exact rate of change at the current moment - it's merely an approximation.
-
makerofthings7 over 13 yearsAs long as it's close, that's fine.