-1

How to compute the cumulative product (or sum) of a Series in Deedle (ideally in c# rather than F#)? Anything smart with Windowing / Aggregation?

Just a reference, i want to code using deedle the equivalent of this line of code in python/pandas

curve['equity_curve'] = (1.0+curve['returns']).cumprod()

Thanks a ton

Cheers, Lamp'

lampalork
  • 40
  • 4

1 Answers1

3

Deedle has cummulative sum function built-in (Stats.expandingSum), but it does not have expanding multiplication. I think the easiest option is to implement Scan method for ordinary IEnumerable and then use it as follows:

 var result = input.Observations.Scan
   (KeyValue.Create(-1, 1.0), (acc, kvp) =>
      KeyValue.Create(kvp.Key, acc.Value * kvp.Value)).Skip(1).ToSeries();

For completeness, the Scan extension method looks something like this:

public static IEnumerable<U> Scan<T, U>
    (this IEnumerable<T> input, U state, Func<U, T, U> next) {
  yield return state;
  foreach (var item in input) {
    state = next(state, item);
    yield return state;
  }
}
Community
  • 1
  • 1
Tomas Petricek
  • 240,744
  • 19
  • 378
  • 553