1

I am trying to minimize a function using spicy but the estimate of sigma is way off. Any help would be greatly appreciated.

Here's my code so far:-

import numpy as np
from scipy.optimize import minimize
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
from pylab import plot, show, grid, xlabel, ylabel
np.set_printoptions(linewidth=999999)
from scipy.stats import multivariate_normal
import scipy.stats as stats
from numpy import sqrt
from numpy import exp
from numpy import log
from numpy import pi



# Process parameters
beta=1.5
sigma=0.01
theta=0.7
T=50
N=1000
dt = T/N
#n=1 ## number of simulations


M = np.zeros((N))
#for k in range(n):
   # Iterate to compute the steps of the Brownian motion.
for i in range(N):
   M[i]=(theta + (M[i-1]-theta)*exp(-beta*dt)) + sigma*np.random.normal(0,sqrt((1-exp(-2*beta*dt))/2*beta))
   M[0] = 0.7 ## initial value
#print(M)

def mle(params):
    beta = params[0]
    theta = params[1]   
    sigma = params[2]
    alpha = exp(-beta*dt)
    eta = (sigma**2)*(1-exp(-2*beta*dt))/(2*beta)
    LL = -(-N/2*log(2*pi)-N*log(sqrt(eta)) - (np.sum((M[i]-M[i-1]*alpha - theta*(1-alpha))**2))/(2*eta))
    return(LL)  


initParams = [1, 1, 1]
#params1 = np.array([1,1,1])

res = minimize(mle, initParams ,method='nelder-mead')
print(res.x)

What I am getting is this:-

runfile('/Users/achalawasthi/Desktop/testmle.py', wdir='/Users/achalawasthi/Desktop')
[  1.84035906e+00   7.41336913e-01   2.00523821e-23]

As you can see, the estimate of sigma is way off. I do not understand why this is happening ? My intuition is that there is some instability and that sigma just goes away. Would putting some bounds on sigma help?

Thanks

aL_eX
  • 1,453
  • 2
  • 15
  • 30
aa777
  • 121
  • 4
  • What happens when you vary `params1`? Also, do you work in Python 2 or 3? If 2, you might want to use floats for `T` and `N`. What is `i` in `mle`? – Cleb Jan 18 '18 at 17:57
  • `LL` is an input for `mle()`, but not used in this function. Why? Anyhow, `LL` returns in each iteration the same value, because only `beta, sigma, theta` are used from the input and they don't change. – Mr. T Jan 18 '18 at 18:20
  • Hello, thanks for the comments. I found my mistake and have updated the code accordingly. I have a new problem though. I have updated my question accordingly. – aa777 Jan 18 '18 at 18:53
  • i was the just the index by which I sum it over N. I work in Python 3. – aa777 Jan 18 '18 at 18:54

0 Answers0