# Normal distribution - probability density function

## 0. Quick guide

In this guide, we code the normal distribution probability density function in JavaScript (Google Apps Script :: GAS) and VBA. The outcome is equivalent to the WS NORM.DIST and NORM.S.DIST functions. This is not reinventing the wheel, but illustrating the link between algebraic formulas and code development.

The normal distribution probability density function (PDF) is:

$$$$f(x \ | \ \mu, \ \sigma^2) = \frac{1}{\sigma \sqrt{2 \pi}}e^{-\frac{(x-\mu)^2}{2 \sigma^2}} = \frac{1}{\sigma \sqrt{2 \pi}}e^{-\frac{1}{2} \left( \frac{x - \mu}{ \sigma} \right)^2}$$$$

where

• $\mu$ (mu) is the mean of the distribution
• $\sigma$ (sigma) is the standard deviation
• $\pi$ (pi) = 3.14159
• $e$ = 2.71828

For the standard normal distribution with $\mu = 1$ and $\sigma = 0$, the $z$ value is: $$z = \frac{x - \mu}{\sigma}$$ with probability density function: $$$$f(z) = \frac{1}{\sqrt{2 \pi}}e^{-\frac{1}{2}z^2}$$$$

## 1. JavaScript

### 1.1 (x, mu, sigma)

The JavaScript version of equation 1 centre $\frac{1}{\sigma \sqrt{2 \pi}}e^{-\frac{(x-\mu)^2}{2 \sigma^2}}$

JavaScript Code 1.1.1: Function xlfNormalPDF1a returns the height of the normal distribution with mean mu, and standard deviation sigma
/* ================================================== */
/* The Normal distribution probability density function (PDF)
for the specified mean and specified standard deviation: */
function xlfNormalPDF1a (x, mu, sigma) {
var num = Math.exp(-Math.pow((x - mu), 2) / (2 * Math.pow(sigma, 2)))
var denom = sigma * Math.sqrt(2 * Math.PI)
return num / denom
}


The JavaScript version of equation 1 right $\frac{1}{\sigma \sqrt{2 \pi}}e^{-\frac{1}{2} \left( \frac{x - \mu}{ \sigma} \right)^2}$

JavaScript Code 1.1.2: Function xlfNormalPDF1b returns the height of the normal distribution with mean mu, and standard deviation sigma
function xlfNormalPDF1b (x, mu, sigma) {
var num = Math.exp(- 1 / 2 * Math.pow((x - mu) / sigma, 2))
var denom = sigma * Math.sqrt(2 * Math.PI)
return num / denom
}



### 1.2 (z)

The JavaScript version of equation 2 $\frac{1}{\sqrt{2 \pi}}e^{-\frac{1}{2}z^2}$

JavaScript Code 1.2: Function xlfNormalPDF returns the height of the normal distribution with mean 0, and standard deviation 1
/* ================================================== */
/* The Normal distribution probability density function (PDF)
for the standard normal distribution: */
function xlfNormalSdistPDF (z) {
var num = Math.exp(-1 / 2 * x * x )
var denom = Math.sqrt(2 * Math.PI)
return num / denom
}


## 2. VBA

### 2.1 (x, mu, sigma)

The VBA version of equation 1 centre $\frac{1}{\sigma \sqrt{2 \pi}}e^{-\frac{(x-\mu)^2}{2 \sigma^2}}$

Code 2.1.1: xlfNormalPDF returns the height of the bell-shaped probability density curve for value x with mean mu and
standard deviation sigma. Equivalent to WS NORM.DIST(x,mu,sigma,FALSE) function
Function xlfNormalPDF1a(x As Double, _
mu As Double, _
sigma As Double) As Double
Dim num As Double, denom As Double

num = VBA.Exp(-(x - mu) ^ 2 / (2 * sigma ^ 2))
denom = sigma * VBA.Sqr(2 * WorksheetFunction.Pi)
xlfNormalPDF1a = num / denom

End Function


The VBA version of equation 1 right $\frac{1}{\sigma \sqrt{2 \pi}}e^{-\frac{1}{2} \left( \frac{x - \mu}{ \sigma} \right)^2}$

Code 2.1.2: xlfNormalPDF returns the height of the bell-shaped probability density curve for value x with mean mu and
standard deviation sigma. Equivalent to WS NORM.DIST(x,mu,sigma,FALSE) function
Function xlfNormalPDF1b(x As Double, _
mu As Double, _
sigma As Double) As Double
Dim num As Double, denom As Double

num = VBA.Exp(-1 / 2 * ((x - mu) / sigma) ^ 2)
denom = sigma * VBA.Sqr(2 * WorksheetFunction.Pi)
xlfNormalPDF1b = num / denom

End Function


### 2.2 (z)

The VBA version of equation 2 $\frac{1}{\sqrt{2 \pi}}e^{-\frac{1}{2}z^2}$

Code 2.2: xlfNormalPDF2 returns the probability mass (density) function for mean of zero and standard deviation of one.
Equivalent to WS NORM.S.DIST(z,FALSE) function
Function xlfNormalPDF2(z As Double) As Double
Dim num As Double, denom As Double

num = VBA.Exp(-1 / 2 * z ^ 2)
denom = VBA.Sqr(2 * WorksheetFunction.Pi)
xlfNormalPDF2 = num / denom

End Function


• Related material: xlfNormDist 2.4 xlfNormSDist cumulative: Accessed 14 July 2019
• Development platform: Excel (64 bit) Office 365 ProPlus version 1906
• Published: 16 July 2019
• References: Math - JavaScript | MDNMozilla Math Accessed: 14 July 2019
• Revised: Wednesday 17th of July 2019 - 03:26 PM, [Australian Eastern Time (AET)]