Assembla home | Assembla project page
 

Changeset 197

Show
Ignore:
Timestamp:
05/29/08 17:36:15 (3 months ago)
Author:
snovik
Message:

Fix: Quasi MC (Sobol) for European Option

Files:

Legend:

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

    r195 r197  
    218218            Console.WriteLine("{0,-" + widths[3] + ":0.000000}", americanOption.NPV()); 
    219219 
     220 
    220221            // Monte Carlo Method: MC (crude) 
    221222            timeSteps = 1; 
     
    234235            Console.WriteLine("{0,-" + widths[3] + ":0.000000}", "N/A"); 
    235236 
    236             //// Monte Carlo Method: QMC (Sobol) 
    237             //method = "QMC (Sobol)"; 
    238             //Size nSamples = 32768;  // 2^15 
    239  
    240             //boost::shared_ptr<PricingEngine> mcengine2; 
    241             //mcengine2 = MakeMCEuropeanEngine<LowDiscrepancy>(bsmProcess) 
    242             //    .withSteps(timeSteps) 
    243             //    .withSamples(nSamples); 
    244             //europeanOption.setPricingEngine(mcengine2); 
    245             //std::cout << std::setw(widths[0]) << std::left << method 
    246             //          << std::fixed 
    247             //          << std::setw(widths[1]) << std::left << europeanOption.NPV() 
    248             //          << std::setw(widths[2]) << std::left << "N/A" 
    249             //          << std::setw(widths[3]) << std::left << "N/A" 
    250             //          << std::endl; 
     237 
     238            // Monte Carlo Method: QMC (Sobol) 
     239            method = "QMC (Sobol)"; 
     240            int nSamples = 32768;  // 2^15 
     241 
     242            IPricingEngine mcengine2 = new MakeMCEuropeanEngine<LowDiscrepancy>(bsmProcess) 
     243                                            .withSteps(timeSteps) 
     244                                            .withSamples(nSamples) 
     245                                            .value(); 
     246            europeanOption.setPricingEngine(mcengine2); 
     247            Console.Write("{0,-" + widths[0] + "}", method); 
     248            Console.Write("{0,-" + widths[1] + ":0.000000}", europeanOption.NPV()); 
     249            Console.Write("{0,-" + widths[2] + ":0.000000}", "N/A"); 
     250            Console.WriteLine("{0,-" + widths[3] + ":0.000000}", "N/A"); 
    251251 
    252252            //// Monte Carlo Method: MC (Longstaff Schwartz) 
  • trunk/QLNet/QLNet/Math/randomnumbers/inversecumulativersg.cs

    r178 r197  
    4545        \endcode 
    4646    */ 
    47     public class InverseCumulativeRsg<USG, IC> : IRNG where USG : IRNG where IC : IValue, new()
     47    public class InverseCumulativeRsg<USG, IC> : IRNG where USG : IRNG where IC : IValue
    4848        //typedef Sample<std::vector<Real> > sample_type; 
    4949 
     
    5151        private int dimension_; 
    5252        private Sample<List<double>> x_; 
    53         private IC ICD_ = new IC()
     53        private IC ICD_
    5454 
    5555        public InverseCumulativeRsg(USG uniformSequenceGenerator) { 
     
    6363        } 
    6464 
     65 
     66        #region IRNG interface 
    6567        //! returns next sample from the Gaussian distribution 
    6668        public Sample<List<double>> nextSequence() { 
     
    7577        public Sample<List<double>> lastSequence() { return x_; } 
    7678        public int dimension() { return dimension_; } 
     79 
     80        public IRNG factory(int dimensionality, ulong seed) { throw new NotSupportedException(); }  
     81        #endregion 
    7782    } 
    7883} 
  • trunk/QLNet/QLNet/Math/randomnumbers/randomsequencegenerator.cs

    r192 r197  
    2727        Sample<List<double>> nextSequence(); 
    2828        Sample<List<double>> lastSequence(); 
     29 
     30        IRNG factory(int dimensionality, ulong seed); 
    2931    } 
    3032 
     
    4143        // typedef Sample<std::vector<Real> > sample_type; 
    4244        private int dimensionality_; 
    43         public int dimension() { return dimensionality_; } 
    4445 
    4546        private RNG rng_; 
     
    6465        } 
    6566 
     67        public List<ulong> nextInt32Sequence() { 
     68            for (int i = 0; i < dimensionality_; i++) { 
     69                int32Sequence_[i] = rng_.nextInt32(); 
     70            } 
     71            return int32Sequence_; 
     72        } 
     73 
     74        #region IRGN interface 
    6675        public Sample<List<double>> nextSequence() { 
    6776            sequence_.weight = 1.0; 
    68             for (int i=0; i<dimensionality_; i++) { 
     77            for (int i = 0; i < dimensionality_; i++) { 
    6978                Sample<double> x = rng_.next();  // typename RNG::sample_type x(rng_.next()); 
    7079                sequence_.value[i] = x.value; 
    71                 sequence_.weight *= x.weight; 
     80                sequence_.weight *= x.weight; 
    7281            } 
    7382            return sequence_; 
    7483        } 
    7584 
    76         public List<ulong> nextInt32Sequence() { 
    77             for (int i=0; i<dimensionality_; i++) { 
    78                 int32Sequence_[i] = rng_.nextInt32(); 
    79             } 
    80             return int32Sequence_; 
     85        public Sample<List<double>> lastSequence() { return sequence_; } 
     86 
     87        public int dimension() { return dimensionality_; } 
     88 
     89        public IRNG factory(int dimensionality, ulong seed) { 
     90            return new RandomSequenceGenerator<RNG>(dimensionality, seed); 
    8191        } 
    82         public Sample<List<double>> lastSequence() { 
    83             return sequence_; 
    84         } 
     92        #endregion 
    8593    } 
    8694} 
  • trunk/QLNet/QLNet/Math/randomnumbers/rngtraits.cs

    r194 r197  
    3030    } 
    3131 
     32    public interface IRSG { 
     33        int allowsErrorEstimate { get; } 
     34        object make_sequence_generator(int dimension, ulong seed); 
     35    } 
     36 
    3237    // random number traits 
    33     public class GenericPseudoRandom<URNG, IC> 
    34         where URNG : IRNGTraits, new() 
    35         where IC : IValue, new() { 
     38    public class GenericPseudoRandom<URNG, IC> : IRSG where URNG : IRNGTraits, new() where IC : IValue, new() { 
    3639        // data 
    37         public static IC icInstance
     40        public static IC icInstance = new IC()
    3841 
    3942        //// typedefs 
     
    4447 
    4548        // more traits 
    46         public const int allowsErrorEstimate = 1; 
     49        public int allowsErrorEstimate { get { return 1; } } 
    4750 
    4851        // factory 
    49         public static InverseCumulativeRsg<RandomSequenceGenerator<URNG>,IC> make_sequence_generator(int dimension, ulong seed) { 
     52        public object make_sequence_generator(int dimension, ulong seed) { 
    5053            RandomSequenceGenerator<URNG> g = new RandomSequenceGenerator<URNG>(dimension, seed); 
    5154            return (icInstance != null ? new InverseCumulativeRsg<RandomSequenceGenerator<URNG>, IC>(g, icInstance) 
     
    6568    // typedef GenericPseudoRandom<MersenneTwisterUniformRng, InverseCumulativePoisson> PoissonPseudoRandom; 
    6669    public class PoissonPseudoRandom : GenericPseudoRandom<MersenneTwisterUniformRng, InverseCumulativePoisson> { } 
     70 
     71 
     72    public class GenericLowDiscrepancy<URSG, IC> : IRSG where URSG : IRNG, new() where IC : IValue, new() { 
     73        // typedefs 
     74        //typedef URSG ursg_type; 
     75        //typedef InverseCumulativeRsg<ursg_type,IC> rsg_type; 
     76 
     77        // data 
     78        public static IC icInstance = new IC(); 
     79 
     80        // more traits 
     81        public int allowsErrorEstimate { get { return 0; } } 
     82 
     83        // factory 
     84        public object make_sequence_generator(int dimension, ulong seed) { 
     85            URSG g = (URSG)new URSG().factory(dimension, seed); 
     86            return (icInstance != null ? new InverseCumulativeRsg<URSG, IC>(g, icInstance) 
     87                                       : new InverseCumulativeRsg<URSG, IC>(g)); 
     88        } 
     89    } 
     90 
     91    //! default traits for low-discrepancy sequence generation 
     92    //typedef GenericLowDiscrepancy<SobolRsg, InverseCumulativeNormal> LowDiscrepancy; 
     93    public class LowDiscrepancy : GenericLowDiscrepancy<SobolRsg, InverseCumulativeNormal> { } 
    6794} 
  • trunk/QLNet/QLNet/Math/randomnumbers/sobolrsg.cs

    r173 r197  
    9595          their discrepancy against known good values. 
    9696    */ 
    97     public partial class SobolRsg
     97    public partial class SobolRsg : IRNG
    9898        //typedef Sample<List<double>> sample_type; 
    9999 
     
    114114            Kuo, Kuo2, Kuo3 }; 
    115115 
     116        // required for generics 
     117        public SobolRsg() { } 
     118 
    116119        /*! \pre dimensionality must be <= PPMT_MAX_DIM */ 
    117         //public SobolRsg(int dimensionality, ulong seed = 0, DirectionIntegers directionIntegers = Jaeckel); 
    118120        public SobolRsg(int dimensionality) : this(dimensionality, 0, DirectionIntegers.Jaeckel) { } 
     121        public SobolRsg(int dimensionality, ulong seed) : this(dimensionality, seed, DirectionIntegers.Jaeckel) { } 
    119122        public SobolRsg(int dimensionality, ulong seed, DirectionIntegers directionIntegers) { 
    120123            dimensionality_ = dimensionality; 
     
    451454        } 
    452455 
     456        #region IRNG interface 
    453457        public Sample<List<double>> nextSequence() { 
    454458            List<ulong> v = nextInt32Sequence(); 
    455459            // normalize to get a double in (0,1) 
    456             for (int k=0; k<dimensionality_; ++k) 
     460            for (int k = 0; k < dimensionality_; ++k) 
    457461                sequence_.value[k] = v[k] * normalizationFactor_; 
    458462            return sequence_; 
     
    460464 
    461465        public Sample<List<double>> lastSequence() { return sequence_; } 
    462          
     466 
    463467        public int dimension() { return dimensionality_; } 
     468 
     469        public IRNG factory(int dimensionality, ulong seed) { 
     470            return new SobolRsg(dimensionality, seed); 
     471        }  
     472        #endregion 
    464473    } 
    465474} 
  • trunk/QLNet/QLNet/Pricingengines/vanilla/mceuropeanengine.cs

    r196 r197  
    2929              checking it against analytic results. 
    3030    */ 
    31     public class MCEuropeanEngine<RNG, S> : MCVanillaEngine<SingleVariate, RNG, S>  
     31    public class MCEuropeanEngine<RNG, S> : MCVanillaEngine<SingleVariate, RNG, S> 
     32        where RNG : IRSG, new()  
    3233        where S : IGeneralStatistics, new() { 
    3334 
     
    5657    //! Monte Carlo European engine factory 
    5758    // template <class RNG = PseudoRandom, class S = Statistics> 
    58     public class MakeMCEuropeanEngine<RNG> : MakeMCEuropeanEngine<RNG, Statistics>
     59    public class MakeMCEuropeanEngine<RNG> : MakeMCEuropeanEngine<RNG, Statistics> where RNG : IRSG, new()
    5960        public MakeMCEuropeanEngine(GeneralizedBlackScholesProcess process) : base(process) { } 
    6061    } 
    6162 
    62     public class MakeMCEuropeanEngine<RNG, S> where S : IGeneralStatistics, new() { 
     63    public class MakeMCEuropeanEngine<RNG, S> where RNG : IRSG, new() where S : IGeneralStatistics, new() { 
    6364        private GeneralizedBlackScholesProcess process_; 
    6465        private bool antithetic_, controlVariate_; 
     
    9596            if(samples_ != 0) 
    9697                throw new ApplicationException("number of samples already set"); 
    97             if (PseudoRandom.allowsErrorEstimate == 0) 
     98            if (new RNG().allowsErrorEstimate == 0) 
    9899                throw new ApplicationException("chosen random generator policy does not allow an error estimate"); 
    99100            tolerance_ = tolerance; 
  • trunk/QLNet/QLNet/Pricingengines/vanilla/mcvanillaengine.cs

    r196 r197  
    2626    /*! \ingroup vanillaengines */ 
    2727    public abstract class MCVanillaEngine<MC, RNG, S> : MCVanillaEngine<MC, RNG, S, VanillaOption> 
     28            where RNG : IRSG, new() 
    2829            where S : IGeneralStatistics, new() { 
    2930        protected MCVanillaEngine(StochasticProcess process, int timeSteps, int timeStepsPerYear, bool brownianBridge, 
     
    3637    public abstract class MCVanillaEngine<MC, RNG, S, Inst> : McSimulation<MC, RNG, S>, 
    3738            IGenericEngine<OneAssetOption.Arguments, OneAssetOption.Results> 
    38             where S : IGeneralStatistics, new() { 
     39            where RNG : IRSG, new() where S : IGeneralStatistics, new() { 
    3940        //typedef typename McSimulation<MC,RNG,S>::path_generator_type path_generator_type; 
    4041        //typedef typename McSimulation<MC,RNG,S>::path_pricer_type path_pricer_type; 
     
    7475            base.calculate(requiredTolerance_, requiredSamples_, maxSamples_); 
    7576            results_.value = mcModel_.sampleAccumulator().mean(); 
    76             if (PseudoRandom.allowsErrorEstimate != 0) 
     77            if (new RNG().allowsErrorEstimate != 0) 
    7778                results_.errorEstimate = mcModel_.sampleAccumulator().errorEstimate(); 
    7879        } 
     
    9596            int dimensions = process_.factors(); 
    9697            TimeGrid grid = timeGrid(); 
    97             var generator = PseudoRandom.make_sequence_generator(dimensions * (grid.size() - 1), seed_); 
     98            IRNG generator = (IRNG)new RNG().make_sequence_generator(dimensions * (grid.size() - 1), seed_); 
    9899            return new PathGenerator<IRNG>(process_, grid, generator, brownianBridge_); 
    99100        } 
  • trunk/QLNet/Test2008/T_RNGTraits.cs

    r189 r197  
    3434         //("Testing Gaussian pseudo-random number generation..."); 
    3535 
    36          InverseCumulativeRsg<RandomSequenceGenerator<MersenneTwisterUniformRng>, InverseCumulativeNormal> rsg = 
    37              PseudoRandom.make_sequence_generator(100, 1234); 
     36         var rsg = (InverseCumulativeRsg<RandomSequenceGenerator<MersenneTwisterUniformRng>, InverseCumulativeNormal>) 
     37             new PseudoRandom().make_sequence_generator(100, 1234); 
    3838 
    3939         List<double> values = rsg.nextSequence().value; 
     
    5757 
    5858         PoissonPseudoRandom.icInstance = new InverseCumulativePoisson(); 
    59          var rsg = PoissonPseudoRandom.make_sequence_generator(100, 1234); 
     59         IRNG rsg = (IRNG)new PoissonPseudoRandom().make_sequence_generator(100, 1234); 
    6060 
    6161         List<double> values = rsg.nextSequence().value; 
     
    7878 
    7979         PoissonPseudoRandom.icInstance = new InverseCumulativePoisson(4.0); 
    80          var rsg = PoissonPseudoRandom.make_sequence_generator(100, 1234); 
     80         IRNG rsg = (IRNG)new PoissonPseudoRandom().make_sequence_generator(100, 1234); 
    8181 
    8282         List<double> values = rsg.nextSequence().value;