I'm trying to optimize the parameters for a series of functions to best match a set of data.
When I execute the script, for some loops the function works normally and for some I get the following message:
script.py:37: RuntimeWarning: overflow encountered in double_scalars h.append(x[2]+x[0]*e[i]**2+x[1]h[i])
C:\Users\ ... \Anaconda2\lib\site-packages\scipy\optimize\optimize.py:628: RuntimeWarning: invalid value encountered in double_scalars grad[k] = (f(((xk + d,) + args)) - f0) / d[k]
I cannot manage to solve it as it seems not to come from me since the function works for some sets of data and doesn't work for others. I don't believe it's a problem of data format either as it is all numbers on a .txt
file. What's weirder is that sometimes the function works for a set of numbers but doesn't for a subset of that same set.
Any ideas?
The code is below. I use python 2.7.12 on windows 8.
from numpy import *
from scipy import stats
from scipy import optimize
from math import *
#Get the data
price=loadtxt("DAX.txt")
#define arrays for return and excess return
r=[]
subr=[]
e=[]
optimparams = []
listsuml=[]
#calculate return
for i in range(len(price)-1):
r.append(log(price[i+1]/price[i]))
def sumloglikelihood (x):
#define function parameters
h=[]
z=[]
l=[]
h.append(sigma**2)
for i in range(999):
h.append(x[2]+x[0]*e[i]**2+x[1]*h[i])
for i in range(1000):
z.append(e[i]/sqrt(h[i]))
l.append(-0.5*(log(2*math.pi)+log(h[i])+z[i]**2))
#sum of log likelihoods
suml=0
for i in range(1000):
suml=suml+l[i]
suml=-suml
return suml
#for j in range (len(r)-1000):
for j in range (1):
del subr[:]
del e[:]
for i in range(1000):
subr.append(r[j+i])
#calculate some stats about the return
mu=mean(subr)
sigma=std(subr)
#calculate the excessive return
for i in range(1000):
e.append(subr[i]-mu)
params=[.06,.92]
params.append(sigma**2*(1-params[0]-params[1]))
#define the function to be minimized
#optimise the function and print the sum of log likelihoods with the new parameters
xxx=optimize.minimize(sumloglikelihood,params,method='L-BFGS-B',bounds=((.000001,3),(.000001,3),(.000001,3)))
optimparams.append(xxx.x)
listsuml.append(sumloglikelihood(optimparams[j]))
print optimparams
print listsuml
savetxt("optimparamsDAX.txt",optimparams)
savetxt("listsumlDAX.txt",listsuml)