Assembla home | Assembla project page
 

Changeset 173

Show
Ignore:
Timestamp:
05/22/08 09:30:08 (6 months ago)
Author:
snovik
Message:

Fix: Risk & Gaussian Statistics
New: RiskStat? test

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/QLNet/QLNet.sln

    r171 r173  
    22Microsoft Visual Studio Solution File, Format Version 10.00 
    33# Visual Studio 2008 
    4 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{A1B2F21C-BD08-4751-9FBE-6DD0405E3DBC}" 
    5         ProjectSection(SolutionItems) = preProject 
    6                 localtestrun.testrunconfig = localtestrun.testrunconfig 
    7                 QLNet.vsmdi = QLNet.vsmdi 
    8                 QLNet1.vsmdi = QLNet1.vsmdi 
    9         EndProjectSection 
    10 EndProject 
    114Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QLNet", "QLNet\QLNet.csproj", "{F6E762BD-DCDF-4CA0-ABAD-CB21C7D03BEC}" 
    125EndProject 
     
    2720Project("{F184B08F-C81C-45F6-A57F-5ABD9991F28F}") = "Swap", "VB\Swap\Swap.vbproj", "{5724FF23-DB57-4D10-B88E-B1DFF1249C88}" 
    2821EndProject 
     22Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{0A13061B-C0D9-4D34-981A-E506240D6A75}" 
     23        ProjectSection(SolutionItems) = preProject 
     24                QLNet.vsmdi = QLNet.vsmdi 
     25        EndProjectSection 
     26EndProject 
    2927Global 
    3028        GlobalSection(TestCaseManagementSettings) = postSolution 
    31                 CategoryFile = QLNet1.vsmdi 
     29                CategoryFile = QLNet.vsmdi 
    3230        EndGlobalSection 
    3331        GlobalSection(SolutionConfigurationPlatforms) = preSolution 
  • trunk/QLNet/QLNet.vsmdi

    r165 r173  
    55      <TestLink id="bbe2d937-bb70-a666-a879-b3063800a42e" name="testFairSpread" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel,   PublicKeyToken=b03f5f7f11d50a3a" /> 
    66      <TestLink id="25b4118b-8066-f12f-3687-a5ef1886eb1c" name="testInArrears" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel,   PublicKeyToken=b03f5f7f11d50a3a" /> 
     7      <TestLink id="0ee842b0-6275-252f-a1d0-000b0a6a4c52" name="testCachedValue" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel,   PublicKeyToken=b03f5f7f11d50a3a" /> 
    78      <TestLink id="27ded393-6046-4057-bd65-31f629c9bf55" name="testFairRate" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel,   PublicKeyToken=b03f5f7f11d50a3a" /> 
    8       <TestLink id="0ee842b0-6275-252f-a1d0-000b0a6a4c52" name="testCachedValue" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel,   PublicKeyToken=b03f5f7f11d50a3a" /> 
    99      <TestLink id="3b1816a9-7785-f64f-bc52-7014af11e8e5" name="testSpreadDependency" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel,   PublicKeyToken=b03f5f7f11d50a3a" /> 
    1010      <TestLink id="2b04fe68-610a-b2de-e241-ef9a50fdb3fb" name="testRateDependency" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel,   PublicKeyToken=b03f5f7f11d50a3a" /> 
     
    4242    </TestLinks> 
    4343  </TestList> 
     44  <TestList name="Risk Stats" id="a33eb53e-6299-480c-8958-5bcb922a4801" parentListId="8c43106b-9dc1-4907-a29f-aa66a61bf5b6"> 
     45    <TestLinks> 
     46      <TestLink id="b87fb037-4727-72de-6cc9-162a99e11210" name="RiskStatisticsTest" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel,   PublicKeyToken=b03f5f7f11d50a3a" /> 
     47    </TestLinks> 
     48  </TestList> 
    4449  <TestList name="SampledCurve" id="a6f5cea2-0706-4e42-a89c-afd9f63d2645" parentListId="8c43106b-9dc1-4907-a29f-aa66a61bf5b6"> 
    4550    <TestLinks> 
     
    4954  <TestList name="American Option" id="da9a808d-acf7-4baf-9dfb-e4b091d30fef" parentListId="8c43106b-9dc1-4907-a29f-aa66a61bf5b6"> 
    5055    <TestLinks> 
    51       <TestLink id="33c4d801-48ac-44c4-329e-d6fe84f5e034" name="testJuValues" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel,   PublicKeyToken=b03f5f7f11d50a3a" /> 
     56      <TestLink id="2bbf531b-68cb-84c0-a06f-6bedfb89f20f" name="testBaroneAdesiWhaleyValues" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel,   PublicKeyToken=b03f5f7f11d50a3a" /> 
    5257      <TestLink id="3be50179-04ee-902d-73d8-3c94e9305a67" name="testFdShoutGreeks" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel,   PublicKeyToken=b03f5f7f11d50a3a" /> 
    5358      <TestLink id="783f96db-cce8-bf61-b280-f5010f7d7eb3" name="testFdValues" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel,   PublicKeyToken=b03f5f7f11d50a3a" /> 
    5459      <TestLink id="1e42c90a-8e82-1369-9ed9-52b036b1c07e" name="testBjerksundStenslandValues" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel,   PublicKeyToken=b03f5f7f11d50a3a" /> 
    5560      <TestLink id="9f4ff6f8-9938-8ecb-5608-7dbc928cbda3" name="testFdAmericanGreeks" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel,   PublicKeyToken=b03f5f7f11d50a3a" /> 
    56       <TestLink id="2bbf531b-68cb-84c0-a06f-6bedfb89f20f" name="testBaroneAdesiWhaleyValues" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel,   PublicKeyToken=b03f5f7f11d50a3a" /> 
     61      <TestLink id="33c4d801-48ac-44c4-329e-d6fe84f5e034" name="testJuValues" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel,   PublicKeyToken=b03f5f7f11d50a3a" /> 
    5762    </TestLinks> 
    5863  </TestList> 
  • trunk/QLNet/QLNet/Math/randomnumbers/sobolrsg.cs

    r172 r173  
    116116        /*! \pre dimensionality must be <= PPMT_MAX_DIM */ 
    117117        //public SobolRsg(int dimensionality, ulong seed = 0, DirectionIntegers directionIntegers = Jaeckel); 
     118        public SobolRsg(int dimensionality) : this(dimensionality, 0, DirectionIntegers.Jaeckel) { } 
    118119        public SobolRsg(int dimensionality, ulong seed, DirectionIntegers directionIntegers) { 
    119120            dimensionality_ = dimensionality; 
  • trunk/QLNet/QLNet/Math/statistics/gaussianstatistics.cs

    r172 r173  
    3333 
    3434        public GenericGaussianStatistics() { } 
    35         // public GenericGaussianStatistics(Stat s) : base(s) { } 
     35        public GenericGaussianStatistics(Stat s) { 
     36            impl_ = s; 
     37        } 
    3638 
    3739        #region wrap-up Stat 
    38         Stat impl_ = new Stat(); 
     40        protected Stat impl_ = new Stat(); 
    3941 
    4042        public int samples() { return impl_.samples(); } 
    4143        public double mean() { return impl_.mean(); } 
     44        public double min() { return impl_.min(); } 
     45        public double max() { return impl_.max(); } 
    4246        public double standardDeviation() { return impl_.standardDeviation(); } 
     47        public double variance() { return impl_.variance(); } 
     48        public double skewness() { return impl_.skewness(); } 
     49        public double kurtosis() { return impl_.kurtosis(); } 
    4350        public double percentile(double percent) { return impl_.percentile(percent); } 
     51        public double weightSum() { return impl_.weightSum(); } 
     52 
     53        public void reset() { impl_.reset(); } 
     54        public void addSequence(List<double> data, List<double> weight) { impl_.addSequence(data, weight); } 
    4455 
    4556        public KeyValuePair<double, int> expectationValue(Func<KeyValuePair<double, double>, double> f, 
     
    168179    public class GaussianStatistics : GenericGaussianStatistics<GeneralStatistics> { } 
    169180 
     181     
     182    //! Helper class for precomputed distributions 
     183    public class StatsHolder : IGeneralStatistics { 
     184        private double mean_, standardDeviation_; 
     185        public double mean() { return mean_; } 
     186        public double standardDeviation() { return standardDeviation_; } 
     187 
     188        public StatsHolder() { } // required for generics 
     189        public StatsHolder(double mean, double standardDeviation) { 
     190            mean_ = mean; 
     191            standardDeviation_ = standardDeviation; 
     192        } 
     193 
     194        #region IGeneralStatistics 
     195        public int samples() { throw new NotSupportedException(); } 
     196        public double min() { throw new NotSupportedException(); } 
     197        public double max() { throw new NotSupportedException(); } 
     198        public double variance() { throw new NotSupportedException(); } 
     199        public double skewness() { throw new NotSupportedException(); } 
     200        public double kurtosis() { throw new NotSupportedException(); } 
     201        public double percentile(double percent) { throw new NotSupportedException(); } 
     202        public double weightSum() { throw new NotSupportedException(); } 
     203 
     204        public void reset() { throw new NotSupportedException(); } 
     205        public void addSequence(List<double> data, List<double> weight) { throw new NotSupportedException(); } 
     206 
     207        public KeyValuePair<double, int> expectationValue(Func<KeyValuePair<double, double>, double> f, 
     208                                                          Func<KeyValuePair<double, double>, bool> inRange) { 
     209            throw new NotSupportedException(); 
     210        } 
     211        #endregion 
     212    } 
    170213} 
  • trunk/QLNet/QLNet/Math/statistics/generalstatistics.cs

    r172 r173  
    2626        int samples(); 
    2727        double mean(); 
     28        double min(); 
     29        double max(); 
    2830        double standardDeviation(); 
     31        double variance(); 
     32        double skewness(); 
     33        double kurtosis(); 
    2934        double percentile(double percent); 
     35        double weightSum(); 
     36 
     37        void reset(); 
     38        void addSequence(List<double> data, List<double> weight); 
    3039 
    3140        KeyValuePair<double, int> expectationValue(Func<KeyValuePair<double, double>, double> f, 
     
    5160 
    5261        private bool sorted_; 
     62        private double? mean_ = null, weightSum_ = null, variance_ = null, skewness_ = null, kurtosis_ = null; 
    5363 
    5464 
     
    7484 
    7585        //! adds a datum to the set, possibly with a weight 
    76         //public void add(double value, double weight = 1.0) { 
     86        public void add(double value) { add(value, 1); } 
    7787        public void add(double value, double weight) { 
    7888            if (!(weight>=0.0)) throw new ApplicationException("negative weight not allowed"); 
    7989            samples_.Add(new KeyValuePair<double,double>(value,weight)); 
     90             
    8091            sorted_ = false; 
     92            mean_ = weightSum_ = variance_ = skewness_ = kurtosis_ = null; 
    8193        } 
    8294 
     
    8496        public void reset(){ 
    8597            samples_ = new List<KeyValuePair<double,double>>(); 
     98 
    8699            sorted_ = true; 
     100            mean_ = weightSum_ = variance_ = skewness_ = kurtosis_ = null; 
    87101        } 
    88102 
     
    90104        public void sort()  { 
    91105            if (!sorted_) { 
    92                 samples_.Sort(); 
     106                samples_.Sort((x, y) => x.Key.CompareTo(y.Key)); 
    93107                sorted_ = true; 
    94108            } 
     
    98112        //! sum of data weights 
    99113        public double weightSum() { 
    100             double result = 0.0; 
    101             result = samples_.Sum<KeyValuePair<double, double>>(x => x.Value); 
    102             return result
     114            if (weightSum_ == null) 
     115                weightSum_ = samples_.Sum<KeyValuePair<double, double>>(x => x.Value); 
     116            return weightSum_.GetValueOrDefault()
    103117        } 
    104118 
     
    106120            \f[ \langle x \rangle = \frac{\sum w_i x_i}{\sum w_i}. \f] */ 
    107121        public double mean() { 
    108             int N = samples(); 
    109             if (!(samples() > 0)) throw new ApplicationException("empty sample set"); 
    110             // eat our own dog food 
    111             return expectationValue(x => x.Key * x.Value, x => true).Key; 
     122            if (mean_ == null) { 
     123                int N = samples(); 
     124                if (!(samples() > 0)) throw new ApplicationException("empty sample set"); 
     125                // eat our own dog food 
     126                mean_ = expectationValue(x => x.Key * x.Value, x => true).Key; 
     127            } 
     128            return mean_.GetValueOrDefault(); 
    112129        } 
    113130 
     
    120137                x-\langle x \rangle \right)^2 \right\rangle. \f] */ 
    121138        public double variance()  { 
    122             int N = samples(); 
    123             if (!(N > 1)) throw new ApplicationException("sample number <=1, unsufficient"); 
    124             // Subtract the mean and square. Repeat on the whole range. 
    125             // Hopefully, the whole thing will be inlined in a single loop. 
    126             double s2 = expectationValue(x => Math.Pow(x.Key * x.Value - mean(), 2), x => true).Key; 
     139            if (variance_ == null) { 
     140                int N = samples(); 
     141                if (!(N > 1)) throw new ApplicationException("sample number <=1, unsufficient"); 
     142                // Subtract the mean and square. Repeat on the whole range. 
     143                // Hopefully, the whole thing will be inlined in a single loop. 
     144                double s2 = expectationValue(x => Math.Pow(x.Key * x.Value - mean(), 2), x => true).Key; 
    127145 
    128146                //compose(square<Real>(), std::bind2nd(std::minus<Real>(), mean())), () => true).Key; 
    129             return s2*N/(N-1.0); 
     147                variance_ = s2 * N / (N - 1.0); 
     148            } 
     149            return variance_.GetValueOrDefault(); 
    130150        } 
    131151 
     
    136156        */ 
    137157        public double skewness() { 
    138             int N = samples(); 
    139             if (!(N > 2)) throw new ApplicationException("sample number <=2, unsufficient"); 
    140  
    141             double x = expectationValue(y => Math.Pow(y.Key * y.Value - mean(), 3), y => true).Key; 
    142             double sigma = standardDeviation(); 
    143  
    144             return (x/(sigma*sigma*sigma))*(N/(N-1.0))*(N/(N-2.0)); 
     158            if (skewness_ == null) { 
     159                int N = samples(); 
     160                if (!(N > 2)) throw new ApplicationException("sample number <=2, unsufficient"); 
     161 
     162                double x = expectationValue(y => Math.Pow(y.Key * y.Value - mean(), 3), y => true).Key; 
     163                double sigma = standardDeviation(); 
     164 
     165                skewness_ = (x / Math.Pow(sigma, 3)) * (N / (N - 1.0)) * (N / (N - 2.0)); 
     166            } 
     167            return skewness_.GetValueOrDefault(); 
    145168        } 
    146169 
     
    152175        */ 
    153176        public double kurtosis() { 
    154             int N = samples(); 
    155             if (!(N > 3)) throw new ApplicationException("sample number <=3, unsufficient"); 
    156  
    157             double x = expectationValue(y => Math.Pow(y.Key * y.Value - mean(), 4), y => true).Key; 
    158             double sigma2 = variance(); 
    159  
    160             double c1 = (N/(N-1.0)) * (N/(N-2.0)) * ((N+1.0)/(N-3.0)); 
    161             double c2 = 3.0 * ((N-1.0)/(N-2.0)) * ((N-1.0)/(N-3.0)); 
    162  
    163             return c1*(x/(sigma2*sigma2))-c2; 
     177            if (kurtosis_ == null) { 
     178                int N = samples(); 
     179                if (!(N > 3)) throw new ApplicationException("sample number <=3, unsufficient"); 
     180 
     181                double x = expectationValue(y => Math.Pow(y.Key * y.Value - mean(), 4), y => true).Key; 
     182                double sigma2 = variance(); 
     183 
     184                double c1 = (N / (N - 1.0)) * (N / (N - 2.0)) * ((N + 1.0) / (N - 3.0)); 
     185                double c2 = 3.0 * ((N - 1.0) / (N - 2.0)) * ((N - 1.0) / (N - 3.0)); 
     186 
     187                kurtosis_ = c1 * (x / (sigma2 * sigma2)) - c2; 
     188            } 
     189            return kurtosis_.GetValueOrDefault(); 
    164190        } 
    165191 
     
    178204            int N = 0; 
    179205 
    180             foreach(KeyValuePair<double,double> x in samples_) { 
    181                 if (inRange(x)) { 
    182                     num += f(x)*x.Value; 
    183                     den += x.Value; 
    184                     N += 1;                 
    185                 } 
     206            foreach(KeyValuePair<double,double> x in samples_.Where<KeyValuePair<double,double>>(x => inRange(x))) { 
     207                num += f(x)*x.Value; 
     208                den += x.Value; 
     209                N += 1;                 
    186210            } 
    187211 
     
    233257        } 
    234258 
    235         ////! adds a sequence of data to the set, with default weight 
    236         //template <class DataIterator> 
    237         //void addSequence(DataIterator begin, DataIterator end) { 
    238         //    for (;begin!=end;++begin) 
    239         //        add(*begin); 
    240         //} 
    241         ////! adds a sequence of data to the set, each with its weight 
    242         //template <class DataIterator, class WeightIterator> 
    243         //void addSequence(DataIterator begin, DataIterator end, 
    244         //                 WeightIterator wbegin) { 
    245         //    for (;begin!=end;++begin,++wbegin) 
    246         //        add(*begin, *wbegin); 
    247         //} 
     259        //! adds a sequence of data to the set, with default weight 
     260        public void addSequence(List<double> list) { 
     261            foreach(double v in list)  
     262                add(v); 
     263        } 
     264        //! adds a sequence of data to the set, each with its weight 
     265        public void addSequence(List<double> data, List<double> weight) { 
     266            for(int i=0; i< data.Count; i++) 
     267                add(data[i], weight[i]); 
     268        } 
    248269    } 
    249270} 
  • trunk/QLNet/QLNet/Math/statistics/incrementalstatistics.cs

    r172 r173  
    177177        //! adds a datum to the set, possibly with a weight 
    178178        /*! \pre weight must be positive or null */ 
    179         //void add(Real value, Real weight = 1.0); 
     179        public void add(double value) { add(value, 1); } 
    180180        public void add(double value, double weight) { 
    181181            if (!(weight>=0.0)) throw new ApplicationException("negative weight (" + weight + ") not allowed"); 
     
    223223        } 
    224224 
    225         ////! adds a sequence of data to the set, with default weight 
    226         //template <class DataIterator> 
    227         //void addSequence(DataIterator begin, DataIterator end) { 
    228         //    for (;begin!=end;++begin) 
    229         //        add(*begin); 
    230         //} 
    231         ////! adds a sequence of data to the set, each with its weight 
    232         ///*! \pre weights must be positive or null */ 
    233         //template <class DataIterator, class WeightIterator> 
    234         //void addSequence(DataIterator begin, DataIterator end, 
    235         //                 WeightIterator wbegin) { 
    236         //    for (;begin!=end;++begin,++wbegin) 
    237         //        add(*begin, *wbegin); 
    238         //} 
     225        //! adds a sequence of data to the set, with default weight 
     226        public void addSequence(List<double> list) { 
     227            foreach (double v in list) 
     228                add(v, 1); 
     229        } 
     230        //! adds a sequence of data to the set, each with its weight 
     231        /*! \pre weights must be positive or null */ 
     232        public void addSequence(List<double> data, List<double> weight) { 
     233            for (int i = 0; i < data.Count; i++) 
     234                add(data[i], weight[i]); 
     235        } 
    239236    } 
    240237} 
  • trunk/QLNet/QLNet/Math/statistics/riskstatistics.cs

    r172 r173  
    3535 
    3636        #region wrap-up Stat 
    37         Stat impl_ = new Stat(); 
     37        protected Stat impl_ = new Stat(); 
    3838 
    3939        public int samples() { return impl_.samples(); } 
    4040        public double mean() { return impl_.mean(); } 
     41        public double min() { return impl_.min(); } 
     42        public double max() { return impl_.max(); } 
    4143        public double standardDeviation() { return impl_.standardDeviation(); } 
     44        public double variance() { return impl_.variance(); } 
     45        public double skewness() { return impl_.skewness(); } 
     46        public double kurtosis() { return impl_.kurtosis(); } 
    4247        public double percentile(double percent) { return impl_.percentile(percent); } 
     48        public double weightSum() { return impl_.weightSum(); } 
     49 
     50        public void reset() { impl_.reset(); } 
     51        public void addSequence(List<double> data, List<double> weight) { impl_.addSequence(data, weight); } 
    4352 
    4453        public KeyValuePair<double, int> expectationValue(Func<KeyValuePair<double, double>, double> f, 
     
    7988        public double regret(double target) { 
    8089            // average over the range below the target 
    81             KeyValuePair<double, int> result = expectationValue(z => Math.Pow(z.Key * z.Value - mean(), 2), 
    82                                                                 z => z.Value < target); 
     90            KeyValuePair<double, int> result = expectationValue(z => Math.Pow(z.Key - target, 2), 
     91                                                                z => z.Key < target); 
    8392            double x = result.Key; 
    8493            int N = result.Value; 
     
    153162        */ 
    154163        public double averageShortfall(double target) { 
    155             KeyValuePair<double, int> result = expectationValue(z => z.Key - target, z => z.Key < target); 
     164            KeyValuePair<double, int> result = expectationValue(z => target - z.Key, z => z.Key < target); 
    156165            double x = result.Key; 
    157166            int N = result.Value; 
     
    164173    /*! \test the correctness of the returned values is tested by checking them against numerical calculations. */ 
    165174    //typedef GenericRiskStatistics<GaussianStatistics> RiskStatistics; 
    166     public class RiskStatistics : GenericRiskStatistics<GaussianStatistics> { } 
     175    public class RiskStatistics : GenericRiskStatistics<GaussianStatistics> { 
     176        public double gaussianPercentile(double value) { 
     177            return ((GaussianStatistics)impl_).gaussianPercentile(value); 
     178        } 
     179        public double gaussianPotentialUpside(double value) { 
     180            return ((GaussianStatistics)impl_).gaussianPotentialUpside(value); 
     181        } 
     182        public double gaussianValueAtRisk(double value) { 
     183            return ((GaussianStatistics)impl_).gaussianValueAtRisk(value); 
     184        } 
     185        public double gaussianExpectedShortfall(double value) { 
     186            return ((GaussianStatistics)impl_).gaussianExpectedShortfall(value); 
     187        } 
     188        public double gaussianShortfall(double value) { 
     189            return ((GaussianStatistics)impl_).gaussianShortfall(value); 
     190        } 
     191        public double gaussianAverageShortfall(double value) { 
     192            return ((GaussianStatistics)impl_).gaussianAverageShortfall(value); 
     193        } 
     194        public double gaussianRegret(double value) { 
     195            return ((GaussianStatistics)impl_).gaussianRegret(value); 
     196        } 
     197        public double gaussianDownsideVariance() { 
     198            return ((GaussianStatistics)impl_).gaussianDownsideVariance(); 
     199        } 
     200    } 
    167201} 
  • trunk/QLNet/QLNet/Types.cs

    r172 r173  
    2828 
    2929        public const double M_LN2 = 0.693147180559945309417; 
     30        public const double M_PI = 3.141592653589793238462643383280; 
    3031    } 
    3132 
  • trunk/QLNet/Test2008/T_RiskStats.cs

    r172 r173  
    2525 
    2626namespace TestSuite { 
    27     class IncrementalGaussianStatistics : GenericGaussianStatistics<IncrementalStatistics> { } 
     27    class IncrementalGaussianStatistics : GenericGaussianStatistics<IncrementalStatistics> { 
     28        public double downsideVariance() { return ((IncrementalStatistics)impl_).downsideVariance(); } 
     29    } 
    2830 
    2931    [TestClass()] 
    30     class T_RiskStats { 
     32    public class T_RiskStats { 
    3133        [TestMethod()] 
    3234        public void RiskStatisticsTest() { 
     
    5052                    InverseCumulativeNormal inverseCum = new InverseCumulativeNormal(averages[i],sigmas[j]); 
    5153 
    52         //            SobolRsg rng(1); 
    53         //            dataMin = QL_MAX_REAL; 
    54         //            dataMax = QL_MIN_REAL; 
    55         //            for (k=0; k<N; k++) { 
    56         //                data[k] = inverseCum(rng.nextSequence().value[0]); 
    57         //                dataMin = std::min(dataMin, data[k]); 
    58         //                dataMax = std::max(dataMax, data[k]); 
    59         //                weights[k]=1.0; 
    60         //            } 
    61  
    62         //            igs.addSequence(data.begin(),data.end(),weights.begin()); 
    63         //            s.addSequence(data.begin(),data.end(),weights.begin()); 
    64  
    65         //            // checks 
    66         //            Real calculated, expected; 
    67         //            Real tolerance; 
    68  
    69         //            if (igs.samples() != N) 
    70         //                BOOST_FAIL("IncrementalGaussianStatistics: " 
    71         //                           << "wrong number of samples\n" 
    72         //                           << "    calculated: " << igs.samples() << "\n" 
    73         //                           << "    expected:   " << N); 
    74         //            if (s.samples() != N) 
    75         //                BOOST_FAIL("RiskStatistics: wrong number of samples\n" 
    76         //                           << "    calculated: " << s.samples() << "\n" 
    77         //                           << "    expected:   " << N); 
    78  
    79  
    80         //            // weightSum() 
    81         //            tolerance = 1e-10; 
    82         //            expected = std::accumulate(weights.begin(),weights.end(),0.0); 
    83         //            calculated = igs.weightSum(); 
    84         //            if (std::fabs(calculated-expected) > tolerance) 
    85         //                BOOST_FAIL("IncrementalGaussianStatistics: " 
    86         //                           << "wrong sum of weights\n" 
    87         //                           << std::setprecision(16) 
    88         //                           << "    calculated: " << calculated << "\n" 
    89         //                           << "    expected:   " << expected << "\n" 
    90         //                           << "    tolerance:  " << tolerance); 
    91         //            calculated = s.weightSum(); 
    92         //            if (std::fabs(calculated-expected) > tolerance) 
    93         //                BOOST_FAIL("RiskStatistics: wrong sum of weights\n" 
    94         //                           << std::setprecision(16) 
    95         //                           << "    calculated: " << calculated << "\n" 
    96         //                           << "    expected:   " << expected << "\n" 
    97         //                           << "    tolerance:  " << tolerance); 
    98  
    99  
    100         //            // min 
    101         //            tolerance = 1e-12; 
    102         //            expected = dataMin; 
    103         //            calculated = igs.min(); 
    104         //            if (std::fabs(calculated-expected)>tolerance) 
    105         //                BOOST_FAIL("IncrementalGaussianStatistics: " 
    106         //                           << "wrong minimum value\n" 
    107         //                           << std::setprecision(16) 
    108         //                           << "    calculated: " << calculated << "\n" 
    109         //                           << "    expected:   " << expected << "\n" 
    110         //                           << "    tolerance:  " << tolerance); 
    111         //            calculated = s.min(); 
    112         //            if (std::fabs(calculated-expected)>tolerance) 
    113         //                BOOST_FAIL("RiskStatistics: " 
    114         //                           << "wrong minimum value\n" 
    115         //                           << std::setprecision(16) 
    116         //                           << "    calculated: " << calculated << "\n" 
    117         //                           << "    expected:   " << expected << "\n" 
    118         //                           << "    tolerance:  " << tolerance); 
    119  
    120  
    121         //            // max 
    122         //            expected = dataMax; 
    123         //            calculated = igs.max(); 
    124         //            if (std::fabs(calculated-expected)>tolerance) 
    125         //                BOOST_FAIL("IncrementalGaussianStatistics: " 
    126         //                           << "wrong maximum value\n" 
    127         //                           << std::setprecision(16) 
    128         //                           << "    calculated: " << calculated << "\n" 
    129         //                           << "    expected:   " << expected << "\n" 
    130         //                           << "    tolerance:  " << tolerance); 
    131         //            calculated = s.max(); 
    132         //            if (std::fabs(calculated-expected)>tolerance) 
    133         //                BOOST_FAIL("RiskStatistics: " 
    134         //                           << "wrong maximum value\n" 
    135         //                           << std::setprecision(16) 
    136         //                           << "    calculated: " << calculated << "\n" 
    137         //                           << "    expected:   " << expected << "\n" 
    138         //                           << "    tolerance:  " << tolerance); 
    139  
    140  
    141         //            // mean 
    142         //            expected = averages[i]; 
    143         //            tolerance = (expected == 0.0 ? 1.0e-13 : 
    144         //                                           std::fabs(expected)*1.0e-13); 
    145         //            calculated = igs.mean(); 
    146         //            if (std::fabs(calculated-expected) > tolerance) 
    147         //                BOOST_FAIL("IncrementalGaussianStatistics: " 
    148         //                           << "wrong mean value" 
    149         //                           << " for N(" << averages[i] << ", " 
    150         //                           << sigmas[j] << ")\n" 
    151         //                           << std::setprecision(16) 
    152         //                           << "    calculated: " << calculated << "\n" 
    153         //                           << "    expected:   " << expected << "\n" 
    154         //                           << "    tolerance:  " << tolerance); 
    155         //            calculated = s.mean(); 
    156         //            if (std::fabs(calculated-expected) > tolerance) 
    157         //                BOOST_FAIL("RiskStatistics: wrong mean value" 
    158         //                           << " for N(" << averages[i] << ", " 
    159         //                           << sigmas[j] << ")\n" 
    160         //                           << std::setprecision(16) 
    161         //                           << "    calculated: " << calculated << "\n" 
    162         //                           << "    expected:   " << expected << "\n" 
    163         //                           << "    tolerance:  " << tolerance); 
    164  
    165  
    166         //            // variance 
    167         //            expected = sigmas[j]*sigmas[j]; 
    168         //            tolerance = expected*1.0e-1; 
    169         //            calculated = igs.variance(); 
    170         //            if (std::fabs(calculated-expected) > tolerance) 
    171         //                BOOST_FAIL("IncrementalGaussianStatistics: " 
    172         //                           << "wrong variance" 
    173         //                           << " for N(" << averages[i] << ", " 
    174         //                           << sigmas[j] << ")\n" 
    175         //                           << std::setprecision(16) 
    176         //                           << "    calculated: " << calculated << "\n" 
    177         //                           << "    expected:   " << expected << "\n" 
    178         //                           << "    tolerance:  " << tolerance); 
    179         //            calculated = s.variance(); 
    180         //            if (std::fabs(calculated-expected) > tolerance) 
    181         //                BOOST_FAIL("RiskStatistics: wrong variance" 
    182         //                           << " for N(" << averages[i] << ", " 
    183         //                           << sigmas[j] << ")\n" 
    184         //                           << std::setprecision(16) 
    185         //                           << "    calculated: " << calculated << "\n" 
    186         //                           << "    expected:   " << expected << "\n" 
    187         //                           << "    tolerance:  " << tolerance); 
    188  
    189  
    190         //            // standardDeviation 
    191         //            expected = sigmas[j]; 
    192         //            tolerance = expected*1.0e-1; 
    193         //            calculated = igs.standardDeviation(); 
    194         //            if (std::fabs(calculated-expected) > tolerance) 
    195         //                BOOST_FAIL("IncrementalGaussianStatistics: " 
    196         //                           << "wrong standard deviation" 
    197         //                           << " for N(" << averages[i] << ", " 
    198         //                           << sigmas[j] << ")\n" 
    199         //                           << std::setprecision(16) 
    200         //                           << "    calculated: " << calculated << "\n" 
    201         //                           << "    expected:   " << expected << "\n" 
    202         //                           << "    tolerance:  " << tolerance); 
    203         //            calculated = s.standardDeviation(); 
    204         //            if (std::fabs(calculated-expected) > tolerance) 
    205         //                BOOST_FAIL("RiskStatistics: wrong standard deviation" 
    206         //                           << " for N(" << averages[i] << ", " 
    207         //                           << sigmas[j] << ")\n" 
    208         //                           << std::setprecision(16) 
    209         //                           << "    calculated: " << calculated << "\n" 
    210         //                           << "    expected:   " << expected << "\n" 
    211         //                           << "    tolerance:  " << tolerance); 
    212  
    213  
    214         //            // missing errorEstimate() test 
    215  
    216         //            // skewness 
    217         //            expected = 0.0; 
    218         //            tolerance = 1.0e-4; 
    219         //            calculated = igs.skewness(); 
    220         //            if (std::fabs(calculated-expected) > tolerance) 
    221         //                BOOST_FAIL("IncrementalGaussianStatistics: " 
    222         //                           << "wrong skewness" 
    223         //                           << " for N(" << averages[i] << ", " 
    224         //                           << sigmas[j] << ")\n" 
    225         //                           << std::setprecision(16) 
    226         //                           << "    calculated: " << calculated << "\n" 
    227         //                           << "    expected:   " << expected << "\n" 
    228         //                           << "    tolerance:  " << tolerance); 
    229         //            calculated = s.skewness(); 
    230         //            if (std::fabs(calculated-expected) > tolerance) 
    231         //                BOOST_FAIL("RiskStatistics: wrong skewness" 
    232         //                           << " for N(" << averages[i] << ", " 
    233         //                           << sigmas[j] << ")\n" 
    234         //                           << std::setprecision(16) 
    235         //                           << "    calculated: " << calculated << "\n" 
    236         //                           << "    expected:   " << expected << "\n" 
    237         //                           << "    tolerance:  " << tolerance); 
    238  
    239  
    240         //            // kurtosis 
    241         //            expected = 0.0; 
    242         //            tolerance = 1.0e-1; 
    243         //            calculated = igs.kurtosis(); 
    244         //            if (std::fabs(calculated-expected) > tolerance) 
    245         //                BOOST_FAIL("IncrementalGaussianStatistics: " 
    246         //                           << "wrong kurtosis" 
    247         //                           << " for N(" << averages[i] << ", " 
    248         //                           << sigmas[j] << ")\n" 
    249         //                           << std::setprecision(16) 
    250         //                           << "    calculated: " << calculated << "\n" 
    251         //                           << "    expected:   " << expected << "\n" 
    252         //                           << "    tolerance:  " << tolerance); 
    253         //            calculated = s.kurtosis(); 
    254         //            if (std::fabs(calculated-expected) > tolerance) 
    255         //                BOOST_FAIL("RiskStatistics: wrong kurtosis" 
    256         //                           << " for N(" << averages[i] << ", " 
    257         //                           << sigmas[j] << ")\n" 
    258         //                           << std::setprecision(16) 
    259         //                           << "    calculated: " << calculated << "\n" 
    260         //                           << "    expected:   " << expected << "\n" 
    261         //                           << "    tolerance:  " << tolerance); 
    262  
    263  
    264         //            // percentile 
    265         //            expected = averages[i]; 
    266         //            tolerance = (expected == 0.0 ? 1.0e-3 : 
    267         //                                           std::fabs(expected*1.0e-3)); 
    268         //            calculated = igs.gaussianPercentile(0.5); 
    269         //            if (std::fabs(calculated-expected) > tolerance) 
    270         //                BOOST_FAIL("IncrementalGaussianStatistics: " 
    271         //                           << "wrong Gaussian percentile" 
    272         //                           << " for N(" << averages[i] << ", " 
    273         //                           << sigmas[j] << ")\n" 
    274         //                           << std::setprecision(16) 
    275         //                           << "    calculated: " << calculated << "\n" 
    276         //                           << "    expected:   " << expected << "\n" 
    277         //                           << "    tolerance:  " << tolerance); 
    278         //            calculated = s.gaussianPercentile(0.5); 
    279         //            if (std::fabs(calculated-expected) > tolerance) 
    280         //                BOOST_FAIL("RiskStatistics: wrong Gaussian percentile" 
    281         //                           << " for N(" << averages[i] << ", " 
    282         //                           << sigmas[j] << ")\n" 
    283         //                           << std::setprecision(16) 
    284         //                           << "    calculated: " << calculated << "\n" 
    285         //                           << "    expected:   " << expected << "\n" 
    286         //                           << "    tolerance:  " << tolerance); 
    287         //            calculated = s.percentile(0.5); 
    288         //            if (std::fabs(calculated-expected) > tolerance) 
    289         //                BOOST_FAIL("RiskStatistics: wrong percentile" 
    290         //                           << " for N(" << averages[i] << ", " 
    291         //                           << sigmas[j] << ")\n" 
    292         //                           << std::setprecision(16) 
    293         //                           << "    calculated: " << calculated << "\n" 
    294         //                           << "    expected:   " << expected << "\