double value(Option opt){
	double s1=opt.spot*(CumulativeNormal(d(opt.spot/opt.Strike,opt.Expiry,opt,true))-CumulativeNormal(d(opt.spot/opt.Barrier,opt.Expiry,opt,true)));
	double s2=-exp(-opt.Rate*opt.Expiry)*  opt.Strike*(CumulativeNormal(d(  opt.spot/  opt.Strike, opt.Expiry,opt,false)) - CumulativeNormal(d( opt.spot/ opt.Barrier,opt.Expiry,opt,false)));
	double s3=-opt.Barrier*pow( opt.spot/ opt.Barrier,-2* opt.Rate/( opt.Vol* opt.Vol))*(CumulativeNormal(d( opt.Barrier* opt.Barrier/( opt.Strike* opt.spot), opt.Expiry,opt,true))-CumulativeNormal(d( opt.Barrier/ opt.spot, opt.Expiry,opt,true)));
	double s4=exp(- opt.Rate* opt.Expiry)* opt.Strike*pow( opt.spot/ opt.Barrier,(-2* opt.Rate/( opt.Vol* opt.Vol)+1))*(CumulativeNormal(d( (opt.Barrier* opt.Barrier/( opt.Strike* opt.spot)),opt.Expiry,opt,false))-CumulativeNormal(d( (opt.Barrier/ opt.spot),opt.Expiry,opt,false)));
	return s1+s2+s3+s4;
}
Beispiel #2
0
double CallOption::calc(double spot, double r, double d, double vol) {
	double standardDeviation = vol*sqrt(t_);
	double moneyness = log(spot / strike_);
	double d1 = (moneyness + (r - d)*t_ + 0.5* standardDeviation*standardDeviation) / standardDeviation;
	double d2 = d1 - standardDeviation;
	double price = spot*exp(-d*t_) * CumulativeNormal(d1)
		- strike_*exp(-r*t_)*CumulativeNormal(d2);
	return price;
}
Beispiel #3
0
double CallBS(const Wrapper<Parameter>& r, const Wrapper<Parameter>& sig, double Expiry,double Spot, double Strike)
{
	double d1,d2;

	d1=1./(sig->Mean(1,2)*sqrt(Expiry)) * ( log(Spot/Strike) + (r->Mean(1,2) + .5 * sig->Mean(1,2) * sig->Mean(1,2) ) * Expiry);
	d2=1./(sig->Mean(1,2)*sqrt(Expiry)) * ( log(Spot/Strike) + (r->Mean(1,2) - .5 * sig->Mean(1,2) * sig->Mean(1,2) ) * Expiry);
	double ans = CumulativeNormal(d1) * Spot - CumulativeNormal(d2) * Strike * exp(-r->Mean(1,2) * Expiry);

	return ans;
}
Beispiel #4
0
VAttrList
VBayes(VImage cbeta_images[], VImage sd_images[], int nimages, VBoolean level, VBoolean zscore) {
    VAttrList out_list = NULL;
    VImage dest = NULL, sigma_image = NULL;
    int    i, j, k, npixels;
    VFloat *dest_pp, *beta_pp[N], *sd_pp[N], *sigma_pp = NULL;
    VFloat pmin, pmax;
    double mean = 0, sigma = 0;
    float  rx, wsum, msum, w0, wx, s, msumold;
    float  result = 0;
    float  gmean[N], gvar[N];
    float  tiny = 1.0e-12;
    /*
    ** create output image
    */
    dest = VCopyImage(cbeta_images[0], NULL, VAllBands);
    if(!dest)
        return NULL;
    VFillImage(dest, VAllBands, 0);
    VSetAttr(VImageAttrList(dest), "num_images", NULL, VShortRepn, (VShort)nimages);
    VSetAttr(VImageAttrList(dest), "modality", NULL, VStringRepn, "bayes_map");
    VSetAttr(VImageAttrList(dest), "name", NULL, VStringRepn, "bayes");
    if(level == TRUE) {
        VSetAttr(VImageAttrList(dest), "modality", NULL, VStringRepn, "mean");
        VSetAttr(VImageAttrList(dest), "name", NULL, VStringRepn, "mean");
        sigma_image = VCopyImage(dest, NULL, VAllBands);
        if(!sigma_image)
            return NULL;
        VFillImage(sigma_image, VAllBands, 0);
        VSetAttr(VImageAttrList(sigma_image), "num_images", NULL, VShortRepn, (VShort)nimages);
        VSetAttr(VImageAttrList(sigma_image), "modality", NULL, VStringRepn, "std_dev");
        VSetAttr(VImageAttrList(sigma_image), "name", NULL, VStringRepn, "std_dev");
    }
    /*
    ** for each voxel
    */
    pmax = VRepnMinValue(VFloatRepn);
    pmin = VRepnMaxValue(VFloatRepn);
    npixels = VImageNPixels(cbeta_images[0]);
    for(i = 0; i < nimages; i++) {
        beta_pp[i] = (VFloat *) VImageData(cbeta_images[i]);
        sd_pp[i]   = (VFloat *) VImageData(sd_images[i]);
    }
    dest_pp = (VFloat *) VImageData(dest);
    if(level == TRUE)
        sigma_pp = (VFloat *) VImageData(sigma_image);
    for(j = 0; j < npixels; j++) {
        if(j % 10000 == 0)
            fprintf(stderr, "...%.2f%%\r", (float)100 * j / (float)npixels);
        result = mean = sigma = 0;
        /*
        ** read data from input images
        */
        for(k = 0; k < nimages; k++) {
            gmean[k] = *beta_pp[k]++;  /* mean c*beta */
            rx       = *sd_pp[k]++;    /* standard deviation of c*beta*/
            gvar[k]  = rx * rx;        /* variation of c*beta*/
        }
        /* see Box,Tiao, pp.17-18     calculate probability distribution */
        wsum = 0;
        msum = 0;
        w0 = 0;
        for(k = 0; k < nimages; k++) {    /* for each image */
            s = gvar[k];
            if(s < tiny)
                goto next;
            s = sqrt((double)s);
            if(s < tiny)
                goto next;
            wx = 1.0 / (s * s);
            wsum = w0 + wx;
            msumold = msum;
            msum = (w0 * msum  +  wx * gmean[k]) / wsum;
            w0 = wsum;
            sigma = 1.0 / sqrt(wsum);
        }
        if(wsum < tiny)
            goto next;
        /* resulting mean and std dev */
        mean = msum;
        sigma = 1.0 / sqrt(wsum);
        if(level == TRUE) {
            result = mean;
            goto next;
        }
        /* calculate probability under distribution */
        result = CumulativeNormal(mean, sigma);
        /* output */
        if(zscore) {
            /* CHECK HERE IF "1.0-" is correct */
            result = (float)p2z((double)(1.0 - result));
            if(mean < 0)
                result = -result;
            if(result >  20)
                result =  20;
            if(result < -20)
                result = -20;
        } else {
            result *= 100.0;
            if(result >  100)
                result =  100;
            if(mean < 0)
                result = -result;
        }
        if(result < pmin)
            pmin = result;
        if(result > pmax)
            pmax = result;
next:
        *dest_pp++ = result;
        if(level)
            *sigma_pp++ = sigma;
    }
    out_list = VCreateAttrList();
    fprintf(stderr, "...100.00%%\n");
    if(level == FALSE) {
        fprintf(stderr, "\n min: %.3f, max: %.3f\n", pmin, pmax);
        VAppendAttr(out_list, "zmap", NULL, VImageRepn, dest);
        return out_list;
    } else {
        VAppendAttr(out_list, "mean", NULL, VImageRepn, dest);
        VAppendAttr(out_list, "std_dev", NULL, VImageRepn, sigma_image);
        return out_list;
    }
    return NULL;
}