Poker
2013-May-20 09:14 UTC
[R] How to fit a normal inverse gaussian distribution to my data using optim
Dear R Help Please forgive my lack of knowledge,.I would be very thankful for some help. Here is my problem: I was using optim to estimate parameters of a model and I get this error message "Error in optim(x0, fn = riskll, method = "L-BFGS-B", lower = lbs, upper = ubs, : L-BFGS-B needs finite values of 'fn'" Below is the R code I have written. library('GeneralizedHyperbolic') data=read.table(file="MSCI_USA.csv",sep=',',header=T) data=data[1:8173,] #starting value x0 <- c(-0.011,0.146, 0.013, 0.639, 0.059,0.939, -0.144 , 1.187, 1.601, -0.001) #lower bound and upper bound lbs <- c(-5, -5, -5, -0.99999, 0.00001, 0, -1, 0.1, 1.2000001, -2) ubs<- c( 5, 5, 10, 0.99999, 5, 2, 0, 3, 1000, 10) #the likelihood function riskll <- function(data,para) { m0 <- para[1] m1 <- para[2] omega <- para[3] tau <- para[4] a <- para[5] b <- para[6] beta <- para[7] theta <- para[8] gamma <- para[9] phi <- para[10] T <- nrow(data) ret <- data[,2]; rate <- data[,3] exret=100*(ret+1-((rate/100)+1)^(1/365)) h = rep(0,T); vx = rep(0,T); h[1] = 10000*exret[1]^2 vx[1] = (exret[1]-m0-(m1+beta*((gamma^0.5)/(gamma^2+beta^2)^0.5))*h[1])/h[1] for ( i in (2:T) ) { h[i] = (omega+a*(abs(h[i-1]*vx[i-1])-tau*h[i-1]*vx[i-1])^theta+b*(h[i-1]^theta))^(1/theta) vx[i] = (exret[i]-phi*exret[i-1]-m0-(m1+beta*((gamma^0.5)/(gamma^2+beta^2)^0.5))*h[i])/h[i] } mu = -1*beta*((gamma^0.5)/(gamma^2+beta^2)^0.5) delta=((gamma^1.5)/(gamma^2+beta^2)^0.5) alpha=gamma beta=beta param = c(mu, delta, alpha, beta) riskll <- -1*sum(log(dnig(vx,param=param))) return(riskll) } #optimization optim(x0,fn=riskll,method ="L-BFGS-B",lower=lbs,upper=ubs, data = data) [[alternative HTML version deleted]]
Poker
2013-May-20 09:25 UTC
[R] How to fit a normal inverse gaussian distribution to my data using optim
Dear R Help Please forgive my lack of knowledge,.I would be very thankful for some help. Here is my problem: I was using optim to estimate parameters of a model and I get this error message "Error in optim(x0, fn = riskll, method = "L-BFGS-B", lower = lbs, upper = ubs, : L-BFGS-B needs finite values of 'fn'" Below is the R code I have written. library('GeneralizedHyperbolic') data=read.table(file="MSCI_USA.csv",sep=',',header=T) data=data[1:8173,] #starting value x0 <- c(-0.011,0.146, 0.013, 0.639, 0.059,0.939, -0.144 , 1.187, 1.601, -0.001) #lower bound and upper bound lbs <- c(-5, -5, -5, -0.99999, 0.00001, 0, -1, 0.1, 1.2000001, -2) ubs<- c( 5, 5, 10, 0.99999, 5, 2, 0, 3, 1000, 10) #the likelihood function riskll <- function(data,para) { m0 <- para[1] m1 <- para[2] omega <- para[3] tau <- para[4] a <- para[5] b <- para[6] beta <- para[7] theta <- para[8] gamma <- para[9] phi <- para[10] T <- nrow(data) ret <- data[,2]; rate <- data[,3] exret=100*(ret+1-((rate/100)+1)^(1/365)) h = rep(0,T); vx = rep(0,T); h[1] = 10000*exret[1]^2 vx[1] = (exret[1]-m0-(m1+beta*((gamma^0.5)/(gamma^2+beta^2)^0.5))*h[1])/h[1] for ( i in (2:T) ) { h[i] = (omega+a*(abs(h[i-1]*vx[i-1])-tau*h[i-1]*vx[i-1])^theta+b*(h[i-1]^theta))^(1/theta) vx[i] = (exret[i]-phi*exret[i-1]-m0-(m1+beta*((gamma^0.5)/(gamma^2+beta^2)^0.5))*h[i])/h[i] } mu = -1*beta*((gamma^0.5)/(gamma^2+beta^2)^0.5) delta=((gamma^1.5)/(gamma^2+beta^2)^0.5) alpha=gamma beta=beta param = c(mu, delta, alpha, beta) riskll <- -1*sum(log(dnig(vx,param=param))) return(riskll) } #optimization optim(x0,fn=riskll,method ="L-BFGS-B",lower=lbs,upper=ubs, data = data) [[alternative HTML version deleted]]