Assembla home | Assembla project page
 

Changeset 222

Show
Ignore:
Timestamp:
07/02/08 19:42:11 (3 months ago)
Author:
snovik
Message:

New: lsmbasissystem

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/QLNet/QLNet/Methods/montecarlo/lsmbasissystem.cs

    r219 r222  
    11/* 
     2 Copyright (C) 2008 Siarhei Novik (snovik@gmail.com) 
     3   
     4 This file is part of QLNet Project http://www.qlnet.org 
     5 
     6 QLNet is free software: you can redistribute it and/or modify it 
     7 under the terms of the QLNet license.  You should have received a 
     8 copy of the license along with this program; if not, license is   
     9 available online at <http://trac2.assembla.com/QLNet/wiki/License>. 
     10   
     11 QLNet is a based on QuantLib, a free-software/open-source library 
     12 for financial quantitative analysts and developers - http://quantlib.org/ 
     13 The QuantLib license is available online at http://quantlib.org/license.shtml. 
     14  
     15 This program is distributed in the hope that it will be useful, but WITHOUT 
     16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
     17 FOR A PARTICULAR PURPOSE.  See the license for more details. 
     18*/ 
     19/* 
    220 Copyright (C) 2008 Siarhei Novik (snovik@gmail.com) 
    321   
     
    2442namespace QLNet { 
    2543    public static class LsmBasisSystem { 
    26         public enum PolynomType { Monomial, Laguerre, Hermite, Hyperbolic, 
    27                                   Legendre, Chebyshev, Chebyshev2th }; 
     44        public enum PolynomType { 
     45            Monomial, Laguerre, Hermite, Hyperbolic, 
     46            Legendre, Chebyshev, Chebyshev2th 
     47        }; 
    2848 
    2949        public static List<Func<double, double>> pathBasisSystem(int order, PolynomType polynomType) { 
    30             List<Func<double, double>> ret = new List<Func<double,double>>(); 
     50            List<Func<double, double>> ret = new List<Func<double, double>>(); 
    3151            for (int i=0; i<=order; ++i) { 
    32                 //switch (polynomType) { 
    33                 //  case PolynomType.Monomial: 
    34                 //      ret.Add(MonomialFct(i)); 
    35                 //    break; 
    36                 //  case PolynomType.Laguerre: 
    37                 //    ret.Add( 
    38                 //        bind(&GaussianOrthogonalPolynomial::weightedValue, 
    39                 //                    GaussLaguerrePolynomial(), i, _1)); 
    40                 //    break; 
    41                 //  case PolynomType.Hermite: 
    42                 //    ret.Add( 
    43                 //        bind(&GaussianOrthogonalPolynomial::weightedValue, 
    44                 //                    GaussHermitePolynomial(), i, _1)); 
    45                 //    break; 
    46                 //  case PolynomType.Hyperbolic: 
    47                 //    ret.Add( 
    48                 //        bind(&GaussianOrthogonalPolynomial::weightedValue, 
    49                 //                    GaussHyperbolicPolynomial(), i, _1)); 
    50                 //    break; 
    51                 //  case PolynomType.Legendre: 
    52                 //    ret.Add( 
    53                 //        bind(&GaussianOrthogonalPolynomial::weightedValue, 
    54                 //                    GaussLegendrePolynomial(), i, _1)); 
    55                 //    break; 
    56                 //  case PolynomType.Chebyshev: 
    57                 //    ret.Add( 
    58                 //        bind(&GaussianOrthogonalPolynomial::weightedValue, 
    59                 //                    GaussChebyshevPolynomial(), i, _1)); 
    60                 //    break; 
    61                 //  case PolynomType.Chebyshev2th: 
    62                 //    ret.Add( 
    63                 //        bind(&GaussianOrthogonalPolynomial::weightedValue, 
    64                 //                    GaussChebyshev2thPolynomial(), i, _1)); 
    65                 //    break; 
    66                 //  default: 
    67                 //    QL_FAIL("unknown regression type"); 
    68                 //} 
     52                switch (polynomType) { 
     53                    case PolynomType.Monomial: 
     54                        ret.Add(new MonomialFct(i).value); 
     55                        break; 
     56                    case PolynomType.Laguerre: 
     57                        ret.Add((x) => new GaussLaguerrePolynomial().weightedValue(i, x)); 
     58                        break; 
     59                    case PolynomType.Hermite: 
     60                        ret.Add((x) => new GaussHermitePolynomial().weightedValue(i, x)); 
     61                        break; 
     62                    case PolynomType.Hyperbolic: 
     63                        ret.Add((x) => new GaussHyperbolicPolynomial().weightedValue(i, x)); 
     64                        break; 
     65                    case PolynomType.Legendre: 
     66                        ret.Add((x) => new GaussLegendrePolynomial().weightedValue(i, x)); 
     67                        break; 
     68                    case PolynomType.Chebyshev: 
     69                        ret.Add((x) => new GaussChebyshevPolynomial().weightedValue(i, x)); 
     70                        break; 
     71                    case PolynomType.Chebyshev2th: 
     72                        ret.Add((x) => new GaussChebyshev2thPolynomial().weightedValue(i, x)); 
     73                        break; 
     74                    default: 
     75                        throw new ApplicationException("unknown regression type"); 
     76                } 
     77            } 
     78            return ret; 
     79        } 
     80 
     81 
     82        public static List<Func<Vector, double>> multiPathBasisSystem(int dim, int order, PolynomType polynomType) { 
     83 
     84            List<Func<double, double>> b = pathBasisSystem(order, polynomType); 
     85 
     86            List<Func<Vector, double>> ret = new List<Func<Vector,double>>(); 
     87            ret.Add((xx) => 1.0); 
     88 
     89            for (int i=1; i<=order; ++i) { 
     90                List<Func<Vector, double>> a = w(dim, i, polynomType, b); 
     91 
     92                foreach (var iter in a) { 
     93                    ret.Add(iter); 
     94                } 
    6995            } 
    7096 
    71         return ret; 
    72     } 
     97            // remove-o-zap: now remove redundant functions. 
     98            // usually we do have a lot of them due to the construction schema. 
     99            // We use a more "hands on" method here. 
     100            List<bool> rm = new InitializedList<bool>(ret.Count, true); 
    73101 
    74         //public static List<Func<double, Vector>> multiPathBasisSystem(int dim, int order, PolynomType polynomType); 
     102            Vector x = new Vector(dim), v = new Vector(ret.Count); 
     103            MersenneTwisterUniformRng rng = new MersenneTwisterUniformRng(1234UL); 
    75104 
    76         //private static List<Func<double, Vector>> 
    77         //    w(int dim, int order, PolynomType polynomType, List<Func<double, double>> basis); 
     105            for (int i=0; i<10; ++i) { 
     106                int k; 
     107 
     108                // calculate random x vector 
     109                for (k=0; k<dim; ++k) { 
     110                    x[k] = rng.next().value; 
     111                } 
     112 
     113                // get return values for all basis functions 
     114                for (k = 0; k < ret.Count; ++k) { 
     115                    v[k] = ret[k](x); 
     116                } 
     117 
     118                // find duplicates 
     119                for (k = 0; k < ret.Count; ++k) { 
     120                    if (v.First(xx => (Math.Abs(v[k] - xx) <= 10*v[k]*Const.QL_Epsilon)) == v.First() + k) { 
     121                        // don't remove this item, it's unique! 
     122                        rm[k] = false; 
     123                    } 
     124                } 
     125            } 
     126 
     127            int iter2 = 0; 
     128            for (int i = 0; i < rm.Count; ++i) { 
     129                if (rm[i]) { 
     130                    ret.RemoveAt(iter2); 
     131                } 
     132                else { 
     133                    ++iter2; 
     134                } 
     135            } 
     136 
     137            return ret; 
     138        } 
     139 
     140        private static List<Func<Vector, double>> w(int dim, int order, PolynomType polynomType, List<Func<double, double>> b) { 
     141 
     142           List<Func<Vector, double>> ret = new List<Func<Vector,double>>(); 
     143 
     144           for (int i=order; i>=1; --i) { 
     145               List<Func<Vector, double>> left = w(dim, order-i, polynomType, b); 
     146 
     147               for (int j=0; j<dim; ++j) { 
     148                   Func<Vector, double> a = (xx => b[i](xx[j])); 
     149 
     150                   if (i == order) 
     151                       ret.Add(a); 
     152                   else // add linear combinations 
     153                       for (j=0; j<left.Count; ++j) 
     154                           ret.Add( xx => a(xx * left[j](xx))); 
     155               } 
     156           } 
     157           return ret; 
     158        } 
    78159    } 
    79160 
     
    88169        public double value(double x) { 
    89170            double ret = 1.0; 
    90             for (int i=0; i<order_; ++i) { 
    91                 ret*=x; 
     171            for (int i = 0; i < order_; ++i) { 
     172                ret *= x; 
    92173            } 
    93174            return ret; 
  • trunk/QLNet/QLNet/QLNet.csproj

    r219 r222  
    141141    <Compile Include="Math\Distributions\poissondistribution.cs" /> 
    142142    <Compile Include="Math\factorial.cs" /> 
     143    <Compile Include="Math\integrals\gaussianorthogonalpolynomial.cs" /> 
    143144    <Compile Include="Math\integrals\Integral.cs" /> 
    144145    <Compile Include="Math\integrals\Kronrodintegral.cs" /> 
  • trunk/QLNet/QLNet/Types.cs

    r217 r222  
    3434        public const double M_LN2 = 0.693147180559945309417; 
    3535        public const double M_PI = 3.141592653589793238462643383280; 
     36        public const double M_PI_2 = 1.57079632679489661923; 
    3637    } 
    3738