# Understanding Joint Probability of Less Likely Samples :: calculate method

**URL:** <https://discuss.elastic.co/t/understanding-joint-probability-of-less-likely-samples-calculate-method/256562>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-machine-learning\
**Created:** [November 24, 2020, 7:15pm UTC](https://discuss.elastic.co/t/understanding-joint-probability-of-less-likely-samples-calculate-method/256562 "2020-11-24T19:15:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ProfSimoes](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/profsimoes/32/45707_2.png) [@ProfSimoes](https://discuss.elastic.co/u/ProfSimoes)\
**Post date:** [November 24, 2020, 7:15pm UTC](https://discuss.elastic.co/t/understanding-joint-probability-of-less-likely-samples-calculate-method/256562/1 "2020-11-24T19:15:51Z")

</div>

I bought great Collier's book, I've read the Prelert paper about APM monitoring, and then I've been trough the ML-CPP code at github...

A tiny sense of curiosity sparkled inside me about the method:

> ProbabilityAggregators::CJointProbabilityOfLessLikelySamples::calculate

is it correct to say that this method answer the (eg) question _"If I have 3 samples with value 0,4; 0,2 and 0,3; what's the probability of seeing a set of n samples with a lower joint probability than that of this joint 3 samples?_"  
Am I getting this the right way?

I'm confused because the github code states in its description the following:

> We want to find the probability of seeing a more extreme collection  
> of independent samples {y(1), y(2), ... , y(n)} than a specified **(1)**  
> collection {x(1), x(2), ... , x(n)}, where this is defined as the  
> probability of the set R:  
> { {y(i)} : Product\_j{ L(y(j)) } \<= Product\_j{ L(y(j)) } }. **(2)**
> 
> We will assume that y(i) ~ N(m(i), v(i)). In this case, it is possible  
> to show that:  
> P(R) = gi(n/2, Sum\_i{ z(i)^2 } / 2) / g(n/2) **(3)**
> 
> where,  
> z(i) = (x(i) - m(i)) / v(i) ^ (1/2).  
> gi(., .) is the upper incomplete gamma function.  
> g(.) is the gamma function.

**(1)** Looking at the unit test, it seems this probability is then multiplied by the sample size (n==20000) in order to get the expected count of samples with lower probability than the (n==3) set in the case tested, but according to description it should be n==3, as {y(1), y(2), y(3)}... what am I getting wrong in this?

**(2)** Left and Right hands seems equal to me, is that ok?

**(3)** Is it possible to get some info about the theorem that probes this?

I would be very happy if you could lend me a hand with this _stat choke_ that's cranking up in my brain...

---

<div class="post-metadata">

**Author:** ![Tom\_Veasey](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tom_veasey/32/47095_2.png) [@Tom\_Veasey](https://discuss.elastic.co/u/Tom_Veasey)\
**Post date:** [November 24, 2020, 8:22pm UTC](https://discuss.elastic.co/t/understanding-joint-probability-of-less-likely-samples-calculate-method/256562/2 "2020-11-24T20:22:28Z")

</div>

This is a typo in the github comment: it should read { y(i) : \prod\_{i}{ L(y(i)) } \<= \prod\_{i}{ L(x(i)) } }, i.e. the set of all points for which the joint likelihood is less than the joint likelihood of the values {x(i)} just observed. If the likelihoods are normal then this is just the integral of the multivariate normal distribution outside the ellipsoid, i.e. for which (y - m)^t \Sigma^{-1} (y - m) \>= (x - m)^t \Sigma^{-1} (x - m). (In fact, even if they are non-normal, but the tails aren't too heavy, this often gives something remarkably close to the true value as per the definition.) The fact that this is expressible as an incomplete gamma function is a standard result, for example see [here](https://en.wikipedia.org/wiki/Multivariate_normal_distribution#Cumulative_distribution_function) and particularly [this](https://upload.wikimedia.org/wikipedia/commons/a/a2/Cumulative_function_n_dimensional_Gaussians_12.2013.pdf) reference. The equations in that reference aren't numbered so I can't link the exact one, but the relevant one is towards the end, i.e. F(r) = \int\_0^{r^2} x^{(n-2)/2}e^{-x/2}/(2^{n/2}\Gamma(n/2)) dx and compare with the definition of the gamma function. To get a sense of what this is doing, have a look at [this](https://github.com/elastic/ml-cpp/blob/master/lib/maths/unittest/CProbabilityAggregatorsTest.cc#L145) test which compares this to the value calculated by sampling.

---

<div class="post-metadata">

**Author:** ![ProfSimoes](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/profsimoes/32/45707_2.png) [@ProfSimoes](https://discuss.elastic.co/u/ProfSimoes)\
**Post date:** [November 24, 2020, 8:34pm UTC](https://discuss.elastic.co/t/understanding-joint-probability-of-less-likely-samples-calculate-method/256562/3 "2020-11-24T20:34:09Z")

</div>

Tom & Rich, many thanks for the fast, kind & comprehensive reply! My _stat choke_ on my brain passed all the way out... 😛

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [December 22, 2020, 8:34pm UTC](https://discuss.elastic.co/t/understanding-joint-probability-of-less-likely-samples-calculate-method/256562/4 "2020-12-22T20:34:26Z")

</div>

Closed on December 22, 2020, 8:34pm UTC

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
