示例#1
0
        def compute_stability_exponent(n=1000, num_reps=10000, seed=1234): 
            """
            Uses fact that

                M_j = β**θ * exp(g_d_j - γ * g_c_j) * (w(x_j) / (w(x_{j-1}) - 1)**(θ - 1)

            where w is the value function.
            """
            phi_obs = np.empty(num_reps)

            np.random.seed(seed)

            for m in prange(num_reps):


                # Reset accumulator and state to initial conditions
                phi_sum = 1.0
                z, h_z, h_c, h_d = z_0, h_z_0, h_c_0, h_d_0

                # Calculate W_0
                r = z, h_z, h_c
                W = lininterp_3d(z_grid, h_z_grid, h_c_grid, w_star, r)

                # We'll accumulate using 4 terms
                a1 = 0.0
                a2 = 0.0
                a3 = 0.0
                a4 = 0.0

                for t in range(n):
                    # Update state 
                    σ_z = τ_z * exp(h_z)
                    z = ρ * z + κ * σ_z * randn()
                    h_z = ρ_hz * h_z + σ_hz * randn()
                    h_c = ρ_hc * h_c + σ_hc * randn()
                    h_d = ρ_hd * h_d + σ_hd * randn()
                    # Map h to σ for the updated states
                    σ_c = τ_c * exp(h_c)
                    σ_d = τ_d * exp(h_d)
                    # Calculate w_hat
                    r = z, h_z, h_c
                    W_next = lininterp_3d(z_grid, h_z_grid, h_c_grid, w_star, r)
                    # Accumulate
                    a1 += z
                    a2 += σ_c * randn()
                    a3 += σ_d * randn()
                    a4 += log(W_next / (W - 1.0))
                    # Update W
                    W = W_next

                total = (α - γ) * a1 + (δ - γ) * a2 + a3 + (θ - 1.0) * a4
                phi_obs[m] = exp(total)

            return θ * log(β) + μ_d - γ * μ_c + (1/n) * log(np.mean(phi_obs))
示例#2
0
        def compute_spec_rad(z_0=z_0, h_z_0=h_z_0, h_c_0=h_c_0, h_d_0=h_d_0):
            """
            Uses fact that

                M_j = β**θ * exp(g_d_j - γ * g_c_j) * (w(x_j) / 
                                (w(x_{j-1}) - 1)**(θ - 1)

            where w is the value function.
            """
            phi_obs = np.empty(num_reps)

            for m in prange(num_reps):

                # Set seed
                np.random.seed(m)

                # Reset accumulator and state to initial conditions
                phi_prod = 1.0
                z, h_z, h_c, h_d = z_0, h_z_0, h_c_0, h_d_0

                # Calculate W_0
                r = z, h_z, h_c
                W = lininterp_3d(z_grid, h_z_grid, h_c_grid, w_star, r)

                for t in range(n):
                    # Map h to σ (only h_z required at this step)
                    σ_z = τ_z * exp(h_z)
                    # Update state to t+1
                    z = ρ * z + κ * σ_z * randn()
                    h_z = ρ_hz * h_z + σ_hz * randn()
                    h_c = ρ_hc * h_c + σ_hc * randn()
                    h_d = ρ_hd * h_d + σ_hd * randn()
                    # Map h to σ for the updated states
                    σ_c = τ_c * exp(h_c)
                    σ_d = τ_d * exp(h_d)
                    # Calculate g^c_{t+1}
                    g_c = μ_c + z + σ_c * randn()
                    # Calculate g^d_{t+1}
                    g_d = μ_d + α * z + δ * σ_c * randn() + σ_d * randn()
                    # Calculate W_{t+1}
                    r = z, h_z, h_c
                    W_next = lininterp_3d(z_grid, h_z_grid, h_c_grid, w_star,
                                          r)
                    # Calculate M_{t+1} without β**θ
                    M = exp(g_d - γ * g_c) * (W_next / (W - 1))**(θ - 1)
                    # Update phi_prod and store W_next for following step
                    phi_prod = phi_prod * M
                    W = W_next

                phi_obs[m] = phi_prod

            return β**θ * np.mean(phi_obs)**(1 / n)
示例#3
0
        def T(w):
            g = w**θ
            Kg = np.empty_like(g)

            # Apply the operator K to g, computing Kg
            for i in prange(nz):
                z = z_grid[i]
                for k in range(nh_c):
                    h_c = h_c_grid[k]
                    σ_c = τ_c * exp(h_c)
                    mf = exp((1 - γ) * (μ_c + z) + (1 - γ)**2 * σ_c**2 / 2)
                    for j in range(nh_z):
                        h_z = h_z_grid[j]
                        σ_z = τ_z * exp(h_z)
                        g_exp = 0.0
                        for l in range(ns):
                            η, ω, ϵ = shocks[:, l]
                            zp = ρ * z + κ * σ_z * η
                            h_cp = ρ_hc * h_c + σ_hc * ω
                            h_zp = ρ_hz * h_z + σ_hz * ϵ
                            r = zp, h_zp, h_cp
                            g_exp += lininterp_3d(z_grid, h_z_grid, h_c_grid, g, r)
                        Kg[i, j, k] = mf * (g_exp / ns)

            return 1.0 + β * Kg**(1/θ)  # Tw