gpt4 book ai didi

algorithm - 如何设计一种算法来查找多个变量的协方差?

转载 作者:行者123 更新时间:2023-12-02 21:35:53 25 4
gpt4 key购买 nike

以下是两个变量ABC和XYZ的协方差方程。

covariance equation for two variables

下面是找到ABC和XYZ之间的协方差的算法。

Algorithm 1: Calculate Covariance
1. cov = 0
2. for i = 0 to size - 1
3. cov = cov + (return_abc[i] - avg_abc) * (return_xyz[i] - avg_xyz)
4. end for
5. cov = cov / size - 1
6. end

如何设计一种算法来查找样本中一个变量与其余变量之间的协方差?
例如,如何找到APPLE的股票与纳斯达克的其余股票(约3,100家公司)之间的协方差?

最佳答案

http://www.investopedia.com/articles/financial-theory/11/calculating-covariance.asp

如果您仅查看日末价格(为简单起见),并且希望将单个股票与其余所有股票进行比较,请尝试将其余全部视为一个元素,即:

n=number of days to analyze
marketReturn = average(return of each stock that isn't APPLE)
marketAverage = (sum of marketReturn's)/n
appleAverage = (sum of APPLE returns)/n

然后将总和应用于您要分析的天数的样本量。
Answer = (((day 1 APPLE return %)-(appleAverage))*((day 1 marketReturn %)-(marketAverage)) + ((day 2 APPLE return %)-(appleAverage))*((day 2 marketReturn %)-(marketAverage)) + ...)/(n-1)

我想如果您要单独检查每个库存:
for(x=0;x<numStocksNotAPPLE;++x)//increment through each stock
{
covariantList[x]=(((day 1 APPLE return percent)-(appleAverage))*((day 1 stock[x] percent)-(stock[x] average)) + ... + (((day n APPLE return %)-(appleAverage))*((day n stock[x] %)-(stock[x] average)))/(n-1)
}//that should give you a covariant APPLE vs each stock

然后,如果只需要一个数字,则可以取covariantList的平均值:
foreach(covariantList c)
{
sum+=c
}

averageCovariant = sum/covariantList.size

请记住,这都是伪代码,我并不是在声称任何功能都可以

关于algorithm - 如何设计一种算法来查找多个变量的协方差?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32332751/

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