Assembla home | Assembla project page
 

Changeset 172

Show
Ignore:
Timestamp:
05/21/08 17:16:35 (5 months ago)
Author:
snovik
Message:

New: Sobol RG

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/QLNet/QLNet/Math/statistics/generalstatistics.cs

    r170 r172  
    2323 
    2424namespace 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 
    2535    //! Statistics tool 
    2636    /*! This class accumulates a set of data and returns their 
     
    3343        samples, thus increasing the memory requirements. 
    3444    */ 
    35     public class GeneralStatistics
     45    public class GeneralStatistics : IGeneralStatistics
    3646        private List<KeyValuePair<double,double>> samples_; 
    3747        //! number of samples collected 
     
    4353 
    4454 
    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 
    5357 
    5458        /*! returns the error estimate on the mean value, defined as 
     
    107111            return expectationValue(x => x.Key * x.Value, x => true).Key; 
    108112        } 
     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()); } 
    109117 
    110118        /*! returns the variance, defined as 
     
    189197            \pre \f$ y \f$ must be in the range \f$ (0-1]. \f$ 
    190198        */ 
    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        } 
    212213 
    213214        /*! \f$ y \f$-th top percentile, defined as the value 
     
    218219            \pre \f$ y \f$ must be in the range \f$ (0-1]. \f$ 
    219220        */ 
    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        } 
    242234 
    243235        ////! adds a sequence of data to the set, with default weight 
  • trunk/QLNet/QLNet/QLNet.csproj

    r171 r172  
    162162    <Compile Include="Math\Optimization\SteepestDescent.cs" /> 
    163163    <Compile Include="Math\randomnumbers\mt19937uniformrng.cs" /> 
     164    <Compile Include="Math\randomnumbers\primitivepolynomials.cs" /> 
    164165    <Compile Include="Math\randomnumbers\randomsequencegenerator.cs" /> 
    165166    <Compile Include="Math\randomnumbers\rngtraits.cs" /> 
    166167    <Compile Include="Math\randomnumbers\seedgenerator.cs" /> 
     168    <Compile Include="Math\randomnumbers\sobolrsg.cs" /> 
     169    <Compile Include="Math\randomnumbers\sobolrsg2.cs" /> 
    167170    <Compile Include="Math\SampledCurve.cs" /> 
    168171    <Compile Include="Math\Solver1d.cs" /> 
     
    183186    </Compile> 
    184187    <Compile Include="Math\Solvers1d\Secant.cs" /> 
     188    <Compile Include="Math\statistics\gaussianstatistics.cs" /> 
    185189    <Compile Include="Math\statistics\generalstatistics.cs" /> 
     190    <Compile Include="Math\statistics\incrementalstatistics.cs" /> 
     191    <Compile Include="Math\statistics\riskstatistics.cs" /> 
    186192    <Compile Include="Math\transformedgrid.cs" /> 
    187193    <Compile Include="Math\Vector.cs" /> 
  • trunk/QLNet/QLNet/Types.cs

    r109 r172  
    2626        public const double M_SQRT_2 = 0.7071067811865475244008443621048490392848359376887; 
    2727        public const double M_1_SQRTPI = 0.564189583547756286948; 
     28 
     29        public const double M_LN2 = 0.693147180559945309417; 
    2830    } 
    2931 
  • trunk/QLNet/Test2008/Test2008.csproj

    r144 r172  
    5555    <Compile Include="T_Money.cs" /> 
    5656    <Compile Include="T_Operators.cs" /> 
     57    <Compile Include="T_RiskStats.cs" /> 
    5758    <Compile Include="T_Rounding.cs" /> 
    5859    <Compile Include="T_SampledCurve.cs" />