You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fix SymPy OverflowError in solow.md by using Rational (#687)
* Fix SymPy OverflowError in solow.md by using Rational
The SymPy solve() function was failing with 'mpz too large to convert
to float' when using Python float values (0.3, 0.5, 2.0) in symbolic
expressions with fractional exponents.
Using sympy.Rational for exact arithmetic avoids the large intermediate
mpz values that caused the overflow in factorint().
* minor updates
---------
Co-authored-by: Humphrey Yang <u6474961@anu.edu.au>
Let's find the value of $s$ that maximizes $c^*$ using [scipy.optimize.minimize_scalar](https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize_scalar.html#scipy.optimize.minimize_scalar).
@@ -469,8 +470,8 @@ from scipy.optimize import minimize_scalar
469
470
470
471
```{code-cell} ipython3
471
472
def calc_c_star(s):
472
-
k = ((s * A) / delta)**(1/(1 - alpha))
473
-
return - (1 - s) * A * k ** alpha
473
+
k = ((s * A) / δ)**(1/(1 - α))
474
+
return - (1 - s) * A * k ** α
474
475
```
475
476
476
477
```{code-cell} ipython3
@@ -510,21 +511,26 @@ plt.show()
510
511
One can also try to solve this mathematically by differentiating $c^*(s)$ and solve for $\frac{d}{ds}c^*(s)=0$ using [sympy](https://www.sympy.org/en/index.html).
511
512
512
513
```{code-cell} ipython3
513
-
from sympy import solve, Symbol
514
+
from sympy import solve, Symbol, Rational
514
515
```
515
516
516
517
```{code-cell} ipython3
518
+
# Use Rational for exact symbolic computation
519
+
A = Rational(2)
520
+
α = Rational(3, 10)
521
+
δ = Rational(1, 2)
522
+
517
523
s_symbol = Symbol('s', real=True)
518
-
k = ((s_symbol * A) / delta)**(1/(1 - alpha))
519
-
c = (1 - s_symbol) * A * k ** alpha
524
+
k = ((s_symbol * A) / δ)**(1/(1 - α))
525
+
c = (1 - s_symbol) * A * k ** α
520
526
```
521
527
522
528
Let's differentiate $c$ and solve using [sympy.solve](https://docs.sympy.org/latest/modules/solvers/solvers.html#sympy.solvers.solvers.solve)
523
529
524
530
```{code-cell} ipython3
525
531
# Solve using sympy
526
532
s_star = solve(c.diff())[0]
527
-
print(f"s_star = {s_star}")
533
+
print(f"s_star = {float(s_star)}")
528
534
```
529
535
530
536
Incidentally, the rate of savings which maximizes steady state level of per capita consumption is called the [Golden Rule savings rate](https://en.wikipedia.org/wiki/Golden_Rule_savings_rate).
@@ -576,23 +582,23 @@ Let's define the constants for lognormal distribution and initial values used fo
576
582
577
583
```{code-cell} ipython3
578
584
# Define the constants
579
-
sig = 0.2
580
-
mu = np.log(2) - sig**2 / 2
585
+
σ = 0.2
586
+
μ = np.log(2) - σ**2 / 2
581
587
A = 2.0
582
588
s = 0.6
583
-
alpha = 0.3
584
-
delta = 0.5
589
+
α = 0.3
590
+
δ = 0.5
585
591
x0 = [.25, 3.25] # list of initial values used for simulation
586
592
```
587
593
588
594
Let's define the function *k_next* to find the next value of $k$
0 commit comments