{ "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", "< [10.0 Gaussian Process Emulators and Surrogate Models](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.00-Gaussian-Process-Emulators-and-Surrogate-Models.html) | [Contents](toc.html) | [10.2 A simple example of Bayesian quadrature](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.02-Bayesian-quadrature.html)
"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 1,
"link": "[10.1 Using GPflow package for Gaussian Process Regression](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1-Using-GPflow-package-for-Gaussian-Process-Regression)",
"section": "10.1 Using GPflow package for Gaussian Process Regression"
}
},
"source": [
"# 10.1 Using GPflow package for Gaussian Process Regression\n",
"\n",
"\n",
"Created by Bridgette Befort (bbefort@nd.edu)\n",
"\n",
"The following example was adapted from:\n",
"\n",
"De G. Matthews, A. G., Van Der Wilk, M., Nickson, T., Fujii, K., Boukouvalas, A., León-Villagrá, P., ... & Hensman, J. (2017). GPflow: A Gaussian process library using TensorFlow. The Journal of Machine Learning Research, 18(1), 1299-1304.\n",
"\n",
"McClarren, Ryan G (2018). Uncertainty Quantification and Predictive Computational Science: A Foundation for Physical Scientists and Engineers, Chapter 10: Gaussian Process Emulators and Surrogate Models, Springer, https://link.springer.com/chapter/10.1007/978-3-319-99525-0_10"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 2,
"link": "[10.1.1 Objectives and Organization](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.1-Objectives-and-Organization)",
"section": "10.1.1 Objectives and Organization"
}
},
"source": [
"## 10.1.1 Objectives and Organization\n",
"\n",
"1. GPflow example for the function $y = sin(x) + cos(x)$\n",
" * Setup steps\n",
" * Kernels\n",
" * Varying amount of train/test data\n",
" \n",
" \n",
"2. Apply GPflow tool to shock breakout time dataset\n",
" * Without scaling\n",
" * With scaling\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 2,
"link": "[10.1.2 Import Libraries](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.2-Import-Libraries)",
"section": "10.1.2 Import Libraries"
}
},
"source": [
"## 10.1.2 Import Libraries\n",
"\n",
"Note: GPflow needs to be installed\n",
"\n",
"https://gpflow.readthedocs.io/en/master/intro.html\n",
"\n",
"ACTION ITEM: Streamline this installation on Colab. Which version of GPFlow should be installed?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 2,
"link": "[10.1.2 Import Libraries](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.2-Import-Libraries)",
"section": "10.1.2 Import Libraries"
}
},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"import unyt as u\n",
"import matplotlib.pyplot as plt\n",
"import gpflow\n",
"import tensorflow as tf\n",
"from gpflow.utilities import print_summary"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 2,
"link": "[10.1.3 Define Functions](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.3-Define-Functions)",
"section": "10.1.3 Define Functions"
}
},
"source": [
"## 10.1.3 Define Functions"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 2,
"link": "[10.1.3 Define Functions](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.3-Define-Functions)",
"section": "10.1.3 Define Functions"
}
},
"outputs": [],
"source": [
"def shuffle_and_split(df, n_params, fraction_train=0.8):\n",
" \"\"\"Randomly shuffles the DataFrame and extracts the train and test sets\n",
" Parameters\n",
" ----------\n",
" df : pandas.DataFrame\n",
" The dataframe with the\n",
" n_params : int\n",
" Number of parameters in the model\n",
" fraction_train : float\n",
" Fraction to use as training data. The remainder will be used for testing. Default is 0.8\n",
" Returns\n",
" -------\n",
" x_train : np.ndarray\n",
" Training inputs\n",
" y_train : np.ndarray\n",
" Training results\n",
" x_test : np.ndarray\n",
" Testing inputs\n",
" y_test : np.ndarray\n",
" Testing results\n",
" \"\"\"\n",
"\n",
" # Return values for all samples (liquid and vapor)\n",
" data = df.values\n",
" fraction_test = 1.0 - fraction_train\n",
" total_entries = data.shape[0]\n",
" train_entries = int(total_entries * fraction_train)\n",
" # Shuffle the data before splitting train/test sets\n",
" np.random.shuffle(data)\n",
"\n",
" # x = params, y = output\n",
" x_train = data[:train_entries, : n_params].astype(np.float64)\n",
" y_train = data[:train_entries, -1].astype(np.float64)\n",
" x_test = data[train_entries:, : n_params].astype(np.float64)\n",
" y_test = data[train_entries:, -1].astype(np.float64)\n",
"\n",
" return x_train, y_train, x_test, y_test"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 2,
"link": "[10.1.3 Define Functions](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.3-Define-Functions)",
"section": "10.1.3 Define Functions"
}
},
"outputs": [],
"source": [
"def run_gpflow_scipy(x_train, y_train, kernel):\n",
" \"\"\"Fits GP model to the training data\n",
" Parameters\n",
" ----------\n",
" x_train : np.ndarray\n",
" Training inputs\n",
" y_train : np.ndarray\n",
" Training results\n",
" kernel : function\n",
" GP flow kernel function\n",
" Returns\n",
" -------\n",
" model : \n",
" fitted GP flow model\n",
" \"\"\"\n",
" # Create the model\n",
" model = gpflow.models.GPR(\n",
" data=(x_train, y_train.reshape(-1, 1)),\n",
" kernel=kernel,\n",
" mean_function=gpflow.mean_functions.Linear(\n",
" A=np.zeros(x_train.shape[1]).reshape(-1, 1)\n",
" ),\n",
" )\n",
"\n",
" # Print initial values\n",
" print_summary(model, fmt=\"notebook\")\n",
"\n",
" # Optimize model with scipy\n",
" optimizer = gpflow.optimizers.Scipy()\n",
" optimizer.minimize(model.training_loss, model.trainable_variables)\n",
"\n",
" # Print the optimized values\n",
" print_summary(model, fmt=\"notebook\")\n",
"\n",
" # Return the model\n",
" return model"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 2,
"link": "[10.1.3 Define Functions](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.3-Define-Functions)",
"section": "10.1.3 Define Functions"
}
},
"outputs": [],
"source": [
"def plot_models(models, x_data, y_data, xylim_low=0, xylim_high=1):\n",
" \"\"\"Plot the performance of one or more GP models for some data x_data\n",
" Parameters\n",
" ----------\n",
" models : dict { label : model }\n",
" Each model to be plotted (value, GPFlow model) is provided\n",
" with a label (key, string)\n",
" x_data : np.array\n",
" data to create model predictions for\n",
" y_data : np.ndarray\n",
" correct answer\n",
" xylim_low : float, opt\n",
" lower x and y limits of the plot, default 0\n",
" xylim_high : float, opt\n",
" upper x and y limits of the plot, default 1\n",
" Returns\n",
" -------\n",
" \"\"\"\n",
"\n",
" plt.plot(\n",
" np.arange(xylim_low, xylim_high + 100, 100),\n",
" np.arange(xylim_low, xylim_high + 100, 100),\n",
" color=\"xkcd:blue grey\",\n",
" label=\"y=x\",\n",
" )\n",
"\n",
" for (label, model) in models.items():\n",
" gp_mu, gp_var = model.predict_f(x_data)\n",
" y_data_physical = y_data\n",
" gp_mu_physical = gp_mu\n",
" plt.scatter(y_data_physical, gp_mu_physical, label=label)\n",
" sumsqerr = np.sum((gp_mu_physical - y_data_physical.reshape(-1, 1)) ** 2)\n",
" print(\"Model: {}. Sum squared err: {:f}\".format(label, sumsqerr))\n",
"\n",
" plt.xlim(xylim_low, xylim_high)\n",
" plt.ylim(xylim_low, xylim_high)\n",
" plt.xlabel(\"Actual\")\n",
" plt.ylabel(\"Model Prediction\")\n",
" plt.legend()\n",
" ax = plt.gca()\n",
" ax.set_aspect(\"equal\", \"box\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 2,
"link": "[10.1.4 GPFlow Example](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4-GPFlow-Example)",
"section": "10.1.4 GPFlow Example"
}
},
"source": [
"## 10.1.4 GPFlow Example"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 2,
"link": "[10.1.4 GPFlow Example](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4-GPFlow-Example)",
"section": "10.1.4 GPFlow Example"
}
},
"source": [
"**Objective**: Use GP flow to predict output of $y = sin(x) + cos(x)$"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[10.1.4.1 Setup](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.1-Setup)",
"section": "10.1.4.1 Setup"
}
},
"source": [
"### 10.1.4.1 Setup"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.1.1 Step 1: Generate dataset](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.1.1-Step-1:-Generate-dataset)",
"section": "10.1.4.1.1 Step 1: Generate dataset"
}
},
"source": [
"#### 10.1.4.1.1 Step 1: Generate dataset"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.1.1 Step 1: Generate dataset](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.1.1-Step-1:-Generate-dataset)",
"section": "10.1.4.1.1 Step 1: Generate dataset"
}
},
"outputs": [],
"source": [
"#Specify number of samples\n",
"n = 25\n",
"\n",
"#Generate samples of x\n",
"x = np.random.rand(n,1)\n",
"\n",
"#Calculate y\n",
"y = np.sin(x) + np.cos(x)\n",
"\n",
"#Visualize\n",
"plt.plot(x,y,'.')\n",
"plt.xlabel('x')\n",
"plt.ylabel('y')"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.1.2 Step 2: Split into train/test sets](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.1.2-Step-2:-Split-into-train/test-sets)",
"section": "10.1.4.1.2 Step 2: Split into train/test sets"
}
},
"source": [
"#### 10.1.4.1.2 Step 2: Split into train/test sets"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.1.2 Step 2: Split into train/test sets](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.1.2-Step-2:-Split-into-train/test-sets)",
"section": "10.1.4.1.2 Step 2: Split into train/test sets"
}
},
"outputs": [],
"source": [
"#To use shuffle_and_split function, the data needs to be in a pandas dataframe\n",
"data = np.concatenate((x,y),axis=1)\n",
"data = pd.DataFrame(data,columns=['x','y'])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.1.2 Step 2: Split into train/test sets](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.1.2-Step-2:-Split-into-train/test-sets)",
"section": "10.1.4.1.2 Step 2: Split into train/test sets"
}
},
"outputs": [],
"source": [
"#Specify number of params\n",
"n_params = 1\n",
"\n",
"#Apply shuffle_and_split function\n",
"x_train, y_train, x_test, y_test = shuffle_and_split(data, n_params, fraction_train=0.8)"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.1.3 Step 3: Fit GP model](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.1.3-Step-3:-Fit-GP-model)",
"section": "10.1.4.1.3 Step 3: Fit GP model"
}
},
"source": [
"#### 10.1.4.1.3 Step 3: Fit GP model"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.1.3 Step 3: Fit GP model](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.1.3-Step-3:-Fit-GP-model)",
"section": "10.1.4.1.3 Step 3: Fit GP model"
}
},
"outputs": [],
"source": [
"# Fit model--using RBF kernel\n",
"model_RBF = run_gpflow_scipy(x_train, y_train, gpflow.kernels.RBF(lengthscales=np.ones(n_params)))\n",
"model = {'RBF': model_RBF}"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.1.4 Step 4: Compare Models](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.1.4-Step-4:-Compare-Models)",
"section": "10.1.4.1.4 Step 4: Compare Models"
}
},
"source": [
"#### 10.1.4.1.4 Step 4: Compare Models"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 5,
"link": "[10.1.4.1.4.1 Train](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.1.4.1-Train)",
"section": "10.1.4.1.4.1 Train"
}
},
"source": [
"##### 10.1.4.1.4.1 Train"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 5,
"link": "[10.1.4.1.4.1 Train](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.1.4.1-Train)",
"section": "10.1.4.1.4.1 Train"
}
},
"outputs": [],
"source": [
"plot_models(model, x_train, y_train,0,2)"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 5,
"link": "[10.1.4.1.4.2 Test](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.1.4.2-Test)",
"section": "10.1.4.1.4.2 Test"
}
},
"source": [
"##### 10.1.4.1.4.2 Test"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 5,
"link": "[10.1.4.1.4.2 Test](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.1.4.2-Test)",
"section": "10.1.4.1.4.2 Test"
}
},
"outputs": [],
"source": [
"plot_models(model, x_test, y_test,0,2)"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.1.5 Step 5: Analyze model predictions ](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.1.5-Step-5:-Analyze-model-predictions)",
"section": "10.1.4.1.5 Step 5: Analyze model predictions "
}
},
"source": [
"#### 10.1.4.1.5 Step 5: Analyze model predictions "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.1.5 Step 5: Analyze model predictions ](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.1.5-Step-5:-Analyze-model-predictions)",
"section": "10.1.4.1.5 Step 5: Analyze model predictions "
}
},
"outputs": [],
"source": [
"def plot_function(models,train,test):\n",
" \n",
" \"\"\"Plot the performance (mean and variance) of one or more GP models along with the original train and test data\n",
" Parameters\n",
" ----------\n",
" models : dict { label : model }\n",
" Each model to be plotted (value, GPFlow model) is provided\n",
" with a label (key, string)\n",
" train : np.array\n",
" array of training data, both and x and y values\n",
" test : np.ndarray\n",
" array of test data, both and x and y values\n",
" Returns\n",
" -------\n",
" \"\"\"\n",
" #x data samples\n",
" xx = np.linspace(0, 1.0, 100)[:,None]\n",
" \n",
" for (label, model) in models.items():\n",
" \n",
" #use model to predict output (y) given x data\n",
" mean, var = model.predict_f(xx)\n",
" #plot mean as line\n",
" plt.plot(xx, mean, lw=2, label=\"GP model\" + label)\n",
" #plot variance as a shaded area\n",
" plt.fill_between(\n",
" xx[:, 0],\n",
" mean[:, 0] - 1.96 * np.sqrt(var[:, 0]),\n",
" mean[:, 0] + 1.96 * np.sqrt(var[:, 0]),\n",
" alpha=0.25,\n",
" )\n",
"\n",
" #Plot training and testing points\n",
" if train.shape[0] > 0:\n",
" x_train = train[:, 0]\n",
" y_train = train[:, 1]\n",
" plt.plot(x_train, y_train, \"s\", color=\"black\", label=\"Train\")\n",
" if test.shape[0] > 0:\n",
" x_test = test[:, 0]\n",
" y_test = test[:, 1]\n",
" plt.plot(x_test, y_test, \"ro\", label=\"Test\")\n",
" \n",
" plt.xlabel(\"x\")\n",
" plt.ylabel(\"y\")\n",
" plt.legend()\n",
" plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.1.5 Step 5: Analyze model predictions ](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.1.5-Step-5:-Analyze-model-predictions)",
"section": "10.1.4.1.5 Step 5: Analyze model predictions "
}
},
"outputs": [],
"source": [
"#Make sure the y training and testing data has the correct shape\n",
"y_train.shape = (20,1)\n",
"y_test.shape = (5,1)\n",
"#Make arrays\n",
"train = np.concatenate((x_train,y_train),axis=1)\n",
"test = np.concatenate((x_test,y_test),axis=1)\n",
" \n",
"plot_function(model,train,test)"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[10.1.4.2 What happens if we use different kernels?](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.2-What-happens-if-we-use-different-kernels?)",
"section": "10.1.4.2 What happens if we use different kernels?"
}
},
"source": [
"### 10.1.4.2 What happens if we use different kernels?"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[10.1.4.2 What happens if we use different kernels?](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.2-What-happens-if-we-use-different-kernels?)",
"section": "10.1.4.2 What happens if we use different kernels?"
}
},
"source": [
"GP flow has many different available kernel functions: https://gpflow.readthedocs.io/en/master/gpflow/kernels/\n",
"\n",
"Here we will examine a few options:\n",
"\n",
"1. Constant\n",
"\n",
"$k(x,y) = \\sigma^2$\n",
"\n",
"where $\\sigma^2$ is the variance parameter\n",
"\n",
"2. Linear\n",
"\n",
"$k(x,y) = (\\sigma^2xy+\\gamma)^d$\n",
"\n",
"where $\\sigma^2$ is the variance parameter, $\\gamma$ is the offset parameter, and $d$ is the degree parameter\n",
"\n",
"3. Radial Basis Function\n",
"\n",
"$k(r) = \\sigma^2 exp[-\\frac{r^2}{2}]$\n",
"\n",
"where $r$ is the Euclidean distance and $\\sigma^2$ is the variance parameter\n",
"\n",
"4. Cosine\n",
"\n",
"$k(r) = \\sigma^2cos(2\\pi d)$\n",
"\n",
"where $r$ is the Euclidean distance, $\\sigma^2$ is the variance parameter, and $d$ is the sum of the per-dimension differences between the input points scaled by the lenghtscale parameter $l$ \n",
"\n",
"5. Matern12\n",
"\n",
"$k(r) = \\sigma^2exp[-r]$\n",
"\n",
"where $r$ is the Euclidean distance and $\\sigma^2$ is the variance parameter\n",
"\n",
"6. Matern32\n",
"\n",
"$k(r) = \\sigma^2(1+\\sqrt{3}r)exp[-\\sqrt{3}r]$\n",
"\n",
"where $r$ is the Euclidean distance and $\\sigma^2$ is the variance parameter\n",
"\n",
"6. Matern52\n",
"\n",
"$k(r) = \\sigma^2(1+\\sqrt{5}r+\\frac{5}{3}r^2)exp[-\\sqrt{5}r]$\n",
"\n",
"where $r$ is the Euclidean distance and $\\sigma^2$ is the variance parameter"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 3,
"link": "[10.1.4.2 What happens if we use different kernels?](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.2-What-happens-if-we-use-different-kernels?)",
"section": "10.1.4.2 What happens if we use different kernels?"
}
},
"outputs": [],
"source": [
"#Fit models using different kernels\n",
"model_constant = run_gpflow_scipy(x_train, y_train, gpflow.kernels.Constant())\n",
"model_linear = run_gpflow_scipy(x_train, y_train, gpflow.kernels.Linear())\n",
"model_RBF = run_gpflow_scipy(x_train, y_train, gpflow.kernels.RBF())\n",
"model_cosine = run_gpflow_scipy(x_train, y_train, gpflow.kernels.Cosine())\n",
"model_M12 = run_gpflow_scipy(x_train, y_train, gpflow.kernels.Matern12())\n",
"model_M32 = run_gpflow_scipy(x_train, y_train, gpflow.kernels.Matern32())\n",
"model_M52 = run_gpflow_scipy(x_train, y_train, gpflow.kernels.Matern52())"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.2.1 Constant](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.2.1-Constant)",
"section": "10.1.4.2.1 Constant"
}
},
"source": [
"#### 10.1.4.2.1 Constant"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.2.1 Constant](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.2.1-Constant)",
"section": "10.1.4.2.1 Constant"
}
},
"outputs": [],
"source": [
"model = {'Constant':model_constant}"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.2.1 Constant](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.2.1-Constant)",
"section": "10.1.4.2.1 Constant"
}
},
"source": [
"Train"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.2.1 Constant](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.2.1-Constant)",
"section": "10.1.4.2.1 Constant"
}
},
"outputs": [],
"source": [
"plot_models(model, x_train, y_train,0,2)"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.2.1 Constant](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.2.1-Constant)",
"section": "10.1.4.2.1 Constant"
}
},
"source": [
"Test"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.2.1 Constant](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.2.1-Constant)",
"section": "10.1.4.2.1 Constant"
}
},
"outputs": [],
"source": [
"plot_models(model, x_test, y_test,0,2)"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.2.2 Linear](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.2.2-Linear)",
"section": "10.1.4.2.2 Linear"
}
},
"source": [
"#### 10.1.4.2.2 Linear"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.2.2 Linear](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.2.2-Linear)",
"section": "10.1.4.2.2 Linear"
}
},
"outputs": [],
"source": [
"model = {'Linear':model_linear}"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.2.2 Linear](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.2.2-Linear)",
"section": "10.1.4.2.2 Linear"
}
},
"source": [
"Train"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.2.2 Linear](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.2.2-Linear)",
"section": "10.1.4.2.2 Linear"
}
},
"outputs": [],
"source": [
"plot_models(model, x_train, y_train,0,2)"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.2.2 Linear](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.2.2-Linear)",
"section": "10.1.4.2.2 Linear"
}
},
"source": [
"Test"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.2.2 Linear](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.2.2-Linear)",
"section": "10.1.4.2.2 Linear"
}
},
"outputs": [],
"source": [
"plot_models(model, x_test, y_test,0,2)"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.2.3 RBF](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.2.3-RBF)",
"section": "10.1.4.2.3 RBF"
}
},
"source": [
"#### 10.1.4.2.3 RBF"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.2.3 RBF](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.2.3-RBF)",
"section": "10.1.4.2.3 RBF"
}
},
"outputs": [],
"source": [
"model = {'RBF':model_RBF}"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.2.3 RBF](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.2.3-RBF)",
"section": "10.1.4.2.3 RBF"
}
},
"source": [
"Train"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.2.3 RBF](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.2.3-RBF)",
"section": "10.1.4.2.3 RBF"
}
},
"outputs": [],
"source": [
"plot_models(model, x_train, y_train,0,2)"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.2.3 RBF](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.2.3-RBF)",
"section": "10.1.4.2.3 RBF"
}
},
"source": [
"Test"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.2.3 RBF](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.2.3-RBF)",
"section": "10.1.4.2.3 RBF"
}
},
"outputs": [],
"source": [
"plot_models(model, x_test, y_test,0,2)"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.2.4 Cosine](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.2.4-Cosine)",
"section": "10.1.4.2.4 Cosine"
}
},
"source": [
"#### 10.1.4.2.4 Cosine"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.2.4 Cosine](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.2.4-Cosine)",
"section": "10.1.4.2.4 Cosine"
}
},
"outputs": [],
"source": [
"model = {'Cosine':model_cosine}"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.2.4 Cosine](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.2.4-Cosine)",
"section": "10.1.4.2.4 Cosine"
}
},
"source": [
"Train"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.2.4 Cosine](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.2.4-Cosine)",
"section": "10.1.4.2.4 Cosine"
}
},
"outputs": [],
"source": [
"plot_models(model, x_train, y_train,0,2)"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.2.4 Cosine](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.2.4-Cosine)",
"section": "10.1.4.2.4 Cosine"
}
},
"source": [
"Test"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.2.4 Cosine](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.2.4-Cosine)",
"section": "10.1.4.2.4 Cosine"
}
},
"outputs": [],
"source": [
"plot_models(model, x_test, y_test,0,2)"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.2.5 Matern](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.2.5-Matern)",
"section": "10.1.4.2.5 Matern"
}
},
"source": [
"#### 10.1.4.2.5 Matern"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.2.5 Matern](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.2.5-Matern)",
"section": "10.1.4.2.5 Matern"
}
},
"outputs": [],
"source": [
"model = {'Matern12':model_M12,'Matern32':model_M32,'Matern52':model_M52}"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.2.5 Matern](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.2.5-Matern)",
"section": "10.1.4.2.5 Matern"
}
},
"source": [
"Train"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.2.5 Matern](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.2.5-Matern)",
"section": "10.1.4.2.5 Matern"
}
},
"outputs": [],
"source": [
"plot_models(model, x_train, y_train,0,2)"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.2.5 Matern](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.2.5-Matern)",
"section": "10.1.4.2.5 Matern"
}
},
"source": [
"Test"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.2.5 Matern](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.2.5-Matern)",
"section": "10.1.4.2.5 Matern"
}
},
"outputs": [],
"source": [
"plot_models(model, x_test, y_test,0,2)"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.2.6 Analyze Model Predictions](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.2.6-Analyze-Model-Predictions)",
"section": "10.1.4.2.6 Analyze Model Predictions"
}
},
"source": [
"#### 10.1.4.2.6 Analyze Model Predictions"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.2.6 Analyze Model Predictions](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.2.6-Analyze-Model-Predictions)",
"section": "10.1.4.2.6 Analyze Model Predictions"
}
},
"source": [
"All Kernels"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.2.6 Analyze Model Predictions](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.2.6-Analyze-Model-Predictions)",
"section": "10.1.4.2.6 Analyze Model Predictions"
}
},
"outputs": [],
"source": [
"model = {'Constant':model_constant,'Linear':model_linear,'RBF':model_RBF,'Cosine':model_cosine,'Matern12':model_M12,'Matern32':model_M32,'Matern52':model_M52}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.2.6 Analyze Model Predictions](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.2.6-Analyze-Model-Predictions)",
"section": "10.1.4.2.6 Analyze Model Predictions"
}
},
"outputs": [],
"source": [
"plot_function(model,train,test)"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.2.6 Analyze Model Predictions](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.2.6-Analyze-Model-Predictions)",
"section": "10.1.4.2.6 Analyze Model Predictions"
}
},
"source": [
"RBF and Matern Kernels"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.2.6 Analyze Model Predictions](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.2.6-Analyze-Model-Predictions)",
"section": "10.1.4.2.6 Analyze Model Predictions"
}
},
"outputs": [],
"source": [
"model = {'RBF':model_RBF,'Matern12':model_M12,'Matern32':model_M32,'Matern52':model_M52}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.2.6 Analyze Model Predictions](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.2.6-Analyze-Model-Predictions)",
"section": "10.1.4.2.6 Analyze Model Predictions"
}
},
"outputs": [],
"source": [
"plot_function(model,train,test)"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.2.6 Analyze Model Predictions](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.2.6-Analyze-Model-Predictions)",
"section": "10.1.4.2.6 Analyze Model Predictions"
}
},
"source": [
"RBF, Matern32, and Matern52 Kernels\n",
"\n",
"Note: All three of these kernels give very small variances."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.2.6 Analyze Model Predictions](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.2.6-Analyze-Model-Predictions)",
"section": "10.1.4.2.6 Analyze Model Predictions"
}
},
"outputs": [],
"source": [
"model = {'RBF':model_RBF,'Matern32':model_M32,'Matern52':model_M52}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.2.6 Analyze Model Predictions](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.2.6-Analyze-Model-Predictions)",
"section": "10.1.4.2.6 Analyze Model Predictions"
}
},
"outputs": [],
"source": [
"plot_function(model,train,test)"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[10.1.4.3 What happens if we use different train/test fractions?](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.3-What-happens-if-we-use-different-train/test-fractions?)",
"section": "10.1.4.3 What happens if we use different train/test fractions?"
}
},
"source": [
"### 10.1.4.3 What happens if we use different train/test fractions?\n",
"\n",
"The example above used an 80/20 train/test split. How much data do we need to train with using the RBF and Matern kernels?"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.3.1 50/50 Split](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.3.1-50/50-Split)",
"section": "10.1.4.3.1 50/50 Split"
}
},
"source": [
"#### 10.1.4.3.1 50/50 Split"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.3.1 50/50 Split](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.3.1-50/50-Split)",
"section": "10.1.4.3.1 50/50 Split"
}
},
"outputs": [],
"source": [
"x_train, y_train, x_test, y_test = shuffle_and_split(data, n_params, fraction_train=0.5)\n",
"# Fit model\n",
"model_RBF = run_gpflow_scipy(x_train, y_train, gpflow.kernels.RBF(lengthscales=np.ones(n_params)))\n",
"model_M12 = run_gpflow_scipy(x_train, y_train, gpflow.kernels.Matern12())\n",
"model_M32 = run_gpflow_scipy(x_train, y_train, gpflow.kernels.Matern32())\n",
"model_M52 = run_gpflow_scipy(x_train, y_train, gpflow.kernels.Matern52())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.3.1 50/50 Split](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.3.1-50/50-Split)",
"section": "10.1.4.3.1 50/50 Split"
}
},
"outputs": [],
"source": [
"model = {'RBF':model_RBF,'Matern12':model_M12,'Matern32':model_M32,'Matern52':model_M52}\n",
"\n",
"y_train.shape = (12,1)\n",
"y_test.shape = (13,1)\n",
"train = np.concatenate((x_train,y_train),axis=1)\n",
"test = np.concatenate((x_test,y_test),axis=1)\n",
" \n",
"plot_function(model,train,test)"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.3.2 25/75 Split](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.3.2-25/75-Split)",
"section": "10.1.4.3.2 25/75 Split"
}
},
"source": [
"#### 10.1.4.3.2 25/75 Split"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.3.2 25/75 Split](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.3.2-25/75-Split)",
"section": "10.1.4.3.2 25/75 Split"
}
},
"outputs": [],
"source": [
"x_train, y_train, x_test, y_test = shuffle_and_split(data, n_params, fraction_train=0.25)\n",
"# Fit model\n",
"model_RBF = run_gpflow_scipy(x_train, y_train, gpflow.kernels.RBF(lengthscales=np.ones(n_params)))\n",
"model_M12 = run_gpflow_scipy(x_train, y_train, gpflow.kernels.Matern12())\n",
"model_M32 = run_gpflow_scipy(x_train, y_train, gpflow.kernels.Matern32())\n",
"model_M52 = run_gpflow_scipy(x_train, y_train, gpflow.kernels.Matern52())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.3.2 25/75 Split](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.3.2-25/75-Split)",
"section": "10.1.4.3.2 25/75 Split"
}
},
"outputs": [],
"source": [
"model = {'RBF':model_RBF,'Matern12':model_M12,'Matern32':model_M32,'Matern52':model_M52}\n",
"\n",
"y_train.shape = (6,1)\n",
"y_test.shape = (19,1)\n",
"train = np.concatenate((x_train,y_train),axis=1)\n",
"test = np.concatenate((x_test,y_test),axis=1)\n",
" \n",
"plot_function(model,train,test)"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.3.3 10/90 Split](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.3.3-10/90-Split)",
"section": "10.1.4.3.3 10/90 Split"
}
},
"source": [
"#### 10.1.4.3.3 10/90 Split"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.3.3 10/90 Split](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.3.3-10/90-Split)",
"section": "10.1.4.3.3 10/90 Split"
}
},
"outputs": [],
"source": [
"x_train, y_train, x_test, y_test = shuffle_and_split(data, n_params, fraction_train=0.1)\n",
"# Fit model\n",
"model_RBF = run_gpflow_scipy(x_train, y_train, gpflow.kernels.RBF(lengthscales=np.ones(n_params)))\n",
"model_M12 = run_gpflow_scipy(x_train, y_train, gpflow.kernels.Matern12())\n",
"model_M32 = run_gpflow_scipy(x_train, y_train, gpflow.kernels.Matern32())\n",
"model_M52 = run_gpflow_scipy(x_train, y_train, gpflow.kernels.Matern52())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 4,
"link": "[10.1.4.3.3 10/90 Split](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.4.3.3-10/90-Split)",
"section": "10.1.4.3.3 10/90 Split"
}
},
"outputs": [],
"source": [
"model = {'RBF':model_RBF,'Matern12':model_M12,'Matern32':model_M32,'Matern52':model_M52}\n",
"\n",
"y_train.shape = (2,1)\n",
"y_test.shape = (23,1)\n",
"train = np.concatenate((x_train,y_train),axis=1)\n",
"test = np.concatenate((x_test,y_test),axis=1)\n",
" \n",
"plot_function(model,train,test)"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 2,
"link": "[10.1.5 Apply GPflow code to Breakout Time Dataset](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.5-Apply-GPflow-code-to-Breakout-Time-Dataset)",
"section": "10.1.5 Apply GPflow code to Breakout Time Dataset"
}
},
"source": [
"## 10.1.5 Apply GPflow code to Breakout Time Dataset"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 2,
"link": "[10.1.5 Apply GPflow code to Breakout Time Dataset](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.5-Apply-GPflow-code-to-Breakout-Time-Dataset)",
"section": "10.1.5 Apply GPflow code to Breakout Time Dataset"
}
},
"source": [
"**Objective**: Use GPflow to predict breakout time given five parameters (thickness, laser energy, Be gamma, wall opacity, and flux limiter)\n",
"\n",
"Kernels: RBF and Matern"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[10.1.5.1 Load in Dataset as a dataframe](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.5.1-Load-in-Dataset-as-a-dataframe)",
"section": "10.1.5.1 Load in Dataset as a dataframe"
}
},
"source": [
"### 10.1.5.1 Load in Dataset as a dataframe"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 3,
"link": "[10.1.5.1 Load in Dataset as a dataframe](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.5.1-Load-in-Dataset-as-a-dataframe)",
"section": "10.1.5.1 Load in Dataset as a dataframe"
}
},
"outputs": [],
"source": [
"csv_path = '/scratch365/bbefort/DS_Seminar_2020/CRASHBreakout.csv'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 3,
"link": "[10.1.5.1 Load in Dataset as a dataframe](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.5.1-Load-in-Dataset-as-a-dataframe)",
"section": "10.1.5.1 Load in Dataset as a dataframe"
}
},
"outputs": [],
"source": [
"df = pd.read_csv(csv_path)\n",
"df = df.drop(['cmeasure.1','measure.2','measure.3'],axis=1)\n",
"df.columns = [\"thickness\",\n",
" \"laser_energy\",\n",
" \"Be_gamma\",\n",
" \"wall_opacity\",\n",
" \"flux_limiter\",\n",
" \"breakout_time\"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 3,
"link": "[10.1.5.1 Load in Dataset as a dataframe](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.5.1-Load-in-Dataset-as-a-dataframe)",
"section": "10.1.5.1 Load in Dataset as a dataframe"
}
},
"outputs": [],
"source": [
"pd.options.display.max_rows=104\n",
"df"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[10.1.5.2 Split into training and test sets and fit GP model without normalizing](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.5.2-Split-into-training-and-test-sets-and-fit-GP-model-without-normalizing)",
"section": "10.1.5.2 Split into training and test sets and fit GP model without normalizing"
}
},
"source": [
"### 10.1.5.2 Split into training and test sets and fit GP model without normalizing\n",
"\n",
"I was curious how this would look"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 3,
"link": "[10.1.5.2 Split into training and test sets and fit GP model without normalizing](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.5.2-Split-into-training-and-test-sets-and-fit-GP-model-without-normalizing)",
"section": "10.1.5.2 Split into training and test sets and fit GP model without normalizing"
}
},
"outputs": [],
"source": [
"n_params=5\n",
"x_train, y_train, x_test, y_test = shuffle_and_split(df, 5, fraction_train=0.8)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 3,
"link": "[10.1.5.2 Split into training and test sets and fit GP model without normalizing](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.5.2-Split-into-training-and-test-sets-and-fit-GP-model-without-normalizing)",
"section": "10.1.5.2 Split into training and test sets and fit GP model without normalizing"
}
},
"outputs": [],
"source": [
"# Fit model\n",
"model_RBF = run_gpflow_scipy(x_train, y_train, gpflow.kernels.RBF(lengthscales=np.ones(n_params)))\n",
"model_M12 = run_gpflow_scipy(x_train, y_train, gpflow.kernels.Matern12(lengthscales=np.ones(n_params)))\n",
"model_M32 = run_gpflow_scipy(x_train, y_train, gpflow.kernels.Matern32(lengthscales=np.ones(n_params)))\n",
"model_M52 = run_gpflow_scipy(x_train, y_train, gpflow.kernels.Matern52(lengthscales=np.ones(n_params)))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 3,
"link": "[10.1.5.2 Split into training and test sets and fit GP model without normalizing](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.5.2-Split-into-training-and-test-sets-and-fit-GP-model-without-normalizing)",
"section": "10.1.5.2 Split into training and test sets and fit GP model without normalizing"
}
},
"outputs": [],
"source": [
"model = {'RBF':model_RBF,'Matern12':model_M12,'Matern32':model_M32,'Matern52':model_M52}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 3,
"link": "[10.1.5.2 Split into training and test sets and fit GP model without normalizing](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.5.2-Split-into-training-and-test-sets-and-fit-GP-model-without-normalizing)",
"section": "10.1.5.2 Split into training and test sets and fit GP model without normalizing"
}
},
"outputs": [],
"source": [
"plot_models(model, x_train, y_train,250,550)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 3,
"link": "[10.1.5.2 Split into training and test sets and fit GP model without normalizing](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.5.2-Split-into-training-and-test-sets-and-fit-GP-model-without-normalizing)",
"section": "10.1.5.2 Split into training and test sets and fit GP model without normalizing"
}
},
"outputs": [],
"source": [
"plot_models(model, x_test, y_test,250,550)"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[10.1.5.2 Split into training and test sets and fit GP model without normalizing](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.5.2-Split-into-training-and-test-sets-and-fit-GP-model-without-normalizing)",
"section": "10.1.5.2 Split into training and test sets and fit GP model without normalizing"
}
},
"source": [
"Observation: Without scaling, we can see that none of the GP models give good predictions."
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[10.1.5.3 Scaled data](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.5.3-Scaled-data)",
"section": "10.1.5.3 Scaled data"
}
},
"source": [
"### 10.1.5.3 Scaled data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 3,
"link": "[10.1.5.3 Scaled data](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.5.3-Scaled-data)",
"section": "10.1.5.3 Scaled data"
}
},
"outputs": [],
"source": [
"n_params=5\n",
"\n",
"#Split dataframe into parameters and outputs to do the normalization\n",
"param_values = df.values[:, :n_params]\n",
"breakout_time_values = df.values[:, n_params]"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[10.1.5.3 Scaled data](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.5.3-Scaled-data)",
"section": "10.1.5.3 Scaled data"
}
},
"source": [
"Define functions to scale data and apply"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 3,
"link": "[10.1.5.3 Scaled data](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.5.3-Scaled-data)",
"section": "10.1.5.3 Scaled data"
}
},
"outputs": [],
"source": [
"def param_bounds():\n",
" \"\"\"Return parameter bounds\"\"\"\n",
"\n",
" #units: mm\n",
" bounds_thickness = ( np.asarray(\n",
" [[ 17.5, 22.5 ]]\n",
" \n",
" ))\n",
"\n",
" #units: kJ/mol\n",
" bounds_laser_energy = (np.asarray(\n",
" [[ 3650. , 4000. ]\n",
" ]\n",
" ))\n",
" \n",
" bounds_Be_gamma = (np.asarray(\n",
" [[ 1.35 , 1.8 ]\n",
" ]\n",
" ))\n",
" \n",
" bounds_wall_opacity = (np.asarray(\n",
" [[ 0.65 , 1.35 ]\n",
" ]\n",
" ))\n",
" \n",
" bounds_flux_limiter = (np.asarray(\n",
" [[ 0.045 , 0.08 ]\n",
" ]\n",
" ))\n",
"\n",
" bounds = np.vstack((bounds_thickness,bounds_laser_energy,bounds_Be_gamma,bounds_wall_opacity,bounds_flux_limiter))\n",
"\n",
" return bounds"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 3,
"link": "[10.1.5.3 Scaled data](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.5.3-Scaled-data)",
"section": "10.1.5.3 Scaled data"
}
},
"outputs": [],
"source": [
"def params_real_to_scaled(params, bounds):\n",
" \"\"\"Convert sample with physical units to values between 0 and 1\"\"\"\n",
" return (params - bounds[:, 0]) / (bounds[:, 1] - bounds[:, 0])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 3,
"link": "[10.1.5.3 Scaled data](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.5.3-Scaled-data)",
"section": "10.1.5.3 Scaled data"
}
},
"outputs": [],
"source": [
"scaled_param_values = params_real_to_scaled(param_values, param_bounds())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 3,
"link": "[10.1.5.3 Scaled data](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.5.3-Scaled-data)",
"section": "10.1.5.3 Scaled data"
}
},
"outputs": [],
"source": [
"def breakout_time_bounds():\n",
" \"\"\"Return the bounds on breakout time in units of ps\"\"\"\n",
"\n",
" bounds = ( np.asarray(\n",
" [ 305.013, 520.389 ],\n",
" ))\n",
"\n",
" return bounds"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 3,
"link": "[10.1.5.3 Scaled data](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.5.3-Scaled-data)",
"section": "10.1.5.3 Scaled data"
}
},
"outputs": [],
"source": [
"def values_real_to_scaled(values, bounds):\n",
" \"\"\"Convert breakout time with physical units to value between 0 and 1\"\"\"\n",
" return (values - bounds[0]) / (bounds[1] - bounds[0])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 3,
"link": "[10.1.5.3 Scaled data](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.5.3-Scaled-data)",
"section": "10.1.5.3 Scaled data"
}
},
"outputs": [],
"source": [
"scaled_breakout_time_values = values_real_to_scaled(breakout_time_values, breakout_time_bounds())"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[10.1.5.3 Scaled data](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.5.3-Scaled-data)",
"section": "10.1.5.3 Scaled data"
}
},
"source": [
"After scaling, combine into a new dataframe."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 3,
"link": "[10.1.5.3 Scaled data](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.5.3-Scaled-data)",
"section": "10.1.5.3 Scaled data"
}
},
"outputs": [],
"source": [
"scaled_data = np.hstack((scaled_param_values,\n",
" scaled_breakout_time_values.reshape(-1,1)\n",
" ))\n",
"\n",
"column_names = [\"thickness\",\n",
" \"laser_energy\",\n",
" \"Be_gamma\",\n",
" \"wall_opacity\",\n",
" \"flux_limiter\",\n",
" \"breakout_time\"]\n",
"\n",
"df_scaled = pd.DataFrame(scaled_data, columns=column_names)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 3,
"link": "[10.1.5.3 Scaled data](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.5.3-Scaled-data)",
"section": "10.1.5.3 Scaled data"
}
},
"outputs": [],
"source": [
"pd.options.display.max_rows=104\n",
"df_scaled"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[10.1.5.3 Scaled data](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.5.3-Scaled-data)",
"section": "10.1.5.3 Scaled data"
}
},
"source": [
"Split into training and test sets and fit GP model"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 3,
"link": "[10.1.5.3 Scaled data](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.5.3-Scaled-data)",
"section": "10.1.5.3 Scaled data"
}
},
"outputs": [],
"source": [
"n_params=5\n",
"x_train, y_train, x_test, y_test = shuffle_and_split(df_scaled, 5, fraction_train=0.8)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 3,
"link": "[10.1.5.3 Scaled data](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.5.3-Scaled-data)",
"section": "10.1.5.3 Scaled data"
}
},
"outputs": [],
"source": [
"# Fit model\n",
"model_RBF = run_gpflow_scipy(x_train, y_train, gpflow.kernels.RBF(lengthscales=np.ones(n_params)))\n",
"model_M12 = run_gpflow_scipy(x_train, y_train, gpflow.kernels.Matern12(lengthscales=np.ones(n_params)))\n",
"model_M32 = run_gpflow_scipy(x_train, y_train, gpflow.kernels.Matern32(lengthscales=np.ones(n_params)))\n",
"model_M52 = run_gpflow_scipy(x_train, y_train, gpflow.kernels.Matern52(lengthscales=np.ones(n_params)))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 3,
"link": "[10.1.5.3 Scaled data](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.5.3-Scaled-data)",
"section": "10.1.5.3 Scaled data"
}
},
"outputs": [],
"source": [
"model = {'RBF':model_RBF,'Matern12':model_M12,'Matern32':model_M32,'Matern52':model_M52}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 3,
"link": "[10.1.5.3 Scaled data](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.5.3-Scaled-data)",
"section": "10.1.5.3 Scaled data"
}
},
"outputs": [],
"source": [
"plot_models(model, x_train, y_train,0,1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 3,
"link": "[10.1.5.3 Scaled data](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.5.3-Scaled-data)",
"section": "10.1.5.3 Scaled data"
}
},
"outputs": [],
"source": [
"plot_models(model, x_test, y_test,0,1)"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[10.1.5.3 Scaled data](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.5.3-Scaled-data)",
"section": "10.1.5.3 Scaled data"
}
},
"source": [
"Observation: GP predictions improve when scaling is used. In this case, Matern12 gives the lowest sum of squares error."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 3,
"link": "[10.1.5.3 Scaled data](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.01-Contributed-Example.html#10.1.5.3-Scaled-data)",
"section": "10.1.5.3 Scaled data"
}
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"< [10.0 Gaussian Process Emulators and Surrogate Models](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.00-Gaussian-Process-Emulators-and-Surrogate-Models.html) | [Contents](toc.html) | [10.2 A simple example of Bayesian quadrature](https://ndcbe.github.io/cbe67701-uncertainty-quantification/10.02-Bayesian-quadrature.html)
"
]
}
],
"metadata": {
"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.6.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
}