pythonclassmethods

Python Class to run the equation but can't understand one of def ()


I'm self-learning python and it teaches concept of "Class" from https://python-programming.quantecon.org/python_oop.html

The below is full codes. I can understand that

  1. def h() is the one calculating the equation (the image at the bottom has equation)
  2. def update() is the one calculating t and t+1
  3. def denerate_sequecence() is the one append all returns.

But my question is: anybody can explain what def steady_state() is? I can't find where this computing is originated from.

(((s * z) / (n + δ))**(1 / (1 - α))

Am I lacking of understanding this equation, or understanding how class and methods are interacting?

class Solow:
    r"""
    Implements the Solow growth model with the update rule

        k_{t+1} = [(s z k^α_t) + (1 - δ)k_t] /(1 + n)

    """
    def __init__(self, n=0.05,  # population growth rate
                       s=0.25,  # savings rate
                       δ=0.1,   # depreciation rate
                       α=0.3,   # share of labor
                       z=2.0,   # productivity
                       k=1.0):  # current capital stock

        self.n, self.s, self.δ, self.α, self.z = n, s, δ, α, z
        self.k = k

    def h(self):
        "Evaluate the h function"
        # Unpack parameters (get rid of self to simplify notation)
        n, s, δ, α, z = self.n, self.s, self.δ, self.α, self.z
        # Apply the update rule
        return (s * z * self.k**α + (1 - δ) * self.k) / (1 + n)

    def update(self):
        "Update the current state (i.e., the capital stock)."
        self.k =  self.h()

    def steady_state(self):
        "Compute the steady state value of capital."
        # Unpack parameters (get rid of self to simplify notation)
        n, s, δ, α, z = self.n, self.s, self.δ, self.α, self.z
        # Compute and return steady state
        return ((s * z) / (n + δ))**(1 / (1 - α))

    def generate_sequence(self, t):
        "Generate and return a time series of length t"
        path = []
        for i in range(t):
            path.append(self.k)
            self.update()
        return path 

enter image description here


Solution

  • The code provided may not be correct. You are missing an explanation of how the formula was derived. solow.steady_state() essentially solves for k when k_t=k_t+1.

    In the provided formula, we set all these k and solve for k:

    k = ( sz(k**a) + (1 - δ)k ) / (1 + n)

    Swap k and (1 + n):

    1+n = ( sz(k**a) + (1 - δ)k ) / k

    Simplify expression on the right:

    1+n = sz(k**a)/k + 1 - δ

    Again:

    1+n = sz(k**(a-1)) + 1 - δ

    Cancel the ones and move δ:

    n + δ= sz(k**(a-1))

    Move sz over:

    (n + δ) / sz = k**(a-1)

    From here, remove the exponent:

    ((n + δ) / sz)**(1 / (1 - α)) = k

    The expression in the code is essentially similar:

    ((sz) / (n + δ))**(1 / (1 - α) = k

    I do not know why this code has the expressions flipped from how I solved it, but if it works, I assume there is some reason I am not see that reversing those two terms works. Perhaps it is computing 1/k instead of k directly, which would be the best explanation if it produces the desired results. Or the other possibility is the k always equals 1/k for some logical reason in this equation.