FIX: index fsolve result in un_insure r_error so brentq gets a scalar#17
Merged
Conversation
In `r_error`, `sp.optimize.fsolve(...)` returns a length-1 array, so the
function returned a shape-(1,) array. The outer `brentq` then coerces that
return value to a Python float, which raises under numpy 2.x / scipy 1.17
(the quantecon-build:latest image, numpy 2.4.4 / scipy 1.17.1):
TypeError: only 0-dimensional arrays can be converted to Python scalars
This is why the build (and main's scheduled builds) were failing on
un_insure.md. Index `[0]` to return a scalar, matching the existing
`Vu_aut = fsolve(...)[0]` call just below.
Mirrors upstream fix QuantEcon/lecture-python-advanced.myst#327 (un_insure.md
is sourced from there; see issue #7). Verified end-to-end inside
ghcr.io/quantecon/quantecon-build:latest.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Fixes a CI build failure in un_insure.md caused by fsolve returning a length-1 array, which numpy 2.x no longer silently coerces to a scalar when consumed by brentq. Indexing [0] makes r_error return a scalar, matching the existing pattern used for Vu_aut a few lines below.
Changes:
- Index the
fsolveresult with[0]soVu_staris a scalar, allowingbrentqto work under numpy 2.x.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
mmcky
added a commit
that referenced
this pull request
May 28, 2026
Pulls in un_insure.md execution fix (#17) so CI passes.
mmcky
added a commit
that referenced
this pull request
May 28, 2026
Pulls in un_insure.md execution fix (#17) so CI passes.
mmcky
added a commit
that referenced
this pull request
May 28, 2026
Pulls in un_insure.md execution fix (#17) so CI passes.
mmcky
added a commit
that referenced
this pull request
May 28, 2026
Pulls in un_insure.md execution fix (#17) so CI passes.
mmcky
added a commit
that referenced
this pull request
May 28, 2026
Pulls in un_insure.md execution fix (#17) so CI passes.
mmcky
added a commit
that referenced
this pull request
May 28, 2026
Pulls in un_insure.md execution fix (#17) so CI passes.
mmcky
added a commit
that referenced
this pull request
May 28, 2026
Pulls in un_insure.md execution fix (#17) so CI passes.
mmcky
added a commit
that referenced
this pull request
May 28, 2026
Pulls in un_insure.md execution fix (#17) so CI passes.
mmcky
added a commit
that referenced
this pull request
May 28, 2026
Pulls in un_insure.md execution fix (#17) so CI passes.
mmcky
added a commit
that referenced
this pull request
May 28, 2026
Pulls in un_insure.md execution fix (#17) so CI passes.
This was referenced May 28, 2026
mmcky
added a commit
that referenced
this pull request
May 28, 2026
Brings lecture-dp's un_insure.md back into byte-identical alignment with its primary source (see #7) after upstream PR #327 merged. The minimal [0] index fix that landed earlier in lecture-dp #17 was a first-aid measure to unblock CI. This SYNC pulls in the full upstream version, which adds: * the same [0] index on fsolve(...) so r_error returns a scalar (the original CI fix from #17); * args=(r,) tuple correctness on both fsolve calls; * max(0, invp_prime(...)) inside Vu_error so search effort is clamped to non-negative, matching what calc_a already did. This eliminates a "RuntimeWarning: iteration not making good progress" from fsolve; * PEP8 sweep across the file (spaces around operators, no spaces around = in kwargs, two blank lines between top-level defs, comment spacing, long-line wrapping); * two spelling fixes (utiliy -> utility, consumpton -> consumption). Verified in ghcr.io/quantecon/quantecon-build:latest (numpy 2.4.4 / scipy 1.17.1): runs clean with no warnings, calibration r matches the published live page to ~10 significant figures, and the rendered figure is byte-identical (same PNG SHA256). Mirrors upstream change in QuantEcon/lecture-python-advanced.myst#327 (authored by @HumphreyYang). Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The CI
buildhas been failing onun_insure.md(also onmain's scheduled builds and on every open sync PR):This is not caused by any of the sync PRs — it's a pre-existing notebook execution error that surfaces on a fresh build with the current
ghcr.io/quantecon/quantecon-build:latestimage (numpy 2.4.4 / scipy 1.17.1).Cause
In
r_error,sp.optimize.fsolve(...)returns a length-1 array, sor_errorreturns a shape-(1,)array. The outersp.optimize.brentq(r_error_Λ, ...)then coerces that return value to a Python float; under numpy 2.x this raises (older numpy silently accepted 1-element arrays, so cache-warmed builds masked it).Fix
Returns a scalar, matching the existing
Vu_aut = sp.optimize.fsolve(...)[0]call a few lines below.Verification
Ran the full
un_insure.mdnotebook insideghcr.io/quantecon/quantecon-build:latest(numpy 2.4.4 / scipy 1.17.1): completes end-to-end, calibration result unchanged (r = 0.000343...).Provenance
un_insure.mdis sourced fromlecture-python-advanced.myst(see #7). The same fix is opened upstream as QuantEcon/lecture-python-advanced.myst#327, so a future re-sync stays consistent.🤖 Generated with Claude Code