A complete example
https://blog.kiarashs.ir/blog/a-complete-example
Contents18 sections
All models are wrong, but some are useful.
This post fits a logistic regression and reports on it, using every feature the renderer has along the way. The numbers are made up for the example. Rest the pointer on any reference below, "Figure 4.1" or "(2.1)" or a citation, and it shows what it points at.
1 Introduction
Quotes and dashes are shown as typed on this site. With smartQuotes: true in
a post's frontmatter, or typography.smartQuotes in the site config, "the
model," she said, "isn't done" -- see pages 10--12 --- would be set with curly
quotes, an en dash and an em dash, while code such as print("isn't") kept what
was typed.
An empty link to another post writes its title: Writing like a paper
covers the basics, and Math and citations the maths. One to a section
here writes its number: the method is in Section 3, the results in
Section 4. A citation works as before (Knuth, 1984), and so does a
footnote, which on a wide screen stands in the margin.11 Numbered sections are asked for with numbered: true in the
frontmatter; most posts do not need them.
2 Theory
The symbols used throughout:
- The matrix of inputs, one example to a row.
- The labels, each or .
- The weights, the model's only parameters.
- The logistic function, which maps a score to a probability.
2.1 The loss
\loss and \KL are this post's own macros, defined in its frontmatter; \R
and \E come with the site. Minimising (2.1) is the same as minimising
, up to a constant.
Its derivatives share a number, (2.2a) and (2.2b), as related equations do in a paper:
Theorem 2.2 (Convexity). is convex in , so gradient descent with a small enough step reaches its minimum .
Proof of Theorem 2.2. The Hessian is (2.2b), with , and for every . ∎
2.2 Exercises
Exercise 2.3. Show that the gradient of (2.1) is .
Solution to Exercise 2.3.
Write . Since , the chain rule gives . Averaging over gives .
3 Method
3.1 The algorithm
Algorithm 3.1 is Theorem 2.2 put to work, one step at a time as in Figure 3.1. It uses all the data for every step; drawing a few rows at a time instead is stochastic gradient descent (SGD), which scales to data that does not fit in memory. SGD is left out here for clarity.
3.2 The code
Listing 3.1 is read from the file the post links to, lines 16 to 23, so it is the code that ran. Lines 19 to 21 are the loop.
fit method of fit.py. def fit(self, X, y):
n, d = X.shape
self.w = np.zeros(d)
for _ in range(self.steps):
p = sigmoid(X @ self.w)
gradient = X.T @ (p - y) / n
self.w -= self.step * gradient
return selfA file can also be read from elsewhere. Listing 3.2 is the same file in
this site's repository on GitHub, at the commit that added it, so it cannot
change under the post; the build keeps a copy in
src/content/remote-code.lock.json and needs no network to show it again.
predict method, read from GitHub at a fixed commit. def predict(self, X):
return (sigmoid(X @ self.w) >= 0.5).astype(int)Callouts explain a listing line by line:
model = LogisticRegression(step=0.5)
model.fit(X_train, y_train)
accuracy = (model.predict(X_test) == y_test).mean()- A larger step than the default, which Figure 4.1 shows is still safe.
- Five hundred steps of Algorithm 3.1.
- The share of test points classified correctly; see Table 4.1.
The same model in two languages:
model = LogisticRegression().fit(X, y)
model <- glm(y ~ ., data = d, family = binomial)
And what changed when the loop learnt to stop early:
for _ in range(self.steps):
p = sigmoid(X @ self.w)
gradient = X.T @ (p - y) / n
if np.linalg.norm(gradient) < 1e-6:
break
self.w -= self.step * gradient4 Results
4.1 Step sizes
The step in Figure 4.1b is the one used for the rest of the post;
Figure 4.1c diverges. The row is set wider than the text, #wide on its
first picture, so the three plots are not squeezed into the column.
4.2 Before and after
Two images flagged #compare are laid one over the other with a divider to
drag, or to move with the arrow keys once it has focus. Printed, or with no
script, they are two pictures side by side.


4.3 Accuracy
Table 4.1 is read from accuracy.csv beside the post, so it shows the
numbers the results came out in; the numeric columns line up on the right.
Accuracy depends on where the threshold is set; area under the ROC curve (AUC), which does not, would be
the other number to report.
| Model | Test accuracy | Training time (s) | Note |
|---|---|---|---|
| Logistic regression | 0.86 | 1.2 | linear, fast |
| Decision tree | 0.81 | 0.4 | depth 6 |
| Random forest | 0.89 | 7.9 | 200 trees, depth 8 |
Data (3 rows)
| model | accuracy |
|---|---|
| Logistic regression | 0.86 |
| Decision tree | 0.81 |
| Random forest | 0.89 |
4.4 The pipeline
4.5 The boundary
A black-on-white sketch with no dark version of its own is inverted on the
dark theme when its path ends in #invert; switch the theme to see it.
4.6 Chemistry
The renderer has chemistry too: , with .
5 Media
5.1 A recording
5.2 A place
6 Notes
How the references are written
Every reference in this post is a link with no text: [](#fig-lr-b),
[](#eq-loss), [](/blog/writing-like-a-paper). The build fills in the
words, and warns about any that point at nothing.
What the frontmatter of this post says
numbered: true
numberWithin: section
macros:
# Quoted: in YAML a space before # starts a comment.
\KL: 'D_{\mathrm{KL}}\left(#1 \,\Vert\, #2\right)'
\loss: \mathcal{L}Footnotes
-
Numbered sections are asked for with
numbered: truein the frontmatter; most posts do not need them. ↩
Cite this post
@misc{soleimanzadeh2026complete,
author = {Soleimanzadeh, Kiarash},
title = {A complete example},
year = {2026},
url = {https://blog.kiarashs.ir/blog/a-complete-example},
urldate = {2026-10-29}
}Soleimanzadeh, K. (2026, October 29). A complete example. Kiarash Soleimanzadeh. https://blog.kiarashs.ir/blog/a-complete-exampleK. Soleimanzadeh, "A complete example," Kiarash Soleimanzadeh, Oct. 29, 2026. [Online]. Available: https://blog.kiarashs.ir/blog/a-complete-exampleKiarash Soleimanzadeh. “A complete example.” Kiarash Soleimanzadeh, 29 October 2026. https://blog.kiarashs.ir/blog/a-complete-example
Comments