gpt4 book ai didi

c# - 具有不同内核的指数移动平均线

转载 作者:太空宇宙 更新时间:2023-11-03 18:15:33 25 4
gpt4 key购买 nike

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

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

在 C# 中:

out[1] = values[1];
for (i in 2:N(X)) {
tmp = (times[i] - times[i-1]) / tau;
w = exp(-tmp);
w2 = (1 - w) / tmp;
out[i] = out[i-1] * w + values[i] * (1 - w2) + values[i-1] * (w2 - w);
}

在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)
])

我希望能够指定不同的内核,但不确定如何按照此处所述进行操作: EMA kernel MA这一切都完成了,所以我最终可以重新创建这里给出的移动差值: enter image description here

感谢您提供的任何帮助

最佳答案

这里一种可能的方法是使用返回内核的方法。

据我所知,此方法的输入将是 kerneltypeiotherInputs

一个简单的方法是:

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

这当然非常非常粗糙,并且可以进行很多改进,但其目的只是为了传达要点。

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

关于c# - 具有不同内核的指数移动平均线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25070796/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com