0

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)
Cyber Progs
  • 3,656
  • 3
  • 30
  • 39
Habtra
  • 21
  • 3
  • Have you checked that none of the values in the array `price` are zero or negative? – Warren Weckesser Jul 29 '17 at 00:00
  • Yes, that array only contains positive numbers. – Habtra Jul 29 '17 at 08:43
  • Have you looked through this post https://stackoverflow.com/questions/7559595/python-runtimewarning-overflow-encountered-in-long-scalars – DrBwts Jul 29 '17 at 12:15
  • @DrBwts It seems that in that other thread the array's values were becoming extremely high and that this was what caused an overflow. But for this case, the values of h are supposed to stay relatively small. Plus I find it hard to try and force the list's values to be 64 bit since it's a regular python list and not a numpy array.Should I upload the data? – Habtra Jul 29 '17 at 21:52
  • do you know the limits of `h`? – DrBwts Jul 30 '17 at 15:12
  • Sorry for not answering earlier. So basically yeah it seems h shot towards infinity in some instances of the optimisation. I prevented it by putting the upper bound of the parameters equal to one. I wouldn't have expected this to happen since the optimal values are obviously small enough so that h doesn't 'explode', but I guess it has something to do with the way the optimiser works. – Habtra Aug 08 '17 at 14:26

0 Answers0