Skip to content

Commit

Permalink
boost::shared_ptr used throughout instead of Handle
Browse files Browse the repository at this point in the history
  • Loading branch information
lballabio committed Mar 25, 2004
1 parent 9517da1 commit c093842
Show file tree
Hide file tree
Showing 258 changed files with 2,403 additions and 2,150 deletions.
2 changes: 1 addition & 1 deletion Docs/pages/coreclasses.docs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<ul>
<li>numeric types such as Time, Rate, Spread, or DiscountFactor;</li>

<li>concrete data classes such as Date, Handle, Currency, or History;
<li>concrete data classes such as Date, Currency, or History;
base classes such as Calendar or DayCounter; and helper classes such
as Scheduler;

Expand Down
45 changes: 24 additions & 21 deletions Examples/AmericanOption/AmericanOption.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,26 +76,28 @@ int main(int argc, char* argv[])
exDates[0]=midlifeDate;
exDates[1]=exerciseDate;

Handle<Exercise> exercise(new EuropeanExercise(exerciseDate));
Handle<Exercise> amExercise(new AmericanExercise(settlementDate,
exerciseDate));
Handle<Exercise> berExercise(new BermudanExercise(exDates));
boost::shared_ptr<Exercise> exercise(
new EuropeanExercise(exerciseDate));
boost::shared_ptr<Exercise> amExercise(
new AmericanExercise(settlementDate,
exerciseDate));
boost::shared_ptr<Exercise> berExercise(new BermudanExercise(exDates));


RelinkableHandle<Quote> underlyingH(
Handle<Quote>(new SimpleQuote(underlying)));
boost::shared_ptr<Quote>(new SimpleQuote(underlying)));

// bootstrap the yield/dividend/vol curves
RelinkableHandle<TermStructure> flatTermStructure(
Handle<TermStructure>(
boost::shared_ptr<TermStructure>(
new FlatForward(todaysDate, settlementDate,
riskFreeRate, rateDayCounter)));
RelinkableHandle<TermStructure> flatDividendTS(
Handle<TermStructure>(
boost::shared_ptr<TermStructure>(
new FlatForward(todaysDate, settlementDate,
dividendYield, rateDayCounter)));
RelinkableHandle<BlackVolTermStructure> flatVolTS(
Handle<BlackVolTermStructure>(
boost::shared_ptr<BlackVolTermStructure>(
new BlackConstantVol(settlementDate, volatility)));

std::vector<Date> dates(4);
Expand All @@ -119,13 +121,14 @@ int main(int argc, char* argv[])
vols[3][0] = volatility*1.1; vols[3][1] = volatility;
vols[3][2] = volatility*0.9; vols[3][3] = volatility*0.8;
RelinkableHandle<BlackVolTermStructure> blackSurface(
Handle<BlackVolTermStructure>(
new BlackVarianceSurface(settlementDate, dates, strikes, vols)));
boost::shared_ptr<BlackVolTermStructure>(
new BlackVarianceSurface(settlementDate, dates,
strikes, vols)));

Handle<StrikedTypePayoff> payoff(new
boost::shared_ptr<StrikedTypePayoff> payoff(new
PlainVanillaPayoff(type, strike));

Handle<BlackScholesStochasticProcess> stochasticProcess(new
boost::shared_ptr<BlackScholesStochasticProcess> stochasticProcess(new
BlackScholesStochasticProcess(
underlyingH,
flatDividendTS,
Expand All @@ -134,7 +137,7 @@ int main(int argc, char* argv[])

// European option
VanillaOption euroOption(stochasticProcess, payoff, exercise,
Handle<PricingEngine>(new AnalyticEuropeanEngine()));
boost::shared_ptr<PricingEngine>(new AnalyticEuropeanEngine()));

// method: Black Scholes Engine
method = "equivalent european option ";
Expand All @@ -152,7 +155,7 @@ int main(int argc, char* argv[])

// Binomial Method (JR)
method = "Binomial Jarrow-Rudd ";
option.setPricingEngine(Handle<PricingEngine>(
option.setPricingEngine(boost::shared_ptr<PricingEngine>(
new BinomialVanillaEngine<JarrowRudd>(timeSteps)));
value = option.NPV();
discrepancy = QL_FABS(value-rightValue);
Expand All @@ -165,7 +168,7 @@ int main(int argc, char* argv[])

// Binomial Method (CRR)
method = "Binomial Cox-Ross-Rubinstein ";
option.setPricingEngine(Handle<PricingEngine>(
option.setPricingEngine(boost::shared_ptr<PricingEngine>(
new BinomialVanillaEngine<CoxRossRubinstein>(timeSteps)));
value = option.NPV();
discrepancy = QL_FABS(value-rightValue);
Expand All @@ -178,7 +181,7 @@ int main(int argc, char* argv[])

// Equal Probability Additive Binomial Tree (EQP)
method = "Additive Equiprobabilities ";
option.setPricingEngine(Handle<PricingEngine>(
option.setPricingEngine(boost::shared_ptr<PricingEngine>(
new BinomialVanillaEngine<AdditiveEQPBinomialTree>(timeSteps)));
value = option.NPV();
discrepancy = QL_FABS(value-rightValue);
Expand All @@ -191,7 +194,7 @@ int main(int argc, char* argv[])

// Equal Jumps Additive Binomial Tree (Trigeorgis)
method = "Binomial Trigeorgis ";
option.setPricingEngine(Handle<PricingEngine>(
option.setPricingEngine(boost::shared_ptr<PricingEngine>(
new BinomialVanillaEngine<Trigeorgis>(timeSteps)));
value = option.NPV();
discrepancy = QL_FABS(value-rightValue);
Expand All @@ -204,7 +207,7 @@ int main(int argc, char* argv[])

// Tian Binomial Tree (third moment matching)
method = "Binomial Tian ";
option.setPricingEngine(Handle<PricingEngine>(
option.setPricingEngine(boost::shared_ptr<PricingEngine>(
new BinomialVanillaEngine<Tian>(timeSteps)));
value = option.NPV();
discrepancy = QL_FABS(value-rightValue);
Expand All @@ -217,7 +220,7 @@ int main(int argc, char* argv[])

// Leisen-Reimer Binomial Tree
method = "Binomial Leisen-Reimer ";
option.setPricingEngine(Handle<PricingEngine>(
option.setPricingEngine(boost::shared_ptr<PricingEngine>(
new BinomialVanillaEngine<LeisenReimer>(timeSteps)));
value = option.NPV();
discrepancy = QL_FABS(value-rightValue);
Expand All @@ -230,7 +233,7 @@ int main(int argc, char* argv[])

// Barone-Adesi and Whaley approximation
method = "Barone-Adesi and Whaley approx. ";
option.setPricingEngine(Handle<PricingEngine>(
option.setPricingEngine(boost::shared_ptr<PricingEngine>(
new BaroneAdesiWhaleyApproximationEngine));
value = option.NPV();
discrepancy = QL_FABS(value-rightValue);
Expand All @@ -243,7 +246,7 @@ int main(int argc, char* argv[])

// Bjerksund and Stensland approximation
method = "Bjerksund and Stensland approx. ";
option.setPricingEngine(Handle<PricingEngine>(
option.setPricingEngine(boost::shared_ptr<PricingEngine>(
new BjerksundStenslandApproximationEngine));
value = option.NPV();
discrepancy = QL_FABS(value-rightValue);
Expand Down
80 changes: 43 additions & 37 deletions Examples/BermudanSwaption/BermudanSwaption.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ double swaptionVols[] = {
12.00, 11.40, 11.5, 10.8, 10.3, 10.00, 9.80, 9.60, 9.5, 9.10,
11.50, 11.20, 11.3, 10.6, 10.2, 10.10, 9.70, 9.50, 9.4, 8.60};

void calibrateModel(const Handle<ShortRateModel>& model,
void calibrateModel(const boost::shared_ptr<ShortRateModel>& model,
CalibrationSet& calibs,
double lambda) {

Expand Down Expand Up @@ -83,7 +83,7 @@ int main(int argc, char* argv[])
Date settlementDate(19, February, 2002);

//Instruments used to bootstrap the yield curve:
std::vector<Handle<RateHelper> > instruments;
std::vector<boost::shared_ptr<RateHelper> > instruments;

//Deposit rates
DayCounter depositDayCounter = Thirty360();
Expand All @@ -93,8 +93,8 @@ int main(int argc, char* argv[])
Size i;
for (i=0; i<3; i++) {
RelinkableHandle<Quote> depositRate(
Handle<Quote>(new SimpleQuote(weekRates[i]*0.01)));
Handle<RateHelper> depositHelper(new DepositRateHelper(
boost::shared_ptr<Quote>(new SimpleQuote(weekRates[i]*0.01)));
boost::shared_ptr<RateHelper> depositHelper(new DepositRateHelper(
depositRate, i+1, Weeks, settlementDays, calendar,
ModifiedFollowing, depositDayCounter));
instruments.push_back(depositHelper);
Expand All @@ -106,8 +106,9 @@ int main(int argc, char* argv[])

for (i=0; i<11; i++) {
RelinkableHandle<Quote> depositRate(
Handle<Quote>(new SimpleQuote(depositRates[i]*0.01)));
Handle<RateHelper> depositHelper(new DepositRateHelper(
boost::shared_ptr<Quote>(
new SimpleQuote(depositRates[i]*0.01)));
boost::shared_ptr<RateHelper> depositHelper(new DepositRateHelper(
depositRate, i+1, Months, settlementDays, calendar,
ModifiedFollowing, depositDayCounter));
instruments.push_back(depositHelper);
Expand All @@ -125,8 +126,9 @@ int main(int argc, char* argv[])
int swFloatingLegFrequency = 2;

for (i=0; i<13; i++) {
Handle<Quote> swapRate(new SimpleQuote(swapRates[i]*0.01));
Handle<RateHelper> swapHelper(new SwapRateHelper(
boost::shared_ptr<Quote> swapRate(
new SimpleQuote(swapRates[i]*0.01));
boost::shared_ptr<RateHelper> swapHelper(new SwapRateHelper(
RelinkableHandle<Quote>(swapRate),
swapYears[i], Years, settlementDays,
calendar, ModifiedFollowing,
Expand All @@ -138,7 +140,7 @@ int main(int argc, char* argv[])


// bootstrapping the yield curve
Handle<PiecewiseFlatForward> myTermStructure(new
boost::shared_ptr<PiecewiseFlatForward> myTermStructure(new
PiecewiseFlatForward(todaysDate, settlementDate, instruments,
depositDayCounter));

Expand All @@ -154,26 +156,27 @@ int main(int argc, char* argv[])
bool payFixedRate = true;
int fixingDays = 2;
Rate dummyFixedRate = 0.03;
Handle<Xibor> indexSixMonths(new Euribor(6, Months, rhTermStructure));
boost::shared_ptr<Xibor> indexSixMonths(
new Euribor(6, Months, rhTermStructure));

Handle<SimpleSwap> swap(new SimpleSwap(
boost::shared_ptr<SimpleSwap> swap(new SimpleSwap(
payFixedRate, settlementDate.plusYears(1), 5, Years,
calendar, roll, 1000.0, fixedLegFrequency, dummyFixedRate,
fixedLegIsAdjusted, fixedLegDayCounter, floatingLegFrequency,
indexSixMonths, fixingDays, 0.0, rhTermStructure));
Rate fixedATMRate = swap->fairRate();

Handle<SimpleSwap> atmSwap(new SimpleSwap(
boost::shared_ptr<SimpleSwap> atmSwap(new SimpleSwap(
payFixedRate, settlementDate.plusYears(1), 5, Years,
calendar, roll, 1000.0, fixedLegFrequency, fixedATMRate,
fixedLegIsAdjusted, fixedLegDayCounter, floatingLegFrequency,
indexSixMonths, fixingDays, 0.0, rhTermStructure));
Handle<SimpleSwap> otmSwap(new SimpleSwap(
boost::shared_ptr<SimpleSwap> otmSwap(new SimpleSwap(
payFixedRate, settlementDate.plusYears(1), 5, Years,
calendar, roll, 1000.0, fixedLegFrequency, fixedATMRate * 1.2,
fixedLegIsAdjusted, fixedLegDayCounter, floatingLegFrequency,
indexSixMonths, fixingDays, 0.0, rhTermStructure));
Handle<SimpleSwap> itmSwap(new SimpleSwap(
boost::shared_ptr<SimpleSwap> itmSwap(new SimpleSwap(
payFixedRate, settlementDate.plusYears(1), 5, Years,
calendar, roll, 1000.0, fixedLegFrequency, fixedATMRate * 0.8,
fixedLegIsAdjusted, fixedLegDayCounter, floatingLegFrequency,
Expand All @@ -199,8 +202,9 @@ int main(int argc, char* argv[])
for (i=0; i<numRows; i++) {
for (unsigned int j=0; j<numCols; j++) {
unsigned int k = i*10 + j;
Handle<Quote> vol(new SimpleQuote(swaptionVols[k]*0.01));
swaptions.push_back(Handle<CalibrationHelper>(
boost::shared_ptr<Quote> vol(
new SimpleQuote(swaptionVols[k]*0.01));
swaptions.push_back(boost::shared_ptr<CalibrationHelper>(
new SwaptionHelper(swaptionMaturities[j],
Period(swaptionLengths[i], Years),
RelinkableHandle<Quote>(vol),
Expand All @@ -215,15 +219,16 @@ int main(int argc, char* argv[])
// Building time-grid
TimeGrid grid(times.begin(), times.end(), 30);

Handle<HullWhite> modelHW(new HullWhite(rhTermStructure));
Handle<HullWhite> modelHW2(new HullWhite(rhTermStructure));
Handle<BlackKarasinski> modelBK(new BlackKarasinski(rhTermStructure));
boost::shared_ptr<HullWhite> modelHW(new HullWhite(rhTermStructure));
boost::shared_ptr<HullWhite> modelHW2(new HullWhite(rhTermStructure));
boost::shared_ptr<BlackKarasinski> modelBK(
new BlackKarasinski(rhTermStructure));

std::cout << "Calibrating to swaptions" << std::endl;

std::cout << "Hull-White (analytic formulae):" << std::endl;
swaptions.setPricingEngine(
Handle<PricingEngine>(new JamshidianSwaption(modelHW)));
boost::shared_ptr<PricingEngine>(new JamshidianSwaption(modelHW)));


calibrateModel(modelHW, swaptions, 0.05);
Expand All @@ -234,7 +239,7 @@ int main(int argc, char* argv[])

std::cout << "Hull-White (numerical calibration):" << std::endl;
swaptions.setPricingEngine(
Handle<PricingEngine>(new TreeSwaption(modelHW2, grid)));
boost::shared_ptr<PricingEngine>(new TreeSwaption(modelHW2,grid)));


calibrateModel(modelHW2, swaptions, 0.05);
Expand All @@ -245,7 +250,7 @@ int main(int argc, char* argv[])

std::cout << "Black-Karasinski: " << std::endl;
swaptions.setPricingEngine(
Handle<PricingEngine>(new TreeSwaption(modelBK, grid)));
boost::shared_ptr<PricingEngine>(new TreeSwaption(modelBK,grid)));
calibrateModel(modelBK, swaptions, 0.05);
std::cout << "calibrated to "
<< modelBK->params()
Expand All @@ -256,70 +261,71 @@ int main(int argc, char* argv[])

//Define the bermudan swaption
std::vector<Date> bermudanDates;
const std::vector<Handle<CashFlow> >& leg = swap->floatingLeg();
const std::vector<boost::shared_ptr<CashFlow> >& leg =
swap->floatingLeg();
for (i=0; i<leg.size(); i++) {
Handle<Coupon> coupon =
boost::shared_ptr<Coupon> coupon =
boost::dynamic_pointer_cast<Coupon>(leg[i]);
bermudanDates.push_back(coupon->accrualStartDate());
}

Handle<Exercise> bermudaExercise(new BermudanExercise(
boost::shared_ptr<Exercise> bermudaExercise(new BermudanExercise(
bermudanDates));

Swaption bermudanSwaption(atmSwap,
bermudaExercise,
rhTermStructure,
Handle<PricingEngine>(new TreeSwaption(modelHW, 100)));
boost::shared_ptr<PricingEngine>(new TreeSwaption(modelHW, 100)));

//Do the pricing for each model
bermudanSwaption.setPricingEngine(
Handle<PricingEngine>(new TreeSwaption(modelHW, 100)));
boost::shared_ptr<PricingEngine>(new TreeSwaption(modelHW, 100)));
std::cout << "HW: " << bermudanSwaption.NPV() << std::endl;

bermudanSwaption.setPricingEngine(
Handle<PricingEngine>(new TreeSwaption(modelHW2, 100)));
boost::shared_ptr<PricingEngine>(new TreeSwaption(modelHW2, 100)));
std::cout << "HW (num): " << bermudanSwaption.NPV() << std::endl;

bermudanSwaption.setPricingEngine(
Handle<PricingEngine>(new TreeSwaption(modelBK, 100)));
boost::shared_ptr<PricingEngine>(new TreeSwaption(modelBK, 100)));
std::cout << "BK: " << bermudanSwaption.NPV() << std::endl;

std::cout << "Pricing an OTM bermudan swaption" << std::endl;

Swaption otmBermudanSwaption(otmSwap,
bermudaExercise,
rhTermStructure,
Handle<PricingEngine>(new TreeSwaption(modelHW, 100)));
boost::shared_ptr<PricingEngine>(new TreeSwaption(modelHW, 100)));

//Do the pricing for each model
otmBermudanSwaption.setPricingEngine(
Handle<PricingEngine>(new TreeSwaption(modelHW, 100)));
boost::shared_ptr<PricingEngine>(new TreeSwaption(modelHW, 100)));
std::cout << "HW: " << otmBermudanSwaption.NPV() << std::endl;

otmBermudanSwaption.setPricingEngine(
Handle<PricingEngine>(new TreeSwaption(modelHW2, 100)));
boost::shared_ptr<PricingEngine>(new TreeSwaption(modelHW2, 100)));
std::cout << "HW (num): " << otmBermudanSwaption.NPV() << std::endl;

otmBermudanSwaption.setPricingEngine(
Handle<PricingEngine>(new TreeSwaption(modelBK, 100)));
boost::shared_ptr<PricingEngine>(new TreeSwaption(modelBK, 100)));
std::cout << "BK: " << otmBermudanSwaption.NPV() << std::endl;

std::cout << "Pricing an ITM bermudan swaption" << std::endl;

Swaption itmBermudanSwaption(itmSwap,
bermudaExercise,
rhTermStructure,
Handle<PricingEngine>(new TreeSwaption(modelHW, 100)));
boost::shared_ptr<PricingEngine>(new TreeSwaption(modelHW, 100)));

//Do the pricing for each model
itmBermudanSwaption.setPricingEngine(
Handle<PricingEngine>(new TreeSwaption(modelHW, 100)));
boost::shared_ptr<PricingEngine>(new TreeSwaption(modelHW, 100)));
std::cout << "HW: " << itmBermudanSwaption.NPV() << std::endl;
itmBermudanSwaption.setPricingEngine(
Handle<PricingEngine>(new TreeSwaption(modelHW2, 100)));
boost::shared_ptr<PricingEngine>(new TreeSwaption(modelHW2, 100)));
std::cout << "HW (num): " << itmBermudanSwaption.NPV() << std::endl;
itmBermudanSwaption.setPricingEngine(
Handle<PricingEngine>(new TreeSwaption(modelBK, 100)));
boost::shared_ptr<PricingEngine>(new TreeSwaption(modelBK, 100)));
std::cout << "BK: " << itmBermudanSwaption.NPV() << std::endl;

return 0;
Expand Down
Loading

0 comments on commit c093842

Please sign in to comment.