Wayne's Github Page

A place to learn about statistics

Pseodo Code

Pseudo code is an outline of how your code would be structured in a format that is friendly to people who do not understand the syntax of the programming language you’re using but is familiar with for-loops, functions, and the algorithm you’re coding up.

An example is “use programming to demonstrate the central limit theorem”. Such a task involves several steps. In particular:

First, the central limit theorem states that if a random variables $Y\sim F$ where $E(Y^2) < \infty$ (but $F$ may not be Normal) then $\sqrt{n} \bar{Y} \Rightarrow N(E(Y), Var(Y))$.

To demonstrate this, we should

The bullet points above are almost an outline, pseudo code just asks you to add in the for-loops and some key function calls. The key is to give clarity in the structure of the code even if one does not program in Python specifically (but does know programming).

Here’s an example of what the pseudo-code above could be translated into:

import numpy as np
import seaborn as sns

# It's best practice to place all tunable parameters together
B = 1000
n = 100
p = 0.1

# Simulations
ms = []
for _ in range(B):
    ys = np.random.binomial(n=1, p=p, size=n)
    m = np.mean(ys)
    ms.append(m)

# Theoretical target
x_range = np.linspace(np.min(ms), np.max(ms), B)

def normal_density(x, m=0, v=1):
    kernel = np.exp(-0.5 * np.power(x - m, 2) / v)
    norm = np.sqrt(2 * np.pi * v)
    return kernel / norm

y_dens = normal_density(x_range, m = p, v = p * (1 - p) / n)

# plotting
sns.lineplot(x=x_range, y=y_dens, alpha=0.5)
sns.kdeplot(ms, alpha=0.5)
plt.show()

Notice that the actual execution includes: