【发布时间】:2022-03-26 11:34:42
【问题描述】:
我需要下载 DJI 股票的月度价格并计算月度收益。 我有一个名为“数据”的数据框,它是股票的每月价格。然后我想使用每月收益并使用核密度估计。然后我想从中提取随机样本并将其用于生成 100 条路径(蒙特卡罗求和)。 问题是当我生成样本时,值在 20 到 200 之间。我给它的返回数据是百分比,所以我认为样本也是百分比?如何从 KDE 中获取与原始数据相对应的随机样本(返回百分比)? 'data' 有月度价格,returns 是月度收益,我认为 new_data 将是原始数据的样本,以 % 为单位
这里是代码
returns=data.pct_change(1)
returns=returns.iloc[1:]
positive_returns = returns.apply(lambda x: x[x > 0].mean())
negative_returns = returns.apply(lambda x: x[x < 0].mean())
freq = returns.apply(lambda x: float(len(x[x > 0])) / float(len(returns)))
# use kernel density estimation to estimate the monthly return distribution for each stock
params = {'bandwidth': np.logspace(-1, 1, 20)}
grid = GridSearchCV(KernelDensity(), params)
models = returns.apply(lambda x: grid.fit(x.values.reshape(-1, 1)).best_estimator_)
new_data=data.sample(60,random_state=0)
print(new_data)
【问题讨论】:
标签: python sample kernel-density