Skip to content

FIX: index fsolve result in un_insure r_error so brentq gets a scalar#17

Merged
mmcky merged 1 commit into
mainfrom
fix-un_insure-fsolve-scalar-return
May 28, 2026
Merged

FIX: index fsolve result in un_insure r_error so brentq gets a scalar#17
mmcky merged 1 commit into
mainfrom
fix-un_insure-fsolve-scalar-return

Conversation

@mmcky
Copy link
Copy Markdown
Contributor

@mmcky mmcky commented May 28, 2026

Problem

The CI build has been failing on un_insure.md (also on main's scheduled builds and on every open sync PR):

TypeError: only 0-dimensional arrays can be converted to Python scalars

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:latest image (numpy 2.4.4 / scipy 1.17.1).

Cause

In r_error, sp.optimize.fsolve(...) returns a length-1 array, so r_error returns a shape-(1,) array. The outer sp.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

-    Vu_star = sp.optimize.fsolve(Vu_error_Λ,15000,args = (r))
+    Vu_star = sp.optimize.fsolve(Vu_error_Λ,15000,args = (r))[0]

Returns a scalar, matching the existing Vu_aut = sp.optimize.fsolve(...)[0] call a few lines below.

Verification

Ran the full un_insure.md notebook inside ghcr.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.md is sourced from lecture-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

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>
Copilot AI review requested due to automatic review settings May 28, 2026 01:22
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 fsolve result with [0] so Vu_star is a scalar, allowing brentq to work under numpy 2.x.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@mmcky mmcky merged commit 1367629 into main May 28, 2026
2 checks passed
@mmcky mmcky deleted the fix-un_insure-fsolve-scalar-return branch May 28, 2026 02:07
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.
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants