提问人:Ranit Bagchi 提问时间:11/5/2023 最后编辑:AJ BifflRanit Bagchi 更新时间:11/6/2023 访问量:57
最小化使用 Python 查找我的 Heston Nandi 模型参数的对数可能性
minimizing log likelihood to find the parameters for my heston nandi model using python
问:
我有一个目标函数,我想使用 MLE 来查找模型的参数。这是我到目前为止的代码。此外,我的returnarr是股票的日志返回数组 以下是我的 returnarr 的前 30 个值,仅供参考:
returnarr = filterdata['logreturn'].to_numpy(dtype=np.longdouble)
returnarr[0:30]
array([ 0.00075018, -0.00696353, -0.00080357, 0.00287779, -0.00384807,
-0.00496853, 0.00608317, -0.00252242, -0.00731938, 0.01879965,
-0.00673696, -0.00757294, -0.02209292, 0.01152935, 0.00609485,
0.004072 , -0.01832714, -0.01604845, 0.0059958 , 0.00251289,
0.00740885, -0.01594026, 0.0076436 , -0.00298763, 0.00460656,
-0.00237508, 0.0078305 , 0.003722 , 0.00649657])
import numpy as np
from scipy.optimize import minimize
# Define the log-likelihood function
def log_likelihood(parameters,returnarr):
size=len(returnarr)
phi = parameters[0]
w = parameters[1]
B = parameters[2]
alpha = parameters[3]
lambda1=parameters[4]
epsilon = np.zeros(size, dtype='longdouble')
variance = np.zeros(size + 1, dtype='longdouble')
variance[0]=0.001
for x,val in enumerate(returnarr):
epsilon[x] = (val - (rate + (lambda1 * variance[x]))) / np.sqrt(variance[x])
variance[x+1] = w + (B * variance[x]) + (alpha * np.square(epsilon[x]-(phi*np.sqrt(variance[x]))))
variance=variance[:-1]#remove last term
log_likelihood_on_return = -0.5 * np.sum(np.log(variance) +np.square(epsilon))
return log_likelihood_on_return # Minimize the negative log-likelihood
关于如何使用最小化在此处查找参数的任何建议?
我试过这个:
initial_parameters=[0.01,0.01,0.01,0.01,0.01]
bounds = [(0, 1), (0, 1), (0, 1), (0, 1), (0, 1)]
result = minimize(log_likelihood, initial_parameters, args=(returnarr,),bounds=bounds)
我仍然收到错误,上面写着
RuntimeWarning: overflow encountered in square
variance[x+1] = w + (B * variance[x]) + (alpha * np.square(epsilon[x]-(phi*np.sqrt(variance[x]))))
RuntimeWarning: invalid value encountered in double_scalars
epsilon[x] = (val - (rate + (lambda1 * variance[x]))) / np.sqrt(variance[x])```
答: 暂无答案
评论
scipy.optimize.minimize
的存在,你的问题是如何使用它吗?rate
returnarr
bounds
optimize