Search code examples
scala-breeze

Breeze linear algebra probability distribution


I am studying statistics and probability (aleks.com) and they have an online calculator that calculates probabilities of a standard normal random variable.

example: P(Z > 1.26)

However, I am also learning to use the Breeze linear algebra Scala library and I would like to understand how to implement this calculation using the breeze.stats package.

Thanks in advance.

EDIT:

I'm augmenting my question subsequent to the answer from @dlwh in order to fill in more of my understanding on the Gausian class: How do I then reverse the pattern to use a probability value to get the concrete value(s) for the area?

Example: I need to determine the value of c given the probability 0.9426 (which is 1.90)

// P(-c <= Z <= c) = 0.9426

val gau_dist = new Gaussian(0.0, 1.0)

val tailArea = (1 - 0.9426) / 2 //> Double = 0.028700000000000003

1 - gau_dist.cdf(1.90) // = tailArea //> Double = 0.02871655981600174


Solution

  • Use the Gaussian class from breeze.stats.distributions._:

    scala> import breeze.stats.distributions._
    import breeze.stats.distributions._
    
    scala> Gaussian(0, 1).cdf(1.26)
    res1: Double = 0.8961653188786995
    
    scala> 1.0 - Gaussian(0, 1).cdf(1.26)
    res2: Double = 0.10383468112130045
    
    scala> Gaussian(0, 1).sample(1000000).count(_ > 1.26)
    res3: Int = 103981
    

    The first (using the cdf method) gives you the analytic way, and the second is the empirical/monte carlo estimate.