【问题标题】:Python: how to generate a sample from Kernel density estimation?Python:如何从核密度估计中生成样本?
【发布时间】: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


    【解决方案1】:

    您正在使用“数据”作为新样本空间的样本空间。如上所述,“数据”具有每月价格,因此不确定这将如何给出百分比。更好的选择是使用返回数据框来分发 new_data。

    new_data=returns.sample(60,random_state=0)
    

    【讨论】:

      猜你喜欢
      • 2019-12-21
      • 2020-04-23
      • 2018-12-19
      • 2017-02-11
      • 2020-05-30
      • 1970-01-01
      • 2013-07-23
      • 2017-02-06
      • 2019-02-14
      相关资源
      最近更新 更多