{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "*This notebook contains material from [cbe67701-uncertainty-quantification](https://ndcbe.github.io/cbe67701-uncertainty-quantification);\n", "content is available [on Github](https://github.com/ndcbe/cbe67701-uncertainty-quantification.git).*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "< [7.0 Sampling-Based Uncertainty Quantification: Monte Carlo and Beyond](https://ndcbe.github.io/cbe67701-uncertainty-quantification/07.00-Sampling-Based-Uncertainty-Quantification.html) | [Contents](toc.html) | [7.2 Latin Hypercube Sampling](https://ndcbe.github.io/cbe67701-uncertainty-quantification/07.02-Latin-Hypercube-sampling.html)
"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 1,
"link": "[7.1 Latin Hypercube and Quasi-Monte Carlo Sampling](https://ndcbe.github.io/cbe67701-uncertainty-quantification/07.01-Sampling-Based-Uncertainty-Quantification.html#7.1-Latin-Hypercube-and-Quasi-Monte-Carlo-Sampling)",
"section": "7.1 Latin Hypercube and Quasi-Monte Carlo Sampling"
}
},
"source": [
"# 7.1 Latin Hypercube and Quasi-Monte Carlo Sampling\n",
"\n",
"Created by Dezhao Huang (dhuang2@nd.edu)\n",
"\n",
"These examples and codes were adapted from:\n",
"* https://risk-engineering.org/notebook/monte-carlo-LHS.html\n",
"* McClarren, Ryan G (2018). *Uncertainty Quantification and Predictive Computational Science: A Foundation for Physical Scientists and Engineers*, *Chapter 7 : Sampling-Based Uncertainty Quantification Monte Carlo and Beyond*, Springer, https://link.springer.com/chapter/10.1007%2F978-3-319-99525-0_7\n",
"\n",
"## Chapter 7 Overview\n",
"\n",
"1. Basic Monte Carlo methods: maximum likelihood estimation and methods of moments\n",
"2. Design based sampling: Latin Hypercube Sampling\n",
"3. Quasi-Monte Carlo: Halton sequences and Sobol sequence"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"autoscroll": false,
"ein.hycell": false,
"ein.tags": "worksheet-0",
"nbpages": {
"level": 1,
"link": "[7.1 Latin Hypercube and Quasi-Monte Carlo Sampling](https://ndcbe.github.io/cbe67701-uncertainty-quantification/07.01-Sampling-Based-Uncertainty-Quantification.html#7.1-Latin-Hypercube-and-Quasi-Monte-Carlo-Sampling)",
"section": "7.1 Latin Hypercube and Quasi-Monte Carlo Sampling"
},
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"# Load libraries\n",
"import math\n",
"import numpy\n",
"import scipy.stats\n",
"import matplotlib.pyplot as plt\n",
"plt.style.use(\"bmh\")\n",
"import sympy\n",
"from IPython.display import Image\n",
"from IPython.core.display import HTML "
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"nbpages": {
"level": 1,
"link": "[7.1 Latin Hypercube and Quasi-Monte Carlo Sampling](https://ndcbe.github.io/cbe67701-uncertainty-quantification/07.01-Sampling-Based-Uncertainty-Quantification.html#7.1-Latin-Hypercube-and-Quasi-Monte-Carlo-Sampling)",
"section": "7.1 Latin Hypercube and Quasi-Monte Carlo Sampling"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: sobol_seq in /anaconda3/lib/python3.7/site-packages (0.2.0)\n",
"Requirement already satisfied: numpy in /anaconda3/lib/python3.7/site-packages (from sobol_seq) (1.16.2)\n",
"Requirement already satisfied: scipy in /anaconda3/lib/python3.7/site-packages (from sobol_seq) (1.2.1)\n",
"Requirement already satisfied: pyDOE in /anaconda3/lib/python3.7/site-packages (0.3.8)\n",
"Requirement already satisfied: scipy in /anaconda3/lib/python3.7/site-packages (from pyDOE) (1.2.1)\n",
"Requirement already satisfied: numpy in /anaconda3/lib/python3.7/site-packages (from pyDOE) (1.16.2)\n"
]
}
],
"source": [
"## Install missing packages\n",
"!pip install sobol_seq\n",
"\n",
"import sobol_seq\n",
"\n",
"!pip install pyDOE\n",
"from pyDOE import lhs"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"nbpages": {
"level": 1,
"link": "[7.1 Latin Hypercube and Quasi-Monte Carlo Sampling](https://ndcbe.github.io/cbe67701-uncertainty-quantification/07.01-Sampling-Based-Uncertainty-Quantification.html#7.1-Latin-Hypercube-and-Quasi-Monte-Carlo-Sampling)",
"section": "7.1 Latin Hypercube and Quasi-Monte Carlo Sampling"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Checking for figures/LHS.png\n",
"\tFile found!\n"
]
}
],
"source": [
"# Download figures (if needed)\n",
"import os, requests, urllib\n",
"\n",
"# GitHub pages url\n",
"url = \"https://ndcbe.github.io/cbe67701-uncertainty-quantification/\"\n",
"\n",
"# relative file paths to download\n",
"# this is the only line of code you need to change\n",
"file_paths = ['figures/LHS.png']\n",
"\n",
"# loop over all files to download\n",
"for file_path in file_paths:\n",
" print(\"Checking for\",file_path)\n",
" # split each file_path into a folder and filename\n",
" stem, filename = os.path.split(file_path)\n",
" \n",
" # check if the folder name is not empty\n",
" if stem:\n",
" # check if the folder exists\n",
" if not os.path.exists(stem):\n",
" print(\"\\tCreating folder\",stem)\n",
" # if the folder does not exist, create it\n",
" os.mkdir(stem)\n",
" # if the file does not exist, create it by downloading from GitHub pages\n",
" if not os.path.isfile(file_path):\n",
" file_url = urllib.parse.urljoin(url,\n",
" urllib.request.pathname2url(file_path))\n",
" print(\"\\tDownloading\",file_url)\n",
" with open(file_path, 'wb') as f:\n",
" f.write(requests.get(file_url).content)\n",
" else:\n",
" print(\"\\tFile found!\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"ein.tags": "worksheet-0",
"nbpages": {
"level": 1,
"link": "[7.1 Latin Hypercube and Quasi-Monte Carlo Sampling](https://ndcbe.github.io/cbe67701-uncertainty-quantification/07.01-Sampling-Based-Uncertainty-Quantification.html#7.1-Latin-Hypercube-and-Quasi-Monte-Carlo-Sampling)",
"section": "7.1 Latin Hypercube and Quasi-Monte Carlo Sampling"
},
"slideshow": {
"slide_type": "-"
}
},
"source": [
"The simplest 1D integration problem, \n",
"\n",
"$\\int_1^5 x^2$\n",
"\n",
"We can estimate this integral using a standard Monte Carlo method, where we use the fact that the expectation of a random variable is related to its integral\n",
"\n",
"$\\mathbb{E}(f(x)) = \\int_I f(x) dx$\n",
"\n",
"We will sample a large number $N$ of points in $I$ and calculate their average, and multiply by the range over which we are integrating.\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"autoscroll": false,
"ein.hycell": false,
"ein.tags": "worksheet-0",
"nbpages": {
"level": 1,
"link": "[7.1 Latin Hypercube and Quasi-Monte Carlo Sampling](https://ndcbe.github.io/cbe67701-uncertainty-quantification/07.01-Sampling-Based-Uncertainty-Quantification.html#7.1-Latin-Hypercube-and-Quasi-Monte-Carlo-Sampling)",
"section": "7.1 Latin Hypercube and Quasi-Monte Carlo Sampling"
},
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Analytical result: 41.333333333333336\n"
]
}
],
"source": [
"# First, let's take a look at the analytical result:\n",
"result = {} \n",
"x = sympy.Symbol(\"x\")\n",
"i = sympy.integrate(x**2)\n",
"result[\"analytical\"] = float(i.subs(x, 5) - i.subs(x, 1))\n",
"print(\"Analytical result: {}\".format(result[\"analytical\"]))"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 1,
"link": "[7.1 Latin Hypercube and Quasi-Monte Carlo Sampling](https://ndcbe.github.io/cbe67701-uncertainty-quantification/07.01-Sampling-Based-Uncertainty-Quantification.html#7.1-Latin-Hypercube-and-Quasi-Monte-Carlo-Sampling)",
"section": "7.1 Latin Hypercube and Quasi-Monte Carlo Sampling"
}
},
"source": [
"Monte Carlo Simulator : $
"
]
}
],
"metadata": {
"anaconda-cloud": null,
"kernelspec": {
"display_name": "Python 3",
"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.7.3"
},
"name": "monte-carlo-LHS.ipynb"
},
"nbformat": 4,
"nbformat_minor": 1
}