Calc<-function(th,t){
th[1]->mean
th[2]->beta.0
th[3]->beta.1
th[4]->beta.2
th[5]->sigma.0
th[6]->lambda
returns<-returnzzzz$ReturnontheSP500Index
r<-0.000137
n<-length(returns)
z<-rnorm(n,0,1)#sigma is variance
sigma.sqs<-vector(length=n)
sigma.sqs[1]<-sigma.0**2
for(i in c(1:(n))){
returns[i+1]<-( r+lambda*sqrt(beta.0+beta.1*sigma.sqs[i]+beta.2*sigma.sqs[i]*z**2)
-0.5*(beta.0+beta.1*sigma.sqs[i]+beta.2*sigma.sqs[i]*z**2)+sqrt(beta.0+beta.1*sigma.sqs[i]+beta.2*sigma.sqs[i]*z**2)*z
)
}
return(list(et=returns, ht=sigma.sqs))
}
GarchLogL<-function(th,t)
{
res<-Calc(th,t)
sigma.sqs<-res$ht
returns<-res$et
return (-sum(dnorm(returns[-1],mean=th[1],sd=sqrt(sigma.sqs[-1]),log=TRUE)))
}
GarchLogLSimpl<-function(th,y){GarchLogL(c(0,th),y)}
fit2<-nlm(GarchLogLSimpl,
p=rep(1,5),
hessian=TRUE,
data<-returnzzzz,
iterlim=500)
sqrt(diag(solve(fit2$hessian)))
Hi this is the first time for me here, i hope i do everything right. In this code i want to implement a maximum likelihood function of the N-GARCH, i get this error out of R: There were 50 or more warnings (use warnings() to see the first 50) and this one Error in solve.default(fit2$hessian) :
Lapack routine dgesv: system is exactly singular: U[1,1] = 0.
Unfortunately i am not a really good programmer and i just changed a GARCH model code for my needs. My return data is
# [,1]
#1996-01-02 0.007793
#1996-01-03 0.000950
#1996-01-04 -0.005826
#1996-01-05 -0.001587
#1996-01-08 0.002821
#1996-01-09 -0.014568
like this, as an xts. I hope i provided enough information, if not pls just comment.
I really appreciate your help!!
I got a new code that might be able to solve it, but I get another error in this one:
Ngarch<-function(rtn){
mu=mean(rtn)
par=c(mu,0.01,0.8,0.01,0.7) #Startwerte
low=c(-10,0,0,0,0)
upp=c(10,1,1,0.4,2)
mm=optim(par,glkn,method="Nelder-Mead",hessian=T)
#mm=optim(par,glkn,method="L-BFGS-B",hessian=T,lower=low,upper=upp)
par=mm$par
H=mm$hessian
Hi = solve(H)
cat(" ","\n")
cat("Estimation results of NGARCH(1,1) model:","\n")
cat("estimates: ",par,"\n")
se=sqrt(diag(Hi))
cat("std.errors: ",se,"\n")
tra=par/se
cat("t-ratio: ",tra,"\n")
# compute the volatility series and residuals
ht=var(rtn)
T=length(rtn)
if(T > 40)ht=var(rtn[1:40])
at=rtn-par[1]
for (i in 2:T){
sig2t=par[2]+par[3]*ht[i-1]+par[4]*(at[i-1]-par[5]*sqrt(ht[i-1]))^2
ht=c(ht,sig2t)
}
sigma.t=sqrt(ht)
Ngarch <- list(residuals=at,volatility=sigma.t)
}
glkn <- function(par){
rtn=read.table("tmp.txt")[,1]
glkn=0
ht=var(rtn)
T=length(rtn)
if(T > 40)ht=var(rtn[1:40])
at=rtn[1]-par[1]
for (i in 2:T){
ept=rtn[i]-par[1]
at=c(at,ept)
sig2t=par[2]+par[3]*ht[i-1]+par[4]*ht[i-1]*(at[i-1]/sqrt(ht[i-1])-par[5])^2
ht=c(ht,sig2t)
glkn=glkn + 0.5*(log(sig2t) + ept^2/sig2t)
}
glkn
}
The error I get is Error in optim(par, glkn, method = "Nelder-Mead", hessian = T) :
non-finite finite-difference value [2].
An again these warnings, how can I get rid of them?
Thanks for your help!
Related
I want to minimize this function with constraint
The step is I need to find t(i) that optimize (minimize) the E(TC)
Here are my codes for n=3 and want to minimize E(TC) with the optimum t(i) ,i =1,2,3. Note that t(1) must equal to zero, and with constraint t(2)<t(3)<T
OptExp<-function(te){
mu=0.001299059
sigm=0.00006375925
D=80
K=500
F=0.7
T=40
Po=-0.0208801593
mu=0.001299059
n=3
t=as.vector(n,mode="numeric")
P1=as.vector(n,mode="numeric")
P2=as.vector(n,mode="numeric")
Pt1=as.vector(n,mode="numeric")
Pt2=as.vector(n,mode="numeric")
for (i in 2:(n)){
t[1]=0
t[i]=te[i-1]}
for (i in 1:n){
if(i!=n){
P1[i]=Po*exp((mu+(sigm^2)/2)*t[i])*D*(t[i+1]-t[i])
P2[i]=(1/2)*Po*exp((mu+(sigm^2)/2)*t[i])*F*D*(t[i+1]-t[i])^2}
else {
P1[i]=Po*exp((mu+(sigm^2)/2)*t[i])*D*(T-t[i])
P2[i]=(1/2)*Po*exp((mu+(sigm^2)/2)*t[i])*F*D*(T-t[i])^2}}
Pt1=sum(P1)
Pt2=sum(P2)
E=n*K+Pt1+Pt2
#constraint
if (t[3]<T & t[1]<t[2] & t[2]<t[3]){
return(E)}
}
optmz=optim(c(3,5),fn=OptExp)
But the result is
Error in optim(c(3, 5), fn = OptExp) :
objective function in optim evaluates to length 0 not 1
Anyone knows what is wrong from my code?
*ps: I also try with consrtOptim
n=2
t=as.vector(n,mode="numeric")
t[1]=0
OptExp<-function(te){
mu=0.001299059
sigm=0.00006375925
D=80
K=500
F=0.7
T=40
Po=-0.0208801593
mu=0.001299059
P1=as.vector(n,mode="numeric")
P2=as.vector(n,mode="numeric")
Pt1=as.vector(n,mode="numeric")
Pt2=as.vector(n,mode="numeric")
for (i in 2:(n)){
t[1]=0
t[i]=te[i-1]}
for (i in 1:n){
if(i!=n){
P1[i]=Po*exp((mu+(sigm^2)/2)*t[i])*D*(t[i+1]-t[i])
P2[i]=(1/2)*Po*exp((mu+(sigm^2)/2)*t[i])*F*D*(t[i+1]-t[i])^2}
else {
P1[i]=Po*exp((mu+(sigm^2)/2)*t[i])*D*(T-t[i])
P2[i]=(1/2)*Po*exp((mu+(sigm^2)/2)*t[i])*F*D*(T-t[i])^2}}
Pt1=sum(P1)
Pt2=sum(P2)
E=n*K+Pt1+Pt2
return(E)
}
lb=t[n-1]
u1=cbind(c(1,-1));u1
c1=c(lb,-40)
init=c(3)
value<-constrOptim(init,f=OptExp,ui=u1,ci=c1,grad=NULL)
note that the constraint for n=2 is t(1)=0<t(2)<T=40
and it returns
one-dimensional optimization by Nelder-Mead is unreliable:
use "Brent" or optimize() directly
Just simply following the instructions in the error messages (no thinking required) leads to:
value<-constrOptim(init,f=OptExp,ui=u1,ci=c1,grad=NULL,
method="Brent",lower=0,upper=40)
and
OptExp<-function(te,...){
For more than one t use method="Nelder-Mead" (or better BFGS)
I don't have as much computer science experience so I'm a bit overwhelmed! I have an R package that is already released on the CRAN. It passed its CMD checks and worked fine, but I made a change. It is a Gibbs sampler, so I had it output quantile based CIs, but I changed it to HPD. But now it's failing it's CMD checks and I don't know why. The specific problem is with the examples. I have run the examples and they seem to work. I have also tried dontrun and donttest, but the same error occurred. The strangest thing is that R automatically provides the result of its attempts to run the code, with the error report. But the output provided appears accurate. Everything seems to be working, except the CMD check flags it as an error and stops the checks. Please help! I am really not a computer scientist, so I am very much out of my element! The error is at the bottom! Thank you all!
Error:
base::assign(".dptime", (proc.time() - get(".ptime", pos = "CheckExEnv")), pos = "CheckExEnv")
base::cat("bcor", base::get(".format_ptime", pos = 'CheckExEnv')(get(".dptime", pos = "CheckExEnv")), "\n", file=base::get(".ExTimings", pos = 'CheckExEnv'), append=TRUE, sep="\t")
Error in format(x[1L:3L], digits = 7L) : unused argument (digits = 7)
Calls: ->
Execution halted
Problem code (though this isn't the only one that's had issues, depending on which is tested first):
bcor=function(data,iter,burn,seed,CI,S0,nu0,mu0){
filler=matrix(nrow=ncol(data),ncol=ncol(data))
for (a in 1:ncol(data)){
for (b in 1:ncol(data)){
filler[a,b]=ifelse(a==b,cov(data,use="pairwise.complete.obs")[a,b],0)}}
filler1=matrix(nrow=ncol(data),ncol=ncol(data))
for (a in 1:ncol(data)){
for (b in 1:ncol(data)){
filler1[a,b]=ifelse(missing(S0),filler[a,b],S0[a,b])}}
for (a in 1:ncol(data)){
for (b in 1:ncol(data)){
filler1[a,b]=ifelse(missing(S0),filler[a,b],S0[a,b])}}
S0=filler1
L0=S0
nu0=ifelse(missing(nu0),ncol(data)*(ncol(data)+1)/2-1,nu0)
filler2=vector(length=ncol(data))
for (a in 1:ncol(data)){
filler2[a]=ifelse(missing(mu0),rep(0,ncol(data)),mu0)
}
mu0=filler2
n=nrow(data)
ybar=colMeans(data,na.rm=T)
Sigma=cov(data,use="pairwise.complete.obs")
seed=ifelse(missing(seed),999,seed)
iter=ifelse(missing(iter),5000,iter)
burn=ifelse(missing(burn),iter/2,burn)
THETA=SIGMA=NULL
set.seed(seed)
pct=rep(0,iter+1)
print(noquote("Sampling, this may take a minute"))
for(s in 1:iter)
{
###Update theta
Ln=solve(solve(L0) + n*solve(Sigma))
mun=Ln%*%(solve(L0)%*%mu0+n*solve(Sigma)%*%ybar)
theta=mvrnorm(1,mun,Ln)
###Update sigma
Sn=S0 + (t(data)-c(theta))%*%t( t(data)-c(theta))
Sigma=solve(rwish(nu0+n, solve(Sn)))
###Save results
THETA=rbind(THETA,theta)
SIGMA=rbind(SIGMA,c(Sigma))
pct[s+1]=(round(s/iter*10,1))*10
if(pct[s+1]!=pct[s]){print(noquote(paste(pct[s+1],"%")))}
}
CI=ifelse(missing(CI),0.95,CI)
CI=ifelse(CI>1,CI/100,CI)
COR=NULL
mat=matrix(nrow=ncol(data),ncol=ncol(data))
cor=matrix(nrow=ncol(data),ncol=ncol(data),0)
print(noquote("Calculating correlations, this may take a minute"))
pct=rep(0,nrow(SIGMA)-burn+1)
for (s in burn:nrow(SIGMA)){
mat=matrix(SIGMA[s,],nrow=ncol(data),ncol=ncol(data))
for (a in 1:ncol(data)){
for (b in 1:ncol(data)){
cor[a,b]=mat[a,b]/sqrt(mat[a,a]*mat[b,b])
COR=rbind(COR,c(cor))
}
}
num=(s-burn+1)
denom=(nrow(SIGMA)-burn)
pct[s-burn+2]=round((num/denom)*10,1)*10
if(pct[s-burn+2]!=pct[s-burn+1]){print(noquote(paste(pct[s-burn+2],"%")))}
}
COR_M=NULL
COR_SD=NULL
COR_LL=NULL
COR_UL=NULL
for (a in 1:ncol(COR)){
COR_M[a]=quantile(probs=c(0.5),COR[,a])
COR_SD=sd(COR[1:nrow(COR),a])
COR_LL[a]=emp.hpd(COR[,a],conf=CI)[1]
COR_UL[a]=emp.hpd(COR[,a],conf=CI)[2]
}
star_ll=ifelse(COR_LL<0,1,0)
star_ul=ifelse(COR_UL<0,1,0)
star=ifelse(star_ll+star_ul==1," ","*")
COR_M1=paste(round(COR_M,2),star)
COR1=matrix(COR_M1,nrow=ncol(data),ncol=ncol(data))
table=data.frame(COR1)
colnames(table)=c(colnames(data))
rownames(table)=c(colnames(data))
diag(table)="1 "
Out=list()
Out$MU=THETA
Out$SIGMA=SIGMA
Out$M=matrix(COR_M,nrow=ncol(data),ncol=ncol(data))
Out$SD=matrix(COR_SD,nrow=ncol(data),ncol=ncol(data))
Out$LL=matrix(COR_LL,nrow=ncol(data),ncol=ncol(data))
Out$UL=matrix(COR_UL,nrow=ncol(data),ncol=ncol(data))
Out$Table=table
return(Out)
my project needs the EM algorithm below, where is all the code. The error is in the while loop, which is where the hope and maximization steps are. The error message is "Error in while (abs (Elogv [r] - Elogv [r - 1])> = 1e-06) {: missing value where TRUE / FALSE needed". How do I resolve this error if the while loop contains no true and false commands, and if I have already checked in detail that there are no errors in the commands and no NA's value? Grateful for the attention, who can save me.
n=100
u<-runif(n)
QUANTIL <- function(u){
Q <- rep(NA, length(u))
for (i in 1:length(u)) {
if(u[i] < 0.2634253829){
Q[i] <- 1*tan(pi*(0.9490353482*u[i]-0.5))+0
}
if(u[i]>=0.2634253829 && u[i] < 0.7365746171){
Q[i] <- 1*qnorm(1.4428629504*u[i]-0.2214315)+0
}
if(u[i]>0.7365746171){
Q[i] <- 1*tan(pi*(0.9490353482*u[i]-0.4490353))+0
}
}
return(Q)
}
x<-QUANTIL(u)
y<-c(sort(x))
i<-seq(1,n)
v<-c(i/(n+1))
t<-QUANTIL(v)
mi<-median(y)
s<-c(y[26:73])
sigma<-sqrt(sum((s-mi)^2)/(n-1))
p=0.4731492342
alpha<-(2*t^3)/(1+t^2)^2
beta<-(1-t^2)/(1+t^2)^2
eta<-(t^4-t^2)/(1+t^2)^2
lambda<-2*t/(1+t^2)^2
gama<-(-t^2)
delta<-2*t
k<-((p*0.6930665173/sigma*sqrt(2*pi))*exp((-1/2*sigma^2)*((y-mi)^2)))/(((p*0.6930665173/sigma*sqrt(2*pi))*exp((-1/2*sigma^2)*(y-mi)^2))+((((1-p)*1.0537015317/sigma*pi))*(1/(1+((y-mi)/sigma)^2))))
r<-2
Elogv<-sum(k*((-1/2)*((y-mi)/sigma)^2))-sum(k*log(sigma*sqrt(2*pi)))-sum((1-k)*log(sigma*pi))-sum((1-k)*log(1+((y-mi)/sigma)^2))+sum(k*log(p))+(n-sum(k))*log(1-p)+log(0.6930665173)*sum(k)+log(1.0537015317)*sum(1-k)
Elogv[1]<-0
while (abs(Elogv[r]-Elogv[r-1])>=0.000001) {
w<-(2*beta-2*k*beta+k)
q<-k*delta+2*lambda*(1-k)
sigma<-(sum(y*w)*sum(q)-sum(w)*sum(y*q))/(-2*sum(alpha*(1-k))*sum(q)+sum(w)*sum(k*gama-1)+2*sum(w)*sum(eta*(1-k)))
mi<-(sum(y*w)+2*sigma*sum(alpha*(1-k)))/sum(w)
k<-((p*0.6930665173/sigma*sqrt(2*pi))*exp((-1/2*sigma^2)*((y-mi)^2)))/(((p*0.6930665173/sigma*sqrt(2*pi))*exp((-1/2*sigma^2)*(y-mi)^2))+((((1-p)*1.0537015317/sigma*pi))*(1/(1+((y-mi)/sigma)^2))))
Elogv[r]<-sum(k*((-1/2)*((y-mi)/sigma)^2))-sum(k*log(sigma*sqrt(2*pi)))-sum((1-k)*log(sigma*pi))-sum((1-k)*log(1+((y-mi)/sigma)^2))+sum(k*log(p))+(n-sum(k))*log(1-p)+log(0.6930665173)*sum(k)+log(1.0537015317)*sum(1-k)
r<-r+1
It looks to me that the length of Elogv is 1? Thus Elogv[r] has no entry (r is 2!), i.e. evaluates to NA, thus the abs(Elogv[r]-Elogv[r-1]) is NA.
You need Elogv[2] <- 0 before starting the loop?
I am trying to compare two distributions by using Likelihood ratio test. I used the maxLik function to obtain mles of both of null and alternative distributions. I want to use BFGSR method because it gives better estimates.
library("maxLik")
library("flexsurv")
n=20
den1=1000
mpar=3
omepar=5
spar=3
Logliknak1=function(param){
m=param[1]
o=param[2]
n*(log(2)+m*log(m)-lgamma(m)-m*log(o))+(2*m-1)*sum(log(y))-(m/o)*sum(y^(2))
}
Loglikgennak= function(param){
s <- param[1]
ome <- param[2]
m<-param[3]
(n*(log(2*s)+m*log(m)-lgamma(m)-m*log(ome))+(2*m*s-1)*sum(log(y))-(m/ome)*sum(y^(2*s)))
}
LR2=rep(0,den1)
ps=0; pome=0; pm=0;
for(i in 1:den1){
repeat
{
x=rgengamma.orig(n,shape=spar,scale=(omepar/mpar),k=mpar)
y=x^0.5
ot=mean(y^2)
mt=(mean(y)*mean(y^2))/(2*(mean(y^3)-(mean(y)*mean(y^2))))
mle2 <- maxLik(logLik=Logliknak1, start = c(m=mt, o=ot),method="BFGSR")
lnull=logLik(mle2)
mm=coef(mle2)[[1]]
mo=coef(mle2)[[2]]
mle3 <- maxLik(logLik=Loglikgennak, start = c(s=1.5,ome=omepar+1,m=mpar+1),method="BFGSR")
lalt=logLik(mle3)
ps=coef(mle3)[[1]]
pome=coef(mle3)[[2]]
pm=coef(mle3)[[3]]
if (lalt>lnull && ps>1 && pome>0 && pm>0)
{break}
}
LR2[i]=2*(lalt-lnull)
print(i)
print(LR2[i])
print(pm)
print(pome)
print(ps)
}
However I keep getting the following error message:
Error in if (all(y == 0)) { : missing value where TRUE/FALSE needed
How do I fix this?
I continue to get an error on my function, possibly I'm overlooking something simple. I cannot run the code without getting an error when applying the function.
k.nn <- function(k,p1,p) {
k > 0
K <-length(k)
p=matrix()
for (i in p) {
matrix <- cbind(p,p1[1],p1[2])
d <- sqrt((matrix[,1]-matrix[,3])^2+(matrix[,2]-matrix[,4])^2)
}
##use the sort function to find the smallest distance from 1:k and return all nearest k values
sort.d <- function(x) { #implement bubble sort
N=length(x)
N>0
c=class(x)
for (n in length(x):2) { #distinguish the last term in the vector, name it, much be of x length, consists an error of length 1. Error if you compute n in length(x):1, cover length of 1
if(length(x)<2)
return(x)
for (m in 1:(n - 1)) { #distinguish the first term in the vector, name it
if(x[m]>x[m + 1]) { #begin comparing each term to neighboring term
swap<-x[m]
x[m]<-x[m + 1]
x[m + 1]<-swap
}
}
}
return(x)
}
sorted=sort.d(d)
for (n in k){
print(sorted[1:k])}
}
p=matrix(c(6.9,7.6,7.1,.4,6.2,1.8,2.5,2.3,5.7,6.9,.9,4.4,5.2,1.9,.6,7.4,1.2,6.6,3.3,4.9),nrow=10,ncol=2) #given matrix
p1=c(6,6)
k=3 nn.3=k.nn(k,p1,p)
print(nn.3)
There's a missing carriage return or ";" in the penultimate line that is throwing an error. If you remove tha last line so that you can use traceback() it tells you that k.nn throws a " subscript out of bounds" error when a matrix index is 4.
Debugging 101 tells you to put in print functions to see where the function fails and putting in a print after
c=class(x) ; print(c)
... ives you a result, but putting another one in the sort.d function does not get executed. Looking at the code upstream from that point we see:
d <- sqrt((matrix[,1]-matrix[,3])^2+(matrix[,2]-matrix[,4])^2)
So looking at the function and the matrix you have given, ... my guess is that you passed a two-column matrix to a function that expected a four-column argument.