Statistics and functional programming languages
Recently, I feel fervent to learn functional programming, because i) (in my opinion), it will become a trend, and ii) the interpreter can be used as an advanced calculator.
Since I am teaching Statistics, I want to do some calculation of the normal distribution probability.
Before I begin, I need to mention, in order to calculate the normal distribution probability something like P(x < X), this can be done by using a spreadsheet software with NORMDIST() and NORM.INV() for the inverse of the former function.
Spreadsheet is good for calculation, but not good as a calculator. My primary calculator is SpeedCrunch, which allows entering expression. But the drawback of SpeedCrunch is the lack of statistical functions.
Therefore, the functional programming comes to my mind.
Firstly, let me introduce the usage of Python. Make sure SciPy is installed. Run the Python interpreter,
[code language=“python”]from scipy.stats import norm norm.cdf(my_value) norm.ppf(my_probability)[/code]
So, the two functions are norm.cdf() (cumulative distribution function) and norm.ppf() (percent point function).
Now, let me introduce the usage of R language.
[code language=“r”]pnorm(my_value) qnorm(my_probability)[/code]
R language is used for statistics, thus, no further module or library is required.
Both Python and R languages are not pure functional programmings. I wanted to try Emacs Lisp, but the arithmetic syntax is not convenient. The syntax is using Polish notation. In order to perform arithmetic calculation,
[code](* 2 (+ 3 6))[/code]
Therefore, I tried the pure functional programming language, that is, Haskell.
In order to calculate the normal distribution probability, the Statistics module is required. After installation,
[code]import Statistics.Distribution import Statistics.Distribution.Normal let d = normalDistr 0 1 cumulative d 0 quantile d 0.5 [/code]
“normalDistr 0 1” is to create a normal distribution with mean 0 and standard deviation 1. Then “cumulative d 0” is the calculation of CDF (cumulative distribution function), which produces 0.5. And “quantile” is the inverse function of CDF.
So, enjoy the functional programming in mathematics and statistics.