| | 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 | /* |
|---|
| 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 | } |
|---|
| 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 | } |
|---|