Changeset 194
- Timestamp:
- 05/27/08 20:20:16 (5 months ago)
- Files:
-
- trunk/QLNet/QLNet/Math/randomnumbers/rngtraits.cs (modified) (1 diff)
- trunk/QLNet/QLNet/Math/statistics/convergencestatistics.cs (modified) (1 diff)
- trunk/QLNet/QLNet/Math/statistics/gaussianstatistics.cs (modified) (2 diffs)
- trunk/QLNet/QLNet/Math/statistics/generalstatistics.cs (modified) (2 diffs)
- trunk/QLNet/QLNet/Math/statistics/riskstatistics.cs (modified) (1 diff)
- trunk/QLNet/QLNet/Methods/montecarlo/mctraits.cs (added)
- trunk/QLNet/QLNet/Methods/montecarlo/montecarlomodel.cs (modified) (2 diffs)
- trunk/QLNet/QLNet/Methods/montecarlo/pathpricer.cs (modified) (1 diff)
- trunk/QLNet/QLNet/Pricingengines/mcsimulation.cs (modified) (1 diff)
- trunk/QLNet/QLNet/Pricingengines/vanilla/mceuropeanengine.cs (added)
- trunk/QLNet/QLNet/Pricingengines/vanilla/mcvanillaengine.cs (added)
- trunk/QLNet/QLNet/QLNet.csproj (modified) (2 diffs)
- trunk/QLNet/QLNet/timegrid.cs (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/QLNet/QLNet/Math/randomnumbers/rngtraits.cs
r178 r194 44 44 45 45 // more traits 46 //enum { allowsErrorEstimate = 1 };46 public const int allowsErrorEstimate = 1; 47 47 48 48 // factory trunk/QLNet/QLNet/Math/statistics/convergencestatistics.cs
r175 r194 123 123 public double percentile(double percent) { return impl_.percentile(percent); } 124 124 public double weightSum() { return impl_.weightSum(); } 125 public double errorEstimate() { return impl_.errorEstimate(); } 125 126 126 127 public KeyValuePair<double, int> expectationValue(Func<KeyValuePair<double, double>, double> f, trunk/QLNet/QLNet/Math/statistics/gaussianstatistics.cs
r174 r194 50 50 public double percentile(double percent) { return impl_.percentile(percent); } 51 51 public double weightSum() { return impl_.weightSum(); } 52 public double errorEstimate() { return impl_.errorEstimate(); } 52 53 53 54 public void reset() { impl_.reset(); } … … 202 203 public double percentile(double percent) { throw new NotSupportedException(); } 203 204 public double weightSum() { throw new NotSupportedException(); } 205 public double errorEstimate() { throw new NotSupportedException(); } 204 206 205 207 public void reset() { throw new NotSupportedException(); } trunk/QLNet/QLNet/Math/statistics/generalstatistics.cs
r174 r194 34 34 double percentile(double percent); 35 35 double weightSum(); 36 double errorEstimate(); 36 37 37 38 void reset(); … … 40 41 41 42 KeyValuePair<double, int> expectationValue(Func<KeyValuePair<double, double>, double> f, 42 Func<KeyValuePair<double, double>, bool> inRange);43 Func<KeyValuePair<double, double>, bool> inRange); 43 44 } 44 45 trunk/QLNet/QLNet/Math/statistics/riskstatistics.cs
r174 r194 47 47 public double percentile(double percent) { return impl_.percentile(percent); } 48 48 public double weightSum() { return impl_.weightSum(); } 49 public double errorEstimate() { return impl_.errorEstimate(); } 49 50 50 51 public void reset() { impl_.reset(); } trunk/QLNet/QLNet/Methods/montecarlo/montecarlomodel.cs
r193 r194 52 52 53 53 private PathGenerator<IRNG> pathGenerator_; 54 private IPathPricer<Path> pathPricer_;54 private PathPricer<Path> pathPricer_; 55 55 private S sampleAccumulator_; 56 56 private bool isAntitheticVariate_; 57 private IPathPricer<Path> cvPathPricer_;57 private PathPricer<Path> cvPathPricer_; 58 58 private double cvOptionValue_; 59 59 private bool isControlVariate_; … … 66 66 // result_type cvOptionValue = result_type(), 67 67 // PathGenerator<IRNG> cvPathGenerator = path_generator_type()) { 68 public MonteCarloModel(PathGenerator<IRNG> pathGenerator, IPathPricer<Path> pathPricer, S sampleAccumulator, 69 bool antitheticVariate, IPathPricer<Path> cvPathPricer, double cvOptionValue, 68 public MonteCarloModel(PathGenerator<IRNG> pathGenerator, PathPricer<Path> pathPricer, S sampleAccumulator, 69 bool antitheticVariate) 70 : this(pathGenerator, pathPricer, sampleAccumulator, antitheticVariate, null, 0, null) { } 71 public MonteCarloModel(PathGenerator<IRNG> pathGenerator, PathPricer<Path> pathPricer, S sampleAccumulator, 72 bool antitheticVariate, PathPricer<Path> cvPathPricer, double cvOptionValue, 70 73 PathGenerator<IRNG> cvPathGenerator) { 71 74 pathGenerator_ = pathGenerator; trunk/QLNet/QLNet/Methods/montecarlo/pathpricer.cs
r193 r194 28 28 \ingroup mcarlo 29 29 */ 30 public interface IPathPricer<PathType> {31 double value(PathType pt);30 public class PathPricer<PathType> { 31 public virtual double value(PathType pt) { throw new NotSupportedException(); } 32 32 } 33 33 } trunk/QLNet/QLNet/Pricingengines/mcsimulation.cs
r193 r194 30 30 See McVanillaEngine as an example. 31 31 */ 32 public class McSimulation<MC, RNG, S> { 32 public abstract class McSimulation<MC, RNG, S> where S : IGeneralStatistics, new() { 33 //typedef typename MonteCarloModel<MC,RNG,S>::path_generator_type path_generator_type; 34 //typedef typename MonteCarloModel<MC,RNG,S>::path_pricer_type path_pricer_type; 35 //typedef typename MonteCarloModel<MC,RNG,S>::stats_type stats_type; 36 //typedef typename MonteCarloModel<MC,RNG,S>::result_type result_type; 37 38 protected MonteCarloModel<MC,RNG,S> mcModel_; 39 protected bool antitheticVariate_, controlVariate_; 40 41 42 protected McSimulation(bool antitheticVariate, bool controlVariate) { 43 antitheticVariate_ = antitheticVariate; 44 controlVariate_ = controlVariate; 45 } 46 47 48 //! add samples until the required absolute tolerance is reached 49 public double value(double tolerance) { return value(tolerance, int.MaxValue, 1023); } 50 public double value(double tolerance, int maxSamples) { return value(tolerance, maxSamples, 1023); } 51 public double value(double tolerance, int maxSamples, int minSamples) { 52 int sampleNumber = mcModel_.sampleAccumulator().samples(); 53 if (sampleNumber<minSamples) { 54 mcModel_.addSamples(minSamples-sampleNumber); 55 sampleNumber = mcModel_.sampleAccumulator().samples(); 56 } 57 58 int nextBatch; 59 double order; 60 double error = mcModel_.sampleAccumulator().errorEstimate(); 61 while (maxError(error) > tolerance) { 62 if (!(sampleNumber<maxSamples)) 63 throw new ApplicationException("max number of samples (" + maxSamples 64 + ") reached, while error (" + error 65 + ") is still above tolerance (" + tolerance + ")"); 66 67 // conservative estimate of how many samples are needed 68 order = maxError(error*error)/tolerance/tolerance; 69 nextBatch = (int)Math.Max(sampleNumber * order * 0.8 - sampleNumber, minSamples); 70 71 // do not exceed maxSamples 72 nextBatch = Math.Min(nextBatch, maxSamples-sampleNumber); 73 sampleNumber += nextBatch; 74 mcModel_.addSamples(nextBatch); 75 error = mcModel_.sampleAccumulator().errorEstimate(); 76 } 77 78 return mcModel_.sampleAccumulator().mean(); 79 } 80 81 //! simulate a fixed number of samples 82 public double valueWithSamples(int samples) { 83 84 int sampleNumber = mcModel_.sampleAccumulator().samples(); 85 86 if (!(samples>=sampleNumber)) 87 throw new ApplicationException("number of already simulated samples (" + sampleNumber 88 + ") greater than requested samples (" + samples + ")"); 89 90 mcModel_.addSamples(samples-sampleNumber); 91 92 return mcModel_.sampleAccumulator().mean(); 93 } 94 95 //! error estimated using the samples simulated so far 96 public double errorEstimate() { return mcModel_.sampleAccumulator().errorEstimate(); } 97 98 //! access to the sample accumulator for richer statistics 99 public S sampleAccumulator() { return mcModel_.sampleAccumulator(); } 100 101 //! basic calculate method provided to inherited pricing engines 102 public void calculate(double requiredTolerance, int requiredSamples, int maxSamples) { 103 104 if (!(requiredTolerance != 0 || requiredSamples != 0)) 105 throw new ApplicationException("neither tolerance nor number of samples set"); 106 107 //! Initialize the one-factor Monte Carlo 108 if (this.controlVariate_) { 109 110 double controlVariateValue = this.controlVariateValue(); 111 if (controlVariateValue == 0) 112 throw new ApplicationException("engine does not provide control-variation price"); 113 114 PathPricer<Path> controlPP = this.controlPathPricer(); 115 if (controlPP == null) 116 throw new ApplicationException("engine does not provide control-variation path pricer"); 117 118 PathGenerator<IRNG> controlPG = this.controlPathGenerator(); 119 120 this.mcModel_ = new MonteCarloModel<MC,RNG,S>(pathGenerator(), pathPricer(), new S(), antitheticVariate_, 121 controlPP, controlVariateValue, controlPG); 122 } else { 123 this.mcModel_ = new MonteCarloModel<MC,RNG,S>(pathGenerator(), pathPricer(), new S(), antitheticVariate_); 124 } 125 126 if (requiredTolerance != 0) { 127 if (maxSamples != 0) 128 value(requiredTolerance, maxSamples); 129 else 130 value(requiredTolerance); 131 } else { 132 valueWithSamples(requiredSamples); 133 } 134 } 135 136 137 protected abstract PathPricer<Path> pathPricer(); 138 protected abstract PathGenerator<IRNG> pathGenerator(); 139 protected abstract TimeGrid timeGrid(); 140 protected virtual PathPricer<Path> controlPathPricer() { return null; } 141 protected virtual PathGenerator<IRNG> controlPathGenerator() { return null; } 142 protected virtual IPricingEngine controlPricingEngine() { return null; } 143 protected virtual double controlVariateValue() { return 0; } 144 145 protected static double maxError(List<double> sequence) { return sequence.Max(); } 146 protected static double maxError(double error) { return error; } 33 147 } 34 148 } trunk/QLNet/QLNet/QLNet.csproj
r193 r194 219 219 <Compile Include="Methods\lattices\tree.cs" /> 220 220 <Compile Include="Methods\montecarlo\brownianbridge.cs" /> 221 <Compile Include="Methods\montecarlo\mctraits.cs" /> 221 222 <Compile Include="Methods\montecarlo\montecarlomodel.cs" /> 222 223 <Compile Include="Methods\montecarlo\multipath.cs" /> … … 267 268 <Compile Include="Pricingengines\vanilla\Integralengine.cs" /> 268 269 <Compile Include="Pricingengines\vanilla\Juquadraticengine.cs" /> 270 <Compile Include="Pricingengines\vanilla\mceuropeanengine.cs" /> 271 <Compile Include="Pricingengines\vanilla\mcvanillaengine.cs" /> 269 272 <Compile Include="processes\BlackScholesProcess.cs" /> 270 273 <Compile Include="processes\Defaultable.cs" /> trunk/QLNet/QLNet/timegrid.cs
r192 r194 114 114 public bool empty() { return times_.Count == 0; } 115 115 public int size() { return times_.Count; } 116 117 public double First() { return times_.First(); } 118 public double Last() { return times_.Last(); } 116 119 } 117 120 }