# Wilks quantile in OpenTURNS

**URL:** <https://openturns.discourse.group/t/wilks-quantile-in-openturns/52>\
**Category:** Python usage\
**Created:** [November 4, 2020, 4:49pm UTC](https://openturns.discourse.group/t/wilks-quantile-in-openturns/52 "2020-11-04T16:49:19Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![sanaaZ](https://yyz2.discourse-cdn.com/free1/user_avatar/openturns.discourse.group/sanaaz/32/241_2.png) [@sanaaZ](https://openturns.discourse.group/u/sanaaZ)\
**Post date:** [November 4, 2020, 4:49pm UTC](https://openturns.discourse.group/t/wilks-quantile-in-openturns/52/1 "2020-11-04T16:49:19Z")

</div>

hello!  
i’ve had some trouble using the Wilks method in OpenTURNS. It actually works on a `RandomVector` object, whereas i already had my `Sample` over which i wanted the calculation to be performed.

One workaround would be to create a `RandomVector` object by applying a constant function over my sample. But this seems to be a lot of trouble and i would think that the wilks method should be able to handle sample objects.

thanks!  
sanaa

---

<div class="post-metadata">

**Author:** ![schueller](https://yyz2.discourse-cdn.com/free1/user_avatar/openturns.discourse.group/schueller/32/3_2.png) [@schueller](https://openturns.discourse.group/u/schueller)\
**Post date:** [November 6, 2020, 11:31am UTC](https://openturns.discourse.group/t/wilks-quantile-in-openturns/52/2 "2020-11-06T11:31:02Z")

</div>

Indeed `Wilks` works the other way around; you specify the quantile level and the confidence interval and it tells you the sample size you need (using the `Wilks.ComputeSampleSize(a,b,i)` method).  
If you want to reuse a sample you could try to tweak the confidence interval until it matches your sample size (unless there’s a direct way of computing this ?).

Then you would just need to do what `Wilks.computeQuantileBound` does:

```auto
 sample.sort(0)[-i]

```

---

<div class="post-metadata">

**Author:** ![regislebrun](https://yyz2.discourse-cdn.com/free1/user_avatar/openturns.discourse.group/regislebrun/32/154_2.png) [@regislebrun](https://openturns.discourse.group/u/regislebrun)\
**Post date:** [December 11, 2020, 10:05pm UTC](https://openturns.discourse.group/t/wilks-quantile-in-openturns/52/3 "2020-12-11T22:05:19Z")

</div>

Hi Sanaa,

I don’t understand the computation you want to perform with your sample. Basically, Wilk’s method could tell you the quantile level you get for a given confidence level if you take the largest value in your sample (or the second largest value aso), or it could tell you the confidence level you get for a given quantile level if you take the largest value in your sample (or the second largest value aso), or the maximum margin you can take to get an estimate of the quantile of a given quantile level for a given confidence level. As you see, the four quantities (sample size, margin, quantile level, confidence level) are linked and you may want to get any of them given the three others. The current implementation is to give you the sample size given the other parameters, but it is not so difficult to implement the 3 other combinations.

---

<div class="post-metadata">

**Author:** ![MerlinKeller](https://yyz2.discourse-cdn.com/free1/user_avatar/openturns.discourse.group/merlinkeller/32/190_2.png) [@MerlinKeller](https://openturns.discourse.group/u/MerlinKeller)\
**Post date:** [December 22, 2020, 12:45pm UTC](https://openturns.discourse.group/t/wilks-quantile-in-openturns/52/4 "2020-12-22T12:45:33Z")

</div>

Hi to all! I stumbled upon this conversation because I was facing the exact same issue as Sanaa: I have a sample with a given size, and I need to compute the Wilks quantile… There are two solutions to date:

1. Use the [openturns.wilks()](https://openturns.github.io/openturns/master/user_manual/_generated/openturns.Wilks.html) class, but:

- It is a shame the constructor takes only RandomVectors, given that Samples is such a commun usecase;

- the Wilks.computeQuantileBound “marginIndex” keyword argument takes 0 as a default value, which is hard to comprehend. Indeed, since it is described as

> Rank of the maximum which will evaluate the empirical quantile

a more logical default value would have been int(alpha\*n), where:

- alpha is the quantile order, already an input of the computeQuantileBound() method
- n is the sample size.

1. Code your own inverse of Wilks\_ComputeSampleSize(alpha, beta, i), to determine, given alpha, beta and a sample size n, what is the rank i of the order statistic evaluating the Wilks quantile. In my opinion, this is a not entirely trivial task. Here is my solution:

```auto
def Wilks_ComputeIndex(p, beta, N):
    i = 0
    n_of_i = ot.Wilks_ComputeSampleSize(p, beta, i)
    if n_of_i > n:
        raise NameError('Insufficient sample size')
    while n_of_i < N:
        i += 1
        n_of_i = ot.Wilks_ComputeSampleSize(p, beta, i)
        # print('i=%s, minimal size=%s'%(i,n_of_i))
    if n_of_i > n:
        i -= 1
    return i

```

No big deal in either case, but the implementation could be a great deal more user-friendly with a few ameliorations / automatization.

Cheers,  
Merlin

---

<div class="post-metadata">

**Author:** ![regislebrun](https://yyz2.discourse-cdn.com/free1/user_avatar/openturns.discourse.group/regislebrun/32/154_2.png) [@regislebrun](https://openturns.discourse.group/u/regislebrun)\
**Post date:** [December 22, 2020, 12:59pm UTC](https://openturns.discourse.group/t/wilks-quantile-in-openturns/52/5 "2020-12-22T12:59:17Z")

</div>

It’s Open Source, feel free to contribute ;-)!

---

<div class="post-metadata">

**Author:** ![regislebrun](https://yyz2.discourse-cdn.com/free1/user_avatar/openturns.discourse.group/regislebrun/32/154_2.png) [@regislebrun](https://openturns.discourse.group/u/regislebrun)\
**Post date:** [December 22, 2020, 1:27pm UTC](https://openturns.discourse.group/t/wilks-quantile-in-openturns/52/6 "2020-12-22T13:27:34Z")

</div>

More seriously, the `marginIndex` argument is here to indicate how many upper statistics you want to put at the right of your estimate. If it is equal to zero, then you consider the max of the sample as your quantile estimate, if it is equal to 1, the second largest value, and so on. It was defined during the CEMRACS 2006 when OpenTURNS was not even publicly released, so it is quite an old piece of code. And yes, the description is not correct. Concerning the constructor, the objective of this class was to provide a sampler of Wilk’s upper bound, hence the choice of a random vector. If you already have a sample at hand, it is too late: what is the interest of computing a sample size in this case? But if you provide an actual use-case for this class I can move things to ease your job 🙂.

Concerning your suggestion, it CANNOT work except for beta=0.5. If you link the rank of the order statistics to take as an estimate of a quantile upper bound to the quantile level, then the confidence level is fixed! In your case, if you choose the rank of the empirical quantile as the rank of your quantile estimate, you will get an asymptotic confidence of 0.5 as this estimator is consistent and asymptotically Gaussian (thus symmetrical).

I started to see what appends in this class. Here is what I get (the OT output is in blue, the analytical result for the maximum is in orange). So everything is good except for the low confidence levels. I suspect the initialization of the sequential search, not the easiest part of the code… ![Figure_1](https://global.discourse-cdn.com/free1/uploads/openturns/original/1X/13a57b96bb5e010602f89b54bc535a81923e95aa.png)

---

<div class="post-metadata">

**Author:** ![MerlinKeller](https://yyz2.discourse-cdn.com/free1/user_avatar/openturns.discourse.group/merlinkeller/32/190_2.png) [@MerlinKeller](https://openturns.discourse.group/u/MerlinKeller)\
**Post date:** [January 8, 2021, 3:39pm UTC](https://openturns.discourse.group/t/wilks-quantile-in-openturns/52/7 "2021-01-08T15:39:41Z")

</div>

I am surely considering doing so!

---

<div class="post-metadata">

**Author:** ![MerlinKeller](https://yyz2.discourse-cdn.com/free1/user_avatar/openturns.discourse.group/merlinkeller/32/190_2.png) [@MerlinKeller](https://openturns.discourse.group/u/MerlinKeller)\
**Post date:** [January 8, 2021, 3:51pm UTC](https://openturns.discourse.group/t/wilks-quantile-in-openturns/52/8 "2021-01-08T15:51:57Z")

</div>

Thanks for the additional feedback, though I’m not really sure what the figure is about? It seems that we have failed with Sanaa to make clear the use-case we have in mind, so let me try again:

Suppose you have a sample of size N, based on which you want to estimate the p-th order quantile of the underlying distribution. This is handily done by computing the empirical quantile, that is, the rank [N\*p] order statistic (if [x] stands for “the integer closest to x”). The Wilks quantile allows to determine a second rank i for the order statistics, which will yield an upper bound with confidence level beta (given that the sample size is not too small). Our problem is: how to determine the corresponding rank i?

The current implementation gives us the reverse information: if I use the rank i order statistic as an upper confidence bound for the p-th order quantile, what is the minimal sample size such that the confidence level is at least beta? This is a natural question if you want to plan a study, but not if you want to compute that actual Wilks quantile on a given sample…

---

<div class="post-metadata">

**Author:** ![MichaelBaudin](https://yyz2.discourse-cdn.com/free1/user_avatar/openturns.discourse.group/michaelbaudin/32/9_2.png) [@MichaelBaudin](https://openturns.discourse.group/u/MichaelBaudin)\
**Post date:** [January 9, 2021, 10:56pm UTC](https://openturns.discourse.group/t/wilks-quantile-in-openturns/52/9 "2021-01-09T22:56:41Z")

</div>

(Just as a reminder : In order to format your source code, you can “triple backquote” your Python code. )

---

<div class="post-metadata">

**Author:** ![MichaelBaudin](https://yyz2.discourse-cdn.com/free1/user_avatar/openturns.discourse.group/michaelbaudin/32/9_2.png) [@MichaelBaudin](https://openturns.discourse.group/u/MichaelBaudin)\
**Post date:** [April 15, 2021, 8:40am UTC](https://openturns.discourse.group/t/wilks-quantile-in-openturns/52/10 "2021-04-15T08:40:39Z")

</div>

Hi @MerlinKeller @sanaaZ,  
Would you be able to provide a use-case as a Python script showing the use of the feature you would need?  
Of course, this script will not work, but this does not matter at the current stage of the development.  
Regards,  
Michaël

---

<div class="post-metadata">

**Author:** ![MerlinKeller](https://yyz2.discourse-cdn.com/free1/user_avatar/openturns.discourse.group/merlinkeller/32/190_2.png) [@MerlinKeller](https://openturns.discourse.group/u/MerlinKeller)\
**Post date:** [September 19, 2021, 9:20pm UTC](https://openturns.discourse.group/t/wilks-quantile-in-openturns/52/11 "2021-09-19T21:20:28Z")

</div>

Hi Michaël,

Sorry for an overly belated answer!!

Here is an example script where I compute on a simulated sample:

- the empirical quantile
- the Wilks quantile

[Wilks\_quantile\_exemple\_script.py](https://openturns.discourse.group/uploads/short-url/9ClqFA52JePaCRHsP6s8sThqes3.py) (1.1 KB)

In it I also suggest to added to the Sample class a method computeWilksQuantile, in analogy to the existing computeEmpiricalQuantile.

Best,  
Merlin
