Ejemplo n.º 1
0
    void Seeder(int RanSeed, time_t curTime)
    {
      //time_t curTime = time(0);
#ifndef _NO_SPRNG
      //init_sprng(SPRNG_LCG, static_cast<int>(curTime) , SPRNG_DEFAULT);
      init_sprng(SPRNG_LCG, RanSeed , SPRNG_DEFAULT);
#else
      //std::cerr << "Ranlib!!!!\n\n";
      //srandom(static_cast<unsigned int>(curTime));
      srandom(RanSeed);
#endif
      long seed1, seed2;
      phrtsd(ctime(&curTime), &seed1, &seed2);
      setall(seed1, seed2);
    }
Ejemplo n.º 2
0
void mexFunction( int nlhs, mxArray *plhs[],
                  int nrhs, const mxArray *prhs[] )
{
  
// Local variables for the gateway function

// semip_gc(bdraw,adraw,pdraw,sdraw,rdraw,vmean,amean,zmean,yhat,
// y,x,W,ndraw,nomit,nsave,n,k,m,mobs,a,nu,d0,rval,mm,kk,detval,ngrid,TI,TIc)
     
	// (The inputs are passed to the subroutine and the outputs
	//  are extracted from the subroutine and passed back to Matlab.)

  double *bdraw,*adraw,*pdraw,*sdraw,*rdraw,*vmean,*amean,*zmean,*yhat;
  double *y,*x,*W,*a,nu,d0,rval,mm,kk,*detval,ngrid,*TI,*TIc,*m_obs;
  int i,ndraw,nomit,nsave,n,k,m,*mobs;
  
  long *Seed1, *Seed2;
  int buflen, status, flag;
  char *phrase;
  static char phrase2[8];
  
    phrase2[0] = 'h';
    phrase2[1] = 'h';
    phrase2[2] = 'i';
    phrase2[3] = 't';
    phrase2[4] = 'h';
    phrase2[5] = 'e';
    phrase2[6] = 'r';
    phrase2[7] = 'e';



  /* Check for proper number of arguments. */

  if(nrhs == 20) {  
     flag = 0;
    phrase = phrase2;
  } else if (nrhs == 21){
    flag = 1;
  } else {
    mexErrMsgTxt("semip_gc: 20 or 21 inputs required.");
  }

 
  if(nlhs != 9) {
    mexErrMsgTxt("semip_gcc: 9 output arguments needed");
  }


    if (flag == 1) {

    // input must be a string
    if ( mxIsChar(prhs[20]) != 1)
      mexErrMsgTxt("semip_gc: seed must be a string.");
    // input must be a row vector
    if (mxGetM(prhs[20])!=1)
      mexErrMsgTxt("semip_gc: seed input must be a row vector.");

    // get the length of the input string
    buflen = (mxGetM(prhs[20]) * mxGetN(prhs[20])) + 1;

    // allocate memory for input string
    phrase = mxCalloc(buflen, sizeof(char));

    // copy the string data from prhs[0] into a C string input_ buf.
    // If the string array contains several rows, they are copied,
    // one column at a time, into one long string array.
    //
    status = mxGetString(prhs[20], phrase, buflen);
    if(status != 0)
      mexWarnMsgTxt("semip_gc: Not enough space. seed string truncated.");
    }
    
    // allow the user to set a seed or rely on clock-based seed
   if (flag == 0) {
    setSeedTimeCore(phrase);
   } else {
	phrtsd(phrase,&Seed1,&Seed2);
    setall(Seed1,Seed2);
   }


//  Parse input arguments

// semip_gcc(y,x,W,ndraw,nomit,nsave,n,k,m,mobs,a,nu,d0,rval,mm,kk,detval,ngrid,TI,TIc);

     y = mxGetPr(prhs[0]);
     x = mxGetPr(prhs[1]);
	 W = mxGetPr(prhs[2]);
	 ndraw = (int) mxGetScalar(prhs[3]);
	 nomit = (int) mxGetScalar(prhs[4]);
	 nsave = (int) mxGetScalar(prhs[5]);
	 n = (int) mxGetScalar(prhs[6]);
	 k = (int) mxGetScalar(prhs[7]);
	 m = (int) mxGetScalar(prhs[8]);
	 m_obs = mxGetPr(prhs[9]);
     a = mxGetPr(prhs[10]);
	 nu = mxGetScalar(prhs[11]);
	 d0 = mxGetScalar(prhs[12]);
	 rval = mxGetScalar(prhs[13]);
	 mm = mxGetScalar(prhs[14]);
	 kk = mxGetScalar(prhs[15]);
	 detval = mxGetPr(prhs[16]);
	 ngrid = (int) mxGetScalar(prhs[17]);	 
	 TI = mxGetPr(prhs[18]);
	 TIc = mxGetPr(prhs[19]);
	 

	// no need for error checking on inputs
	// since this was done in the matlab function

	  // Transfer m_obs into an integer vector

	 //(1) Make integer vector storage
	 
	 mobs = ivector(0,m-1);

	 //(2) Put m_obs into mobs

	for(i=0; i<m; i++){
		mobs[i] = (int) m_obs[i];
	}
			

    /* Create matrix mxArrays for the return arguments */
	 //[bdraw,adraw,pdraw,sdraw,rdraw,vmean,amean,zmean,yhat,cntr]

    plhs[0] = mxCreateDoubleMatrix(nsave,k, mxREAL);	// beta draws
	plhs[1] = mxCreateDoubleMatrix(nsave,m, mxREAL);	// a-draws
	plhs[2] = mxCreateDoubleMatrix(nsave,1, mxREAL);	// rho draws
	plhs[3] = mxCreateDoubleMatrix(nsave,1, mxREAL);	// sige draws
	plhs[4] = mxCreateDoubleMatrix(nsave,1, mxREAL);	// r-draws
	plhs[5] = mxCreateDoubleMatrix(m,1, mxREAL);		// v-means
	plhs[6] = mxCreateDoubleMatrix(m,1, mxREAL);		// a-means
	plhs[7] = mxCreateDoubleMatrix(n,1, mxREAL);		// z-means
	plhs[8] = mxCreateDoubleMatrix(n,1, mxREAL);		// yhat-means


	// Get pointers to the data in the mxArrays for the C-subroutine


    bdraw = mxGetPr(plhs[0]);
	adraw = mxGetPr(plhs[1]);
	pdraw = mxGetPr(plhs[2]);	
	sdraw = mxGetPr(plhs[3]);
	rdraw = mxGetPr(plhs[4]);
	vmean = mxGetPr(plhs[5]);
	amean = mxGetPr(plhs[6]);
	zmean = mxGetPr(plhs[7]);
	yhat  = mxGetPr(plhs[8]);

// Call the Subroutine

    semip_gc(bdraw,adraw,pdraw,sdraw,rdraw,vmean,amean,zmean,yhat,y,x,W,ndraw,nomit,nsave,n,k,m,mobs,a,nu,d0,rval,mm,kk,detval,ngrid,TI,TIc);

// free up mobs
	
	free_ivector(mobs,0);


}
int parse_command_line(command_line_options* clo, int argc, char *argv[]) {
    int opt;
    long n;
    long defaultseed1;
    long defaultseed2;
    time_t timer = time(NULL);
    int error_encountered = 0;
    char *localtime_buffer = (char *)asctime(localtime(&timer));
    
    //Get default seed values if a user doesn't specify one
    phrtsd(localtime_buffer, &defaultseed1, &defaultseed2);

    while((opt = getopt(argc, argv, "H:n:dt:s:hj:k:o:v:r:u:b:g:")) != -1) {
        switch(opt)
        {
            case 'a':
                clo->mean_reversion_rate = atof(optarg); break;
            case 'b':
                clo->mean_reversion_level = atof(optarg); break;
            case 'd':
                clo->debug = 1; break;
            case 'g':
                clo->variance_variance = atof(optarg); break;
            case 'h':
                error_encountered = 1; break; //not really, but they'll get the help :)
            case 'H':
                clo->hurst_exponent = atof(optarg); break;  
            case 'j':
                clo->seed1 = atoi(optarg); break;
            case 'k':
                clo->seed2 = atoi(optarg); break;
            case 'n':
                clo->number_of_simulations = atoi(optarg); break;
            case 'o':
                clo->output_option = atoi(optarg); break;
            case 'r':
                clo->risk_free_rate = atof(optarg); break;
            case 's':
                clo->number_of_steps = atoi(optarg); break;
            case 't':
                clo->end_time = atof(optarg); break;
            case 'u':
                clo->stock_price = atof(optarg); break;
            case 'v':
                clo->variance = atof(optarg); break;
    

            default: 
                //Most likely the user entered an invalid option
                error_encountered = 1;
        }
    }
    //Check if the parameters are all valid.
    if(clo->number_of_simulations <= 0) {
        clo->number_of_simulations = DEFAULT_SIMULATIONS;
    }
    
    DEBUG_MSG("Executing %ld simulations\n", clo->number_of_simulations);
    if(clo->number_of_steps <= 0) {
        clo->number_of_steps = DEFAULT_STEPS;
    }
    n = floor(log2(clo->number_of_steps));
    clo->number_of_steps = pow(2,n);
    DEBUG_MSG("Paths contain 2^%ld (%ld) steps\n", n,  clo->number_of_steps);
    
    if(clo->hurst_exponent < 0.5 || clo->hurst_exponent >= 1) {
        clo->hurst_exponent = DEFAULT_HURST_EXPONENT;
    }
    DEBUG_MSG("Using %f as a Hurst Exponent\n", clo->hurst_exponent);
    
    if(clo->end_time <= 0) {
        clo->end_time = DEFAULT_END_TIME;
    }
    DEBUG_MSG("Simulating to t=%f\n", clo->end_time);

    if(clo->output_option < 1 || clo-> output_option > 6) {
        clo->output_option = DEFAULT_OUTPUT;
    }

    if(clo->variance <= 0) {
        clo->variance = DEFAULT_VARIANCE;
    }
    DEBUG_MSG("Starting variance/volatility is: %f\n", clo->variance);
    
    if(clo->risk_free_rate <= 0) {
        clo->risk_free_rate = DEFAULT_RISK_FREE_RATE;
    }
    DEBUG_MSG("Using %f as a risk free rate\n", clo->risk_free_rate);

    if(clo->stock_price <= 0) {
        clo->stock_price = DEFAULT_STOCK_PRICE;
    }
    DEBUG_MSG("Starting stock price is $ %f\n", clo->stock_price);
    
    DEBUG_MSG("Mean reversion rate is %f\n", clo->mean_reversion_rate);
    if(clo->seed1 == 0) {
        clo->seed1 = defaultseed1;
    }
    if(clo->seed2 == 0) {
        clo->seed2 = defaultseed2;
    }
    DEBUG_MSG("Seeds are set to: 1=>%ld 2=>%ld\n",clo->seed1, clo->seed2);

    if(clo->variance_variance <= 0) {
        clo->variance_variance = DEFAULT_VARIANCE_VARIANCE;
    }
    DEBUG_MSG("Volatility of Volatility is set to %f\n", clo->variance_variance);
    
    return !error_encountered;
}
Ejemplo n.º 4
0
double* generate_random_constraints(char* seed, int N, int n, double* R) 
{
    // struct mt19937p state;
    // sgenrand(seed, &state);

    int i, j;

    initialize();
    int seed1, seed2;
    phrtsd(seed, &seed1, &seed2);
    set_initial_seed(seed1, seed2);
    // for (int i = 0; i < 10; i++)
    //     printf("Random N(0,1): %g\n", gennor(0, 1));

    float meanv[n];
    float varv[n];
    for (i = 0; i < n; i++) {
        float EY = 1.06 + 0.1*(pow(i, 1.1) / (n-1));
        float VarY = pow(0.05 + 0.45*(pow(i, 1.15) / (n-1)), 2);

        varv[i] = log(1 + (VarY / pow(EY,2)));
        meanv[i] = log(EY) - (varv[i] / 2);

        // meanv[i] = 0.057157648387530194;
        // varv[i] = 0.0022225194728912752;
    }

    // float varv[n];
    // for (int i = 0; i < n; i++) {
    //     varv[i] = 1;
    // }
    // float* covm = setcov(n, varv, 0);
    float covm[n*n];
    for (i = 0; i < n; i++)
    {
        for (j = 0; j < n; j++)
        {
            if (i == j) {
                covm[i+j*n] = varv[i];
            }
            else{
                covm[i+j*n] = 0;
            }
        }
    }

    float parm[n*(n+3)/2+1];
    setgmn(meanv, covm, n, parm);

    // Row major layout
    float* mvn;
    for (i = 0; i < N; i++) {
        float* mvn = genmn(parm);
        for (j = 0; j < n; j++) {
            R[i*n + j] = exp(mvn[j]);
            // R[i*n + j] = 1.0 + 0.1*genrand(&state) + 0.01; // [1.01, 1.11]
        }
        free(mvn);
    }

    return R;
}