Home python How to get the value of the Laplace function? Python

How to get the value of the Laplace function? Python





Can you please tell me how to get the value of the Laplace function (standard normal distribution function) in python?

The laplace.pdf () function from scipy gives the wrong value. The values ​​do not match the tabular values ​​of the Laplace integral function:

laplace cdf plot

Answer 1, authority 100%

Found: the values ​​of the mentioned table are given by the function scipy.stats.norm.cdf (x) - 0.5 . Only now, in my opinion, it has nothing to do with Laplace, because it is an integral of the usual Gaussian distribution.

Answer 2, authority 50%

Google suggests that math.erf (x)

Answer 3, authority 25%

scipy.stats.norm.cdf (x)

Gives the integral of the density distribution. In fact, it is the Laplace function. This is the integral from minus infinity to x. It’s called cdf.
enter image description here

The tables use the normalized Laplace function, which (as already noted) is equal to the usual function minus 0.5. It represents the integral from 0 to x.
enter image description here

Here is the conclusion from the book Written by D.T. Lecture notes on probability theory, mathematical statistics and stochastic processes:
enter image description here

This explains why you need to subtract 0.5. After all, the integral of the entire probability density is equal to one. And it is symmetrical. And the negative part is exactly 0.5.

You can also use erf. It is linked to cdf (see Wikipedia article ).

And this answer gave me the ability to speed up cdf even more than scipy.

In my project, I also hang up jit compilation. And it comes out twice as fast as cdf. (Normal (0,1), x) on Julia. As a result, the speedup in comparison with scipy was 10 times.

My implementation is shown below. Unfortunately, it lacks input validation, so provide it yourself.

@ nb.njit (cache = True)
def cdf (x):
  "" "
  Calculation of the Laplace function.
  Attention! Disabled border check
  "" "
  x = x / 1.414213562
  a1 = 0.254829592
  a2 = -0.284496736
  a3 = 1.421413741
  a4 = -1.453152027
  a5 = 1.061405429
  p = 0.3275911
  s = np.sign (x)
  t = 1 / (1 + s * p * x)
  b = np.exp (-x * x)
  y = (s * s + s) / 2 - \
    s * (((((a5 * t + a4) * t) + a3) * t + a2) * t + a1) * t * b / 2
  return y

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions