Skip to content

Commit a90dabb

Browse files
committed
nbsphinx example, better docs nav, etc.
1 parent a91cf84 commit a90dabb

21 files changed

Lines changed: 2845 additions & 918 deletions

docs/api.rst

Lines changed: 0 additions & 17 deletions
This file was deleted.

docs/api/cv.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.. automodule:: hessband.cv
2+
:members:

docs/api/index.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
API Reference
2+
=============
3+
4+
.. toctree::
5+
:maxdepth: 2
6+
7+
kde
8+
cv
9+
kernels
10+
selectors

docs/api/kde.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.. automodule:: hessband.kde
2+
:members:

docs/api/kernels.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.. automodule:: hessband.kernels
2+
:members:

docs/api/selectors.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.. automodule:: hessband.selectors
2+
:members:

docs/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"sphinx.ext.napoleon",
1919
"sphinx.ext.viewcode",
2020
"sphinx.ext.autosummary",
21+
"nbsphinx",
2122
]
2223

2324
templates_path = ["_templates"]

docs/examples.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Examples
2+
========
3+
4+
.. toctree::
5+
:maxdepth: 2
6+
7+
examples/nw_regression

docs/examples/nw_regression.ipynb

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Nadaraya-Watson Regression with `hessband`\n",
8+
"\n",
9+
"This notebook demonstrates how to use `hessband` to perform Nadaraya-Watson kernel regression. We will select the bandwidth using both the analytic method and grid search, and compare the results."
10+
]
11+
},
12+
{
13+
"cell_type": "code",
14+
"execution_count": null,
15+
"metadata": {},
16+
"outputs": [],
17+
"source": [
18+
"import time\n",
19+
"\n",
20+
"import matplotlib.pyplot as plt\n",
21+
"import numpy as np\n",
22+
"\n",
23+
"from hessband import nw_predict, select_nw_bandwidth"
24+
]
25+
},
26+
{
27+
"cell_type": "markdown",
28+
"metadata": {},
29+
"source": [
30+
"## 1. Generate Synthetic Data"
31+
]
32+
},
33+
{
34+
"cell_type": "code",
35+
"execution_count": null,
36+
"metadata": {},
37+
"outputs": [],
38+
"source": [
39+
"np.random.seed(0)\n",
40+
"X = np.linspace(0, 1, 200)\n",
41+
"true_y = np.sin(2 * np.pi * X)\n",
42+
"y = true_y + 0.2 * np.random.randn(200)"
43+
]
44+
},
45+
{
46+
"cell_type": "markdown",
47+
"metadata": {},
48+
"source": [
49+
"## 2. Bandwidth Selection\n",
50+
"\n",
51+
"We will now select the optimal bandwidth using two methods: `analytic` and `grid` search."
52+
]
53+
},
54+
{
55+
"cell_type": "code",
56+
"execution_count": null,
57+
"metadata": {},
58+
"outputs": [],
59+
"source": [
60+
"# Analytic method\n",
61+
"start_time = time.time()\n",
62+
"h_analytic = select_nw_bandwidth(X, y, method='analytic')\n",
63+
"analytic_time = time.time() - start_time\n",
64+
"\n",
65+
"# Grid search method\n",
66+
"start_time = time.time()\n",
67+
"h_grid = select_nw_bandwidth(X, y, method='grid')\n",
68+
"grid_time = time.time() - start_time\n",
69+
"\n",
70+
"print(f\"Analytic method: h = {h_analytic:.4f}, time = {analytic_time:.4f}s\")\n",
71+
"print(f\"Grid search method: h = {h_grid:.4f}, time = {grid_time:.4f}s\")"
72+
]
73+
},
74+
{
75+
"cell_type": "markdown",
76+
"metadata": {},
77+
"source": [
78+
"The analytic method is much faster and gives a comparable bandwidth."
79+
]
80+
},
81+
{
82+
"cell_type": "markdown",
83+
"metadata": {},
84+
"source": [
85+
"## 3. Perform Regression and Plot Results"
86+
]
87+
},
88+
{
89+
"cell_type": "code",
90+
"execution_count": null,
91+
"metadata": {},
92+
"outputs": [],
93+
"source": [
94+
"y_pred_analytic = nw_predict(X, y, X, h_analytic)\n",
95+
"y_pred_grid = nw_predict(X, y, X, h_grid)\n",
96+
"\n",
97+
"plt.figure(figsize=(10, 6))\n",
98+
"plt.scatter(X, y, label='Data', alpha=0.5, s=10)\n",
99+
"plt.plot(X, true_y, label='True function', color='black', linestyle='--')\n",
100+
"plt.plot(X, y_pred_analytic, label=f'Analytic (h={h_analytic:.4f})', color='red')\n",
101+
"plt.plot(X, y_pred_grid, label=f'Grid search (h={h_grid:.4f})', color='green', linestyle=':')\n",
102+
"plt.legend()\n",
103+
"plt.title('Nadaraya-Watson Regression')\n",
104+
"plt.xlabel('X')\n",
105+
"plt.ylabel('y')\n",
106+
"plt.show()"
107+
]
108+
}
109+
],
110+
"metadata": {
111+
"kernelspec": {
112+
"display_name": "Python 3",
113+
"language": "python",
114+
"name": "python3"
115+
},
116+
"language_info": {
117+
"codemirror_mode": {
118+
"name": "ipython",
119+
"version": 3
120+
},
121+
"file_extension": ".py",
122+
"mimetype": "text/x-python",
123+
"name": "python",
124+
"nbconvert_exporter": "python",
125+
"pygments_lexer": "ipython3",
126+
"version": "3.11.6"
127+
}
128+
},
129+
"nbformat": 4,
130+
"nbformat_minor": 2
131+
}

docs/index.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ Analytic-Hessian bandwidth selection for univariate kernel regression.
77
:maxdepth: 2
88
:caption: Contents:
99

10-
api
10+
examples
11+
api/index
1112

1213
Indices and tables
1314
==================

0 commit comments

Comments
 (0)