Skip to content

Instantly share code, notes, and snippets.

@aphearin
Created July 30, 2021 22:06
Show Gist options
  • Save aphearin/5ab8e76f2fc38dc60c6182d8966b997e to your computer and use it in GitHub Desktop.
Save aphearin/5ab8e76f2fc38dc60c6182d8966b997e to your computer and use it in GitHub Desktop.
Calculate the integral of a 2d Gaussian using Latin Hypercube sampling
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "1fdb5f8a",
"metadata": {},
"outputs": [],
"source": [
"%matplotlib inline"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "481b8e94",
"metadata": {},
"outputs": [],
"source": [
"from jax.scipy.stats import multivariate_normal\n",
"mu = np.zeros(2)\n",
"cov = np.eye(2)"
]
},
{
"cell_type": "markdown",
"id": "410bab62",
"metadata": {},
"source": [
"### Create a mesh grid spanning -5, 5 in each dimension"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "b9cfc4fe",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(10000, 2)\n"
]
}
],
"source": [
"npts_per_dim = 100\n",
"xh, yh = 5, 5\n",
"\n",
"xlo, xhi, nx = -xh, xh, npts_per_dim\n",
"ylo, yhi, ny = -yh, yh, npts_per_dim\n",
"dx = (xhi-xlo)/nx\n",
"dy = (yhi-ylo)/ny\n",
"\n",
"x = np.linspace(xlo, xhi, npts_per_dim+1)\n",
"y = np.linspace(ylo, yhi, npts_per_dim+1)\n",
"x, y = np.array(np.meshgrid(x[:-1], y[:-1]))\n",
"x, y = x.flatten(), y.flatten()\n",
"X_grid = np.vstack((x, y)).T\n",
"print(X_grid.shape)"
]
},
{
"cell_type": "markdown",
"id": "dd4b515b",
"metadata": {},
"source": [
"### Compute the grid sampling weight and do the Riemann integral"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "24c8587c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.99999875\n"
]
}
],
"source": [
"npts_grid_sampling = X_grid.shape[0]\n",
"AREA = (xhi-xlo)*(yhi-ylo)\n",
"norm_factor_grid = AREA / npts_grid_sampling\n",
"\n",
"gaussian_integral_grid = multivariate_normal.pdf(X_grid, mu, cov).sum()*norm_factor_grid\n",
"print(gaussian_integral_grid)"
]
},
{
"cell_type": "markdown",
"id": "290c61e1",
"metadata": {},
"source": [
"### Create a latin hypercube"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "fab05caa",
"metadata": {},
"outputs": [],
"source": [
"from scipy.stats.qmc import LatinHypercube\n",
"LH = LatinHypercube(X_grid.shape[1])\n",
"npts_lh_sampling = npts_grid_sampling\n",
"U_LH = LH.random(npts_lh_sampling)\n",
"X_LH = np.zeros_like(U_LH)\n",
"X_LH[:, 0] = (U_LH[:, 0] - 0.5)*(xhi-xlo)\n",
"X_LH[:, 1] = (U_LH[:, 1] - 0.5)*(yhi-ylo)"
]
},
{
"cell_type": "markdown",
"id": "f0ae32af",
"metadata": {},
"source": [
"### Compute the LH sampling weight and do the Riemann integral"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "ebfca950",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.0012085\n"
]
}
],
"source": [
"norm_factor_lhs = AREA / npts_lh_sampling\n",
"\n",
"gaussian_integral_lhs = multivariate_normal.pdf(X_LH, mu, cov).sum()*norm_factor_lhs\n",
"print(gaussian_integral_lhs)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "030b8d8d",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment