I have looked at some different optimizing approaches in R to solve my optimization solution, but it seems that all the algorithms don't work for my Problem. I have the following Problem:
m=n-1 #length option sample (...)
n=5
Dmat <- matrix(0,nrow=n-1,ncol=n+1)
Dmat[row(Dmat)==col(Dmat)]<-1
Dmat[row(Dmat)+1==col(Dmat)]<--2
Dmat[row(Dmat)+2==col(Dmat)]<-1
#for(j in 0:n){
# para<-c(par[j])#Try to have n+1 Parameters(original formula from 0:N)
#}
Amat=matrix(NA,ncol=n+1,nrow=m)
for(u in 1:m){
for(k in 1:(n+1)){
Amat[u,k]=pmax(S[k]-K[u],0)
}
}
para>=0
sum(para)==1
S_0=exp((-r+q)*TTM)*sum(para*S)
objfunc=sum(para*Dmat)^2+alpha*exp(-r*TTM)*(rowsum(Amat*para)-V)
I have also an Approach using optim, but this one uses the Matrix form. My Question is how would be a way to work with this optimization, because of their algorithm usual optimization formulas like quandprog will likely not work. Are there different approaches I should look into?
Thank you for you suggestions!
An Edit:
This is the new Code I have tried so far:
objcfunc<-function(P){#build the central minimization function
n=12
r=0.03
V=test[,"Price"]
alpha=0.001
S=S
K=test[,"strike"]
TTM=test[,"TTM"]
m=n-1
Dmat <- matrix(0,nrow=n-1,ncol=n+1)
Dmat[row(Dmat)==col(Dmat)]<-1
Dmat[row(Dmat)+1==col(Dmat)]<--2
Dmat[row(Dmat)+2==col(Dmat)]<-1
Amat=matrix(NA,ncol=n+1,nrow=m) # build the matrix for model option prices
for(u in 1:m){
for(k in 1:(n+1)){
Amat[u,k]=pmax(S[k]-K[u],0)
}
}
y=(Dmat %*% P)^2+alpha*exp(-r*TTM)*((Amat %*% P)-V)^2
return(sum(y))
}
constr<-function(P){
S0=test[,"Settle"]
r=0.03
q=0
TTM=test[,"TTM"]
S=S
f=NULL
f1=rbind(f,S0-exp(-(r-q)*TTM)*sum(P*S))
f2=rbind(f,sum(P)-1)
return(c(f1,f2))
}
n=12
#NlcOptim(X=rep(0,n+1),objfun=objcfunc,confun=constr,lb=rep(0,n+1))#try to get rid from this rbind solution
library(Rsolnp)
solnp(pars=rep(0,n+1),objcfunc,constr,LB=rep(0,n+1))
The Problem is that I get the error Error in conf$c : $ operator is invalid for atomic vectors.
Is that because NlcOptim uses $ to call anything?
I provide my short sample data that I used here:
Date strike Price Settle TTM Average Moneyness implvola deltaM
2009-09-15 70.0 1.64 70.93 0.12 60.76036 1.01 0.09625975 0.01
2009-09-15 70.5 1.32 70.93 0.12 60.76036 1.01 0.09656526 0.01
2009-09-15 71.0 1.04 70.93 0.12 60.76036 1.00 0.09677672 0.00
2009-09-15 71.5 0.81 70.93 0.12 60.76036 0.99 0.09784297 0.01
2009-09-15 72.0 0.62 70.93 0.12 60.76036 0.99 0.09890358 0.01
2009-09-15 72.5 0.46 70.93 0.12 60.76036 0.98 0.09918868 0.02
2009-09-15 73.0 0.33 70.93 0.12 60.76036 0.97 0.09897400 0.03
2009-09-15 73.5 0.24 70.93 0.12 60.76036 0.97 0.10025021 0.03
2009-09-15 74.0 0.16 70.93 0.12 60.76036 0.96 0.09923239 0.04
2009-09-15 74.5 0.11 70.93 0.12 60.76036 0.95 0.09998572 0.05
2009-09-15 75.0 0.07 70.93 0.12 60.76036 0.95 0.09938658 0.05
The sample is in the Dataframe test. The Problem ran through with solnp, so I will edit the above formula.