4

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?

Mark Lakata
  • 19,989
  • 5
  • 106
  • 123
makerofthings7
  • 60,103
  • 53
  • 215
  • 448
  • This doesn't sound programming-related. It's not a well-specified problem in any case. – Noldorin Jan 27 '11 at 17:10
  • its programming related (how do i do X in C#), but the question would be better if it had a lot more information, like "i have these x values, or this array of floats and...". – John Gardner Jan 28 '11 at 18:14
  • More background on this problem is located here: [Ideas wanted for analyzing near-realtime data over specific intervals with memory/cpu efficiency](http://stackoverflow.com/questions/4813556/ideas-wanted-for-analyzing-near-realtime-data-over-specific-intervals-with-memory "... lots of C# code there.") – makerofthings7 Jan 28 '11 at 18:54

4 Answers4

4

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

Mark Lakata
  • 19,989
  • 5
  • 106
  • 123
3

Rate of change is calculated as follows

  1. Calculate a delta such as "price minus - price 20 days ago"
  2. Calculate rate of change such as "delta / price 99 days ago"
robert
  • 31
  • 1
3

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#.

Tomas Aschan
  • 58,548
  • 56
  • 243
  • 402
  • I 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*. – Tomas Aschan Jan 27 '11 at 17:20
  • 1
    As long as it's close, that's fine. – makerofthings7 Jan 27 '11 at 17:25
0

Total rate of change, i.e. (new_value - original_value)/time?

Chris Shain
  • 50,833
  • 6
  • 93
  • 125