如何计算Deedle中Series的累积乘积(或总和)(理想情况下是c#而不是F#)?

时间:2017-03-08 15:32:44

标签: c# deedle

如何计算Deedle中Series的累积乘积(或总和)(理想情况下是c#而不是F#)?有什么聪明的窗口/聚合?

只是一个引用,我想使用deedle代码相当于python / pandas中的这行代码

  

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

非常感谢

干杯, 灯“

1 个答案:

答案 0 :(得分:1)

Deedle内置了累积和函数(Stats.expandingSum),但它没有扩展乘法。我认为最简单的选择是实现Scan method for ordinary IEnumerable,然后按如下方式使用它:

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

为了完整性,Scan扩展方法如下所示:

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;
  }
}