Does any one used Statistics Value at risk function in QuantLib? I try to use the simple 250 PnL sample to test this function. The confidence level is 0.99, so the value should be between the second one and the third one. If the second one is -0.04 and the third one is -0.05, so the value is -(-0.04+(-0.05))/2=0.045. But, the quantlib value is always smaller than 0.045. Is that wrong with the calculation logic? I can not figure it out why the value is different. Any one could help me figure it out?
Best regards Alex ------------------------------------------------------------------------------ Infragistics Professional Build stunning WinForms apps today! Reboot your WinForms applications with our WinForms controls. Build a bridge from your legacy apps to the future. http://pubads.g.doubleclick.net/gampad/clk?id=153845071&iu=/4140/ostg.clktrk _______________________________________________ QuantLib-users mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/quantlib-users |
What are you getting instead? May you post some sample code that
reproduces the issue? Luigi On Wed, Aug 6, 2014 at 6:20 PM, Yuanhao Zhang <[hidden email]> wrote: > Does any one used Statistics Value at risk function in QuantLib? I try to > use the simple 250 PnL sample to test this function. The confidence level is > 0.99, so the value should be between the second one and the third one. If > the second one is -0.04 and the third one is -0.05, so the value is > -(-0.04+(-0.05))/2=0.045. But, the quantlib value is always smaller than > 0.045. Is that wrong with the calculation logic? I can not figure it out why > the value is different. Any one could help me figure it out? > > Best regards > > Alex > > ------------------------------------------------------------------------------ > Infragistics Professional > Build stunning WinForms apps today! > Reboot your WinForms applications with our WinForms controls. > Build a bridge from your legacy apps to the future. > http://pubads.g.doubleclick.net/gampad/clk?id=153845071&iu=/4140/ostg.clktrk > _______________________________________________ > QuantLib-users mailing list > [hidden email] > https://lists.sourceforge.net/lists/listinfo/quantlib-users > -- <https://implementingquantlib.blogspot.com> <https://twitter.com/lballabio> ------------------------------------------------------------------------------ Infragistics Professional Build stunning WinForms apps today! Reboot your WinForms applications with our WinForms controls. Build a bridge from your legacy apps to the future. http://pubads.g.doubleclick.net/gampad/clk?id=153845071&iu=/4140/ostg.clktrk _______________________________________________ QuantLib-users mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/quantlib-users |
Hi, I am totally confused by the quantlib percentile function, the code is listed below. I have attached an excel file, you can find the issue in sheet1. If I have 250 sample pnl, the 0.99 percentile is just between the second and third one, use the interpolated function the value I have highlighted there. But, the result of QL is smaller than the highlighted one. And I thought the result of Excel is right, I have read the code, but i can not understand it, could you explain it for me. thanks!
Real GeneralStatistics::percentile(Real percent) const { QL_REQUIRE(percent > 0.0 && percent <= 1.0,
"percentile (" << percent << ") must be in (0.0, 1.0]"); Real sampleWeight = weightSum();
QL_REQUIRE(sampleWeight>0.0, "empty sample set");
sort(); std::vector<std::pair<Real,Real> >::iterator k, l; k = samples_.begin();
l = samples_.end()-1; /* the sum of weight is non null, therefore there's at least one sample */
Real integral = k->second, target = percent*sampleWeight; while (integral < target && k != l) {
k++; integral += k->second; } return k->first;
} 2014-08-07 18:10 GMT+08:00 Luigi Ballabio <[hidden email]>: What are you getting instead? May you post some sample code that ------------------------------------------------------------------------------ Infragistics Professional Build stunning WinForms apps today! Reboot your WinForms applications with our WinForms controls. Build a bridge from your legacy apps to the future. http://pubads.g.doubleclick.net/gampad/clk?id=153845071&iu=/4140/ostg.clktrk _______________________________________________ QuantLib-users mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/quantlib-users |
In reply to this post by Luigi Ballabio
Forgot to attach the excel file there. Hi, I am totally confused by the quantlib percentile function, the code is listed below. I have attached an excel file, you can find the issue in sheet1. If I have 250 sample pnl, the 0.99 percentile is just between the second and third one, use the interpolated function the value I have highlighted there. But, the result of QL is smaller than the highlighted one. And I thought the result of Excel is right, I have read the code, but i can not understand it, could you explain it for me. thanks!
Real GeneralStatistics::percentile(Real percent) const { QL_REQUIRE(percent > 0.0 && percent <= 1.0,
"percentile (" << percent << ") must be in (0.0, 1.0]"); Real sampleWeight = weightSum();
QL_REQUIRE(sampleWeight>0.0, "empty sample set");
sort(); std::vector<std::pair<Real,Real> >::iterator k, l; k = samples_.begin();
l = samples_.end()-1; /* the sum of weight is non null, therefore there's at least one sample */
Real integral = k->second, target = percent*sampleWeight; while (integral < target && k != l) {
k++; integral += k->second; } return k->first;
} 2014-08-07 18:10 GMT+08:00 Luigi Ballabio <[hidden email]>: What are you getting instead? May you post some sample code that ------------------------------------------------------------------------------ _______________________________________________ QuantLib-users mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/quantlib-users FX20140730v2_USDCNY_VaR.rar (236K) Download Attachment |
The code below doesn't interpolate. In your case, if you ask for 0.99
VaR, the second loss is 0.05 and the third is 0.04, it just returns 0.04. I don't think I remember the reason for that, though. Luigi On Thu, Aug 7, 2014 at 1:19 PM, Yuanhao Zhang <[hidden email]> wrote: > Forgot to attach the excel file there. > Hi, I am totally confused by the quantlib percentile function, the code is > listed below. I have attached an excel file, you can find the issue in > sheet1. If I have 250 sample pnl, the 0.99 percentile is just between the > second and third one, use the interpolated function the value I have > highlighted there. But, the result of QL is smaller than the highlighted > one. And I thought the result of Excel is right, I have read the code, but i > can not understand it, could you explain it for me. thanks! > > Real GeneralStatistics::percentile(Real percent) const { > > QL_REQUIRE(percent > 0.0 && percent <= 1.0, > "percentile (" << percent << ") must be in (0.0, 1.0]"); > > Real sampleWeight = weightSum(); > QL_REQUIRE(sampleWeight>0.0, > "empty sample set"); > > sort(); > > std::vector<std::pair<Real,Real> >::iterator k, l; > k = samples_.begin(); > l = samples_.end()-1; > /* the sum of weight is non null, therefore there's > at least one sample */ > Real integral = k->second, target = percent*sampleWeight; > while (integral < target && k != l) { > k++; > integral += k->second; > } > return k->first; > } > > > > 2014-08-07 18:10 GMT+08:00 Luigi Ballabio <[hidden email]>: >> >> What are you getting instead? May you post some sample code that >> >> reproduces the issue? >> >> Luigi >> >> On Wed, Aug 6, 2014 at 6:20 PM, Yuanhao Zhang <[hidden email]> >> wrote: >> > Does any one used Statistics Value at risk function in QuantLib? I try >> > to >> > use the simple 250 PnL sample to test this function. The confidence >> > level is >> > 0.99, so the value should be between the second one and the third one. >> > If >> > the second one is -0.04 and the third one is -0.05, so the value is >> > -(-0.04+(-0.05))/2=0.045. But, the quantlib value is always smaller than >> > 0.045. Is that wrong with the calculation logic? I can not figure it out >> > why >> > the value is different. Any one could help me figure it out? >> > >> > Best regards >> > >> > Alex >> > >> > >> > ------------------------------------------------------------------------------ >> > Infragistics Professional >> > Build stunning WinForms apps today! >> > Reboot your WinForms applications with our WinForms controls. >> > Build a bridge from your legacy apps to the future. >> > >> > http://pubads.g.doubleclick.net/gampad/clk?id=153845071&iu=/4140/ostg.clktrk >> > _______________________________________________ >> > QuantLib-users mailing list >> > [hidden email] >> > https://lists.sourceforge.net/lists/listinfo/quantlib-users >> > >> >> >> >> -- >> <https://implementingquantlib.blogspot.com> >> <https://twitter.com/lballabio> > > -- <https://implementingquantlib.blogspot.com> <https://twitter.com/lballabio> ------------------------------------------------------------------------------ Infragistics Professional Build stunning WinForms apps today! Reboot your WinForms applications with our WinForms controls. Build a bridge from your legacy apps to the future. http://pubads.g.doubleclick.net/gampad/clk?id=153845071&iu=/4140/ostg.clktrk _______________________________________________ QuantLib-users mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/quantlib-users |
Thanks, Luigi. But, I have made the test, the result should be between 0.04, and 0.05, not exactly the 0.04. But, lower than 0.045. I donot know why. regards Alex
2014-08-07 22:51 GMT+08:00 Luigi Ballabio <[hidden email]>: The code below doesn't interpolate. In your case, if you ask for 0.99 ------------------------------------------------------------------------------ Want fast and easy access to all the code in your enterprise? Index and search up to 200,000 lines of code with a free copy of Black Duck Code Sight - the same software that powers the world's largest code search on Ohloh, the Black Duck Open Hub! Try it now. http://p.sf.net/sfu/bds _______________________________________________ QuantLib-users mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/quantlib-users |
the "correct" answer depends on how you estimate your distribution
from your sample data, doesn't it ? if you use a discrete distribution just assuming equal probability for each observation (like GeneralStatistics, if no weight is given), then the 1% quantile (which by definition = 99% VaR) q = inf ( x | F(x) >= 0.01 ) is equal to the third loss (-0.04), because (assuming 250 losses -0.06, -0.05, -0.04, ...) F(-0.06) = 1 / 250 < 0.01, F(-0.05) = 2 / 250 < 0.01, F(-0.04) = 3 / 250 >= 0.01, I think. So QuantLib seems to do a good job here ? Peter On 8 August 2014 02:34, Yuanhao Zhang <[hidden email]> wrote: > Thanks, Luigi. But, I have made the test, the result should be between 0.04, > and 0.05, not exactly the 0.04. But, lower than 0.045. I donot know why. > > regards > > Alex > > > 2014-08-07 22:51 GMT+08:00 Luigi Ballabio <[hidden email]>: > >> The code below doesn't interpolate. In your case, if you ask for 0.99 >> VaR, the second loss is 0.05 and the third is 0.04, it just returns >> 0.04. >> >> I don't think I remember the reason for that, though. >> >> Luigi >> >> >> On Thu, Aug 7, 2014 at 1:19 PM, Yuanhao Zhang <[hidden email]> >> wrote: >> > Forgot to attach the excel file there. >> > Hi, I am totally confused by the quantlib percentile function, the code >> > is >> > listed below. I have attached an excel file, you can find the issue in >> > sheet1. If I have 250 sample pnl, the 0.99 percentile is just between >> > the >> > second and third one, use the interpolated function the value I have >> > highlighted there. But, the result of QL is smaller than the highlighted >> > one. And I thought the result of Excel is right, I have read the code, >> > but i >> > can not understand it, could you explain it for me. thanks! >> > >> > Real GeneralStatistics::percentile(Real percent) const { >> > >> > QL_REQUIRE(percent > 0.0 && percent <= 1.0, >> > "percentile (" << percent << ") must be in (0.0, >> > 1.0]"); >> > >> > Real sampleWeight = weightSum(); >> > QL_REQUIRE(sampleWeight>0.0, >> > "empty sample set"); >> > >> > sort(); >> > >> > std::vector<std::pair<Real,Real> >::iterator k, l; >> > k = samples_.begin(); >> > l = samples_.end()-1; >> > /* the sum of weight is non null, therefore there's >> > at least one sample */ >> > Real integral = k->second, target = percent*sampleWeight; >> > while (integral < target && k != l) { >> > k++; >> > integral += k->second; >> > } >> > return k->first; >> > } >> > >> > >> > >> > 2014-08-07 18:10 GMT+08:00 Luigi Ballabio <[hidden email]>: >> >> >> >> What are you getting instead? May you post some sample code that >> >> >> >> reproduces the issue? >> >> >> >> Luigi >> >> >> >> On Wed, Aug 6, 2014 at 6:20 PM, Yuanhao Zhang <[hidden email]> >> >> wrote: >> >> > Does any one used Statistics Value at risk function in QuantLib? I >> >> > try >> >> > to >> >> > use the simple 250 PnL sample to test this function. The confidence >> >> > level is >> >> > 0.99, so the value should be between the second one and the third >> >> > one. >> >> > If >> >> > the second one is -0.04 and the third one is -0.05, so the value is >> >> > -(-0.04+(-0.05))/2=0.045. But, the quantlib value is always smaller >> >> > than >> >> > 0.045. Is that wrong with the calculation logic? I can not figure it >> >> > out >> >> > why >> >> > the value is different. Any one could help me figure it out? >> >> > >> >> > Best regards >> >> > >> >> > Alex >> >> > >> >> > >> >> > >> >> > ------------------------------------------------------------------------------ >> >> > Infragistics Professional >> >> > Build stunning WinForms apps today! >> >> > Reboot your WinForms applications with our WinForms controls. >> >> > Build a bridge from your legacy apps to the future. >> >> > >> >> > >> >> > http://pubads.g.doubleclick.net/gampad/clk?id=153845071&iu=/4140/ostg.clktrk >> >> > _______________________________________________ >> >> > QuantLib-users mailing list >> >> > [hidden email] >> >> > https://lists.sourceforge.net/lists/listinfo/quantlib-users >> >> > >> >> >> >> >> >> >> >> -- >> >> <https://implementingquantlib.blogspot.com> >> >> <https://twitter.com/lballabio> >> > >> > >> >> >> >> -- >> <https://implementingquantlib.blogspot.com> >> <https://twitter.com/lballabio> > > > > ------------------------------------------------------------------------------ > Want fast and easy access to all the code in your enterprise? Index and > search up to 200,000 lines of code with a free copy of Black Duck > Code Sight - the same software that powers the world's largest code > search on Ohloh, the Black Duck Open Hub! Try it now. > http://p.sf.net/sfu/bds > _______________________________________________ > QuantLib-users mailing list > [hidden email] > https://lists.sourceforge.net/lists/listinfo/quantlib-users > ------------------------------------------------------------------------------ Want fast and easy access to all the code in your enterprise? Index and search up to 200,000 lines of code with a free copy of Black Duck Code Sight - the same software that powers the world's largest code search on Ohloh, the Black Duck Open Hub! Try it now. http://p.sf.net/sfu/bds _______________________________________________ QuantLib-users mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/quantlib-users |
Yes, it rather depends on the distribution you assume. In R, there are
9 different quantile algorithms: <http://stat.ethz.ch/R-manual/R-patched/library/stats/html/quantile.html>. I think the one in QuantLib is one of the first three. Luigi On Fri, Aug 8, 2014 at 10:05 AM, Peter Caspers <[hidden email]> wrote: > the "correct" answer depends on how you estimate your distribution > from your sample data, doesn't it ? if you use a discrete distribution > just assuming equal probability for each observation (like > GeneralStatistics, if no weight is given), then the 1% quantile (which > by definition = 99% VaR) > > q = inf ( x | F(x) >= 0.01 ) > > is equal to the third loss (-0.04), because (assuming 250 losses > -0.06, -0.05, -0.04, ...) F(-0.06) = 1 / 250 < 0.01, F(-0.05) = 2 / > 250 < 0.01, F(-0.04) = 3 / 250 >= 0.01, I think. So QuantLib seems to > do a good job here ? > > Peter > > On 8 August 2014 02:34, Yuanhao Zhang <[hidden email]> wrote: >> Thanks, Luigi. But, I have made the test, the result should be between 0.04, >> and 0.05, not exactly the 0.04. But, lower than 0.045. I donot know why. >> >> regards >> >> Alex >> >> >> 2014-08-07 22:51 GMT+08:00 Luigi Ballabio <[hidden email]>: >> >>> The code below doesn't interpolate. In your case, if you ask for 0.99 >>> VaR, the second loss is 0.05 and the third is 0.04, it just returns >>> 0.04. >>> >>> I don't think I remember the reason for that, though. >>> >>> Luigi >>> >>> >>> On Thu, Aug 7, 2014 at 1:19 PM, Yuanhao Zhang <[hidden email]> >>> wrote: >>> > Forgot to attach the excel file there. >>> > Hi, I am totally confused by the quantlib percentile function, the code >>> > is >>> > listed below. I have attached an excel file, you can find the issue in >>> > sheet1. If I have 250 sample pnl, the 0.99 percentile is just between >>> > the >>> > second and third one, use the interpolated function the value I have >>> > highlighted there. But, the result of QL is smaller than the highlighted >>> > one. And I thought the result of Excel is right, I have read the code, >>> > but i >>> > can not understand it, could you explain it for me. thanks! >>> > >>> > Real GeneralStatistics::percentile(Real percent) const { >>> > >>> > QL_REQUIRE(percent > 0.0 && percent <= 1.0, >>> > "percentile (" << percent << ") must be in (0.0, >>> > 1.0]"); >>> > >>> > Real sampleWeight = weightSum(); >>> > QL_REQUIRE(sampleWeight>0.0, >>> > "empty sample set"); >>> > >>> > sort(); >>> > >>> > std::vector<std::pair<Real,Real> >::iterator k, l; >>> > k = samples_.begin(); >>> > l = samples_.end()-1; >>> > /* the sum of weight is non null, therefore there's >>> > at least one sample */ >>> > Real integral = k->second, target = percent*sampleWeight; >>> > while (integral < target && k != l) { >>> > k++; >>> > integral += k->second; >>> > } >>> > return k->first; >>> > } >>> > >>> > >>> > >>> > 2014-08-07 18:10 GMT+08:00 Luigi Ballabio <[hidden email]>: >>> >> >>> >> What are you getting instead? May you post some sample code that >>> >> >>> >> reproduces the issue? >>> >> >>> >> Luigi >>> >> >>> >> On Wed, Aug 6, 2014 at 6:20 PM, Yuanhao Zhang <[hidden email]> >>> >> wrote: >>> >> > Does any one used Statistics Value at risk function in QuantLib? I >>> >> > try >>> >> > to >>> >> > use the simple 250 PnL sample to test this function. The confidence >>> >> > level is >>> >> > 0.99, so the value should be between the second one and the third >>> >> > one. >>> >> > If >>> >> > the second one is -0.04 and the third one is -0.05, so the value is >>> >> > -(-0.04+(-0.05))/2=0.045. But, the quantlib value is always smaller >>> >> > than >>> >> > 0.045. Is that wrong with the calculation logic? I can not figure it >>> >> > out >>> >> > why >>> >> > the value is different. Any one could help me figure it out? >>> >> > >>> >> > Best regards >>> >> > >>> >> > Alex >>> >> > >>> >> > >>> >> > >>> >> > ------------------------------------------------------------------------------ >>> >> > Infragistics Professional >>> >> > Build stunning WinForms apps today! >>> >> > Reboot your WinForms applications with our WinForms controls. >>> >> > Build a bridge from your legacy apps to the future. >>> >> > >>> >> > >>> >> > http://pubads.g.doubleclick.net/gampad/clk?id=153845071&iu=/4140/ostg.clktrk >>> >> > _______________________________________________ >>> >> > QuantLib-users mailing list >>> >> > [hidden email] >>> >> > https://lists.sourceforge.net/lists/listinfo/quantlib-users >>> >> > >>> >> >>> >> >>> >> >>> >> -- >>> >> <https://implementingquantlib.blogspot.com> >>> >> <https://twitter.com/lballabio> >>> > >>> > >>> >>> >>> >>> -- >>> <https://implementingquantlib.blogspot.com> >>> <https://twitter.com/lballabio> >> >> >> >> ------------------------------------------------------------------------------ >> Want fast and easy access to all the code in your enterprise? Index and >> search up to 200,000 lines of code with a free copy of Black Duck >> Code Sight - the same software that powers the world's largest code >> search on Ohloh, the Black Duck Open Hub! Try it now. >> http://p.sf.net/sfu/bds >> _______________________________________________ >> QuantLib-users mailing list >> [hidden email] >> https://lists.sourceforge.net/lists/listinfo/quantlib-users >> -- <https://implementingquantlib.blogspot.com> <https://twitter.com/lballabio> ------------------------------------------------------------------------------ Want fast and easy access to all the code in your enterprise? Index and search up to 200,000 lines of code with a free copy of Black Duck Code Sight - the same software that powers the world's largest code search on Ohloh, the Black Duck Open Hub! Try it now. http://p.sf.net/sfu/bds _______________________________________________ QuantLib-users mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/quantlib-users |
interesting. yes, at first sight the one in ql seems to be of type 1.
maybe we should think about adding type 5 to attract some hydrologists ? Peter On 8 August 2014 11:53, Luigi Ballabio <[hidden email]> wrote: > Yes, it rather depends on the distribution you assume. In R, there are > 9 different quantile algorithms: > <http://stat.ethz.ch/R-manual/R-patched/library/stats/html/quantile.html>. > I think the one in QuantLib is one of the first three. > > Luigi > > > On Fri, Aug 8, 2014 at 10:05 AM, Peter Caspers <[hidden email]> wrote: >> the "correct" answer depends on how you estimate your distribution >> from your sample data, doesn't it ? if you use a discrete distribution >> just assuming equal probability for each observation (like >> GeneralStatistics, if no weight is given), then the 1% quantile (which >> by definition = 99% VaR) >> >> q = inf ( x | F(x) >= 0.01 ) >> >> is equal to the third loss (-0.04), because (assuming 250 losses >> -0.06, -0.05, -0.04, ...) F(-0.06) = 1 / 250 < 0.01, F(-0.05) = 2 / >> 250 < 0.01, F(-0.04) = 3 / 250 >= 0.01, I think. So QuantLib seems to >> do a good job here ? >> >> Peter >> >> On 8 August 2014 02:34, Yuanhao Zhang <[hidden email]> wrote: >>> Thanks, Luigi. But, I have made the test, the result should be between 0.04, >>> and 0.05, not exactly the 0.04. But, lower than 0.045. I donot know why. >>> >>> regards >>> >>> Alex >>> >>> >>> 2014-08-07 22:51 GMT+08:00 Luigi Ballabio <[hidden email]>: >>> >>>> The code below doesn't interpolate. In your case, if you ask for 0.99 >>>> VaR, the second loss is 0.05 and the third is 0.04, it just returns >>>> 0.04. >>>> >>>> I don't think I remember the reason for that, though. >>>> >>>> Luigi >>>> >>>> >>>> On Thu, Aug 7, 2014 at 1:19 PM, Yuanhao Zhang <[hidden email]> >>>> wrote: >>>> > Forgot to attach the excel file there. >>>> > Hi, I am totally confused by the quantlib percentile function, the code >>>> > is >>>> > listed below. I have attached an excel file, you can find the issue in >>>> > sheet1. If I have 250 sample pnl, the 0.99 percentile is just between >>>> > the >>>> > second and third one, use the interpolated function the value I have >>>> > highlighted there. But, the result of QL is smaller than the highlighted >>>> > one. And I thought the result of Excel is right, I have read the code, >>>> > but i >>>> > can not understand it, could you explain it for me. thanks! >>>> > >>>> > Real GeneralStatistics::percentile(Real percent) const { >>>> > >>>> > QL_REQUIRE(percent > 0.0 && percent <= 1.0, >>>> > "percentile (" << percent << ") must be in (0.0, >>>> > 1.0]"); >>>> > >>>> > Real sampleWeight = weightSum(); >>>> > QL_REQUIRE(sampleWeight>0.0, >>>> > "empty sample set"); >>>> > >>>> > sort(); >>>> > >>>> > std::vector<std::pair<Real,Real> >::iterator k, l; >>>> > k = samples_.begin(); >>>> > l = samples_.end()-1; >>>> > /* the sum of weight is non null, therefore there's >>>> > at least one sample */ >>>> > Real integral = k->second, target = percent*sampleWeight; >>>> > while (integral < target && k != l) { >>>> > k++; >>>> > integral += k->second; >>>> > } >>>> > return k->first; >>>> > } >>>> > >>>> > >>>> > >>>> > 2014-08-07 18:10 GMT+08:00 Luigi Ballabio <[hidden email]>: >>>> >> >>>> >> What are you getting instead? May you post some sample code that >>>> >> >>>> >> reproduces the issue? >>>> >> >>>> >> Luigi >>>> >> >>>> >> On Wed, Aug 6, 2014 at 6:20 PM, Yuanhao Zhang <[hidden email]> >>>> >> wrote: >>>> >> > Does any one used Statistics Value at risk function in QuantLib? I >>>> >> > try >>>> >> > to >>>> >> > use the simple 250 PnL sample to test this function. The confidence >>>> >> > level is >>>> >> > 0.99, so the value should be between the second one and the third >>>> >> > one. >>>> >> > If >>>> >> > the second one is -0.04 and the third one is -0.05, so the value is >>>> >> > -(-0.04+(-0.05))/2=0.045. But, the quantlib value is always smaller >>>> >> > than >>>> >> > 0.045. Is that wrong with the calculation logic? I can not figure it >>>> >> > out >>>> >> > why >>>> >> > the value is different. Any one could help me figure it out? >>>> >> > >>>> >> > Best regards >>>> >> > >>>> >> > Alex >>>> >> > >>>> >> > >>>> >> > >>>> >> > ------------------------------------------------------------------------------ >>>> >> > Infragistics Professional >>>> >> > Build stunning WinForms apps today! >>>> >> > Reboot your WinForms applications with our WinForms controls. >>>> >> > Build a bridge from your legacy apps to the future. >>>> >> > >>>> >> > >>>> >> > http://pubads.g.doubleclick.net/gampad/clk?id=153845071&iu=/4140/ostg.clktrk >>>> >> > _______________________________________________ >>>> >> > QuantLib-users mailing list >>>> >> > [hidden email] >>>> >> > https://lists.sourceforge.net/lists/listinfo/quantlib-users >>>> >> > >>>> >> >>>> >> >>>> >> >>>> >> -- >>>> >> <https://implementingquantlib.blogspot.com> >>>> >> <https://twitter.com/lballabio> >>>> > >>>> > >>>> >>>> >>>> >>>> -- >>>> <https://implementingquantlib.blogspot.com> >>>> <https://twitter.com/lballabio> >>> >>> >>> >>> ------------------------------------------------------------------------------ >>> Want fast and easy access to all the code in your enterprise? Index and >>> search up to 200,000 lines of code with a free copy of Black Duck >>> Code Sight - the same software that powers the world's largest code >>> search on Ohloh, the Black Duck Open Hub! Try it now. >>> http://p.sf.net/sfu/bds >>> _______________________________________________ >>> QuantLib-users mailing list >>> [hidden email] >>> https://lists.sourceforge.net/lists/listinfo/quantlib-users >>> > > > > -- > <https://implementingquantlib.blogspot.com> > <https://twitter.com/lballabio> ------------------------------------------------------------------------------ _______________________________________________ QuantLib-users mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/quantlib-users |
Many thanks, I thought QuantLib method is more close to the function of excel 2010 percentile.inc, right? Alex 2014-08-09 18:02 GMT+08:00 Peter Caspers <[hidden email]>: interesting. yes, at first sight the one in ql seems to be of type 1. ------------------------------------------------------------------------------ _______________________________________________ QuantLib-users mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/quantlib-users |
there doesn't seem to be any detailled documentation in excel's online
help on that function, but in wikipedia the method is explained (I didn't verify) under "Microsoft Excel's Algorithm" http://en.wikipedia.org/wiki/Percentile which is clearly different from what is done in ql. Peter On 12 August 2014 14:17, Yuanhao Zhang <[hidden email]> wrote: > Many thanks, I thought QuantLib method is more close to the function of > excel 2010 percentile.inc, right? > > Alex > > > 2014-08-09 18:02 GMT+08:00 Peter Caspers <[hidden email]>: > >> interesting. yes, at first sight the one in ql seems to be of type 1. >> maybe we should think about adding type 5 to attract some hydrologists >> ? >> Peter >> >> On 8 August 2014 11:53, Luigi Ballabio <[hidden email]> wrote: >> > Yes, it rather depends on the distribution you assume. In R, there are >> > 9 different quantile algorithms: >> > >> > <http://stat.ethz.ch/R-manual/R-patched/library/stats/html/quantile.html>. >> > I think the one in QuantLib is one of the first three. >> > >> > Luigi >> > >> > >> > On Fri, Aug 8, 2014 at 10:05 AM, Peter Caspers <[hidden email]> >> > wrote: >> >> the "correct" answer depends on how you estimate your distribution >> >> from your sample data, doesn't it ? if you use a discrete distribution >> >> just assuming equal probability for each observation (like >> >> GeneralStatistics, if no weight is given), then the 1% quantile (which >> >> by definition = 99% VaR) >> >> >> >> q = inf ( x | F(x) >= 0.01 ) >> >> >> >> is equal to the third loss (-0.04), because (assuming 250 losses >> >> -0.06, -0.05, -0.04, ...) F(-0.06) = 1 / 250 < 0.01, F(-0.05) = 2 / >> >> 250 < 0.01, F(-0.04) = 3 / 250 >= 0.01, I think. So QuantLib seems to >> >> do a good job here ? >> >> >> >> Peter >> >> >> >> On 8 August 2014 02:34, Yuanhao Zhang <[hidden email]> wrote: >> >>> Thanks, Luigi. But, I have made the test, the result should be between >> >>> 0.04, >> >>> and 0.05, not exactly the 0.04. But, lower than 0.045. I donot know >> >>> why. >> >>> >> >>> regards >> >>> >> >>> Alex >> >>> >> >>> >> >>> 2014-08-07 22:51 GMT+08:00 Luigi Ballabio <[hidden email]>: >> >>> >> >>>> The code below doesn't interpolate. In your case, if you ask for 0.99 >> >>>> VaR, the second loss is 0.05 and the third is 0.04, it just returns >> >>>> 0.04. >> >>>> >> >>>> I don't think I remember the reason for that, though. >> >>>> >> >>>> Luigi >> >>>> >> >>>> >> >>>> On Thu, Aug 7, 2014 at 1:19 PM, Yuanhao Zhang >> >>>> <[hidden email]> >> >>>> wrote: >> >>>> > Forgot to attach the excel file there. >> >>>> > Hi, I am totally confused by the quantlib percentile function, the >> >>>> > code >> >>>> > is >> >>>> > listed below. I have attached an excel file, you can find the issue >> >>>> > in >> >>>> > sheet1. If I have 250 sample pnl, the 0.99 percentile is just >> >>>> > between >> >>>> > the >> >>>> > second and third one, use the interpolated function the value I >> >>>> > have >> >>>> > highlighted there. But, the result of QL is smaller than the >> >>>> > highlighted >> >>>> > one. And I thought the result of Excel is right, I have read the >> >>>> > code, >> >>>> > but i >> >>>> > can not understand it, could you explain it for me. thanks! >> >>>> > >> >>>> > Real GeneralStatistics::percentile(Real percent) const { >> >>>> > >> >>>> > QL_REQUIRE(percent > 0.0 && percent <= 1.0, >> >>>> > "percentile (" << percent << ") must be in (0.0, >> >>>> > 1.0]"); >> >>>> > >> >>>> > Real sampleWeight = weightSum(); >> >>>> > QL_REQUIRE(sampleWeight>0.0, >> >>>> > "empty sample set"); >> >>>> > >> >>>> > sort(); >> >>>> > >> >>>> > std::vector<std::pair<Real,Real> >::iterator k, l; >> >>>> > k = samples_.begin(); >> >>>> > l = samples_.end()-1; >> >>>> > /* the sum of weight is non null, therefore there's >> >>>> > at least one sample */ >> >>>> > Real integral = k->second, target = percent*sampleWeight; >> >>>> > while (integral < target && k != l) { >> >>>> > k++; >> >>>> > integral += k->second; >> >>>> > } >> >>>> > return k->first; >> >>>> > } >> >>>> > >> >>>> > >> >>>> > >> >>>> > 2014-08-07 18:10 GMT+08:00 Luigi Ballabio >> >>>> > <[hidden email]>: >> >>>> >> >> >>>> >> What are you getting instead? May you post some sample code that >> >>>> >> >> >>>> >> reproduces the issue? >> >>>> >> >> >>>> >> Luigi >> >>>> >> >> >>>> >> On Wed, Aug 6, 2014 at 6:20 PM, Yuanhao Zhang >> >>>> >> <[hidden email]> >> >>>> >> wrote: >> >>>> >> > Does any one used Statistics Value at risk function in QuantLib? >> >>>> >> > I >> >>>> >> > try >> >>>> >> > to >> >>>> >> > use the simple 250 PnL sample to test this function. The >> >>>> >> > confidence >> >>>> >> > level is >> >>>> >> > 0.99, so the value should be between the second one and the >> >>>> >> > third >> >>>> >> > one. >> >>>> >> > If >> >>>> >> > the second one is -0.04 and the third one is -0.05, so the value >> >>>> >> > is >> >>>> >> > -(-0.04+(-0.05))/2=0.045. But, the quantlib value is always >> >>>> >> > smaller >> >>>> >> > than >> >>>> >> > 0.045. Is that wrong with the calculation logic? I can not >> >>>> >> > figure it >> >>>> >> > out >> >>>> >> > why >> >>>> >> > the value is different. Any one could help me figure it out? >> >>>> >> > >> >>>> >> > Best regards >> >>>> >> > >> >>>> >> > Alex >> >>>> >> > >> >>>> >> > >> >>>> >> > >> >>>> >> > >> >>>> >> > ------------------------------------------------------------------------------ >> >>>> >> > Infragistics Professional >> >>>> >> > Build stunning WinForms apps today! >> >>>> >> > Reboot your WinForms applications with our WinForms controls. >> >>>> >> > Build a bridge from your legacy apps to the future. >> >>>> >> > >> >>>> >> > >> >>>> >> > >> >>>> >> > http://pubads.g.doubleclick.net/gampad/clk?id=153845071&iu=/4140/ostg.clktrk >> >>>> >> > _______________________________________________ >> >>>> >> > QuantLib-users mailing list >> >>>> >> > [hidden email] >> >>>> >> > https://lists.sourceforge.net/lists/listinfo/quantlib-users >> >>>> >> > >> >>>> >> >> >>>> >> >> >>>> >> >> >>>> >> -- >> >>>> >> <https://implementingquantlib.blogspot.com> >> >>>> >> <https://twitter.com/lballabio> >> >>>> > >> >>>> > >> >>>> >> >>>> >> >>>> >> >>>> -- >> >>>> <https://implementingquantlib.blogspot.com> >> >>>> <https://twitter.com/lballabio> >> >>> >> >>> >> >>> >> >>> >> >>> ------------------------------------------------------------------------------ >> >>> Want fast and easy access to all the code in your enterprise? Index >> >>> and >> >>> search up to 200,000 lines of code with a free copy of Black Duck >> >>> Code Sight - the same software that powers the world's largest code >> >>> search on Ohloh, the Black Duck Open Hub! Try it now. >> >>> http://p.sf.net/sfu/bds >> >>> _______________________________________________ >> >>> QuantLib-users mailing list >> >>> [hidden email] >> >>> https://lists.sourceforge.net/lists/listinfo/quantlib-users >> >>> >> > >> > >> > >> > -- >> > <https://implementingquantlib.blogspot.com> >> > <https://twitter.com/lballabio> > > ------------------------------------------------------------------------------ _______________________________________________ QuantLib-users mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/quantlib-users |
I have one made by a chinese people, it is very clear to show the calculate steps of percentile.inc, and percentile.exc. If you do not mind the Chinese characters, I will share it with you.
regards Alex 2014-08-12 21:22 GMT+08:00 Peter Caspers <[hidden email]>: there doesn't seem to be any detailled documentation in excel's online ------------------------------------------------------------------------------ _______________________________________________ QuantLib-users mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/quantlib-users |
yes sure, that would be interesting.
thanks a lot Peter On 12 August 2014 15:38, Yuanhao Zhang <[hidden email]> wrote: > I have one made by a chinese people, it is very clear to show the calculate > steps of percentile.inc, and percentile.exc. If you do not mind the Chinese > characters, I will share it with you. > > regards > > Alex > > > 2014-08-12 21:22 GMT+08:00 Peter Caspers <[hidden email]>: > >> there doesn't seem to be any detailled documentation in excel's online >> help on that function, but >> in wikipedia the method is explained (I didn't verify) under >> "Microsoft Excel's Algorithm" >> >> http://en.wikipedia.org/wiki/Percentile >> >> which is clearly different from what is done in ql. >> >> Peter >> >> >> On 12 August 2014 14:17, Yuanhao Zhang <[hidden email]> wrote: >> > Many thanks, I thought QuantLib method is more close to the function of >> > excel 2010 percentile.inc, right? >> > >> > Alex >> > >> > >> > 2014-08-09 18:02 GMT+08:00 Peter Caspers <[hidden email]>: >> > >> >> interesting. yes, at first sight the one in ql seems to be of type 1. >> >> maybe we should think about adding type 5 to attract some hydrologists >> >> ? >> >> Peter >> >> >> >> On 8 August 2014 11:53, Luigi Ballabio <[hidden email]> >> >> wrote: >> >> > Yes, it rather depends on the distribution you assume. In R, there >> >> > are >> >> > 9 different quantile algorithms: >> >> > >> >> > >> >> > <http://stat.ethz.ch/R-manual/R-patched/library/stats/html/quantile.html>. >> >> > I think the one in QuantLib is one of the first three. >> >> > >> >> > Luigi >> >> > >> >> > >> >> > On Fri, Aug 8, 2014 at 10:05 AM, Peter Caspers >> >> > <[hidden email]> >> >> > wrote: >> >> >> the "correct" answer depends on how you estimate your distribution >> >> >> from your sample data, doesn't it ? if you use a discrete >> >> >> distribution >> >> >> just assuming equal probability for each observation (like >> >> >> GeneralStatistics, if no weight is given), then the 1% quantile >> >> >> (which >> >> >> by definition = 99% VaR) >> >> >> >> >> >> q = inf ( x | F(x) >= 0.01 ) >> >> >> >> >> >> is equal to the third loss (-0.04), because (assuming 250 losses >> >> >> -0.06, -0.05, -0.04, ...) F(-0.06) = 1 / 250 < 0.01, F(-0.05) = 2 / >> >> >> 250 < 0.01, F(-0.04) = 3 / 250 >= 0.01, I think. So QuantLib seems >> >> >> to >> >> >> do a good job here ? >> >> >> >> >> >> Peter >> >> >> >> >> >> On 8 August 2014 02:34, Yuanhao Zhang <[hidden email]> >> >> >> wrote: >> >> >>> Thanks, Luigi. But, I have made the test, the result should be >> >> >>> between >> >> >>> 0.04, >> >> >>> and 0.05, not exactly the 0.04. But, lower than 0.045. I donot know >> >> >>> why. >> >> >>> >> >> >>> regards >> >> >>> >> >> >>> Alex >> >> >>> >> >> >>> >> >> >>> 2014-08-07 22:51 GMT+08:00 Luigi Ballabio >> >> >>> <[hidden email]>: >> >> >>> >> >> >>>> The code below doesn't interpolate. In your case, if you ask for >> >> >>>> 0.99 >> >> >>>> VaR, the second loss is 0.05 and the third is 0.04, it just >> >> >>>> returns >> >> >>>> 0.04. >> >> >>>> >> >> >>>> I don't think I remember the reason for that, though. >> >> >>>> >> >> >>>> Luigi >> >> >>>> >> >> >>>> >> >> >>>> On Thu, Aug 7, 2014 at 1:19 PM, Yuanhao Zhang >> >> >>>> <[hidden email]> >> >> >>>> wrote: >> >> >>>> > Forgot to attach the excel file there. >> >> >>>> > Hi, I am totally confused by the quantlib percentile function, >> >> >>>> > the >> >> >>>> > code >> >> >>>> > is >> >> >>>> > listed below. I have attached an excel file, you can find the >> >> >>>> > issue >> >> >>>> > in >> >> >>>> > sheet1. If I have 250 sample pnl, the 0.99 percentile is just >> >> >>>> > between >> >> >>>> > the >> >> >>>> > second and third one, use the interpolated function the value I >> >> >>>> > have >> >> >>>> > highlighted there. But, the result of QL is smaller than the >> >> >>>> > highlighted >> >> >>>> > one. And I thought the result of Excel is right, I have read the >> >> >>>> > code, >> >> >>>> > but i >> >> >>>> > can not understand it, could you explain it for me. thanks! >> >> >>>> > >> >> >>>> > Real GeneralStatistics::percentile(Real percent) const { >> >> >>>> > >> >> >>>> > QL_REQUIRE(percent > 0.0 && percent <= 1.0, >> >> >>>> > "percentile (" << percent << ") must be in >> >> >>>> > (0.0, >> >> >>>> > 1.0]"); >> >> >>>> > >> >> >>>> > Real sampleWeight = weightSum(); >> >> >>>> > QL_REQUIRE(sampleWeight>0.0, >> >> >>>> > "empty sample set"); >> >> >>>> > >> >> >>>> > sort(); >> >> >>>> > >> >> >>>> > std::vector<std::pair<Real,Real> >::iterator k, l; >> >> >>>> > k = samples_.begin(); >> >> >>>> > l = samples_.end()-1; >> >> >>>> > /* the sum of weight is non null, therefore there's >> >> >>>> > at least one sample */ >> >> >>>> > Real integral = k->second, target = >> >> >>>> > percent*sampleWeight; >> >> >>>> > while (integral < target && k != l) { >> >> >>>> > k++; >> >> >>>> > integral += k->second; >> >> >>>> > } >> >> >>>> > return k->first; >> >> >>>> > } >> >> >>>> > >> >> >>>> > >> >> >>>> > >> >> >>>> > 2014-08-07 18:10 GMT+08:00 Luigi Ballabio >> >> >>>> > <[hidden email]>: >> >> >>>> >> >> >> >>>> >> What are you getting instead? May you post some sample code >> >> >>>> >> that >> >> >>>> >> >> >> >>>> >> reproduces the issue? >> >> >>>> >> >> >> >>>> >> Luigi >> >> >>>> >> >> >> >>>> >> On Wed, Aug 6, 2014 at 6:20 PM, Yuanhao Zhang >> >> >>>> >> <[hidden email]> >> >> >>>> >> wrote: >> >> >>>> >> > Does any one used Statistics Value at risk function in >> >> >>>> >> > QuantLib? >> >> >>>> >> > I >> >> >>>> >> > try >> >> >>>> >> > to >> >> >>>> >> > use the simple 250 PnL sample to test this function. The >> >> >>>> >> > confidence >> >> >>>> >> > level is >> >> >>>> >> > 0.99, so the value should be between the second one and the >> >> >>>> >> > third >> >> >>>> >> > one. >> >> >>>> >> > If >> >> >>>> >> > the second one is -0.04 and the third one is -0.05, so the >> >> >>>> >> > value >> >> >>>> >> > is >> >> >>>> >> > -(-0.04+(-0.05))/2=0.045. But, the quantlib value is always >> >> >>>> >> > smaller >> >> >>>> >> > than >> >> >>>> >> > 0.045. Is that wrong with the calculation logic? I can not >> >> >>>> >> > figure it >> >> >>>> >> > out >> >> >>>> >> > why >> >> >>>> >> > the value is different. Any one could help me figure it out? >> >> >>>> >> > >> >> >>>> >> > Best regards >> >> >>>> >> > >> >> >>>> >> > Alex >> >> >>>> >> > >> >> >>>> >> > >> >> >>>> >> > >> >> >>>> >> > >> >> >>>> >> > >> >> >>>> >> > ------------------------------------------------------------------------------ >> >> >>>> >> > Infragistics Professional >> >> >>>> >> > Build stunning WinForms apps today! >> >> >>>> >> > Reboot your WinForms applications with our WinForms controls. >> >> >>>> >> > Build a bridge from your legacy apps to the future. >> >> >>>> >> > >> >> >>>> >> > >> >> >>>> >> > >> >> >>>> >> > >> >> >>>> >> > http://pubads.g.doubleclick.net/gampad/clk?id=153845071&iu=/4140/ostg.clktrk >> >> >>>> >> > _______________________________________________ >> >> >>>> >> > QuantLib-users mailing list >> >> >>>> >> > [hidden email] >> >> >>>> >> > https://lists.sourceforge.net/lists/listinfo/quantlib-users >> >> >>>> >> > >> >> >>>> >> >> >> >>>> >> >> >> >>>> >> >> >> >>>> >> -- >> >> >>>> >> <https://implementingquantlib.blogspot.com> >> >> >>>> >> <https://twitter.com/lballabio> >> >> >>>> > >> >> >>>> > >> >> >>>> >> >> >>>> >> >> >>>> >> >> >>>> -- >> >> >>>> <https://implementingquantlib.blogspot.com> >> >> >>>> <https://twitter.com/lballabio> >> >> >>> >> >> >>> >> >> >>> >> >> >>> >> >> >>> >> >> >>> ------------------------------------------------------------------------------ >> >> >>> Want fast and easy access to all the code in your enterprise? Index >> >> >>> and >> >> >>> search up to 200,000 lines of code with a free copy of Black Duck >> >> >>> Code Sight - the same software that powers the world's largest code >> >> >>> search on Ohloh, the Black Duck Open Hub! Try it now. >> >> >>> http://p.sf.net/sfu/bds >> >> >>> _______________________________________________ >> >> >>> QuantLib-users mailing list >> >> >>> [hidden email] >> >> >>> https://lists.sourceforge.net/lists/listinfo/quantlib-users >> >> >>> >> >> > >> >> > >> >> > >> >> > -- >> >> > <https://implementingquantlib.blogspot.com> >> >> > <https://twitter.com/lballabio> >> > >> > > > ------------------------------------------------------------------------------ _______________________________________________ QuantLib-users mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/quantlib-users |
In reply to this post by jojogh
I am pretty confident than when I coded those lines more than ten years ago, I used Excel implementation as reference. I was much younger then... :-) even if I must confess 10 different definitions of percentile kinda scare me... even as an older, but probably not wiser, man.
On Tue, Aug 12, 2014 at 2:17 PM, Yuanhao Zhang <[hidden email]> wrote:
------------------------------------------------------------------------------ _______________________________________________ QuantLib-users mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/quantlib-users |
Yes, you had Excel as reference, which is why you were all "what, aren't we interpolating?" when we went for a different algorithm in the end... Luigi On Aug 12, 2014 5:05 PM, "Ferdinando M. Ametrano" <[hidden email]> wrote:
------------------------------------------------------------------------------ _______________________________________________ QuantLib-users mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/quantlib-users |
Now I start to recollect... On Aug 12, 2014 5:40 PM, "Luigi Ballabio" <[hidden email]> wrote:
------------------------------------------------------------------------------ _______________________________________________ QuantLib-users mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/quantlib-users |
In reply to this post by Peter Caspers-4
Hi Peter, Attached are the explanation of Percentile.inc and Percentile.exc in Excel 2010. I thought it is easy to understand the algorithm, but not the Chinese characters. regards
Alex 2014-08-12 22:16 GMT+08:00 Peter Caspers <[hidden email]>: yes sure, that would be interesting. ------------------------------------------------------------------------------ _______________________________________________ QuantLib-users mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/quantlib-users =?UTF-8?B?44CQRXhjZWwyMDEw44CRUEVSQ0VOVElMRS5JTkPlkoxQRVJDRU5USUxFLkVYQ+WOn+eQhi54bHN4?= (67K) Download Attachment |
Free forum by Nabble | Edit this page |