Returning Multidimensional Monte Carlo Statistics in QuantLib
Summary
The document explains a type mismatch encountered when building a QuantLib Monte Carlo pricing engine whose path pricer returns an Array. The simulation expects its statistics accumulator to provide results compatible with that type, but SequenceStatistics returns std::vector<Real> for its mean and error estimate. Those values cannot be directly used to initialize the engine’s result type.
The proposed fix is to derive a custom accumulator from SequenceStatistics and override the mean and errorEstimate inspectors to return Array values, converting from the base class results with Array’s iterator constructor. The accepted answer confirms this approach fits the interfaces used by McSimulation and SequenceStatistics. It also explains why returning a vector directly from the path pricer is unsuitable: the result type must support arithmetic such as addition and subtraction. The discussion is specific to this QuantLib interface and does not present a general Monte Carlo pricing method or compare alternative library designs.
Key ideas
- The Monte Carlo engine expects accumulator results to be convertible to its configured result type.
- SequenceStatistics returns vectors, which do not directly initialize the Array result type in this setup.
- A derived accumulator can override mean and errorEstimate to convert the base statistics into Array values.
- The path-pricer result type needs arithmetic operations used by the simulation.
Tags
Full text
# QuantLib C++: Monte Carlo Engine with SequenceStatistics # QuantLib C++: Monte Carlo Engine with SequenceStatistics I'm trying to implement a Monte Carlo PricingEngine that stores multidimensional statistics. I have done the following: - Defined a Monte Carlo Trait that among other things stores as the path_pricer_type `PathPricer<MultiPath,Array>` (my process is one dimensional but has 2 factors). let's call this struct MyMCTrait. - Implemented a class that derives from both `McSimulation<MyMCTrait,PseudoRandom,SequenceStatistics>` and `VanillaOption::engine`. - Implemented a `PathPricer<MultiPath,Array>` in which the first element of the Array returned by the operator() is the price of the option. I'm getting compiling errors because in methods value() and valueWithSamples() of class McSimulation we have the following initializations: a. `result_type(mcModel_->sampleAccumulator().mean());` b. `result_type error(mcModel_->sampleAccumulator().errorEstimate());` that are trying to cast from `std::vector<Real>` (returned by the SequenceStatistics methods) to Array (result_type). I doubt that the right way to go is to implement the cast (if it's possible), could anyone point me to the right direction? Thanks. ### EDIT I got the Pricing Engine working by using a derived class from `SequenceStatistics`. I just overrode the inspectors mean() and errorEstimate() and made them return an Array. To do this I used the base class methods + the Array constructor that takes begin and end iterators. I'm still wondering though if this is the right way to proceed. Thanks for any thoughts. ## Answer by Luigi Ballabio (score 4, accepted) https://quant.stackexchange.com/a/21392 Yes, your solution is correct, given the implementation of `McSimulation` and the interface of `SequenceStatistics`. We should probably have defined `SequenceStatistics` as returning instances of `Array`... As you might have seen, trying to return `std::vector<Real>` from the path pricer wouldn't work; the result type needs to define arithmetic operations such as addition and subtraction.
Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.