Assembla home | Assembla project page
 

Changeset 224

Show
Ignore:
Timestamp:
07/06/08 21:06:38 (3 months ago)
Author:
snovik
Message:

Fix: Monte-carlo (American Option)
New: LinearLeastSquaresRegression? and its test
Change: EquityOption? example is finished
Fix: Matrix methods: column & row

Files:

Legend:

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

    r219 r224  
    222222            timeSteps = 1; 
    223223            method = "MC (crude)"; 
    224             int mcSeed = 42; 
     224            ulong mcSeed = 42; 
    225225            IPricingEngine mcengine1 = new MakeMCEuropeanEngine<PseudoRandom>(bsmProcess) 
    226226                                            .withSteps(timeSteps) 
    227227                                            .withTolerance(0.02) 
    228                                             .withSeed((ulong)mcSeed) 
     228                                            .withSeed(mcSeed) 
    229229                                            .value(); 
    230230            europeanOption.setPricingEngine(mcengine1); 
     
    252252            // Monte Carlo Method: MC (Longstaff Schwartz) 
    253253            method = "MC (Longstaff Schwartz)"; 
    254             //IPricingEngine mcengine3 = new MakeMCAmericanEngine<PseudoRandom>(bsmProcess) 
    255             //                            .withSteps(100) 
    256             //                            .withAntitheticVariate() 
    257             //                            .withCalibrationSamples(4096) 
    258             //                            .withTolerance(0.02) 
    259             //                            .withSeed(mcSeed) 
    260             //                            .value(); 
    261             //americanOption.setPricingEngine(mcengine3); 
    262             //Console.Write("{0,-" + widths[0] + "}", method); 
    263             //Console.Write("{0,-" + widths[1] + ":0.000000}", "N/A"); 
    264             //Console.Write("{0,-" + widths[2] + ":0.000000}", "N/A"); 
    265             //Console.WriteLine("{0,-" + widths[3] + ":0.000000}", americanOption.NPV()); 
     254            IPricingEngine mcengine3 = new MakeMCAmericanEngine<PseudoRandom>(bsmProcess) 
     255                                        .withSteps(100) 
     256                                        .withAntitheticVariate() 
     257                                        .withCalibrationSamples(4096) 
     258                                        .withTolerance(0.02) 
     259                                        .withSeed(mcSeed) 
     260                                        .value(); 
     261            americanOption.setPricingEngine(mcengine3); 
     262            Console.Write("{0,-" + widths[0] + "}", method); 
     263            Console.Write("{0,-" + widths[1] + ":0.000000}", "N/A"); 
     264            Console.Write("{0,-" + widths[2] + ":0.000000}", "N/A"); 
     265            Console.WriteLine("{0,-" + widths[3] + ":0.000000}", americanOption.NPV()); 
    266266 
    267267            // End test 
  • trunk/QLNet/QLNet/Math/Matrix.cs

    r223 r224  
    2828        container. 
    2929    */ 
    30     public struct Matrix
     30    public struct Matrix : ICloneable
    3131        #region properties 
    3232        private int rows_, columns_; 
     
    3838        public double this[int i, int j] { get { return data_[i, j]; } set { data_[i, j] = value; } } 
    3939        public Vector row(int r) { 
     40            Vector result = new Vector(columns_); 
     41            for (int i = 0; i < columns_; i++) 
     42                result[i] = data_[r, i]; 
     43            return result;  
     44        } 
     45        public Vector column(int c) { 
    4046            Vector result = new Vector(rows_); 
    4147            for (int i = 0; i < rows_; i++) 
    42                 result[i] = data_[r, i]; 
    43             return result; 
    44         } 
    45         public Vector column(int c) { 
    46             Vector result = new Vector(columns_); 
    47             for (int i = 0; i < columns_; i++) 
    4848                result[i] = data_[i, c]; 
    4949            return result; 
     
    180180            this[i1, j1] = t; 
    181181        } 
     182 
     183        // IClonable interface 
     184        public object Clone() { return this.MemberwiseClone(); } 
    182185    } 
    183186} 
  • trunk/QLNet/QLNet/Math/linearleastsquaresregression.cs

    r223 r224  
    3131              checking their properties. 
    3232    */ 
     33    public class LinearLeastSquaresRegression : LinearLeastSquaresRegression<double> { 
     34        public LinearLeastSquaresRegression(List<double> x, List<double> y, List<Func<double, double>> v) 
     35            : base(x,y,v) { } 
     36    } 
     37     
    3338    public class LinearLeastSquaresRegression<ArgumentType> { 
    3439        private Vector a_; 
     
    3641 
    3742        public LinearLeastSquaresRegression(List<ArgumentType> x, List<double> y, List<Func<ArgumentType, double>> v) { 
    38             a_ = new Vector(v.Count, 0.0); 
    39             err_ = new Vector(v.Count, 0.0); 
     43            a_ = new Vector(v.Count); 
     44            err_ = new Vector(v.Count); 
    4045 
    4146            if (x.Count != y.Count) 
     
    4954 
    5055            Matrix A = new Matrix(n, m); 
    51             for (i=0; i<m; ++i) 
    52                 for(int j=0;j<x.Count; j++) 
    53                     A[j,i] = v[i](x[i]); 
     56            for (i = 0; i < m; ++i) 
     57                x.ForEach((jj, xx) => A[jj, i] = v[i](xx)); 
    5458 
    5559            SVD svd = new SVD(A); 
    5660            Matrix V = svd.V(); 
    5761            Matrix U = svd.U(); 
    58             Vector w = svd.singularValues(); 
     62            Vector w = svd.singularValues(); 
    5963            double threshold = n*Const.QL_Epsilon; 
    6064 
  • trunk/QLNet/QLNet/Math/matrixutilities/svd.cs

    r223 r224  
    3737 
    3838        public SVD(Matrix M) { 
    39             //using std::swap; 
    40  
    4139            Matrix A; 
    4240 
     
    5452               M = V S U^T             (idempotence of transposition, 
    5553                                        symmetry of diagonal matrix S) 
    56  
    5754            */ 
    5855 
    5956            if (M.rows() >= M.columns()) { 
    60                 A = M
     57                A = (Matrix)M.Clone()
    6158                transpose_ = false; 
    6259            } else { 
     
    6966 
    7067            // we're sure that m_ >= n_ 
    71  
    7268            s_ = new Vector(n_); 
    7369            U_ = new Matrix(m_, n_, 0.0); 
     
    7975            // Reduce A to bidiagonal form, storing the diagonal elements 
    8076            // in s and the super-diagonal elements in e. 
    81  
    8277            int nct = Math.Min(m_ - 1, n_); 
    8378            int nrt = Math.Max(0, n_ - 2); 
     
    107102 
    108103                        // Apply the transformation. 
    109  
    110104                        double t = 0; 
    111105                        for (i = k; i < m_; i++) { 
     
    120114                    // Place the k-th row of A into e for the 
    121115                    // subsequent calculation of the row transformation. 
    122  
    123116                    e[j] = A[k, j]; 
    124117                } 
    125118                if (k < nct) { 
    126119 
    127                     // Place the transformation in U for subsequent back 
    128                     // multiplication. 
    129  
     120                    // Place the transformation in U for subsequent back multiplication. 
    130121                    for (i = k; i < m_; i++) { 
    131122                        U_[i, k] = A[i, k]; 
     
    134125                if (k < nrt) { 
    135126 
    136                     // Compute the k-th row transformation and place the 
    137                     // k-th super-diagonal in e[k]. 
     127                    // Compute the k-th row transformation and place the k-th super-diagonal in e[k]. 
    138128                    // Compute 2-norm without under/overflow. 
    139129                    e[k] = 0; 
     
    154144 
    155145                        // Apply the transformation. 
    156  
    157146                        for (i = k + 1; i < m_; i++) { 
    158147                            work[i] = 0.0; 
     
    171160                    } 
    172161 
    173                     // Place the transformation in V for subsequent 
    174                     // back multiplication. 
    175  
     162                    // Place the transformation in V for subsequent back multiplication. 
    176163                    for (i = k + 1; i < n_; i++) { 
    177164                        V_[i, k] = e[i]; 
     
    181168 
    182169            // Set up the final bidiagonal matrix or order n. 
    183  
    184170            if (nct < n_) { 
    185171                s_[nct] = A[nct, nct]; 
     
    191177 
    192178            // generate U 
    193  
    194179            for (j = nct; j < n_; j++) { 
    195180                for (i = 0; i < m_; i++) { 
     
    226211 
    227212            // generate V 
    228  
    229213            for (k = n_ - 1; k >= 0; --k) { 
    230214                if ((k < nrt) & (e[k] != 0.0)) { 
     
    269253                        break; 
    270254                    } 
    271                     if (Math.Abs(e[k]) <= eps * (Math.Abs(s_[k]) + 
    272                                                 Math.Abs(s_[k + 1]))) { 
     255                    if (Math.Abs(e[k]) <= eps * (Math.Abs(s_[k]) + Math.Abs(s_[k + 1]))) { 
    273256                        e[k] = 0.0; 
    274257                        break; 
  • trunk/QLNet/QLNet/Methods/montecarlo/longstaffschwartzpathpricer.cs

    r223 r224  
    3535              reproducing results available in web/literature 
    3636    */ 
    37     public class LongstaffSchwartzPathPricer<PathType> : PathPricer<PathType> where PathType : Path
     37    public class LongstaffSchwartzPathPricer<PathType> : PathPricer<PathType> where PathType : Path, ICloneable
    3838        protected bool  calibrationPhase_; 
    3939        protected EarlyExercisePathPricer<PathType> pathPricer_; 
     
    4242        protected List<double> dF_; 
    4343 
    44         protected List<PathType> paths_
     44        protected List<PathType> paths_ = new List<PathType>()
    4545        protected List<Func<double, double>> v_; 
    4646 
     
    6363            if (calibrationPhase_) { 
    6464                // store paths for the calibration 
    65                 paths_.Add(path); 
     65                paths_.Add((PathType)path.Clone()); 
    6666                // result doesn't matter 
    6767                return 0.0; 
  • trunk/QLNet/QLNet/Methods/montecarlo/path.cs

    r192 r224  
    2828        \note the path includes the initial asset value as its first point. 
    2929    */ 
    30     public class Path
     30    public class Path : ICloneable
    3131        private TimeGrid timeGrid_; 
    3232        private Vector values_; 
     
    6666        //! time grid 
    6767        public TimeGrid timeGrid() { return timeGrid_; } 
     68 
     69        // ICloneable interface 
     70        public object Clone() { 
     71            Path temp = (Path)this.MemberwiseClone(); 
     72            temp.values_ = new Vector(this.values_); 
     73            return temp; 
     74        } 
    6875    } 
    6976} 
  • trunk/QLNet/QLNet/Methods/montecarlo/sample.cs

    r192 r224  
    2525    //! weighted sample 
    2626    /*! \ingroup mcarlo */ 
    27     public struct Sample<T> : ICloneable { 
     27    public struct Sample<T> { 
     28        public T value; 
     29        public double weight; 
     30 
    2831        public Sample(T value_, double weight_) { 
    2932            value = value_; 
    3033            weight = weight_; 
    3134        } 
    32         public T value; 
    33         public double weight; 
    34  
    35         public object Clone() { return this.MemberwiseClone(); } 
    3635    } 
    3736} 
  • trunk/QLNet/QLNet/Pricingengines/vanilla/mcamericanengine.cs

    r223 r224  
    124124 
    125125            // the payoff gives an additional value 
    126             v_.Add(payoff.value); 
     126            v_.Add(this.payoff); 
    127127 
    128128            StrikedTypePayoff strikePayoff = payoff_ as StrikedTypePayoff; 
     
    137137        public double value(Path path, int t) { return payoff(state(path, t)); } 
    138138        public List<Func<double, double>> basisSystem() { return v_; } 
    139  
    140         protected double payoff(double state) { return state/scalingValue_; } 
     139        protected double payoff(double state) { return payoff_.value(state / scalingValue_); } 
    141140    } 
    142141 
     
    144143    //! Monte Carlo American engine factory 
    145144    //template <class RNG = PseudoRandom, class S = Statistics> 
     145    public class MakeMCAmericanEngine<RNG> : MakeMCAmericanEngine<RNG, Statistics> 
     146        where RNG : IRSG, new() { 
     147        public MakeMCAmericanEngine(GeneralizedBlackScholesProcess process) : base(process) { } 
     148    } 
     149 
    146150    public class MakeMCAmericanEngine<RNG, S> 
    147151        where RNG : IRSG, new() 
     
    204208            return this; 
    205209        } 
    206         //public MakeMCAmericanEngine withAntitheticVariate(bool b = true); b) { 
     210        public MakeMCAmericanEngine<RNG, S> withAntitheticVariate() { return withAntitheticVariate(true); } 
    207211        public MakeMCAmericanEngine<RNG, S> withAntitheticVariate(bool b) { 
    208212            antithetic_ = b; 
  • trunk/QLNet/Test2008/Test2008.csproj

    r204 r224  
    33    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> 
    44    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> 
    5     <ProductVersion>9.0.21022</ProductVersion> 
     5    <ProductVersion>9.0.30428</ProductVersion> 
    66    <SchemaVersion>2.0</SchemaVersion> 
    77    <ProjectGuid>{2EF8B45B-940A-4B77-8776-E8CE0036961E}</ProjectGuid> 
     
    4141  </ItemGroup> 
    4242  <ItemGroup> 
     43    <Compile Include="T_LinearLeastSquaresRegression.cs" /> 
    4344    <Compile Include="T_Optimizers.cs" /> 
    4445    <Compile Include="Properties\AssemblyInfo.cs" />