| | 55 | |
|---|
| | 56 | //! Monte Carlo European engine factory |
|---|
| | 57 | // template <class RNG = PseudoRandom, class S = Statistics> |
|---|
| | 58 | public class MakeMCEuropeanEngine<RNG> : MakeMCEuropeanEngine<RNG, Statistics> { |
|---|
| | 59 | public MakeMCEuropeanEngine(GeneralizedBlackScholesProcess process) : base(process) { } |
|---|
| | 60 | } |
|---|
| | 61 | |
|---|
| | 62 | public class MakeMCEuropeanEngine<RNG, S> where S : IGeneralStatistics, new() { |
|---|
| | 63 | private GeneralizedBlackScholesProcess process_; |
|---|
| | 64 | private bool antithetic_, controlVariate_; |
|---|
| | 65 | private int steps_, stepsPerYear_, samples_, maxSamples_; |
|---|
| | 66 | private double tolerance_; |
|---|
| | 67 | private bool brownianBridge_; |
|---|
| | 68 | private ulong seed_; |
|---|
| | 69 | |
|---|
| | 70 | public MakeMCEuropeanEngine(GeneralizedBlackScholesProcess process) { |
|---|
| | 71 | process_ = process; |
|---|
| | 72 | } |
|---|
| | 73 | |
|---|
| | 74 | // named parameters |
|---|
| | 75 | public MakeMCEuropeanEngine<RNG, S> withSteps(int steps) { |
|---|
| | 76 | steps_ = steps; |
|---|
| | 77 | return this; |
|---|
| | 78 | } |
|---|
| | 79 | public MakeMCEuropeanEngine<RNG, S> withStepsPerYear(int steps) { |
|---|
| | 80 | stepsPerYear_ = steps; |
|---|
| | 81 | return this; |
|---|
| | 82 | } |
|---|
| | 83 | //public MakeMCEuropeanEngine withBrownianBridge(bool b = true); |
|---|
| | 84 | public MakeMCEuropeanEngine<RNG, S> withBrownianBridge(bool brownianBridge) { |
|---|
| | 85 | brownianBridge_ = brownianBridge; |
|---|
| | 86 | return this; |
|---|
| | 87 | } |
|---|
| | 88 | public MakeMCEuropeanEngine<RNG, S> withSamples(int samples) { |
|---|
| | 89 | if(tolerance_ != 0) |
|---|
| | 90 | throw new ApplicationException("tolerance already set"); |
|---|
| | 91 | samples_ = samples; |
|---|
| | 92 | return this; |
|---|
| | 93 | } |
|---|
| | 94 | public MakeMCEuropeanEngine<RNG, S> withTolerance(double tolerance) { |
|---|
| | 95 | if(samples_ != 0) |
|---|
| | 96 | throw new ApplicationException("number of samples already set"); |
|---|
| | 97 | if (PseudoRandom.allowsErrorEstimate != 0) |
|---|
| | 98 | throw new ApplicationException("chosen random generator policy does not allow an error estimate"); |
|---|
| | 99 | tolerance_ = tolerance; |
|---|
| | 100 | return this; |
|---|
| | 101 | } |
|---|
| | 102 | public MakeMCEuropeanEngine<RNG, S> withMaxSamples(int samples) { |
|---|
| | 103 | maxSamples_ = samples; |
|---|
| | 104 | return this; |
|---|
| | 105 | } |
|---|
| | 106 | public MakeMCEuropeanEngine<RNG, S> withSeed(ulong seed) { |
|---|
| | 107 | seed_ = seed; |
|---|
| | 108 | return this; |
|---|
| | 109 | } |
|---|
| | 110 | //public MakeMCEuropeanEngine withAntitheticVariate(bool b = true) |
|---|
| | 111 | public MakeMCEuropeanEngine<RNG, S> withAntitheticVariate(bool b) { |
|---|
| | 112 | antithetic_ = b; |
|---|
| | 113 | return this; |
|---|
| | 114 | } |
|---|
| | 115 | //public MakeMCEuropeanEngine withControlVariate(bool b = true) |
|---|
| | 116 | public MakeMCEuropeanEngine<RNG, S> withControlVariate(bool b) { |
|---|
| | 117 | controlVariate_ = b; |
|---|
| | 118 | return this; |
|---|
| | 119 | } |
|---|
| | 120 | |
|---|
| | 121 | // conversion to pricing engine |
|---|
| | 122 | public IPricingEngine value() { |
|---|
| | 123 | if (steps_ == 0 && stepsPerYear_ == 0) |
|---|
| | 124 | throw new ApplicationException("number of steps not given"); |
|---|
| | 125 | if (!(steps_ == 0 || stepsPerYear_ == 0)) |
|---|
| | 126 | throw new ApplicationException("number of steps overspecified"); |
|---|
| | 127 | return new MCEuropeanEngine<RNG,S>(process_, steps_, stepsPerYear_, brownianBridge_, antithetic_, |
|---|
| | 128 | controlVariate_, samples_, tolerance_, maxSamples_, seed_); |
|---|
| | 129 | } |
|---|
| | 130 | } |
|---|
| | 131 | |
|---|
| | 132 | |
|---|