CompEcon Toolbox:
DemQua04
Area under normal pdf using Simpson's rule
Randall Romero Aguilar, PhD

This demo is based on the original Matlab demo accompanying the Computational Economics and Finance 2001 textbook by Mario Miranda and Paul Fackler.

Last updated: 2020-Sep-10

Initial tasks

In [ ]:
if 'google.colab' in str(get_ipython()):
    print("This notebook is running on Google Colab. Installing the compecon package.")
    !pip install compecon
In [ ]:
import numpy as np
from compecon import qnwsimp, demo
import matplotlib.pyplot as plt
In [ ]:
n, a, z = 11, 0, 1

def f(x):
    return np.sqrt(1/(2*np.pi))*np.exp(-0.5*x**2)
In [ ]:
x, w = qnwsimp(n, a, z)
prob = 0.5 + w.dot(f(x))
In [ ]:
a, b, n = -4, 4, 500
x = np.linspace(a, b, n)
xz = np.linspace(a, z, n)

fig, ax = plt.subplots(figsize=[8,4])
ax.fill_between(xz,f(xz), color='LightSkyBlue')
ax.hlines(0, a, b,'k','solid')
ax.vlines(z, 0, f(z),'r',linewidth=2)
ax.plot(x,f(x), linewidth=3)

ax.annotate(r'$\Pr\left(\tilde Z\leq z\right)$',[-1, 0.08], fontsize=18)
ax.set_yticks([])
ax.set_xticks([z])
ax.set_xticklabels(['$z$'],fontsize=20);
In [ ]:
#demo.savefig([fig], name='demqua04')