Nov 27, 2010 at 9:10pm UTC
Hi,
I am trying to use gsl libraries for simulation variance gamma process. I am getting this error (error: invalid operands of types ‘gsl_rng*’ and ‘double’ to binary ‘operator-’, shown in bold font) and I am not sure how to debug it. Your help in understanding my mistake and helping in resolving it greatly appreciated.
Thanks
AP
#include "VGSA.h"
#include <math.h>
#include <complex>
#include <cmath>
#include <stdio.h>
#include <iostream>
#include <math.h>#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>#include <vector>using namespace std;
extern unsigned long int gsl_rng_default_seed;
const std::complex<double> ImgI = std::complex<double>(0,1.0);
VGSA::VGSA()
{
}
void VGSA::init(double Sigma, double Nu, double Theta, double So, double R, double Q,double Lambda, double Kappa,
double Eta, int numSteps, double dt, double Y_0)
{
sigma = Sigma;
nu = Nu;
theta = Theta;
so = So;
r = R;
q = Q;
lambda = Lambda;
kappa = Kappa;
eta = Eta;
N = numSteps;
h = dt;
y_0 = Y_0;
//dx = Dx;
std::cout<<"sigma is: "<< sigma<<std::endl;
}
complex<double> VGSA::si_VG(complex<double> u)
{
return -1.0*log(1.0-complex<double>(0,1.0)*u*theta*nu + pow(sigma,2.0)*nu*pow(u,2.0)*0.5)/nu;
}
complex<double> VGSA::phi(complex<double> u, double t, double nu, double kappa, double eta, double lambda, double
y_0)
{
complex<double> temp = sqrt(kappa*kappa - 2.0*pow(lambda,2.0)*complex<double>(0,1.0)*u);
complex<double> A = exp(pow(kappa,2.0)*nu*t/pow(lambda,2.0))/pow(cosh(temp*t/2.0)+sinh(temp*t/2.0)*kappa/l
ambda, 2*kappa*nu/pow(lambda,2.0));
complex<double> B = 2.0*u*complex<double>(0,1.0)/(kappa-temp/tanh(temp*t*0.5));
return A*exp(B*y_0);
//return -1.0/nu*log(1.0-complex<double>(0,1.0)*u*theta*)
}
void VGSA::VGSASimulation(int N, double h)
{
vector<double> Y(1024,0);
vector<double> X(1024,0);
vector<double> S(1024,0);
S[0] = 100.0;
const gsl_rng_type * T;
gsl_rng * r;
gsl_rng_env_setup();
T = gsl_rng_default;
r = gsl_rng_alloc (T);
Y[0] = 1.0/nu;
for (int j=1; j<N; j++) {
double z=gsl_ran_ugaussian(r);
Y[j] = Y[j-1]+kappa*(eta-Y[j-1])*h + lambda*sqrt(Y[j-1]*h)*z + lambda*lambda*h*0.5*(z*z-1.0);
double t_hat = 0.5*h*(Y[j]+Y[j-1]);
// scaling sigma, nu, theta by t_hat
double sigma_hat = sigma*sqrt(t_hat);
double nu_hat = nu/t_hat;
double theta_hat = theta*t_hat;
// generate gamma random variable
double g = gsl_ran_gamma(r, 1.0/nu_hat, nu_hat);
// get different random normal number N(0,1)
double z1=gsl_ran_ugaussian(r);
X[j] = theta_hat*g + sigma_hat*sqrt(g)*z1;
complex<double> si_vg = si_VG(-1.0*ImgI);
complex<double> phi_vgsa1 = phi(-1.0*ImgI*si_vg, j*h, 1.0/nu, kappa, eta, lambda, y_0);
complex<double> phi_vgsa2 = (j==1)?1.0:phi(-1.0*ImgI*si_vg, (j-1.0)*h, 1.0/nu, kappa, eta, lambda, y_0);
if(phi_vgsa1.real()<0.0){
phi_vgsa1 = 0.0000002;
}
if(phi_vgsa2.real()<0.0){
phi_vgsa2 = 0.0000001;
}
complex<double> w_hat = log(phi_vgsa1 - phi_vgsa2);
//double w_hat = log(phi_vgsa1 - phi_vgsa2);
std::cout<<"w_hat is :"<<w_hat<<std::endl;
S[j] = S[j-1]*exp((r-q)*h - w_hat.real()+X[j]);
}
for(int i=0; i<7; i++){
std::cout<<"Y[i] is: "<<Y[i]<<std::endl;
std::cout<<"X[i] is: "<<X[i]<<std::endl;
}
}
int main (int argc, const char * argv[]) {
// insert code here...
double So=100.0;
double R=.025;
double Q = 0.5;
double Theta = -0.25;
double Sigma = 0.3;
double Nu = 0.2;
double dx=0.01;
double Lambda = 0.02;
double Kappa = 0.04;
double t = 1.0;
double y_0 = 0.2;
double eta = 0.7;
int numSteps = 1024;
double dt = t/numSteps;
gsl_rng_default_seed=111;
VGSA vgsa;
vgsa.init(Sigma, Nu, Theta, So, R, Q, Lambda, Kappa, eta, numSteps, dt, y_0);
complex<double> si = vgsa.si_VG(ImgI);
complex<double> Phi = vgsa.phi(ImgI,t, Nu, Kappa, eta, Lambda, y_0);
std::cout<<"phi is: "<<Phi<<std::endl;
vgsa.VGSASimulation(numSteps, dt);
printf("Hello, World!\n");
return 0;
}
Last edited on Nov 27, 2010 at 10:01pm UTC