@@ -566,25 +566,25 @@ def y_nonstochastic(y_0=100, y_1=80, a=.92, b=.5, γ=10, n=80):
566566 ρ1 = a + b
567567 ρ2 = -b
568568
569- print(f'ρ_1 is {ρ1}')
570- print(f'ρ_2 is {ρ2}')
569+ print(f'ρ_1 is {ρ1:.2f }')
570+ print(f'ρ_2 is {ρ2:.2f }')
571571
572572 discriminant = ρ1 ** 2 + 4 * ρ2
573573
574574 if discriminant == 0:
575575 roots.append(-ρ1 / 2)
576576 print('Single real root: ')
577- print(''.join(str( roots) ))
577+ print(''.join(f"{r:.2f}" for r in roots))
578578 elif discriminant > 0:
579579 roots.append((-ρ1 + sqrt(discriminant).real) / 2)
580580 roots.append((-ρ1 - sqrt(discriminant).real) / 2)
581581 print('Two real roots: ')
582- print(''.join(str( roots) ))
582+ print(' '.join(f"{r:.2f}" for r in roots))
583583 else:
584584 roots.append((-ρ1 + sqrt(discriminant)) / 2)
585585 roots.append((-ρ1 - sqrt(discriminant)) / 2)
586586 print('Two complex roots: ')
587- print(''.join(str( roots) ))
587+ print(' '.join(f"{r.real:.2f}{r.imag:+.2f}j" for r in roots))
588588
589589 if all(abs(root) < 1 for root in roots):
590590 print('Absolute values of roots are less than one')
@@ -640,14 +640,14 @@ Now let's use the function in an example. Here are the example parameters:
640640
641641``` {code-cell} ipython3
642642r = .95
643- period = 10 # Length of cycle in units of time
643+ period = 10 # Length of cycle in units of time
644644ϕ = 2 * math.pi/period
645645
646646## Apply the function
647647ρ1, ρ2, a, b = f(r, ϕ)
648648
649- print(f"a, b = {a}, {b}")
650- print(f"ρ1, ρ2 = {ρ1}, {ρ2}")
649+ print(f"a, b = {a:.2f }, {b:.2f }")
650+ print(f"ρ1, ρ2 = {ρ1:.2f }, {ρ2:.2f }")
651651```
652652
653653``` {code-cell} ipython3
@@ -656,7 +656,7 @@ print(f"ρ1, ρ2 = {ρ1}, {ρ2}")
656656ρ1 = ρ1.real
657657ρ2 = ρ2.real
658658
659- ρ1 , ρ2
659+ print(f"ρ1 = {ρ1:.2f} , ρ2 = {ρ2:.2f}")
660660```
661661
662662### Root finding using numpy
@@ -670,28 +670,28 @@ r1, r2 = np.roots([1, -ρ1, -ρ2])
670670p1 = cmath.polar(r1)
671671p2 = cmath.polar(r2)
672672
673- print(f"r, ϕ = {r}, {ϕ}")
674- print(f"p1, p2 = {p1}, {p2} ")
675- # print(f"g1, g2 = {g1}, {g2}")
673+ print(f"r, ϕ = {r:.2f }, {ϕ:.2f }")
674+ print(f"p1, p2 = ( {p1[0]:.2f }, {p1[1]:.2f}), ({p2[0]:.2f}, {p2[1]:.2f}) ")
675+ # print(f"g1, g2 = {g1:.2f }, {g2:.2f }")
676676
677- print(f"a, b = {a}, {b}")
678- print(f"ρ1, ρ2 = {ρ1}, {ρ2}")
677+ print(f"a, b = {a:.2f }, {b:.2f }")
678+ print(f"ρ1, ρ2 = {ρ1:.2f }, {ρ2:.2f }")
679679```
680680
681681``` {code-cell} ipython3
682682##=== This method uses numpy to calculate roots ===#
683683
684684
685- def y_nonstochastic(y_0=100, y_1=80, a =.9, b =.8, γ=10, n=80):
685+ def y_nonstochastic(y_0=100, y_1=80, α =.9, β =.8, γ=10, n=80):
686686
687687 """ Rather than computing the roots of the characteristic
688688 polynomial by hand as we did earlier, this function
689689 enlists numpy to do the work for us
690690 """
691691
692692 # Useful constants
693- ρ1 = a + b
694- ρ2 = -b
693+ ρ1 = α + β
694+ ρ2 = -β
695695
696696 categorize_solution(ρ1, ρ2)
697697
@@ -739,16 +739,16 @@ r = 1 # Generates undamped, nonexplosive cycles
739739period = 10 # Length of cycle in units of time
740740ϕ = 2 * math.pi/period
741741
742- # Apply the reverse-engineering function f
742+ ## Apply the reverse-engineering function f
743743ρ1, ρ2, a, b = f(r, ϕ)
744744
745745# Drop the imaginary part so that it is a valid input into y_nonstochastic
746746a = a.real
747747b = b.real
748748
749- print(f"a, b = {a}, {b}")
749+ print(f"a, b = {a:.2f }, {b:.2f }")
750750
751- ytemp = y_nonstochastic(a =a, b =b, y_0=20, y_1=30)
751+ ytemp = y_nonstochastic(α =a, β =b, y_0=20, y_1=30)
752752plot_y(ytemp)
753753```
754754
@@ -783,7 +783,7 @@ demand
783783
784784``` {code-cell} ipython3
785785def y_stochastic(y_0=0, y_1=0, a=0.8, b=0.2, γ=10, n=100, σ=5):
786-
786+
787787 """This function takes parameters of a stochastic version of
788788 the model and proceeds to analyze the roots of the characteristic
789789 polynomial and also generate a simulation.
@@ -798,26 +798,26 @@ def y_stochastic(y_0=0, y_1=0, a=0.8, b=0.2, γ=10, n=100, σ=5):
798798
799799 # Find roots of polynomial
800800 roots = np.roots([1, -ρ1, -ρ2])
801- print(roots)
801+ print(f"Roots are {[f'{root:.2f}' for root in roots]}" )
802802
803803 # Check if real or complex
804804 if all(isinstance(root, complex) for root in roots):
805- print(' Roots are complex' )
805+ print(" Roots are complex" )
806806 else:
807- print(' Roots are real' )
807+ print(" Roots are real" )
808808
809809 # Check if roots are less than one
810810 if all(abs(root) < 1 for root in roots):
811- print(' Roots are less than one' )
811+ print(" Roots are less than one" )
812812 else:
813- print(' Roots are not less than one' )
813+ print(" Roots are not less than one" )
814814
815815 # Generate shocks
816816 ϵ = np.random.normal(0, 1, n)
817817
818818 # Define transition equation
819- def transition(x, t): return ρ1 * \
820- x[t - 1] + ρ2 * x[t - 2] + γ + σ * ϵ[t]
819+ def transition(x, t):
820+ return ρ1 * x[t - 1] + ρ2 * x[t - 2] + γ + σ * ϵ[t]
821821
822822 # Set initial conditions
823823 y_t = [y_0, y_1]
@@ -836,19 +836,18 @@ Let's do a simulation in which there are shocks and the characteristic polynomia
836836``` {code-cell} ipython3
837837r = .97
838838
839- period = 10 # Length of cycle in units of time
839+ period = 10 # Length of cycle in units of time
840840ϕ = 2 * math.pi/period
841841
842- ### Apply the reverse-engineering function f
843-
842+ # Apply the reverse-engineering function f
844843ρ1, ρ2, a, b = f(r, ϕ)
845844
846845# Drop the imaginary part so that it is a valid input into y_nonstochastic
847846a = a.real
848847b = b.real
849848
850- print(f"a, b = {a}, {b}")
851- plot_y(y_stochastic(y_0=40, y_1 = 42, a=a, b=b, σ=2, n=100))
849+ print(f"a, b = {a:.2f }, {b:.2f }")
850+ plot_y(y_stochastic(y_0=40, y_1= 42, a=a, b=b, σ=2, n=100))
852851```
853852
854853## Government spending
@@ -1248,7 +1247,7 @@ calculating the eigenvalues of $A$
12481247``` {code-cell} ipython3
12491248A = np.asarray(A)
12501249w, v = np.linalg.eig(A)
1251- print(w )
1250+ print(np.round(w, 2) )
12521251```
12531252
12541253### Inheriting methods from ` LinearStateSpace `
0 commit comments