{
"cells": [
{
"cell_type": "markdown",
"id": "0",
"metadata": {},
"source": [
"# Direct GPQR (independent)\n",
"\n",
"In this example, the data generating process is\n",
"$$Y = \\cos(2 \\pi (X+0.1)) + \\epsilon, \\quad \\epsilon \\sim \\mathcal{N}(0, X+0.1).$$\n",
"\n",
"Quantile functions $Q_{\\tau_i}(x)$ of $Y$ are individually modeled latent GP $g_i(x)$:\n",
"$$ Q_{\\tau_i}(x) = g_i(x; \\theta), $$\n",
"where \n",
"$$ g_i(x; \\theta) \\sim \\mathcal{N}(\\cos(2 \\pi x + \\theta) + c_i, k(x, x')). $$\n",
"\n",
"Parameters $\\theta$ and $c_i$ are learned by maximizing the marginal likelihood."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"\n",
"import torch\n",
"from torch.distributions import Normal\n",
"from gpytorch.variational import CholeskyVariationalDistribution\n",
"from gpytorch.variational import VariationalStrategy\n",
"from gpytorch.variational import IndependentMultitaskVariationalStrategy\n",
"from gpytorch.means import Mean\n",
"from gpytorch.kernels import RBFKernel, ScaleKernel\n",
"from gpytorch.mlls import VariationalELBO\n",
"import matplotlib.pyplot as plt\n",
"\n",
"from gpytorch_qr.models import DirectQuantileGP\n",
"from gpytorch_qr.likelihoods import DirectQuantileLikelihood\n",
"\n",
"try:\n",
" import sys\n",
"\n",
" sys.path.insert(0, os.path.abspath(\"../..\"))\n",
"\n",
" import config_notebook\n",
"except ImportError:\n",
" print(\"Output will not be deterministic SVG.\")\n",
"\n",
"torch.manual_seed(42)\n",
"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",
"\n",
"n_epochs = int(os.getenv(\"GPYTORCHQR_N_EPOCHS\", 10000))"
]
},
{
"cell_type": "markdown",
"id": "2",
"metadata": {},
"source": [
"## Data preparation"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3",
"metadata": {},
"outputs": [],
"source": [
"def f(x):\n",
" return torch.cos((x + 0.1) * 2 * 3.14)\n",
"\n",
"\n",
"def std(x):\n",
" return x + 0.1\n",
"\n",
"\n",
"x_range = torch.linspace(0, 1, 100).reshape(-1, 1).to(device)\n",
"x = x_range.repeat(5, 1)\n",
"y = (f(x) + torch.randn(x.shape, device=device).mul(std(x))).squeeze()\n",
"\n",
"q = torch.tensor([0.1, 0.25, 0.5, 0.75, 0.9]).to(device)\n",
"true_quantiles = f(x_range) + std(x_range) * Normal(0, 1).icdf(q)\n",
"\n",
"x_pred = torch.linspace(0, 1.5, 100).reshape(-1, 1).to(device)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4",
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"plt.scatter(x.cpu(), y.cpu(), c=\"k\", marker=\".\")\n",
"plt.plot(x_range.cpu(), true_quantiles.cpu(), \"--\", c=\"gray\")\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5",
"metadata": {},
"outputs": [],
"source": [
"class PriorMean(Mean):\n",
" def __init__(self, batch_shape=torch.Size([])):\n",
" super().__init__()\n",
" self.batch_shape = batch_shape\n",
" self.theta = torch.nn.Parameter(torch.tensor(0.0))\n",
" self.register_parameter(\"offset\", torch.nn.Parameter(torch.zeros(*batch_shape)))\n",
"\n",
" def forward(self, x):\n",
" # x: (N, D)\n",
" m = torch.cos(2 * 3.14 * x + self.theta) # (N, D)\n",
" ret = m + self.offset.reshape(*self.offset.shape, 1, 1) # (B, N, D)\n",
" return ret.squeeze(-1) # (B, N)\n",
"\n",
"\n",
"prior_mean = PriorMean(batch_shape=torch.Size([len(q)])).to(device)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6",
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"plt.scatter(x.cpu(), y.cpu(), c=\"k\", marker=\".\")\n",
"plt.plot(x_pred.cpu(), prior_mean(x_pred).detach().cpu().T)\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"id": "7",
"metadata": {},
"source": [
"## Define models and likelihoods"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8",
"metadata": {},
"outputs": [],
"source": [
"class MyGP(DirectQuantileGP):\n",
" def __init__(self, inducing_points, num_quantiles):\n",
" N, D = inducing_points.size()\n",
" variational_distribution = CholeskyVariationalDistribution(\n",
" N,\n",
" batch_shape=torch.Size([num_quantiles]),\n",
" )\n",
" variational_strategy = IndependentMultitaskVariationalStrategy(\n",
" VariationalStrategy(\n",
" self,\n",
" inducing_points,\n",
" variational_distribution,\n",
" learn_inducing_locations=False,\n",
" ),\n",
" num_tasks=num_quantiles,\n",
" )\n",
" mean = PriorMean(batch_shape=torch.Size([num_quantiles]))\n",
" covar = ScaleKernel(\n",
" RBFKernel(ard_num_dims=D, batch_shape=torch.Size([num_quantiles])),\n",
" batch_shape=torch.Size([num_quantiles]),\n",
" )\n",
" super().__init__(variational_strategy, mean, covar)\n",
"\n",
"\n",
"inducing_points = torch.linspace(0, 1, 10).reshape(-1, 1).to(device)\n",
"gp = MyGP(inducing_points, len(q)).to(device)\n",
"likelihood = DirectQuantileLikelihood(q).to(device)"
]
},
{
"cell_type": "markdown",
"id": "9",
"metadata": {},
"source": [
"## Train"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "10",
"metadata": {},
"outputs": [],
"source": [
"gp.train()\n",
"likelihood.train()\n",
"mll = VariationalELBO(likelihood, gp, num_data=y.numel())\n",
"optimizer = torch.optim.Adam(\n",
" list(gp.parameters()) + list(likelihood.parameters()),\n",
" lr=0.001,\n",
")\n",
"\n",
"for _ in range(n_epochs):\n",
" output = gp(x)\n",
" loss = -mll(output, y)\n",
" loss.backward()\n",
" optimizer.step()\n",
" optimizer.zero_grad()"
]
},
{
"cell_type": "markdown",
"id": "11",
"metadata": {},
"source": [
"## Evaluate"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "12",
"metadata": {},
"outputs": [],
"source": [
"gp.eval()\n",
"with torch.no_grad():\n",
" mean_q = gp.mean_quantiles(x_pred)\n",
" lower_q, upper_q = gp.quantile_quantiles(\n",
" x_pred, torch.tensor([0.025, 0.975]).to(device)\n",
" )"
]
},
{
"cell_type": "markdown",
"id": "13",
"metadata": {},
"source": [
"## Plot result"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "14",
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"colors = plt.cm.tab10.colors\n",
"\n",
"plt.scatter(x.cpu(), y.cpu(), c=\"gray\", marker=\".\", alpha=0.1)\n",
"plt.plot(x_range.cpu(), true_quantiles.cpu(), \"--\", c=\"k\")\n",
"\n",
"for i in range(len(q)):\n",
" plt.plot(x_pred.cpu(), mean_q[:, i].cpu(), color=colors[i])\n",
" plt.fill_between(\n",
" x_pred.cpu().squeeze(),\n",
" lower_q[:, i].cpu(),\n",
" upper_q[:, i].cpu(),\n",
" color=colors[i],\n",
" alpha=0.3,\n",
" )\n",
"plt.show()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "heavyedge",
"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.13.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}