C# 不同核函数的指数移动平均

C# 不同核函数的指数移动平均,c#,python,math,statistics,smoothing,C#,Python,Math,Statistics,Smoothing,我试图复制一些公式,但在将数学转换为代码时遇到困难 这是简单的指数移动平均线 在c#中: 在Python中: mu = numpy.exp ((ts[1] - ts[0]) / self.tau) nu = 1.0 - mu return numpy.array ([ mu * el + nu * arr[0] for el, arr in zip (last, arrays) ]) 我希望能够指定不同的内核,但不确定如何按照此处所述进行: 所有这些都完成了,因此我最终可以重新创建此处给出的

我试图复制一些公式,但在将数学转换为代码时遇到困难

这是简单的指数移动平均线

在c#中:

在Python中:

mu = numpy.exp ((ts[1] - ts[0]) / self.tau)
nu = 1.0 - mu
return numpy.array ([
mu * el + nu * arr[0] for el, arr in zip (last, arrays)
])
我希望能够指定不同的内核,但不确定如何按照此处所述进行: 所有这些都完成了,因此我最终可以重新创建此处给出的移动微分:


感谢您提供的帮助这里一个可能的方法是使用一个返回内核的方法

据我所见,此方法的输入将是
kerneltype
I
,以及
otherInputs

一个简单的方法是:

for(int i = 1; i < values.length(); i++)
{
    tmp = (times[i] - times[i-1]) / tau;
    //w = exp(-tmp);
    //w2 = (1 - w) / tmp;
    List<Object> kernelInputsInital = new List<Object>();
    kernelInputsInitial.Add(tmp);  //takes in the first argument
    kernelInputsInitial.Add(true); //expected to calculate the first
    w = GetKernel(KernelType.Exponential, i, kernelInputsInitial);

    List<Object> kernelInputsSecondTerm = new List<Object>();
    kernelInputsSecondTerm.Add(w);  //takes in the first argument
    kernelInputsSecondTerm.Add(false); //expected to calculate the first
    w2 = GetKernel(KernelType.Exponential, i, kernelInputsInitial);

    out[i] = out[i-1] * w + values[i] * (1 - w2) + values[i-1] * (w2 - w);
    ....
}
for(int i=1;i
这当然是非常、非常粗糙的,可以做很多改进,但它只是为了让大家明白这一点

我将使用一个接口来表示内核,并为每个内核派生类。根据我的经验,这会产生足够可读和可维护的代码,但总有改进的余地

for(int i = 1; i < values.length(); i++)
{
    tmp = (times[i] - times[i-1]) / tau;
    //w = exp(-tmp);
    //w2 = (1 - w) / tmp;
    List<Object> kernelInputsInital = new List<Object>();
    kernelInputsInitial.Add(tmp);  //takes in the first argument
    kernelInputsInitial.Add(true); //expected to calculate the first
    w = GetKernel(KernelType.Exponential, i, kernelInputsInitial);

    List<Object> kernelInputsSecondTerm = new List<Object>();
    kernelInputsSecondTerm.Add(w);  //takes in the first argument
    kernelInputsSecondTerm.Add(false); //expected to calculate the first
    w2 = GetKernel(KernelType.Exponential, i, kernelInputsInitial);

    out[i] = out[i-1] * w + values[i] * (1 - w2) + values[i-1] * (w2 - w);
    ....
}