我需要帮助将松树相关函数转换为python,我已经翻译了stdev和swma函数,但是这个函数对我来说有点混乱。
我也找到了这个解释,但不太明白如何实现它:
在python中尝试使用
pandas和.rolling(window).corr,其中window是相关系数周期,pandas允许使用rolling()计算任何滚动统计数据。松树的相关系数用:sma(y*x,length) - sma(y,length)*sma(x,length)除以stdev(length*y,length)*stdev(length*x,length),其中stdev是基于天真算法的。
此功能的松木文档:
> Correlation coefficient. Describes the degree to which two series tend
> to deviate from their sma values. correlation(source_a, source_b,
> length) → series[float] RETURNS Correlation coefficient.论辩
source_a (系列)源系列。
source_b (系列)目标系列。
length (整数)长度(条数回来)。
发布于 2020-05-28 14:20:32
使用熊猫确实是最好的选择,TA-Lib也有一个CORREL功能.为了让您更好地了解correlation函数是如何在松树中实现的,这里是一个使用numpy的python代码,请注意,这不是一个有效的解决方案。
import numpy as np
from matplotlib import pyplot as plt
def sma(src,m):
coef = np.ones(m)/m
return np.convolve(src,coef,mode="valid")
def stdev(src,m):
a = sma(src*src,m)
b = np.power(sma(src,m),2)
return np.sqrt(a-b)
def correlation(x,y,m):
cov = sma(x*y,m) - sma(x,m)*sma(y,m)
den = stdev(x,m)*stdev(y,m)
return cov/den
ts = np.random.normal(size=500).cumsum()
n = np.linspace(0,1,len(ts))
cor = correlation(ts,n,14)
plt.subplot(2,1,1)
plt.plot(ts)
plt.subplot(2,1,2)
plt.plot(cor)
plt.show()https://stackoverflow.com/questions/62041944
复制相似问题