计算优化权重以最大化相关性 [英] Calculating optimized weights to maximize correlation
问题描述
我有两个时间序列数据,A 列和 B 列.
I have two time series data, columns A and B.
我正在计算 A 列上不同持续时间的滚动移动平均线.例如 (5,10,15,20).
I am computing rolling moving averages of different duration on column A. For example (5,10,15,20).
我想为这些平均列中的每一个分配权重,以便权重和平均列的总和与列 B 的相关性最大.换句话说,如何在 Python 中实现类似优化的 excel.
I want to assign weights to each of these average columns so that the sumproduct of weights and average columns has maximum correlation with column B. In other words, how to implement excel like optimization in Python.
请查看示例代码并提出前进的方向.
Please have a look at the sample code and suggest the way forward.
import pandas as pd
import numpy as np
dates = pd.date_range('20130101', periods=100)
df = pd.DataFrame(np.random.randn(100, 2), index=dates, columns=list('AB'))
df['sma_5']=df['A'].rolling(5).mean()
df['sma_10']=df['A'].rolling(10).mean()
df['sma_15']=df['A'].rolling(15).mean()
df['sma_20']=df['A'].rolling(20).mean()
w=[0.25,0.25,0.25,0.25]
df['B_friend'']=
w[0]*df['sma_5']+w[1]*df['sma_10']+w[2]*df['sma_15']+w[3]*df['sma_20']
需要优化权重 'w' 以最大化相关性.
Need to optimize the weights 'w' to maximize the correlation.
df['B'].corr(df['B_friend'])
提前致谢.
推荐答案
scipy.optimize.minimize
函数看起来像你需要的:https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize.html#scipy.optimize.minimize
scipy.optimize.minimize
function looks like what you need: https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize.html#scipy.optimize.minimize
代码看起来像这样:
import pandas as pd
import numpy as np
import scipy.optimize as opt
dates = pd.date_range('20130101', periods=100)
df = pd.DataFrame(np.random.randn(100, 2), index=dates, columns=list('AB'))
df['sma_5']=df['A'].rolling(5).mean()
df['sma_10']=df['A'].rolling(10).mean()
df['sma_15']=df['A'].rolling(15).mean()
df['sma_20']=df['A'].rolling(20).mean()
def fun(x):
w = x
B_friend=w[0]*df['sma_5']+w[1]*df['sma_10']+w[2]*df['sma_15']+w[3]*df['sma_20']
# -np.abs(corr) instead of just corrr is used
# in order to turn a maximization problem into a
# minimization problem
return -np.abs(df['B'].corr(B_friend))
w=[0.25,0.25,0.25,0.25]
opt.minimize(fun, w)
这篇关于计算优化权重以最大化相关性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!