@@ -662,7 +662,7 @@ def plot_y(function=None):
662662The following function calculates roots of the characteristic polynomial
663663using high school algebra.
664664
665- (We'll calculate the roots in other ways later)
665+ (We'll calculate the roots in other ways later using ` analyze_roots ` . )
666666
667667The function also plots a $Y_t$ starting from initial conditions
668668that we set
@@ -673,29 +673,45 @@ def y_nonstochastic(y_0=100, y_1=80, α=0.92, β=0.5, γ=10, n=80):
673673 This function calculates the roots of the characteristic polynomial
674674 by hand and returns a path of y_t starting from initial conditions
675675 """
676+ roots = []
676677
677- y_series, analysis = simulate_samuelson(y_0, y_1, α, β, γ, n, 0, None, 42)
678-
679- print(f"ρ_1 is {analysis['rho1']:.2f}")
680- print(f"ρ_2 is {analysis['rho2']:.2f}")
681-
682- roots = analysis['roots']
683- if len(roots) == 1:
678+ ρ1 = α + β
679+ ρ2 = -β
680+
681+ print(f"ρ_1 is {ρ1:.2f}")
682+ print(f"ρ_2 is {ρ2:.2f}")
683+
684+ discriminant = ρ1**2 + 4 * ρ2
685+
686+ if discriminant == 0:
687+ roots.append(-ρ1 / 2)
684688 print("Single real root: ")
685- print(f"{roots[0]:.2f}")
686- elif analysis['is_complex']:
687- print("Two complex roots: ")
688- print(" ".join(f"{r.real:.2f}{r.imag:+.2f}j" for r in roots))
689- else:
689+ print("".join(f"{r:.2f}" for r in roots))
690+ elif discriminant > 0:
691+ roots.append((-ρ1 + sqrt(discriminant).real) / 2)
692+ roots.append((-ρ1 - sqrt(discriminant).real) / 2)
690693 print("Two real roots: ")
691694 print(" ".join(f"{r:.2f}" for r in roots))
692-
693- if analysis['is_stable']:
695+ else:
696+ roots.append((-ρ1 + sqrt(discriminant)) / 2)
697+ roots.append((-ρ1 - sqrt(discriminant)) / 2)
698+ print("Two complex roots: ")
699+ print(" ".join(f"{r.real:.2f}{r.imag:+.2f}j" for r in roots))
700+
701+ if all(abs(root) < 1 for root in roots):
694702 print("Absolute values of roots are less than one")
695703 else:
696704 print("Absolute values of roots are not less than one")
697-
698- return y_series
705+
706+ def transition(x, t):
707+ return ρ1 * x[t - 1] + ρ2 * x[t - 2] + γ
708+
709+ y_t = [y_0, y_1]
710+
711+ for t in range(2, n):
712+ y_t.append(transition(y_t, t))
713+
714+ return y_t
699715
700716
701717plot_y(y_nonstochastic())
0 commit comments