Sync project files with latest sibling portfolio
This commit is contained in:
@@ -154,5 +154,17 @@
|
||||
"Incident Response",
|
||||
"Training"
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "desert-digital-consulting",
|
||||
"company": "Desert Digital Consulting",
|
||||
"role": "Software Engineer, Web Application Developer, IT Consultant",
|
||||
"location": "Remote",
|
||||
"start": "2010-10",
|
||||
"end": "2021-01",
|
||||
"summary": "Provided freelance services in software development, web design, information technology, and computer hardware design.",
|
||||
"highlights": [],
|
||||
"technologies": [],
|
||||
"projects": []
|
||||
}
|
||||
]
|
||||
|
||||
+2
-2
@@ -35,7 +35,7 @@ PROJECT_DIR = BASE_DIR.parent
|
||||
DATA_DIR = BASE_DIR / "data"
|
||||
FRONTEND_DIST = PROJECT_DIR / "frontend" / "dist"
|
||||
# Pin the website to the selected resume; generating a draft must not publish it.
|
||||
RESUME_PATH = PROJECT_DIR / "resumes" / "resume-v11.pdf"
|
||||
RESUME_PATH = PROJECT_DIR / "resumes" / "resume-v16.pdf"
|
||||
POSTS_PATH = DATA_DIR / "posts.json"
|
||||
POSTS_LOCK = threading.Lock()
|
||||
ADMIN_TOKEN_TTL = 60 * 60 * 4
|
||||
@@ -620,7 +620,7 @@ def resume() -> FileResponse:
|
||||
return FileResponse(
|
||||
RESUME_PATH,
|
||||
media_type="application/pdf",
|
||||
filename="Alexander-Herlan-Resume-v11.pdf",
|
||||
filename="Alexander-Herlan-Resume-v16.pdf",
|
||||
content_disposition_type="inline",
|
||||
)
|
||||
|
||||
|
||||
@@ -0,0 +1,151 @@
|
||||
Fair enough — here it is from the ground up. I verified every number below by running it, not by trusting the MR text.
|
||||
|
||||
## 1. The business question this code answers
|
||||
|
||||
A portfolio manager beat their benchmark by, say, +2.4% over the year. The obvious next question is **"where did that come from?"** Tech stocks? Japan? Being underweight energy?
|
||||
|
||||
**Brinson attribution** is the standard way to answer that. It slices the +2.4% into buckets and says "Tech gave you +0.9%, Japan gave you -0.3%..." and the slices add up to the total. In this codebase each bucket is called a **bin** — confirmed at `brinson.py:374`, where a classification column (sector, country, market-cap band) gets renamed to `"bin"`.
|
||||
|
||||
So the shape of the data is:
|
||||
|
||||
```text
|
||||
date bin allocation_effect
|
||||
2026-01-02 Technology 0.0021
|
||||
2026-01-02 Energy -0.0004
|
||||
2026-01-03 Technology 0.0000
|
||||
...
|
||||
```
|
||||
|
||||
One number per bin per day. A year is ~261 trading days. A heatmap panel (sector x country, say) can have **1,586 bins**. That's the scale that matters here.
|
||||
|
||||
## 2. Why you can't just add up the days
|
||||
|
||||
Here's the concept that makes this whole ticket exist.
|
||||
|
||||
Returns **compound**, they don't add. If you make +10% then +10%, you made 21%, not 20%. So daily effects can't just be summed to get the yearly effect — the arithmetic wouldn't reconcile with the actual total return.
|
||||
|
||||
**Frongello smoothing** is the industry method for fixing this. It walks forward day by day and scales each day's effect by how much the portfolio had already grown, so the pieces still add up to the real compounded total. The formula in the code (`frongello.py`, `_frongello_smooth_active`):
|
||||
|
||||
```text
|
||||
adjusted_t = c_t * (1 + Rp_before_t) + accumulated_so_far * b_t
|
||||
accumulated += adjusted_t
|
||||
```
|
||||
|
||||
where `c_t` = this bin's effect today, `Rp` = portfolio's cumulative return, `b_t` = benchmark's return today.
|
||||
|
||||
Notice: **today's answer needs yesterday's answer**. That's a *recurrence*, and it's why the original author wrote a Python for loop over days and left a comment saying vectorization was impossible.
|
||||
|
||||
## 3. The old code, in a picture
|
||||
|
||||
To produce one panel, the code did this **once per bin**:
|
||||
|
||||
```text
|
||||
for each of 1,586 bins:
|
||||
1. pull out just that bin's rows
|
||||
2. some days the bin has no row at all (nobody held Japanese
|
||||
utilities that Tuesday) -> do a JOIN against the full
|
||||
calendar and fill the holes with 0.0 <-- expensive
|
||||
3. run the Frongello loop over 261 days
|
||||
4. keep the last value
|
||||
```
|
||||
|
||||
Step 2 is the villain. Think of it as **1,586 separate trips to the warehouse, each to fetch one item**. Polars (the dataframe library) is fast, but every one of those joins is a fresh query it has to plan, launch, and collect. That's **5 x bins = 7,932 collect() calls for one panel**.
|
||||
|
||||
And here's the part I find genuinely interesting: the ticket *guessed wrong* about where the time went. Everyone assumed the day-by-day Frongello loop was the slow part, because loops feel slow. cProfile said otherwise:
|
||||
|
||||
| | time |
|
||||
| --- | --- |
|
||||
| the zero-fill joins (step 2) | 19.768 s |
|
||||
| the actual Frongello math (step 3) | 0.647 s |
|
||||
|
||||
**The plumbing cost 30x the arithmetic it was feeding.** That's a lesson worth carrying: measure before optimizing, because intuition points at the loop and the loop was innocent.
|
||||
|
||||
## 4. Fix part one: one warehouse trip, not 1,586
|
||||
|
||||
Instead of padding each bin separately, build **one dense grid** — every date as a row, every bin as a column, holes already filled with zero:
|
||||
|
||||
```text
|
||||
Tech Energy Japan ... (1,586 columns)
|
||||
2026-01-02 0.0021 -0.0004 0.0000
|
||||
2026-01-03 0.0000 0.0011 0.0007
|
||||
... (261 rows)
|
||||
```
|
||||
|
||||
A helper for exactly this **already existed** in the repo: `pivot_long_to_wide` in `analytics/pivot_utils.py`. Its `fill_null(0.0)` is the zero-padding, for free, as a side effect of building the grid. No new machinery invented — that's the good kind of diff.
|
||||
|
||||
One trip to the warehouse with a cart.
|
||||
|
||||
## 5. Fix part two: the chain wasn't really a chain
|
||||
|
||||
Now the grid needs Frongello applied to all 1,586 columns. Still a per-column loop?
|
||||
|
||||
No — and this is the clever bit. The recurrence looks sequential, but look at what it actually does to `c`:
|
||||
|
||||
```text
|
||||
A_t = A_{t-1} * (1 + b_t) + c_t * W_t
|
||||
```
|
||||
|
||||
`c` only ever appears **multiplied by something and added**. Never squared, never inside another c. It's *linear*. And the coefficients (`W`, `b`) come from the portfolio and benchmark — **the same for every bin**.
|
||||
|
||||
So divide both sides by `Cb_t` (the benchmark's cumulative growth). The chain telescopes and collapses into:
|
||||
|
||||
```text
|
||||
A_t = Cb_t * cumulative_sum( c_u * W_u / Cb_u )
|
||||
```
|
||||
|
||||
The chain is gone. It's now: multiply, running-total, multiply. Polars does running-totals on 1,586 columns at once, in Rust, without Python ever seeing a day.
|
||||
|
||||
**I ran both versions side by side to be sure this isn't hand-waving:**
|
||||
|
||||
```text
|
||||
portfolio daily: +1.0% +2.0% -1.0%
|
||||
benchmark daily: +0.5% +1.5% 0.0%
|
||||
one bin's effect: 0.002 0.000 0.003
|
||||
|
||||
W (portfolio growth so far): 1.000000 1.010000 1.030200
|
||||
Cb (benchmark growth so far): 1.005000 1.020075 1.020075
|
||||
|
||||
old loop : 0.002, 0.00203, 0.0051206
|
||||
new formula : 0.002, 0.00203, 0.0051206
|
||||
identical : True (to 1e-15)
|
||||
```
|
||||
|
||||
Same answer. Not an approximation — algebraically the same thing, rearranged.
|
||||
|
||||
## 6. What the result is
|
||||
|
||||
| | before | after |
|
||||
| --- | --- | --- |
|
||||
| collect() calls per panel | 7,932 | **21** |
|
||||
| 261 dates x 1,586 bins | 1881 ms | **152 ms (12.3x)** |
|
||||
|
||||
The number to quote is **21, not 152 ms**. The count is a property of the code's shape and travels to any machine; the milliseconds belong to whatever the CPU pool looked like that afternoon. The author says this explicitly, which is the right instinct.
|
||||
|
||||
One more tell that the diagnosis was correct: the 63-day shape sped up as much as the 261-day shape. If the cost had been per-row, a quarter of the rows would have shown a quarter of the gain. It didn't — so the cost really was per-bin. That's evidence, not a story.
|
||||
|
||||
## 7. New concept the fix introduces: a division that wasn't there
|
||||
|
||||
Dividing by `Cb_u` is new. The old loop never divided by anything.
|
||||
|
||||
What if Cb is zero? That means the benchmark returned exactly -100% — it went to zero. Then you'd divide by zero and get inf where there used to be a number. The code guards it:
|
||||
|
||||
```python
|
||||
if (cb == 0.0).any():
|
||||
return <fall back to the old loop>
|
||||
```
|
||||
|
||||
**This is the one thing I'd push on in review.** The guard catches *exactly* zero. It does not catch *nearly* zero — a benchmark down 99.99%. There, `scale = W / Cb` becomes enormous, you sum enormous numbers, then multiply by a tiny one, and floating-point precision quietly bleeds away. The old loop had no such weak spot.
|
||||
|
||||
It's a **cliff, not a slope**: safe, safe, safe, then wrong, with nothing in between. Only degenerate data reaches it, so I'd accept the MR — but I'd want the docstring to say "exact zero is the whole guard" so the next person doesn't assume near-zero is handled too.
|
||||
|
||||
## 8. Why the tests are better than they look
|
||||
|
||||
The new tests compare the fast version against a slow reference loop. Sounds airtight — but there's a trap: **a test like that also passes if the new code never runs at all** (wrong branch taken, fallback always firing). Green tells you nothing.
|
||||
|
||||
So the author **mutation-tested** them: deliberately broke the new formula by deleting the `* cb` factor, and confirmed **6 of 9 tests went red**. That proves the tests actually execute and inspect the new path. That's the difference between tests and decoration, and it's a habit worth stealing.
|
||||
|
||||
---
|
||||
|
||||
**Bottom line for your review:** the change is sound, the math is exactly equivalent (I checked), it reuses a helper that already existed instead of inventing one, and the measurement is honestly reported — including refusing to extrapolate a full-report number it didn't measure. The near-zero-Cb precision cliff is a docstring nit, not a blocker.
|
||||
|
||||
Skipped: running the suite myself; the MR reports 768 passed on analytics + aggregation. Say the word if you want that confirmed independently.
|
||||
Reference in New Issue
Block a user