Skip to content

Instantly share code, notes, and snippets.

@fehiepsi
Created February 25, 2018 00:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fehiepsi/cf55756ea44dbf87bbb51ab331738a87 to your computer and use it in GitHub Desktop.
Save fehiepsi/cf55756ea44dbf87bbb51ab331738a87 to your computer and use it in GitHub Desktop.
Gaussian Process kernel tests
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"from sklearn.gaussian_process.kernels import ConstantKernel, RBF, WhiteKernel, Matern, ExpSineSquared, DotProduct\n",
"from GPy.kern import Brownian, Cosine, StdPeriodic, RatQuad"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"X = np.array([[1, 0, 1], [2, 1, 3]])\n",
"Z = np.array([[4, 5, 6], [3, 1, 7], [3, 1, 2]])\n",
"length_scale = np.array([2, 1, 2])\n",
"variance = 3"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3.6811172994024588"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"RBF(length_scale)(X, Z).sum() * 3"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2.6856794085006759"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Matern(length_scale, nu=1/2)(X, Z).sum() * 3"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3.229313576906609"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Matern(length_scale, nu=3/2)(X, Z).sum() * 3"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3.3918468034753855"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Matern(length_scale, nu=5/2)(X, Z).sum() * 3"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"291"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"DotProduct(0)(X, Z).sum() * 3"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"7017"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(DotProduct(1)(X, Z)**2).sum() * 3"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.0"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"WhiteKernel(noise_level=3)(np.array(X), np.array(Z)).sum()"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"6.0"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"WhiteKernel(noise_level=3)(np.array(X)).sum()"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"27.0"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Brownian(input_dim=1, variance=3).K(X[:,:1], Z[:,:1]).sum()"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"-0.19323272261606705"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Cosine(input_dim=3, lengthscale=length_scale, variance=3, ARD=True).K(X, Z).sum()"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"18.0"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"StdPeriodic(input_dim=3, lengthscale=length_scale, variance=3, period=1, ARD2=True, ARD1=False).K(X, Z).sum()"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5.6846701931043269"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"RatQuad(input_dim=3, lengthscale=length_scale, variance=3, power=1, ARD=True).K(X, Z).sum()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python (pydata)",
"language": "python",
"name": "pydata"
},
"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.5.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment