Assembla home | Assembla project page
 

Changeset 194

Show
Ignore:
Timestamp:
05/27/08 20:20:16 (5 months ago)
Author:
snovik
Message:

New: More MC classes

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/QLNet/QLNet/Math/randomnumbers/rngtraits.cs

    r178 r194  
    4444 
    4545        // more traits 
    46         //enum { allowsErrorEstimate = 1 }
     46        public const int allowsErrorEstimate = 1
    4747 
    4848        // factory 
  • trunk/QLNet/QLNet/Math/statistics/convergencestatistics.cs

    r175 r194  
    123123        public double percentile(double percent) { return impl_.percentile(percent); } 
    124124        public double weightSum() { return impl_.weightSum(); } 
     125        public double errorEstimate() { return impl_.errorEstimate(); } 
    125126 
    126127        public KeyValuePair<double, int> expectationValue(Func<KeyValuePair<double, double>, double> f, 
  • trunk/QLNet/QLNet/Math/statistics/gaussianstatistics.cs

    r174 r194  
    5050        public double percentile(double percent) { return impl_.percentile(percent); } 
    5151        public double weightSum() { return impl_.weightSum(); } 
     52        public double errorEstimate() { return impl_.errorEstimate(); } 
    5253 
    5354        public void reset() { impl_.reset(); } 
     
    202203        public double percentile(double percent) { throw new NotSupportedException(); } 
    203204        public double weightSum() { throw new NotSupportedException(); } 
     205        public double errorEstimate() { throw new NotSupportedException(); } 
    204206 
    205207        public void reset() { throw new NotSupportedException(); } 
  • trunk/QLNet/QLNet/Math/statistics/generalstatistics.cs

    r174 r194  
    3434        double percentile(double percent); 
    3535        double weightSum(); 
     36        double errorEstimate(); 
    3637 
    3738        void reset(); 
     
    4041 
    4142        KeyValuePair<double, int> expectationValue(Func<KeyValuePair<double, double>, double> f, 
    42                                            Func<KeyValuePair<double, double>, bool> inRange); 
     43                                                   Func<KeyValuePair<double, double>, bool> inRange); 
    4344    } 
    4445 
  • trunk/QLNet/QLNet/Math/statistics/riskstatistics.cs

    r174 r194  
    4747        public double percentile(double percent) { return impl_.percentile(percent); } 
    4848        public double weightSum() { return impl_.weightSum(); } 
     49        public double errorEstimate() { return impl_.errorEstimate(); } 
    4950 
    5051        public void reset() { impl_.reset(); } 
  • trunk/QLNet/QLNet/Methods/montecarlo/montecarlomodel.cs

    r193 r194  
    5252 
    5353        private PathGenerator<IRNG> pathGenerator_; 
    54         private IPathPricer<Path> pathPricer_; 
     54        private PathPricer<Path> pathPricer_; 
    5555        private S sampleAccumulator_; 
    5656        private bool isAntitheticVariate_; 
    57         private IPathPricer<Path> cvPathPricer_; 
     57        private PathPricer<Path> cvPathPricer_; 
    5858        private double cvOptionValue_; 
    5959        private bool isControlVariate_; 
     
    6666        //          result_type cvOptionValue = result_type(), 
    6767        //          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, 
    7073                               PathGenerator<IRNG> cvPathGenerator) { 
    7174            pathGenerator_ = pathGenerator; 
  • trunk/QLNet/QLNet/Methods/montecarlo/pathpricer.cs

    r193 r194  
    2828        \ingroup mcarlo 
    2929    */ 
    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(); } 
    3232    } 
    3333} 
  • trunk/QLNet/QLNet/Pricingengines/mcsimulation.cs

    r193 r194  
    3030        See McVanillaEngine as an example. 
    3131    */ 
    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; } 
    33147    } 
    34148} 
  • trunk/QLNet/QLNet/QLNet.csproj

    r193 r194  
    219219    <Compile Include="Methods\lattices\tree.cs" /> 
    220220    <Compile Include="Methods\montecarlo\brownianbridge.cs" /> 
     221    <Compile Include="Methods\montecarlo\mctraits.cs" /> 
    221222    <Compile Include="Methods\montecarlo\montecarlomodel.cs" /> 
    222223    <Compile Include="Methods\montecarlo\multipath.cs" /> 
     
    267268    <Compile Include="Pricingengines\vanilla\Integralengine.cs" /> 
    268269    <Compile Include="Pricingengines\vanilla\Juquadraticengine.cs" /> 
     270    <Compile Include="Pricingengines\vanilla\mceuropeanengine.cs" /> 
     271    <Compile Include="Pricingengines\vanilla\mcvanillaengine.cs" /> 
    269272    <Compile Include="processes\BlackScholesProcess.cs" /> 
    270273    <Compile Include="processes\Defaultable.cs" /> 
  • trunk/QLNet/QLNet/timegrid.cs

    r192 r194  
    114114        public bool empty() { return times_.Count == 0; } 
    115115        public int size() { return times_.Count; } 
     116 
     117        public double First() { return times_.First(); } 
     118        public double Last() { return times_.Last(); } 
    116119    } 
    117120}