Example #1
0
void RngTraitsTest::testGaussian() {

    BOOST_TEST_MESSAGE("Testing Gaussian pseudo-random number generation...");

    PseudoRandom::rsg_type rsg =
        PseudoRandom::make_sequence_generator(100, 1234);

    const std::vector<Real>& values = rsg.nextSequence().value;
    Real sum = 0.0;
    for (Size i=0; i<values.size(); i++)
        sum += values[i];

    Real stored = 4.09916;
    Real tolerance = 1.0e-5;
    if (std::fabs(sum - stored) > tolerance)
        BOOST_FAIL("the sum of the samples does not match the stored value\n"
                   << "    calculated: " << sum << "\n"
                   << "    expected:   " << stored);
}
void MCIRDLSLSEngine::calculate() const {

	for(Size i=0;i<additionalStats_.size();++i) {
		additionalStats_[i].reset();
	}

	pathPricer_ = this->lsmPathPricer();
	Size NeedRnd=randNum();
	PseudoRandom::rsg_type gen  = PseudoRandom::make_sequence_generator(NeedRnd, seed_);
	const std::vector<Real>& rand = gen.nextSequence().value;

	//Real values=this->pathPricer_->values(rand,nCalibrationSamples_);
	Real values=this->pathPricer_->values(rand,maxSamples_);
	this->pathPricer_->calibrate(nCalibrationSamples_);

	values=this->pathPricer_->values(rand,maxSamples_);

	results_.additionalResults["ImpliedCall"] = values;

	const std::vector<Date> & fixings=this->arguments_.fixingDates;
	Size sizeOfCouponNum=fixings.size();

	Leg expectedCashflows;
	Leg pastLeg = arguments_.payoffLeg->leg();
	expectedCashflows=pastLeg;


	std::vector<Real> cashflows(sizeOfCouponNum);
	std::vector<Real> cashflowsDates(sizeOfCouponNum);
	std::vector<Real> earlyExProbability;

	std::vector<Real>& earlyExProbabilityM = pathPricer_->earlyExProbability(); // 그거 지난 일자는 빠진거.

	//
	//for(Size i=0;i<additionalStats_.size();++i) {
	//	expectedCashflows[sizeOfCouponNum-i-1]=boost::shared_ptr<CashFlow>(
	//				new SimpleCashFlow(additionalStats_[sizeOfCouponNum-i-1].mean(),fixings[sizeOfCouponNum-i-1]));
	//	cashflows[sizeOfCouponNum-i-1]=additionalStats_[sizeOfCouponNum-i-1].mean();
	//
	//	//std::cout << additionalStats_[i].mean() << std::endl;
	//}

	Date today = Settings::instance().evaluationDate();
	Size numberOfPastFixings=0;
	for(Size i=0;i<sizeOfCouponNum;++i){
		if(fixings[i]<today){
			numberOfPastFixings=numberOfPastFixings+1;
		}
	}

	for(Size i=0;i<numberOfPastFixings;++i) {
		expectedCashflows[i]=boost::shared_ptr<CashFlow>(
					new SimpleCashFlow((arguments_.payoffLeg->payoff())[i]->amount(),fixings[i]));
		cashflows[i]=(arguments_.payoffLeg->payoff())[i]->amount(),fixings[i];
		earlyExProbability.push_back(0.0);
	}

	for(Size i=0;i<additionalStats_.size();++i) {

		expectedCashflows[sizeOfCouponNum-i-1]=boost::shared_ptr<CashFlow>(
					new SimpleCashFlow(additionalStats_[additionalStats_.size()-i-1].mean(),fixings[sizeOfCouponNum-i-1]));
		cashflows[sizeOfCouponNum-i-1]=additionalStats_[additionalStats_.size()-i-1].mean();
		earlyExProbability.push_back(earlyExProbabilityM[i]/maxSamples_);

	}

	results_.additionalResults["cashflows"] = cashflows;

	/*for(Size i=0;i<additionalStats_.size();++i) {
		std::cout << "cashflows : "<< cashflows[i] << std::endl;
	}*/
	for(Size i=0;i<sizeOfCouponNum;++i){
		cashflowsDates[i]=static_cast<Real>(fixings[i].serialNumber());
	}
	
	results_.additionalResults["cashflowsDates"] = cashflowsDates;
	results_.additionalResults["EarlyExProbability"] = earlyExProbability;
	
	//pricer에서 Notional을 더해서옴. 언제 상환될지 모르므로...

	//Real Notional=arguments_.Notional;
	//expectedCashflows.push_back(boost::shared_ptr<CashFlow>(new SimpleCashFlow(Notional,fixings.back())));

	results_.expectedCashflows=expectedCashflows;
	//expected Bond Price도 넣어야하나..?

	QL_REQUIRE(!discountTS_.empty(),
			   "discounting term structure handle is empty");

	results_.valuationDate = Settings::instance().evaluationDate();
	
	bool includeRefDateFlows =false;
	Real Notional=arguments_.Notional;
	results_.additionalResults["nonCallableValue"] = pathPricer_->expectedBondPrice()*10000/maxSamples_;
	results_.additionalResults["nonCallableUnitValue"] = pathPricer_->expectedBondPrice()*100000000/(maxSamples_*Notional);


	results_.value = CashFlows::npv(results_.expectedCashflows,
									**discountTS_,
									includeRefDateFlows,
									results_.valuationDate,
									results_.valuationDate);

	results_.settlementValue = CashFlows::npv(results_.expectedCashflows,
											  **discountTS_,
											  false,
											  arguments_.settlementDate,
											  arguments_.settlementDate);

	results_.additionalResults["UnitValue"] = results_.value*10000/Notional;

}