38 class DecoratedHedge :
public CallSpecifiedMultiProduct {
40 explicit DecoratedHedge(
const CallSpecifiedMultiProduct& product)
41 : CallSpecifiedMultiProduct(product) {
42 savedStates_.reserve(product.evolution().numberOfSteps());
44 Size N = product.numberOfProducts();
47 for (
Size i=0; i<N; ++i)
48 cashFlowsGenerated_[i].resize(
49 product.maxNumberOfCashFlowsPerProductPerStep());
54 void reset()
override {
60 numberCashFlowsThisStep_,
65 bool nextTimeStep(
const CurveState& currentState,
66 std::vector<Size>& numberCashFlowsThisStep,
67 std::vector<std::vector<CashFlow> >& cashFlowsGenerated)
override {
72 numberCashFlowsThisStep,
76 std::unique_ptr<MarketModelMultiProduct> clone()
const override {
77 return std::unique_ptr<MarketModelMultiProduct>(
new DecoratedHedge(*
this));
90 void startRecording() {
94 void stopRecording() {
109 ext::shared_ptr<MarketModelEvolver> evolver,
110 std::vector<ext::shared_ptr<MarketModelEvolver> > innerEvolvers,
116 Real initialNumeraireValue)
117 : evolver_(
std::move(evolver)), innerEvolvers_(
std::move(innerEvolvers)),
140 const std::vector<Time>& evolutionTimes =
154 const std::vector<Time>& cashFlowTimes =
156 const std::vector<Rate>& rateTimes =
158 Size n =cashFlowTimes.size();
160 for (
Size j=0; j<
n; ++j)
168 for (
Size i=0; i<outerPaths; ++i) {
170 stats.add(result.first, result.second);
177 auto& callable =
dynamic_cast<DecoratedHedge&
>(
composite_.
item(4));
182 Real numerairesHeld = 0.0;
186 callable.disableCallability();
187 Real principalInNumerairePortfolio = 1.0;
199 Real underlyingCashFlows =
201 principalInNumerairePortfolio,
206 Real hedgeCashFlows =
208 principalInNumerairePortfolio,
215 Real rebateCashFlow =
217 principalInNumerairePortfolio,
221 Real hedgeRebateCashFlow =
223 principalInNumerairePortfolio,
228 numerairesHeld += underlyingCashFlows - hedgeCashFlows;
233 Real unexercisedHedgeValue = 0.0;
242 ext::shared_ptr<MarketModelEvolver> currentEvolver =
244 currentEvolver->setInitialState(
evolver_->currentState());
246 callable.stopRecording();
247 callable.enableCallability();
257 const std::vector<Real>& values = innerStats.
mean();
258 unexercisedHedgeValue =
259 std::accumulate(values.begin(), values.end(),
Real(0.0))
260 / principalInNumerairePortfolio;
262 callable.disableCallability();
263 callable.startRecording();
269 Real portfolioValue = numerairesHeld;
273 rebateCashFlow - hedgeRebateCashFlow;
276 unexercisedHedgeValue - hedgeRebateCashFlow;
279 rebateCashFlow - unexercisedHedgeValue;
283 maximumValue = std::max(maximumValue, portfolioValue);
300 principalInNumerairePortfolio *=
301 evolver_->currentState().discountRatio(numeraire,
309 maximumValue = std::max(maximumValue, numerairesHeld);
315 return std::make_pair(maximumValue, weight);
320 Real principalInNumerairePortfolio,
322 Size endProduct)
const {
325 Real numeraireUnits = 0.0;
327 for (
Size i=beginProduct; i<endProduct; ++i) {
329 const std::vector<MarketModelMultiProduct::CashFlow>& cashflows =
338 numeraireUnits += cashflows[j].amount *
343 return numeraireUnits/principalInNumerairePortfolio;
Engine collecting cash flows along a market-model simulation.
void multiplePathValues(SequenceStatisticsInc &stats, Size numberOfPaths)
bool nextTimeStep(const CurveState ¤tState, std::vector< Size > &numberCashFlowsThisStep, std::vector< std::vector< CashFlow > > &cashFlowsGenerated) override
return value indicates whether path is finished, TRUE means done
void reset() override
during simulation put product at start of path
const std::vector< Time > & rateTimes() const
const std::vector< Time > & evolutionTimes() const
virtual std::vector< Time > exerciseTimes() const =0
virtual bool exercise(const State ¤tState) const =0
empirical-distribution risk measures
Statistics analysis of N-dimensional (sequence) data.
std::vector< Real > mean() const
std::vector< Time > possibleCashFlowTimes() const override
const EvolutionDescription & evolution() const override
const MarketModelMultiProduct & item(Size i) const
void add(const Clone< MarketModelMultiProduct > &, Real multiplier=1.0)
void reset() override
during simulation put product at start of path
Real numeraireBonds(const CurveState &, Size numeraire) const
virtual Size numberOfProducts() const =0
Composition of one or more market-model products.
bool nextTimeStep(const CurveState ¤tState, std::vector< Size > &numberCashFlowsThisStep, std::vector< std::vector< CashFlow > > &cashFlowsGenerated) override
return value indicates whether path is finished, TRUE means done
Size maxNumberOfCashFlowsPerProductPerStep() const override
Size numberOfProducts() const override
ext::shared_ptr< MarketModelEvolver > evolver_
UpperBoundEngine(ext::shared_ptr< MarketModelEvolver > evolver, std::vector< ext::shared_ptr< MarketModelEvolver > > innerEvolvers, const MarketModelMultiProduct &underlying, const MarketModelExerciseValue &rebate, const MarketModelMultiProduct &hedge, const MarketModelExerciseValue &hedgeRebate, const ExerciseStrategy< CurveState > &hedgeStrategy, Real initialNumeraireValue)
std::vector< MarketModelDiscounter > discounters_
Real initialNumeraireValue_
std::vector< ext::shared_ptr< MarketModelEvolver > > innerEvolvers_
MultiProductComposite composite_
std::vector< std::vector< MarketModelMultiProduct::CashFlow > > cashFlowsGenerated_
std::vector< Size > numberCashFlowsThisStep_
std::valarray< bool > isExerciseTime_
std::pair< Real, Real > singlePathValue(Size innerPaths)
Real collectCashFlows(Size currentStep, Real principalInNumerairePortfolio, Size beginProduct, Size endProduct) const
void multiplePathValues(Statistics &stats, Size outerPaths, Size innerPaths)
std::size_t Size
size of a container
std::valarray< bool > isInSubset(const std::vector< Time > &set, const std::vector< Time > &subset)
std::vector< Size > numberCashFlowsThisStep_
std::vector< Clone< CurveState > > savedStates_
std::vector< std::vector< CashFlow > > cashFlowsGenerated_