{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "# Polynomial Chaos with the Ishigami Function\n", "\n", " \n", "**Jacob Sturdy**, Department of Structural Engineering, NTNU\n", "\n", "Date: **Jul 13, 2018**" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# ipython magic\n", "%matplotlib notebook\n", "%load_ext autoreload\n", "%autoreload 2" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# plot configuration\n", "import matplotlib\n", "import matplotlib.pyplot as plt\n", "plt.style.use(\"ggplot\")\n", "# import seaborn as sns # sets another style\n", "matplotlib.rcParams['lines.linewidth'] = 3\n", "fig_width, fig_height = (7.0,5.0)\n", "matplotlib.rcParams['figure.figsize'] = (fig_width, fig_height)\n", "\n", "# font = {'family' : 'sans-serif',\n", "# 'weight' : 'normal',\n", "# 'size' : 18.0}\n", "# matplotlib.rc('font', **font) # pass in the font dict as kwar" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import chaospy as cp\n", "import numpy as np\n", "from numpy import linalg as LA" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Introduction\n", "
\n", "\n", "The Ishigami function" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "\n", "$$\n", "\\begin{equation}\n", "y = \\sin(z_1) + a \\sin^2(z_2) + b z_3^4 \\sin(z_1)\n", "\\label{_auto1} \\tag{1}\n", "\\end{equation}\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "is a commonly used benchmark function for sensitivity analysis. You will now get the chance to evaluate polynomial chaos on this function.\n", "\n", "Each component of $\\mathbf{Z}$ is distributed uniformly over the range $(-\\pi, \\pi)$ and $a=7$ while $b=0.1$.\n", "\n", "These two functions implement the function in python as well as the exact solutions for its mean, variance and sensitivities." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def ishigami_function(sample):\n", " q1 = sample[0]\n", " q2 = sample[1]\n", " q3 = sample[2]\n", " a = 7.\n", " b = 0.1\n", " return np.sin(q1) + a*np.sin(q2)**2 + b* q3**4 * np.sin(q1)\n", "\n", "def ishigami_analytic():\n", " #Analytical values\n", " #Total variance\n", " measures = {}\n", " a = 7.\n", " measures[\"mean\"] = a/2.0\n", " b = 0.1\n", " D = a**2./8 + b*np.pi**4./5 + b**2*np.pi**8./18 + 1./2\n", " measures \n", " measures[\"var\"] = D\n", " # Conditional variances\n", " D1 = b*np.pi**4./5 + b**2*np.pi**8./50. + 1./2\n", " D2 = a**2/8.\n", " D3 = 0\n", " \n", " D12 = 0\n", " D13 = b**2. * np.pi**8 / 18 - b**2*np.pi**8./50.\n", " D23 = 0\n", " D123 = 0\n", " \n", " # Main and total sensitivity indices\n", " measures[\"sens_m\"] = {}\n", " measures[\"sens_m\"][0] = D1/D\n", " measures[\"sens_m\"][1] = D2/D\n", " measures[\"sens_m\"][2] = D3/D\n", " \n", "\n", " measures[\"sens_t\"] = {}\n", " measures[\"sens_t\"][0] = (D1 + D12 + D13 + D123)/D\n", " measures[\"sens_t\"][1] = (D2 + D12 + D23 + D123)/D\n", " measures[\"sens_t\"][2] = (D3 + D13 + D23 + D123)/D\n", " return measures" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Compare the results you get for different methods of performing polynomial chaos with this function. What sampling methods work best?" ] } ], "metadata": {}, "nbformat": 4, "nbformat_minor": 2 }