From 1e02ee8e6db9557e6f69f0a821c9ab398d1adcf6 Mon Sep 17 00:00:00 2001 From: Neil Lawrence Date: Wed, 25 Nov 2026 15:24:12 +0000 Subject: [PATCH 1/2] Fix HMC notebook priors for inferred X and noise. Add a Gaussian prior on infer_newX latents (GP gives p(Y|X), not p(X)) and a Gamma prior on marathon noise so every unfixed parameter is priorised before HMC. --- GPy/sampling_hmc.ipynb | 924 +++++++++++++++++++++-------------------- 1 file changed, 464 insertions(+), 460 deletions(-) diff --git a/GPy/sampling_hmc.ipynb b/GPy/sampling_hmc.ipynb index f638e99..e5ced3d 100644 --- a/GPy/sampling_hmc.ipynb +++ b/GPy/sampling_hmc.ipynb @@ -1,461 +1,465 @@ { - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#A Brief Guide of Using Hybrid Monte Carlo for Gaussian Process Models\n", - "\n", - "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/SheffieldML/notebook/blob/main/GPy/sampling_hmc.ipynb)\n", - "### Zhenwen Dai and Mu Niu, November 2014\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "1", - "metadata": {}, - "outputs": [], - "source": [ - "try:\n", - " import GPy\n", - "except ImportError:\n", - " import subprocess\n", - " import sys\n", - " subprocess.check_call(\n", - " [sys.executable, \"-m\", \"pip\", \"install\", \"-q\", \"gpy[plotting]>=1.15.0\"]\n", - " )\n", - " import GPy\n", - "try:\n", - " import seaborn\n", - "except ImportError:\n", - " import subprocess\n", - " import sys\n", - " subprocess.check_call(\n", - " [sys.executable, \"-m\", \"pip\", \"install\", \"-q\", 'seaborn']\n", - " )\n", - " import seaborn\n", - "\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "from matplotlib import pyplot as plt\n", - "import numpy as np\n", - "import GPy\n", - "#%config InlineBackend.figure_format = 'svg'\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Example 1 HMC Inference for GP Regression" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let's first generate some synthetic data." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Let's make some synthetic data\n", - "x = np.linspace(0.,2*np.pi,100)[:,None]\n", - "y = -np.cos(x)+np.random.randn(*x.shape)*0.3+1\n", - "_ = plt.plot(x,y,'.')\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let us Make a GP Regression model and give some general prior distributions to model parameters." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Make a GP regression model\n", - "m = GPy.models.GPRegression(x,y)\n", - "# Give some general prior distributions for model parameters\n", - "m.kern.lengthscale.set_prior(GPy.priors.Gamma.from_EV(1.,10.))\n", - "m.kern.variance.set_prior(GPy.priors.Gamma.from_EV(1.,10.))\n", - "m.likelihood.variance.set_prior(GPy.priors.Gamma.from_EV(1.,10.))\n", - "_=m.plot()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "###Sampling the posterior distribution of model parameters" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Draw 1000 samples from the GP model" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "hmc = GPy.inference.mcmc.HMC(m,stepsize=5e-2)\n", - "s = hmc.sample(num_samples=1000) # Burnin\n", - "s = hmc.sample(num_samples=1000)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Plot the samples:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "plt.plot(s)\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Plot the posterior marginal distribution of model parameters:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "labels = ['kern variance', 'kern lengthscale','noise variance']\n", - "samples = s[300:] # cut out the burn-in period\n", - "from scipy import stats\n", - "xmin = samples.min()\n", - "xmax = samples.max()\n", - "xs = np.linspace(xmin,xmax,100)\n", - "for i in range(samples.shape[1]):\n", - " kernel = stats.gaussian_kde(samples[:,i])\n", - " plt.plot(xs,kernel(xs),label=labels[i])\n", - "_ = plt.legend()\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Plot the model parameters (lengthscale, variance and noise variance) against each other:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "fig = plt.figure(figsize=(14,4))\n", - "ax = fig.add_subplot(131)\n", - "_=ax.plot(samples[:,0],samples[:,1],'.')\n", - "ax.set_xlabel(labels[0]); ax.set_ylabel(labels[1])\n", - "ax = fig.add_subplot(132)\n", - "_=ax.plot(samples[:,1],samples[:,2],'.')\n", - "ax.set_xlabel(labels[1]); ax.set_ylabel(labels[2])\n", - "ax = fig.add_subplot(133)\n", - "_=ax.plot(samples[:,0],samples[:,2],'.')\n", - "ax.set_xlabel(labels[0]); ax.set_ylabel(labels[2])\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "By setting the model parameters to the posterior mean, we can visualize the model fit:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Set the model parameters as the posterior mean\n", - "m.kern.variance[:] = samples[:,0].mean()\n", - "m.kern.lengthscale[:] = samples[:,1].mean()\n", - "m.likelihood.variance[:] = samples[:,2].mean()\n", - "print(m)\n", - "_=m.plot()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Sample the posterior distribution of X given some new Y" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Given some new observations, inferring the posterior distribution of the corresponding inputs is difficult, because it can lead to multi-modal distributions. " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Assume we have a new observation $1.5$, and try to infer its input distribution." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "y_new = np.array([1.5])[:,None]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Generate the inference model for the new observations. X_new are the MAP estimations by optimizing the log likelihood. As plotted with a red dot, the MAP estimation corresponds to only one of the modes." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "x_new,mi = m.infer_newX(y_new)\n", - "print(mi)\n", - "m.plot()\n", - "plt.plot(x_new,y_new,'or')\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Draw 10,000 samples from the inference model:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "hmc_new = GPy.inference.mcmc.HMC(mi,stepsize=2e-1)\n", - "s_new = hmc_new.sample(num_samples=10000,hmc_iters=10)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Plot the samples:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "_ = plt.plot(s_new[:,:])\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Plot the marginal distribution of inferred inputs. The two modes of inputs are clearly visible from the sampled posterior distribution." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "from scipy import stats\n", - "samples_new = s_new[:]\n", - "xmin = samples_new.min()\n", - "xmax = samples_new.max()\n", - "xs = np.linspace(xmin,xmax,100)\n", - "for i in range(samples_new.shape[1]):\n", - " kernel = stats.gaussian_kde(samples_new[:,i])\n", - " plt.plot(xs,kernel(xs))\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Example 2 HMC for lengthscale and variance with marathon data" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "##### we set prior for lengthscale and variance of kernel. The mean of the prior is close to the result of GP optimisation. we then allow a big variance. In the case below,we set gamma prior to lengthscale and variance. E(lengthscale) = 120, Var(lengthscale)=2000, E(variance) = 25, Var(variance) = 150 " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "m = GPy.examples.regression.olympic_marathon_men()\n", - "#\n", - "#set prior for lengthscale and variance.\n", - "m.kern.variance.set_prior(GPy.priors.Gamma.from_EV(25.,150.))\n", - "m.kern.lengthscale.set_prior(GPy.priors.Gamma.from_EV(120.,2000.))\n", - "print(m)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Run HMC" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "##### we plot the full length of hmc iteration. The first 5000 could be burn in stage and can be ignored. " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# initialise hmc\n", - "hmc = GPy.inference.mcmc.HMC(m,stepsize=2e-1)\n", - "# run hmc\n", - "t = hmc.sample(num_samples=20000,hmc_iters=20)\n", - "\n", - "# Sample parameters \n", - "#hmc = GPy.inference.optimization.HMC(m, stepsize=5e-1)\n", - "#t = hmc.sample(m_iters=50000,hmc_iters=20)\n", - "\n", - "_=plt.plot(t)\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "print(t.mean(axis=0))\n", - "print(t.std(axis=0))\n", - "_=plt.hist(t[:,:2],50)\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Seaborn\n", - "Using Seaborn for plotting distributions over Hyperparameters:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "import seaborn as sns, pandas as pd\n", - "plt.rcParams['text.usetex'] = False" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "df = pd.DataFrame(t, columns=m.parameter_names_flat())\n", - "var_col = next(c for c in df.columns if c.endswith('rbf.variance'))\n", - "ls_col = next(c for c in df.columns if c.endswith('rbf.lengthscale'))\n", - "ax = sns.kdeplot(df[var_col], color=\"b\", fill=True)\n", - "ax = sns.kdeplot(df[ls_col], color=\"r\", fill=True)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "sns.set(style=\"white\", color_codes=True)\n", - "_ = sns.jointplot(data=df, x=var_col, y=ls_col, kind=\"hex\")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "df" - ] - } - ], - "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.12.0" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#A Brief Guide of Using Hybrid Monte Carlo for Gaussian Process Models\n", + "\n", + "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/SheffieldML/notebook/blob/main/GPy/sampling_hmc.ipynb)\n", + "### Zhenwen Dai and Mu Niu, November 2014\n" + ] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "try:\n", + " import GPy\n", + "except ImportError:\n", + " import subprocess\n", + " import sys\n", + " subprocess.check_call(\n", + " [sys.executable, \"-m\", \"pip\", \"install\", \"-q\", \"gpy[plotting]>=1.15.0\"]\n", + " )\n", + " import GPy\n", + "try:\n", + " import seaborn\n", + "except ImportError:\n", + " import subprocess\n", + " import sys\n", + " subprocess.check_call(\n", + " [sys.executable, \"-m\", \"pip\", \"install\", \"-q\", 'seaborn']\n", + " )\n", + " import seaborn\n", + "\n" + ], + "execution_count": null, + "outputs": [], + "id": "1" + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "from matplotlib import pyplot as plt\n", + "import numpy as np\n", + "import GPy\n", + "#%config InlineBackend.figure_format = 'svg'\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 1 HMC Inference for GP Regression" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's first generate some synthetic data." + ] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "# Let's make some synthetic data\n", + "x = np.linspace(0.,2*np.pi,100)[:,None]\n", + "y = -np.cos(x)+np.random.randn(*x.shape)*0.3+1\n", + "_ = plt.plot(x,y,'.')\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let us Make a GP Regression model and give some general prior distributions to model parameters." + ] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "# Make a GP regression model\n", + "m = GPy.models.GPRegression(x,y)\n", + "# Give some general prior distributions for model parameters\n", + "m.kern.lengthscale.set_prior(GPy.priors.Gamma.from_EV(1.,10.))\n", + "m.kern.variance.set_prior(GPy.priors.Gamma.from_EV(1.,10.))\n", + "m.likelihood.variance.set_prior(GPy.priors.Gamma.from_EV(1.,10.))\n", + "_=m.plot()" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "###Sampling the posterior distribution of model parameters" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Draw 1000 samples from the GP model" + ] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "hmc = GPy.inference.mcmc.HMC(m,stepsize=5e-2)\n", + "s = hmc.sample(num_samples=1000) # Burnin\n", + "s = hmc.sample(num_samples=1000)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Plot the samples:" + ] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "plt.plot(s)\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Plot the posterior marginal distribution of model parameters:" + ] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "labels = ['kern variance', 'kern lengthscale','noise variance']\n", + "samples = s[300:] # cut out the burn-in period\n", + "from scipy import stats\n", + "xmin = samples.min()\n", + "xmax = samples.max()\n", + "xs = np.linspace(xmin,xmax,100)\n", + "for i in range(samples.shape[1]):\n", + " kernel = stats.gaussian_kde(samples[:,i])\n", + " plt.plot(xs,kernel(xs),label=labels[i])\n", + "_ = plt.legend()\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Plot the model parameters (lengthscale, variance and noise variance) against each other:" + ] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "fig = plt.figure(figsize=(14,4))\n", + "ax = fig.add_subplot(131)\n", + "_=ax.plot(samples[:,0],samples[:,1],'.')\n", + "ax.set_xlabel(labels[0]); ax.set_ylabel(labels[1])\n", + "ax = fig.add_subplot(132)\n", + "_=ax.plot(samples[:,1],samples[:,2],'.')\n", + "ax.set_xlabel(labels[1]); ax.set_ylabel(labels[2])\n", + "ax = fig.add_subplot(133)\n", + "_=ax.plot(samples[:,0],samples[:,2],'.')\n", + "ax.set_xlabel(labels[0]); ax.set_ylabel(labels[2])\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "By setting the model parameters to the posterior mean, we can visualize the model fit:" + ] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "# Set the model parameters as the posterior mean\n", + "m.kern.variance[:] = samples[:,0].mean()\n", + "m.kern.lengthscale[:] = samples[:,1].mean()\n", + "m.likelihood.variance[:] = samples[:,2].mean()\n", + "print(m)\n", + "_=m.plot()" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Sample the posterior distribution of X given some new Y" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Given some new observations, inferring the posterior distribution of the corresponding inputs is difficult, because it can lead to multi-modal distributions. " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Assume we have a new observation $1.5$, and try to infer its input distribution." + ] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "y_new = np.array([1.5])[:,None]" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Generate the inference model for the new observations. The GP supplies $p(Y\\mid X)$ through the covariance evaluated at the unknown inputs $X$; by default `infer_newX` has a flat prior on $X$, so the red-dot MAP is a mode of the likelihood only. That MAP often sits on just one of several modes." + ] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "x_new,mi = m.infer_newX(y_new)\n", + "print(mi)\n", + "m.plot()\n", + "plt.plot(x_new,y_new,'or')\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "For HMC we need a proper prior on the inputs $X$ as well (the covariance Gaussian is on $f\\mid X$, not on $X$). Place a Gaussian prior with mean and standard deviation matching the observed training inputs, then draw 10,000 samples from $p(X\\mid Y)\\propto p(Y\\mid X)\\,p(X)$:" + ] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "# Covariance terms already encode p(Y|X); set p(X) so the sampler has a proper posterior.\n", + "x_obs = np.asarray(m.X, dtype=float).ravel()\n", + "mi.X.set_prior(GPy.priors.Gaussian(float(x_obs.mean()), float(x_obs.std())))\n", + "\n", + "hmc_new = GPy.inference.mcmc.HMC(mi, stepsize=2e-1)\n", + "s_new = hmc_new.sample(num_samples=10000, hmc_iters=10)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Plot the samples:" + ] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "_ = plt.plot(s_new[:,:])\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Plot the marginal distribution of inferred inputs. The two modes of inputs are clearly visible from the sampled posterior distribution." + ] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "from scipy import stats\n", + "samples_new = s_new[:]\n", + "xmin = samples_new.min()\n", + "xmax = samples_new.max()\n", + "xs = np.linspace(xmin,xmax,100)\n", + "for i in range(samples_new.shape[1]):\n", + " kernel = stats.gaussian_kde(samples_new[:,i])\n", + " plt.plot(xs,kernel(xs))\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Example 2 HMC for lengthscale and variance with marathon data" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "##### we set prior for lengthscale and variance of kernel. The mean of the prior is close to the result of GP optimisation. we then allow a big variance. In the case below,we set gamma prior to lengthscale and variance. E(lengthscale) = 120, Var(lengthscale)=2000, E(variance) = 25, Var(variance) = 150 " + ] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "m = GPy.examples.regression.olympic_marathon_men()\n", + "# Priors on every unfixed parameter (including noise) so HMC includes transform Jacobians.\n", + "m.kern.variance.set_prior(GPy.priors.Gamma.from_EV(25., 150.))\n", + "m.kern.lengthscale.set_prior(GPy.priors.Gamma.from_EV(120., 2000.))\n", + "m.likelihood.variance.set_prior(GPy.priors.Gamma.from_EV(0.5, 1.))\n", + "print(m)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Run HMC" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "##### we plot the full length of hmc iteration. The first 5000 could be burn in stage and can be ignored. " + ] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "# initialise hmc\n", + "hmc = GPy.inference.mcmc.HMC(m,stepsize=2e-1)\n", + "# run hmc\n", + "t = hmc.sample(num_samples=20000,hmc_iters=20)\n", + "\n", + "# Sample parameters \n", + "#hmc = GPy.inference.optimization.HMC(m, stepsize=5e-1)\n", + "#t = hmc.sample(m_iters=50000,hmc_iters=20)\n", + "\n", + "_=plt.plot(t)\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "print(t.mean(axis=0))\n", + "print(t.std(axis=0))\n", + "_=plt.hist(t[:,:2],50)\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Seaborn\n", + "Using Seaborn for plotting distributions over Hyperparameters:" + ] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "import seaborn as sns, pandas as pd\n", + "plt.rcParams['text.usetex'] = False" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "df = pd.DataFrame(t, columns=m.parameter_names_flat())\n", + "var_col = next(c for c in df.columns if c.endswith('rbf.variance'))\n", + "ls_col = next(c for c in df.columns if c.endswith('rbf.lengthscale'))\n", + "ax = sns.kdeplot(df[var_col], color=\"b\", fill=True)\n", + "ax = sns.kdeplot(df[ls_col], color=\"r\", fill=True)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "sns.set(style=\"white\", color_codes=True)\n", + "_ = sns.jointplot(data=df, x=var_col, y=ls_col, kind=\"hex\")" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "df" + ], + "execution_count": null, + "outputs": [] + } + ], + "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.12.0" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} \ No newline at end of file From db4543b7709b4d60ceb689210f190ebbb7d6678c Mon Sep 17 00:00:00 2001 From: Neil Lawrence Date: Wed, 25 Nov 2026 16:13:32 +0000 Subject: [PATCH 2/2] Point tutorial CI and HMC Colab install at GPy main (#1331). --- .github/workflows/gpy-tutorials.yml | 2 +- GPy/sampling_hmc.ipynb | 2 +- installs.yml | 21 +++++++++++---------- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/.github/workflows/gpy-tutorials.yml b/.github/workflows/gpy-tutorials.yml index 515d8d0..9f2fed8 100644 --- a/.github/workflows/gpy-tutorials.yml +++ b/.github/workflows/gpy-tutorials.yml @@ -1,6 +1,6 @@ # Execute GPy/*.ipynb against a pinned GPy (CIP-0003). # -# GPy pin: SheffieldML/GPy main until CIP-0014 (SkewGaussian) ships on PyPI +# GPy pin: SheffieldML/GPy main until CIP-0014 / #1331 ship on PyPI # (see installs.yml). Revert to a PyPI pin after the next GPy release. # # Default package sources: lamd shared catalog (lawrennd/lamd CIP-001C). diff --git a/GPy/sampling_hmc.ipynb b/GPy/sampling_hmc.ipynb index e5ced3d..19adfad 100644 --- a/GPy/sampling_hmc.ipynb +++ b/GPy/sampling_hmc.ipynb @@ -20,7 +20,7 @@ " import subprocess\n", " import sys\n", " subprocess.check_call(\n", - " [sys.executable, \"-m\", \"pip\", \"install\", \"-q\", \"gpy[plotting]>=1.15.0\"]\n", + " [sys.executable, \"-m\", \"pip\", \"install\", \"-q\", \"GPy[plotting] @ git+https://github.com/SheffieldML/GPy.git@main\"]\n", " )\n", " import GPy\n", "try:\n", diff --git a/installs.yml b/installs.yml index 288391e..17c52bd 100644 --- a/installs.yml +++ b/installs.yml @@ -19,22 +19,23 @@ catalog: url: https://raw.githubusercontent.com/lawrennd/lamd/main/lamd/installs/packages.yml # local: ../lamd/lamd/installs/packages.yml -# Temporary until GPy CIP-0014 / pods F1 loader are on the default catalog / PyPI. +# Temporary until GPy #1331 / CIP-0014 / pods F1 loader are on PyPI. overrides: packages: # f1_2011_practice_qualifying is on ods main; not yet on PyPI pods. pods: "pods @ git+https://github.com/lawrennd/ods.git" - # CIP-0014 skew + infer_newX ndarray fix; not yet in PyPI 1.15.0. - GPy: "GPy[plotting] @ git+https://github.com/SheffieldML/GPy.git@fix/infer-newx-ndarray" + # GPy main: CIP-0014 skew, infer_newX ndarray fix, #1331 require_priors. + # Not yet in PyPI 1.15.0. + GPy: "GPy[plotting] @ git+https://github.com/SheffieldML/GPy.git@main" default_profile: gpy-tutorials profiles: gpy-tutorials: description: > - GPy from fix/infer-newx-ndarray (CIP-0014 skew + infer_newX ndarray fix) until merged/released. - pods override points at ods main until the F1 loader is on the shared - catalog / PyPI. + GPy from main (CIP-0014 skew, infer_newX ndarray fix, #1331 MCMC + require_priors) until the next PyPI release. pods override points at ods + main until the F1 loader is on the shared catalog / PyPI. packages: - GPy - pods @@ -46,12 +47,12 @@ profiles: - pandas - climin pins: - GPy: "GPy[plotting] @ git+https://github.com/SheffieldML/GPy.git@fix/infer-newx-ndarray" + GPy: "GPy[plotting] @ git+https://github.com/SheffieldML/GPy.git@main" gpy-tutorials-latest: description: > - Unpinned GPy canary (also CIP-0014 main until skew ships on PyPI). Same - pods override as gpy-tutorials until the F1 loader is on PyPI. + Same GPy main canary as gpy-tutorials until a newer tip / PyPI release. + Same pods override until the F1 loader is on PyPI. packages: - GPy - pods @@ -63,4 +64,4 @@ profiles: - pandas - climin pins: - GPy: "GPy[plotting] @ git+https://github.com/SheffieldML/GPy.git@fix/infer-newx-ndarray" + GPy: "GPy[plotting] @ git+https://github.com/SheffieldML/GPy.git@main"