-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new non_stat kernel and more docs in gp2Scale
- Loading branch information
1 parent
4ddf2a4
commit bafe85a
Showing
5 changed files
with
289 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,225 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# This example shows how fvGP can be used to set up non-stationary kernels " | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## To train the GP using the non-staionary kernel via local or hybrid optimizers fvGP accepts the gradient of the kernel as a callable" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import numpy as np\n", | ||
"#from gpcam.gp_optimizer import GPOptimizer\n", | ||
"import matplotlib.pyplot as plt\n", | ||
"from fvgp.gp import GP" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Make an example data set" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"x = np.linspace(0,1,1000)\n", | ||
"def f(x):\n", | ||
" return np.sin(5. * x) + np.cos(10. * x) + (2.* (x-0.4)**2) * np.cos(100. * x)\n", | ||
"plt.plot(x,f(x))\n", | ||
"x_data = np.random.rand(50)\n", | ||
"y_data = f(x_data) + (np.random.rand(len(x_data))-0.5) * 0.5\n", | ||
"plt.plot(x,f(x))\n", | ||
"plt.scatter(x_data,y_data)\n", | ||
"x_data = x_data.reshape(-1,1)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Define some kernels " | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"#stationary\n", | ||
"def kernel(x1,x2,hps,obj):\n", | ||
" d = obj._get_distance_matrix(x1,x2)\n", | ||
" #print(\"eval\")\n", | ||
" return hps[0] * np.exp(-d**2/hps[1])\n", | ||
" \n", | ||
"#non-stationary and gradient\n", | ||
"def nskernel(x1,x2,hps,obj):\n", | ||
" d = obj._get_distance_matrix(x1,x2)\n", | ||
" x0 = np.array([[0.],[0.5],[1.0]])\n", | ||
" w = hps[1:-1]\n", | ||
" l = hps[-1]\n", | ||
" return obj.non_stat_kernel(x1,x2,x0,w,l) * np.exp(-d**2/hps[0])\n", | ||
"\n", | ||
"def nskernel_gradient(x1,x2,hps,obj):\n", | ||
" d = obj._get_distance_matrix(x1,x2)\n", | ||
" x0 = np.array([[0.],[0.5],[1.0]])\n", | ||
" w = hps[1:-1]\n", | ||
" l = hps[-1]\n", | ||
" grad = np.empty((len(hps), len(x1),len(x2)))\n", | ||
" grad[0] = obj.non_stat_kernel(x1,x2,x0,w,l) * ((d/hps[0])**2) * np.exp(-d**2/hps[0])\n", | ||
" grad[1:] = obj.non_stat_kernel_gradient(x1,x2,x0,w,l) * np.exp(-d**2/hps[0])\n", | ||
" return grad " | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## First the stationary GP " | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"my_gp1 = GP(1, x_data,y_data,np.ones((2)), gp_kernel_function=kernel)\n", | ||
"my_gp1.train(np.array([[0.001,10.],[0.001,10.]]), method=\"global\")\n", | ||
"\n", | ||
"#let's make a prediction\n", | ||
"mean1 = my_gp1.posterior_mean(x.reshape(-1,1))[\"f(x)\"]\n", | ||
"var1 = my_gp1.posterior_covariance(x.reshape(-1,1))[\"v(x)\"]\n", | ||
"#print(my_gp1.hyperparameters)\n", | ||
"#now we can ask for a new point" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"my_gp1.hyperparameters" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Wow the non-stationary GP. We will train globally so the gradient of the kernel is technically not required, but we will check it nontheless " | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"my_gp2 = GP(1, x_data, y_data, np.ones((5)), \n", | ||
" gp_kernel_function=nskernel, gp_kernel_function_grad=nskernel_gradient,\\\n", | ||
" ram_economy=False)\n", | ||
"\n", | ||
"my_gp2.train(np.array([[0.001,10.],[-10.,10.],[-10.,10.],[-10.,10.],[0.1,10.]]), method = \"global\")\n", | ||
"\n", | ||
"\n", | ||
"#let's make a prediction\n", | ||
"mean2 = my_gp2.posterior_mean(x.reshape(-1,1))[\"f(x)\"]\n", | ||
"var2 = my_gp2.posterior_covariance(x.reshape(-1,1))[\"v(x)\"]\n", | ||
"#print(my_gp2.hyperparameters)\n", | ||
"#now we can ask for a new point" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"a,b = my_gp2.test_log_likelihood_gradient(np.ones((5)))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## We'll plot both models, starting eith the stationary. In most cases the non-stationary GP has half the approximation error " | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"plt.figure(figsize = (16,10))\n", | ||
"plt.plot(x,mean1, label = \"posterior mean\")\n", | ||
"plt.plot(x,f(x), label = \"latent function\")\n", | ||
"plt.fill_between(x, mean1 - 3. * np.sqrt(var1), mean1 + 3. * np.sqrt(var1), alpha = 0.5, color = \"grey\", label = \"var\")\n", | ||
"plt.scatter(x_data,y_data)\n", | ||
"plt.legend()\n", | ||
"print(\"error: \", np.sum(f(x)-mean1)**2)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"plt.figure(figsize = (16,10))\n", | ||
"plt.plot(x,mean2, label = \"posterior mean\", linewidth = 4)\n", | ||
"plt.plot(x,f(x), label = \"latent function\", linewidth = 4)\n", | ||
"plt.fill_between(x, mean2 - 3. * np.sqrt(var2), mean2 + 3. * np.sqrt(var2), alpha = 0.5, color = \"grey\", label = \"var\")\n", | ||
"plt.scatter(x_data,y_data)\n", | ||
"plt.legend()\n", | ||
"print(\"error: \", np.sum(f(x)-mean2)**2)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "fvgp", | ||
"language": "python", | ||
"name": "fvgp" | ||
}, | ||
"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.8.10" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 4 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.