最小化使用 Python 查找我的 Heston Nandi 模型参数的对数可能性

minimizing log likelihood to find the parameters for my heston nandi model using python

提问人:Ranit Bagchi 提问时间:11/5/2023 最后编辑:AJ BifflRanit Bagchi 更新时间:11/6/2023 访问量:57

问:

我有一个目标函数,我想使用 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])```
python numpy scipy 金融 数值方法

评论

0赞 AJ Biffl 11/5/2023
从你的措辞来看,你似乎已经知道scipy.optimize.minimize的存在,你的问题是如何使用它吗?
0赞 Ranit Bagchi 11/5/2023
是的,我关于如何在我的情况下使用最小化功能的问题。当我使用它时initial_parameters=[0.01,0.01,0.01,0.01,0.01] 'result = minimize(log_likelihood, initial_parameters, args=(returnarr,),method='L-BFGS-B') 它给了我一个错误:RuntimeWarning: overflow encountered in double_scalars variance[x+1] = w + (B * variance[x]) + (alpha * np.square(epsilon[x]-(phi*np.sqrt(variance[x])))) .这就是为什么我不确定我是否实现了这种方法
0赞 Nick ODell 11/5/2023
你能提供 和 的定义吗?ratereturnarr
0赞 AJ Biffl 11/5/2023
您可以考虑设置参数,以确保猜测不会太失控。对于您正在使用的方法,请查看这些选项,了解如何获取有关正在发生的事情、设置步长等的更多信息boundsoptimize
0赞 Ranit Bagchi 11/5/2023
@NickODell 利率是此问题中使用的利率,即 2.47%,返回数组由 1510 个股票数据的对数返回组成。

答: 暂无答案