Changeset 224
- Timestamp:
- 07/06/08 21:06:38 (3 months ago)
- Files:
-
- trunk/QLNet/Examples/EquityOption/EquityOption.cs (modified) (2 diffs)
- trunk/QLNet/QLNet/Math/Matrix.cs (modified) (3 diffs)
- trunk/QLNet/QLNet/Math/linearleastsquaresregression.cs (modified) (3 diffs)
- trunk/QLNet/QLNet/Math/matrixutilities/svd.cs (modified) (13 diffs)
- trunk/QLNet/QLNet/Methods/montecarlo/longstaffschwartzpathpricer.cs (modified) (3 diffs)
- trunk/QLNet/QLNet/Methods/montecarlo/path.cs (modified) (2 diffs)
- trunk/QLNet/QLNet/Methods/montecarlo/sample.cs (modified) (1 diff)
- trunk/QLNet/QLNet/Pricingengines/vanilla/mcamericanengine.cs (modified) (4 diffs)
- trunk/QLNet/Test2008/T_LinearLeastSquaresRegression.cs (added)
- trunk/QLNet/Test2008/Test2008.csproj (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/QLNet/Examples/EquityOption/EquityOption.cs
r219 r224 222 222 timeSteps = 1; 223 223 method = "MC (crude)"; 224 intmcSeed = 42;224 ulong mcSeed = 42; 225 225 IPricingEngine mcengine1 = new MakeMCEuropeanEngine<PseudoRandom>(bsmProcess) 226 226 .withSteps(timeSteps) 227 227 .withTolerance(0.02) 228 .withSeed( (ulong)mcSeed)228 .withSeed(mcSeed) 229 229 .value(); 230 230 europeanOption.setPricingEngine(mcengine1); … … 252 252 // Monte Carlo Method: MC (Longstaff Schwartz) 253 253 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()); 266 266 267 267 // End test trunk/QLNet/QLNet/Math/Matrix.cs
r223 r224 28 28 container. 29 29 */ 30 public struct Matrix {30 public struct Matrix : ICloneable { 31 31 #region properties 32 32 private int rows_, columns_; … … 38 38 public double this[int i, int j] { get { return data_[i, j]; } set { data_[i, j] = value; } } 39 39 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) { 40 46 Vector result = new Vector(rows_); 41 47 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++)48 48 result[i] = data_[i, c]; 49 49 return result; … … 180 180 this[i1, j1] = t; 181 181 } 182 183 // IClonable interface 184 public object Clone() { return this.MemberwiseClone(); } 182 185 } 183 186 } trunk/QLNet/QLNet/Math/linearleastsquaresregression.cs
r223 r224 31 31 checking their properties. 32 32 */ 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 33 38 public class LinearLeastSquaresRegression<ArgumentType> { 34 39 private Vector a_; … … 36 41 37 42 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); 40 45 41 46 if (x.Count != y.Count) … … 49 54 50 55 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)); 54 58 55 59 SVD svd = new SVD(A); 56 60 Matrix V = svd.V(); 57 61 Matrix U = svd.U(); 58 Vector w = svd.singularValues();62 Vector w = svd.singularValues(); 59 63 double threshold = n*Const.QL_Epsilon; 60 64 trunk/QLNet/QLNet/Math/matrixutilities/svd.cs
r223 r224 37 37 38 38 public SVD(Matrix M) { 39 //using std::swap;40 41 39 Matrix A; 42 40 … … 54 52 M = V S U^T (idempotence of transposition, 55 53 symmetry of diagonal matrix S) 56 57 54 */ 58 55 59 56 if (M.rows() >= M.columns()) { 60 A = M;57 A = (Matrix)M.Clone(); 61 58 transpose_ = false; 62 59 } else { … … 69 66 70 67 // we're sure that m_ >= n_ 71 72 68 s_ = new Vector(n_); 73 69 U_ = new Matrix(m_, n_, 0.0); … … 79 75 // Reduce A to bidiagonal form, storing the diagonal elements 80 76 // in s and the super-diagonal elements in e. 81 82 77 int nct = Math.Min(m_ - 1, n_); 83 78 int nrt = Math.Max(0, n_ - 2); … … 107 102 108 103 // Apply the transformation. 109 110 104 double t = 0; 111 105 for (i = k; i < m_; i++) { … … 120 114 // Place the k-th row of A into e for the 121 115 // subsequent calculation of the row transformation. 122 123 116 e[j] = A[k, j]; 124 117 } 125 118 if (k < nct) { 126 119 127 // Place the transformation in U for subsequent back 128 // multiplication. 129 120 // Place the transformation in U for subsequent back multiplication. 130 121 for (i = k; i < m_; i++) { 131 122 U_[i, k] = A[i, k]; … … 134 125 if (k < nrt) { 135 126 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]. 138 128 // Compute 2-norm without under/overflow. 139 129 e[k] = 0; … … 154 144 155 145 // Apply the transformation. 156 157 146 for (i = k + 1; i < m_; i++) { 158 147 work[i] = 0.0; … … 171 160 } 172 161 173 // Place the transformation in V for subsequent 174 // back multiplication. 175 162 // Place the transformation in V for subsequent back multiplication. 176 163 for (i = k + 1; i < n_; i++) { 177 164 V_[i, k] = e[i]; … … 181 168 182 169 // Set up the final bidiagonal matrix or order n. 183 184 170 if (nct < n_) { 185 171 s_[nct] = A[nct, nct]; … … 191 177 192 178 // generate U 193 194 179 for (j = nct; j < n_; j++) { 195 180 for (i = 0; i < m_; i++) { … … 226 211 227 212 // generate V 228 229 213 for (k = n_ - 1; k >= 0; --k) { 230 214 if ((k < nrt) & (e[k] != 0.0)) { … … 269 253 break; 270 254 } 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]))) { 273 256 e[k] = 0.0; 274 257 break; trunk/QLNet/QLNet/Methods/montecarlo/longstaffschwartzpathpricer.cs
r223 r224 35 35 reproducing results available in web/literature 36 36 */ 37 public class LongstaffSchwartzPathPricer<PathType> : PathPricer<PathType> where PathType : Path {37 public class LongstaffSchwartzPathPricer<PathType> : PathPricer<PathType> where PathType : Path, ICloneable { 38 38 protected bool calibrationPhase_; 39 39 protected EarlyExercisePathPricer<PathType> pathPricer_; … … 42 42 protected List<double> dF_; 43 43 44 protected List<PathType> paths_ ;44 protected List<PathType> paths_ = new List<PathType>(); 45 45 protected List<Func<double, double>> v_; 46 46 … … 63 63 if (calibrationPhase_) { 64 64 // store paths for the calibration 65 paths_.Add( path);65 paths_.Add((PathType)path.Clone()); 66 66 // result doesn't matter 67 67 return 0.0; trunk/QLNet/QLNet/Methods/montecarlo/path.cs
r192 r224 28 28 \note the path includes the initial asset value as its first point. 29 29 */ 30 public class Path {30 public class Path : ICloneable { 31 31 private TimeGrid timeGrid_; 32 32 private Vector values_; … … 66 66 //! time grid 67 67 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 } 68 75 } 69 76 } trunk/QLNet/QLNet/Methods/montecarlo/sample.cs
r192 r224 25 25 //! weighted sample 26 26 /*! \ingroup mcarlo */ 27 public struct Sample<T> : ICloneable { 27 public struct Sample<T> { 28 public T value; 29 public double weight; 30 28 31 public Sample(T value_, double weight_) { 29 32 value = value_; 30 33 weight = weight_; 31 34 } 32 public T value;33 public double weight;34 35 public object Clone() { return this.MemberwiseClone(); }36 35 } 37 36 } trunk/QLNet/QLNet/Pricingengines/vanilla/mcamericanengine.cs
r223 r224 124 124 125 125 // the payoff gives an additional value 126 v_.Add( payoff.value);126 v_.Add(this.payoff); 127 127 128 128 StrikedTypePayoff strikePayoff = payoff_ as StrikedTypePayoff; … … 137 137 public double value(Path path, int t) { return payoff(state(path, t)); } 138 138 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_); } 141 140 } 142 141 … … 144 143 //! Monte Carlo American engine factory 145 144 //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 146 150 public class MakeMCAmericanEngine<RNG, S> 147 151 where RNG : IRSG, new() … … 204 208 return this; 205 209 } 206 //public MakeMCAmericanEngine withAntitheticVariate(bool b = true); b) {210 public MakeMCAmericanEngine<RNG, S> withAntitheticVariate() { return withAntitheticVariate(true); } 207 211 public MakeMCAmericanEngine<RNG, S> withAntitheticVariate(bool b) { 208 212 antithetic_ = b; trunk/QLNet/Test2008/Test2008.csproj
r204 r224 3 3 <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> 4 4 <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> 5 <ProductVersion>9.0. 21022</ProductVersion>5 <ProductVersion>9.0.30428</ProductVersion> 6 6 <SchemaVersion>2.0</SchemaVersion> 7 7 <ProjectGuid>{2EF8B45B-940A-4B77-8776-E8CE0036961E}</ProjectGuid> … … 41 41 </ItemGroup> 42 42 <ItemGroup> 43 <Compile Include="T_LinearLeastSquaresRegression.cs" /> 43 44 <Compile Include="T_Optimizers.cs" /> 44 45 <Compile Include="Properties\AssemblyInfo.cs" />