示例#1
0
double SimpleMonteCarlo3(const VanillaOption& TheOption, 
						 double Spot, 
						 double Vol, 
						 double r, 
						 unsigned long NumberOfPaths)
{

    double Expiry = TheOption.GetExpiry();

	double variance = Vol*Vol*Expiry;
	double rootVariance = sqrt(variance);
	double itoCorrection = -0.5*variance;

	double movedSpot = Spot*exp(r*Expiry +itoCorrection);
	double thisSpot;
	double runningSum=0;

	for (unsigned long i=0; i < NumberOfPaths; i++)
	{
		double thisGaussian = GetOneGaussianByBoxMuller();
		thisSpot = movedSpot*exp( rootVariance*thisGaussian);
		double thisPayOff = TheOption.OptionPayOff(thisSpot);
		runningSum += thisPayOff;
	}

	double mean = runningSum / NumberOfPaths;
	mean *= exp(-r*Expiry);
	return mean;
}
void SimpleMonteCarlo(
	const VanillaOption& theOption,
	double Spot,
	Parameters Vol,
	Parameters r,
	unsigned long NumberOfPaths,
	StatisticsMC& gatherer,
	RandomBase& generator)
{
	double Expiry = theOption.GetExpiry();
	double variance = Vol.IntegralSquare(0, Expiry);
	double rootVariance = sqrt(variance);
	double itoCorrection = -0.5 * variance;

	double movedSpot = Spot * exp(r.Integral(0, Expiry) + itoCorrection);
	double thisSpot;

	double discounting = exp(-r.Integral(0, Expiry));

	std::vector<double> VariateVector(1);

	for (unsigned long i = 0; i < NumberOfPaths; i++)
	{
		generator.GetGaussian(VariateVector);
		thisSpot = movedSpot * exp(rootVariance * VariateVector[0]);
		gatherer.DumpOneResult(discounting * theOption.OptionPayOff(thisSpot));
	}
}
示例#3
0
void SimpleMonteCarloForLRDelta(const VanillaOption& TheOption, 
						 double Spot, 
						 const Parameters& Vol, 
						 const Parameters& r, 
                         unsigned long NumberOfPaths,
						 StatisticsMC& gatherer,
                         RandomBase& generator)
{
    generator.ResetDimensionality(1);

    double Expiry = TheOption.GetExpiry();
	double variance = Vol.IntegralSquare(0,Expiry); // sigma^2T
	double rootVariance = sqrt(variance);
	double itoCorrection = -0.5*variance;
	double movedSpot = Spot*exp(r.Integral(0,Expiry) +itoCorrection);
	double averageR = r.Integral(0,Expiry);
	double subtractNumerator = averageR + itoCorrection; 


	double thisSpot;
    double discounting = exp(-r.Integral(0,Expiry));

    MJArray VariateArray(1);

	for (unsigned long i=0; i < NumberOfPaths; i++)
	{
        generator.GetGaussians(VariateArray);
		thisSpot = movedSpot*exp( rootVariance*VariateArray[0]);
		double thisPayOff = (TheOption.OptionPayOff(thisSpot))*(log(thisSpot/Spot) - subtractNumerator)/(Spot*variance);
        gatherer.DumpOneResult(thisPayOff*discounting); // payoff*Psi/Phi see (p. 195 of JoshiConcepts)
	}

    return;
}
示例#4
0
void SimpleMonteCarlo6(const VanillaOption& TheOption, 
						 double Spot, 
						 const Parameters& Vol, 
						 const Parameters& r, 
                         unsigned long NumberOfPaths,
						 StatisticsMC& gatherer,
                         RandomBase& generator)
{
    generator.ResetDimensionality(1);

    double Expiry = TheOption.GetExpiry();
	double variance = Vol.IntegralSquare(0,Expiry);
	double rootVariance = sqrt(variance);
	double itoCorrection = -0.5*variance;
	double movedSpot = Spot*exp(r.Integral(0,Expiry) +itoCorrection);

	double thisSpot;
    double discounting = exp(-r.Integral(0,Expiry));

    MJArray VariateArray(1);

	for (unsigned long i=0; i < NumberOfPaths; i++)
	{
        generator.GetGaussians(VariateArray);
		thisSpot = movedSpot*exp( rootVariance*VariateArray[0]);
		double thisPayOff = TheOption.OptionPayOff(thisSpot);
        gatherer.DumpOneResult(thisPayOff*discounting);
	}

    return;
}
// Copies the member data
    void VanillaOption::copy(const VanillaOption& rhs)
    {
        K = rhs.getK();
        r = rhs.getr();
        T = rhs.getT();
        S = rhs.getS();
        sigma = rhs.getsigma();
    }
//copies the member data
void VanillaOption::copy(const VanillaOption& rhs)
{
	k = rhs.get_k();
	T = rhs.get_T();
	r = rhs.get_r();
	S = rhs.get_S();
	sigma = rhs.get_sigma();
}
示例#7
0
    const FUNCTIONSDLL_API double simpleMonteCarlo(
        const VanillaOption& vanillaOption, 
        const double spot, const Parameters& volatility,
        const Parameters& interestRate, const size_t numberOfPath,
        Statistics& gatherer)
    {
        const double maturity = vanillaOption.getMaturity();


        const int seed = 100;
        boost::random::mt19937 gen(seed);
        boost::random::uniform_01<> dist;
        boost::random::variate_generator<boost::random::mt19937&, boost::random::uniform_01<> > rand(gen, dist);



        const size_t numberOfSteps = 100;

        const double dt = maturity / numberOfSteps;

        boost::accumulators::accumulator_set<double, boost::accumulators::stats<boost::accumulators::tag::mean> > accumulator;
        
        const double variance = volatility.IntegralSquare(0.0, maturity);
        const double rootVariance = std::sqrt(variance);
        const double itoCorrection = -0.5 * variance;

        const double movedSpot = spot * std::exp(interestRate.Integral(0.0, maturity) + itoCorrection);
        double thisSpot = 0.0;

        const double discountFactor = std::exp(-interestRate.Integral(0.0, maturity));


        // create paths
        for (size_t i = 0; i < numberOfPath; ++i)
        {
            // by one step
            const double randomness = rand();
            thisSpot = movedSpot * std::exp(rootVariance * randomness);
            double thisPayoff = vanillaOption.getPayoff(thisSpot);

            //double nextSpot = spot;
            //for (size_t step = 0; step < numberOfSteps; ++step)
            //{
            //    nextSpot += nextSpot * (interestRate * dt - 0.5 * volatility * volatility * std::sqrt(dt) * dist(generator));

            //}
            gatherer.dumpOneResult(thisPayoff * discountFactor);
            accumulator(thisPayoff);
        }

        // assume interestRate is fixed.
        const double price = boost::accumulators::mean(accumulator) * discountFactor;

        return price;
    }
示例#8
0
void SimpleMonteCarlo_tol(const VanillaOption& TheOption,
						 double Spot,
						 const Parameters& Vol,
						 const Parameters& r,
                         double tol,
                         double maxLoops,
						 StatisticsMC& gatherer,
                         RandomBase& generator,
                         double target)
{
    generator.ResetDimensionality(1);

    double Expiry = TheOption.GetExpiry();
	double variance = Vol.IntegralSquare(0,Expiry);
	double rootVariance = sqrt(variance);
	double itoCorrection = -0.5*variance;
	double movedSpot = Spot*exp(r.Integral(0,Expiry) +itoCorrection);

	double thisSpot;
    double discounting = exp(-r.Integral(0,Expiry));

    MJArray VariateArray(1);

    double error = 100.0;
    double loops = 0.0;
	double running_sum = 0.0;
    double prev_res = 0.0;

	while( (abs(error) > tol) && (loops < maxLoops))
	{
        generator.GetGaussians(VariateArray);
		thisSpot = movedSpot*exp( rootVariance*VariateArray[0]);
		double thisPayOff = TheOption.OptionPayOff(thisSpot);
        gatherer.DumpOneResult(thisPayOff*discounting);
        if(loops > 0){
        	prev_res = running_sum/loops;
        	running_sum += thisPayOff*discounting;
        	loops++;
        	error = abs(target - running_sum/loops);
        }
        else{
        	running_sum += thisPayOff*discounting;
        	loops++;
        }



	}

    return;
}
示例#9
0
// Project 1 - Euler Stepping
void SimpleMonteCarlo7(const VanillaOption& TheOption, 
						 double Spot, 
						 const Parameters& Vol, 
						 const Parameters& r, 
                         unsigned long NumberOfPaths,
						 unsigned long NumberOfSteps,
						 StatisticsMC& gatherer,
                         RandomBase& generator)
{
    //generator.ResetDimensionality(1);
	generator.ResetDimensionality(NumberOfSteps);
    double Expiry = TheOption.GetExpiry();
	double deltaT = Expiry/NumberOfSteps;
	double variance = Vol.IntegralSquare(0,Expiry); // \sigma^2T
	double rootVariance = sqrt(variance/ NumberOfSteps); // \sigma \sqrt{Delta T}
	double rDeltaT= (r.Integral(0,Expiry)/Expiry)*deltaT; // this assumes r is constant. 
	//double itoCorrection = -0.5*variance; // -\sigma^2 T/2
	//double movedSpot = Spot*exp(r.Integral(0,Expiry) +itoCorrection);

	double thisSpot= Spot;
    double discounting = exp(-r.Integral(0,Expiry));

    MJArray VariateArray(NumberOfSteps);
	double nextSpot=0.0;

	for (unsigned long i=0; i < NumberOfPaths; i++)
	{	// I guess this method fills the MJArray with Gaussians
        generator.GetGaussians(VariateArray);
		thisSpot=Spot; //reset starting point for each path.
		for (unsigned long j= 0; j < NumberOfSteps; j++) 
		{
			nextSpot = thisSpot*(1 + rDeltaT + rootVariance*(VariateArray[j]));
			thisSpot=nextSpot;
		}
		double thisPayOff = TheOption.OptionPayOff(nextSpot);
        gatherer.DumpOneResult(thisPayOff*discounting);
	}

    return;
}
示例#10
0
void SimpleStepping_tol(const VanillaOption& TheOption,
		double Spot,
		const Parameters& Vol,
		const Parameters& r,
		double tol,
		double maxLoops,
		int numSteps,
		StatisticsMC& gatherer,
		RandomBase& generator,
		double target)
{

	generator.ResetDimensionality(numSteps);

    double Expiry = TheOption.GetExpiry();
	double variance = Vol.IntegralSquare(0,Expiry)/Expiry;
	double rootVariance = sqrt(variance);
	double rr = r.Integral(0,Expiry)/Expiry;
	double discounting = exp(-r.Integral(0,Expiry));

    MJArray VariateArray(numSteps);

    double error = 100.0;
    double loops = 0.0;
	double running_sum = 0.0;
    double prev_res = 0.0;



	while( (abs(error) > tol) && (loops < maxLoops))
	{
        generator.GetGaussians(VariateArray);
        //Very similar to simple MC but generates thisSpot by stepping
        double thisSpot = GeneratePath(r,
        							Spot,
        							Vol,
        							Expiry,
        							numSteps,
        							generator);

        double thisPayOff = TheOption.OptionPayOff(thisSpot);
        gatherer.DumpOneResult(thisPayOff*discounting);
        if(loops > 0){
        	prev_res = running_sum/loops;
        	running_sum += thisPayOff*discounting;
        	loops++;
        	error = abs(target - running_sum/loops);
        }
        else{
        	running_sum += thisPayOff*discounting;
        	loops++;
        }



	}

    return;



}