Changeset 172
- Timestamp:
- 05/21/08 17:16:35 (5 months ago)
- Files:
-
- trunk/QLNet/QLNet/Math/randomnumbers/primitivepolynomials.cs (added)
- trunk/QLNet/QLNet/Math/randomnumbers/sobolrsg.cs (added)
- trunk/QLNet/QLNet/Math/randomnumbers/sobolrsg2.cs (added)
- trunk/QLNet/QLNet/Math/statistics/gaussianstatistics.cs (added)
- trunk/QLNet/QLNet/Math/statistics/generalstatistics.cs (modified) (6 diffs)
- trunk/QLNet/QLNet/Math/statistics/incrementalstatistics.cs (added)
- trunk/QLNet/QLNet/Math/statistics/riskstatistics.cs (added)
- trunk/QLNet/QLNet/QLNet.csproj (modified) (2 diffs)
- trunk/QLNet/QLNet/Types.cs (modified) (1 diff)
- trunk/QLNet/Test2008/T_RiskStats.cs (added)
- trunk/QLNet/Test2008/Test2008.csproj (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/QLNet/QLNet/Math/statistics/generalstatistics.cs
r170 r172 23 23 24 24 namespace QLNet { 25 public interface IGeneralStatistics { 26 int samples(); 27 double mean(); 28 double standardDeviation(); 29 double percentile(double percent); 30 31 KeyValuePair<double, int> expectationValue(Func<KeyValuePair<double, double>, double> f, 32 Func<KeyValuePair<double, double>, bool> inRange); 33 } 34 25 35 //! Statistics tool 26 36 /*! This class accumulates a set of data and returns their … … 33 43 samples, thus increasing the memory requirements. 34 44 */ 35 public class GeneralStatistics {45 public class GeneralStatistics : IGeneralStatistics { 36 46 private List<KeyValuePair<double,double>> samples_; 37 47 //! number of samples collected … … 43 53 44 54 45 public GeneralStatistics() { 46 reset(); 47 } 48 49 50 /*! returns the standard deviation \f$ \sigma \f$, defined as the 51 square root of the variance. */ 52 public double standardDeviation() { return Math.Sqrt(variance()); } 55 public GeneralStatistics() { reset(); } 56 53 57 54 58 /*! returns the error estimate on the mean value, defined as … … 107 111 return expectationValue(x => x.Key * x.Value, x => true).Key; 108 112 } 113 114 /*! returns the standard deviation \f$ \sigma \f$, defined as the 115 square root of the variance. */ 116 public double standardDeviation() { return Math.Sqrt(variance()); } 109 117 110 118 /*! returns the variance, defined as … … 189 197 \pre \f$ y \f$ must be in the range \f$ (0-1]. \f$ 190 198 */ 191 //public double percentile(double percent) { 192 193 // if (!(percent > 0.0 && percent <= 1.0)) 194 // throw new ApplicationException("percentile (" + percent + ") must be in (0.0, 1.0]"); 195 196 // double sampleWeight = weightSum(); 197 // if (!(sampleWeight > 0)) throw new ApplicationException("empty sample set"); 198 199 // sort(); 200 201 // List<KeyValuePair<double, double>>::iterator k, l; 202 // k = samples_.begin(); 203 // l = samples_.end()-1; 204 // /* the sum of weight is non null, therefore there's at least one sample */ 205 // double integral = k->second, target = percent*sampleWeight; 206 // while (integral < target && k != l) { 207 // k++; 208 // integral += k->second; 209 // } 210 // return k->first; 211 //} 199 public double percentile(double percent) { 200 201 if (!(percent > 0.0 && percent <= 1.0)) 202 throw new ApplicationException("percentile (" + percent + ") must be in (0.0, 1.0]"); 203 204 double sampleWeight = weightSum(); 205 if (!(sampleWeight > 0)) throw new ApplicationException("empty sample set"); 206 207 sort(); 208 209 double integral = 0, target = percent*sampleWeight; 210 int pos = samples_.Count<KeyValuePair<double, double>>(x => { integral += x.Value; return integral < target; } ); 211 return samples_[pos].Key; 212 } 212 213 213 214 /*! \f$ y \f$-th top percentile, defined as the value … … 218 219 \pre \f$ y \f$ must be in the range \f$ (0-1]. \f$ 219 220 */ 220 //public double topPercentile(double y) { 221 222 // QL_REQUIRE(percent > 0.0 && percent <= 1.0, 223 // "percentile (" << percent << ") must be in (0.0, 1.0]"); 224 225 // double sampleWeight = weightSum(); 226 // if (!(sampleWeight > 0)) throw new ApplicationException("empty sample set"); 227 228 // sort(); 229 230 // std::vector<std::pair<Real,Real> >::reverse_iterator k, l; 231 // k = samples_.rbegin(); 232 // l = samples_.rend()-1; 233 // /* the sum of weight is non null, therefore there's 234 // at least one sample */ 235 // double integral = k->second, target = percent*sampleWeight; 236 // while (integral < target && k != l) { 237 // k++; 238 // integral += k->second; 239 // } 240 // return k->first; 241 //} 221 public double topPercentile(double percent) { 222 if (!(percent > 0.0 && percent <= 1.0)) 223 throw new ApplicationException("percentile (" + percent + ") must be in (0.0, 1.0]"); 224 225 double sampleWeight = weightSum(); 226 if (!(sampleWeight > 0)) throw new ApplicationException("empty sample set"); 227 228 sort(); 229 230 double integral = 0, target = 1 - percent*sampleWeight; 231 int pos = samples_.Count<KeyValuePair<double, double>>(x => { integral += x.Value; return integral < target; } ); 232 return samples_[pos].Key; 233 } 242 234 243 235 ////! adds a sequence of data to the set, with default weight trunk/QLNet/QLNet/QLNet.csproj
r171 r172 162 162 <Compile Include="Math\Optimization\SteepestDescent.cs" /> 163 163 <Compile Include="Math\randomnumbers\mt19937uniformrng.cs" /> 164 <Compile Include="Math\randomnumbers\primitivepolynomials.cs" /> 164 165 <Compile Include="Math\randomnumbers\randomsequencegenerator.cs" /> 165 166 <Compile Include="Math\randomnumbers\rngtraits.cs" /> 166 167 <Compile Include="Math\randomnumbers\seedgenerator.cs" /> 168 <Compile Include="Math\randomnumbers\sobolrsg.cs" /> 169 <Compile Include="Math\randomnumbers\sobolrsg2.cs" /> 167 170 <Compile Include="Math\SampledCurve.cs" /> 168 171 <Compile Include="Math\Solver1d.cs" /> … … 183 186 </Compile> 184 187 <Compile Include="Math\Solvers1d\Secant.cs" /> 188 <Compile Include="Math\statistics\gaussianstatistics.cs" /> 185 189 <Compile Include="Math\statistics\generalstatistics.cs" /> 190 <Compile Include="Math\statistics\incrementalstatistics.cs" /> 191 <Compile Include="Math\statistics\riskstatistics.cs" /> 186 192 <Compile Include="Math\transformedgrid.cs" /> 187 193 <Compile Include="Math\Vector.cs" /> trunk/QLNet/QLNet/Types.cs
r109 r172 26 26 public const double M_SQRT_2 = 0.7071067811865475244008443621048490392848359376887; 27 27 public const double M_1_SQRTPI = 0.564189583547756286948; 28 29 public const double M_LN2 = 0.693147180559945309417; 28 30 } 29 31 trunk/QLNet/Test2008/Test2008.csproj
r144 r172 55 55 <Compile Include="T_Money.cs" /> 56 56 <Compile Include="T_Operators.cs" /> 57 <Compile Include="T_RiskStats.cs" /> 57 58 <Compile Include="T_Rounding.cs" /> 58 59 <Compile Include="T_SampledCurve.cs" />