neversaint - 1 year ago 62

Perl Question

Is there a package in Perl that allows you to compute the height of probability distribution at each given point. For example this can be done in R this way:

`> dnorm(0, mean=4,sd=10)`

> 0.03682701

Namely the probability of point x=0 falls into a normal distribution, with mean=4 and sd=10, is 0.0368.

I looked at Statistics::Distribution but it doesn't give that very

function to do it.

Answer Source

Why not something along these lines (I am writing in R, but it could be done in perl with Statistics::Distribution):

```
dn <- function(x=0 # value
,mean=0 # mean
,sd=1 # sd
,sc=10000 ## scale the precision
) {
res <- (pnorm(x+1/sc, mean=mean, sd=sd)-pnorm(x, mean=mean, sd=sd))*sc
res
}
> dn(0,4,10,10000)
0.03682709
> dn(2.02,2,.24)
1.656498
```

[edit:1] I should mention that this approximation can get pretty horrible at the far tails. it might or might not matter depending on your application.

[edit:2] @foolishbrat Turned the code into a function. The result should always be positive. Perhaps you are forgetting that in the perl module you mention the function returns the upper probability 1-F, and R returns F?

[edit: 3] fixed a copy and paste error.