Scipy: 能否将指数加权纳入分布拟合?
Scipy: Can you incorporate exponential weighting into distribution fitting?
给定一些时间序列数据:
np.random.seed(123)
r = pd.Series(np.random.beta(a=0.5, b=0.5, size=1000),
index=pd.date_range('2013', periods=1000))
和 scipy.stats._continuous_distns._distn_names
内的分布:
import scipy.stats as scs
dists = scs._continuous_distns._distn_names
我希望能够建立一个新的分布,然后将其称为 .ppf
(百分比函数),同时将指数权重纳入分布的构建中。
例如,对于 normal 分布,这只需要估计指数加权平均值和标准差:
All continuous distributions take loc and scale as keyword parameters
to adjust the location and scale of the distribution, e.g. for the
standard normal distribution the location is the mean and the scale is
the standard deviation. [source]
ewm = r.ewm(span=60)
loc = ewm.mean().iloc[-1]
scale = ewm.std().iloc[-1]
print(scs.norm.ppf(q=0.05, loc=loc, scale=scale))
-0.196734019969
但我希望能够将其扩展到更广泛的连续分布系列,其中经常涉及其他参数 (shape
)。例如,
我如何将此过程扩展到 distributions,它具有 loc
和 scale
之外的参数?
上面的组合片段:
import scipy.stats as scs
import numpy as np
import pandas as pd
np.random.seed(123)
r = pd.Series(np.random.beta(a=0.5, b=0.5, size=1000),
index=pd.date_range('2013', periods=1000))
ewm = r.ewm(span=60)
loc = ewm.mean().iloc[-1]
scale = ewm.std().iloc[-1]
print(scs.norm.ppf(q=0.05, loc=loc, scale=scale))
# -0.196734019969
这是我的实现:
- 给定经验分布
x
,为每个 x
分配指数权重。
- 使用这些权重 bootstrap 对新分布进行抽样。权重是
p
到 np.random.choice
的参数。
- 然后可以对该 bootstrapped 数据调用任何分布的
.fit
方法。
代码:
def ewm_weights(i, alpha):
w = (1 - alpha) ** np.arange(i)[::-1]
w /= w.sum()
return w
def bootstrap(a, alpha, size=None):
p = ewm_weights(i=len(a), alpha=alpha)
return np.random.choice(a=a, size=size, p=p)
ewm_weights
的定义如下:
http://pandas.pydata.org/pandas-docs/stable/computation.html#exponentially-weighted-windows
与 adjust=True
.
示例:
# Create a nonstationary `x` variable with larger mean and stdev in period 2
x1 = np.random.normal(loc=4, scale=3, size=1000)
x2 = np.random.normal(loc=10, scale=5, size=1000)
x = np.hstack((x1,x2))
x
的直方图如下所示:
plt.hist(x, bins=25)
虽然 bootstrapped b
与 alpha=0.03
看起来像:
b = bootstrap(x, alpha=0.03, size=int(1e6))
plt.hist(b, bins=25)
来自 scipy.stats._continuous_distns._distn_names
的任何连续分布都可以适合 b
。
问题:
- softmax 函数可能会使
ewm_weights
更安全。
- 这种方法忽略了
x
中的自相关。
给定一些时间序列数据:
np.random.seed(123)
r = pd.Series(np.random.beta(a=0.5, b=0.5, size=1000),
index=pd.date_range('2013', periods=1000))
和 scipy.stats._continuous_distns._distn_names
内的分布:
import scipy.stats as scs
dists = scs._continuous_distns._distn_names
我希望能够建立一个新的分布,然后将其称为 .ppf
(百分比函数),同时将指数权重纳入分布的构建中。
例如,对于 normal 分布,这只需要估计指数加权平均值和标准差:
All continuous distributions take loc and scale as keyword parameters to adjust the location and scale of the distribution, e.g. for the standard normal distribution the location is the mean and the scale is the standard deviation. [source]
ewm = r.ewm(span=60)
loc = ewm.mean().iloc[-1]
scale = ewm.std().iloc[-1]
print(scs.norm.ppf(q=0.05, loc=loc, scale=scale))
-0.196734019969
但我希望能够将其扩展到更广泛的连续分布系列,其中经常涉及其他参数 (shape
)。例如,
我如何将此过程扩展到 distributions,它具有 loc
和 scale
之外的参数?
上面的组合片段:
import scipy.stats as scs
import numpy as np
import pandas as pd
np.random.seed(123)
r = pd.Series(np.random.beta(a=0.5, b=0.5, size=1000),
index=pd.date_range('2013', periods=1000))
ewm = r.ewm(span=60)
loc = ewm.mean().iloc[-1]
scale = ewm.std().iloc[-1]
print(scs.norm.ppf(q=0.05, loc=loc, scale=scale))
# -0.196734019969
这是我的实现:
- 给定经验分布
x
,为每个x
分配指数权重。 - 使用这些权重 bootstrap 对新分布进行抽样。权重是
p
到np.random.choice
的参数。 - 然后可以对该 bootstrapped 数据调用任何分布的
.fit
方法。
代码:
def ewm_weights(i, alpha):
w = (1 - alpha) ** np.arange(i)[::-1]
w /= w.sum()
return w
def bootstrap(a, alpha, size=None):
p = ewm_weights(i=len(a), alpha=alpha)
return np.random.choice(a=a, size=size, p=p)
ewm_weights
的定义如下:
http://pandas.pydata.org/pandas-docs/stable/computation.html#exponentially-weighted-windows
与 adjust=True
.
示例:
# Create a nonstationary `x` variable with larger mean and stdev in period 2
x1 = np.random.normal(loc=4, scale=3, size=1000)
x2 = np.random.normal(loc=10, scale=5, size=1000)
x = np.hstack((x1,x2))
x
的直方图如下所示:
plt.hist(x, bins=25)
虽然 bootstrapped b
与 alpha=0.03
看起来像:
b = bootstrap(x, alpha=0.03, size=int(1e6))
plt.hist(b, bins=25)
来自 scipy.stats._continuous_distns._distn_names
的任何连续分布都可以适合 b
。
问题:
- softmax 函数可能会使
ewm_weights
更安全。 - 这种方法忽略了
x
中的自相关。