{ "cells": [ { "cell_type": "markdown", "id": "50176287", "metadata": {}, "source": [ "# 1D parameter estimation using maximum likelihood estimation\n", "\n", "This example will cover:\n", "\n", " - Use maximum likelihood estimation to optimise kernel parameters" ] }, { "cell_type": "code", "execution_count": 1, "id": "ba0576fc", "metadata": {}, "outputs": [], "source": [ "from gptide import cov\n", "from gptide import GPtideScipy\n", "import numpy as np\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "markdown", "id": "1e300d22", "metadata": {}, "source": [ "## Generate some data" ] }, { "cell_type": "markdown", "id": "bef3bf50", "metadata": {}, "source": [ "Start off with the same kernel as Example 1 and generate some data." ] }, { "cell_type": "code", "execution_count": 6, "id": "0b153f56", "metadata": {}, "outputs": [], "source": [ "####\n", "# These are our kernel input parameters\n", "np.random.seed(1)\n", "noise = 0.5\n", "η = 1.5\n", "ℓ = 100\n", "covfunc = cov.expquad_1d\n", "\n", "###\n", "# Domain size parameters\n", "dx = 25.\n", "N = 100\n", "covparams = (η, ℓ)\n", "\n", "# Input data points\n", "xd = np.arange(0,dx*N,dx)[:,None]\n", "\n", "GP = GPtideScipy(xd, xd, noise, covfunc, covparams)\n", "\n", "# Use the .prior() method to obtain some samples\n", "yd = GP.prior(samples=1)\n" ] }, { "cell_type": "markdown", "id": "e66b5d8e", "metadata": {}, "source": [ "## Inference" ] }, { "cell_type": "markdown", "id": "0197a84e", "metadata": {}, "source": [ "We now use the `gptide.mle` function do the parameter estimation. This calls the `scipy.optimize.minimize` routine.\n" ] }, { "cell_type": "code", "execution_count": 7, "id": "bbd4da06", "metadata": {}, "outputs": [], "source": [ "# mle function import\n", "from gptide import mle" ] }, { "cell_type": "code", "execution_count": 10, "id": "561a3727", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Noise (true): 0.50, |Noise| (mle): 0.45\n", "ℓ (true): 1.50, ℓ (mle): 1.21\n", "η (true): 100.00, η (mle): 95.02\n" ] } ], "source": [ "# Initial guess of the noise and covariance parameters (these can matter)\n", "noise_ic = 0.01\n", "covparams_ic = [1., 10.]\n", "\n", "# There is no mean function in this case\n", "# meanfunc = None\n", "# meanparams_ic = ()\n", "\n", "\n", "soln = mle(\n", " xd, yd, \n", " covfunc, \n", " covparams_ic,\n", " noise_ic,\n", " verbose=False)\n", "\n", "print('Noise (true): {:3.2f}, |Noise| (mle): {:3.2f}'.format(noise, abs(soln['x'][0])))\n", "print('ℓ (true): {:3.2f}, ℓ (mle): {:3.2f}'.format(covparams[0], soln['x'][1]))\n", "print('η (true): {:3.2f}, η (mle): {:3.2f}'.format(covparams[1], soln['x'][2]))" ] } ], "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.10.4" } }, "nbformat": 4, "nbformat_minor": 5 }