Assembla home | Assembla project page
 

Changeset 195

Show
Ignore:
Timestamp:
05/27/08 20:40:06 (6 months ago)
Author:
snovik
Message:

New: MC, first successful compilation

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/QLNet/Examples/EquityOption/EquityOption.cs

    r166 r195  
    218218            Console.WriteLine("{0,-" + widths[3] + ":0.000000}", americanOption.NPV()); 
    219219 
    220             //// Monte Carlo Method: MC (crude) 
    221             //timeSteps = 1; 
    222             //method = "MC (crude)"; 
    223             //Size mcSeed = 42; 
    224             //boost::shared_ptr<PricingEngine> mcengine1; 
    225             //mcengine1 = MakeMCEuropeanEngine<PseudoRandom>(bsmProcess) 
    226             //    .withSteps(timeSteps) 
    227             //    .withTolerance(0.02) 
    228             //    .withSeed(mcSeed); 
    229             //europeanOption.setPricingEngine(mcengine1); 
    230             //// Real errorEstimate = europeanOption.errorEstimate(); 
    231             //std::cout << std::setw(widths[0]) << std::left << method 
    232             //          << std::fixed 
    233             //          << std::setw(widths[1]) << std::left << europeanOption.NPV() 
    234             //          << std::setw(widths[2]) << std::left << "N/A" 
    235             //          << std::setw(widths[3]) << std::left << "N/A" 
    236             //          << std::endl; 
     220            // Monte Carlo Method: MC (crude) 
     221            timeSteps = 1; 
     222            method = "MC (crude)"; 
     223            int mcSeed = 42; 
     224            IPricingEngine mcengine1 = new MakeMCEuropeanEngine<PseudoRandom>(bsmProcess) 
     225                                            .withSteps(timeSteps) 
     226                                            .withTolerance(0.02) 
     227                                            .withSeed((ulong)mcSeed) 
     228                                            .value(); 
     229            europeanOption.setPricingEngine(mcengine1); 
     230            // Real errorEstimate = europeanOption.errorEstimate(); 
     231            Console.Write("{0,-" + widths[0] + "}", method); 
     232            Console.Write("{0,-" + widths[1] + ":0.000000}", europeanOption.NPV()); 
     233            Console.Write("{0,-" + widths[2] + ":0.000000}", "N/A"); 
     234            Console.WriteLine("{0,-" + widths[3] + ":0.000000}", "N/A"); 
    237235 
    238236            //// Monte Carlo Method: QMC (Sobol) 
  • trunk/QLNet/QLNet/Math/statistics/riskstatistics.cs

    r194 r195  
    201201        } 
    202202    } 
     203 
     204    //! default statistics tool 
     205    /*! \test the correctness of the returned values is tested by checking them against numerical calculations. */ 
     206    public class Statistics : RiskStatistics { } 
    203207} 
  • trunk/QLNet/QLNet/Pricingengines/vanilla/mceuropeanengine.cs

    r194 r195  
    5353    } 
    5454 
     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 
    55133    public class EuropeanPathPricer : PathPricer<Path> { 
    56134        private PlainVanillaPayoff payoff_;