Quantcast
Channel: First steps - JuliaLang
Viewing all articles
Browse latest Browse all 2795

Updating Values via Iteration

$
0
0

I’m having a few issues with using a while loop to update values and return. The code seems to work fine when wrapped in functions but not otherwise. As I’m still learning the language I want to understand where I’m going wrong. I’m fairly sure it’s to do with scoping rules that I don’t fully understand.

The issue is that whenever I run the code it seems to converge at iteration 1 with error 0.0 even though the pre and updated values are clearly different. Somewhere in my code I must be setting the pre values equal to the updated without intending too. Please see the code below. The variables of interest are policy_new and policy. I apologise if the code is rather poor; as I mentioned I’m still learning. Any help and advice is greatly appreciated. Feel free to comment on all aspects.

using Plots
using SparseArrays
using LinearAlgebra


#                     Model Parameters, Grid, Utility                                    

β = 0.95;
α = 0.65;
δ = 1;
cover = 0.1;
N = 5;
k_ss = ((1/β-1+δ)/α)^(1/(α-1));
k_grid = range((1-cover)*k_ss, (1+cover)*k_ss, length=N);
max_iter = 500;
diff = Inf; 
tol = 1e-6;
u(c) =  c <= 0 ? -Inf : log(c);

function find_maximiser(A)
    v, B = findmax(A, dims=2)
    B = getindex.(B,2)
    return v, B
end


#                    Start Iterations                                         
policy = k_grid
v = u.(k_grid.^α)./(1-β)

for iter ∈ 1:max_iter
    global policy
    global v
    
    V_matrix = repeat(v, 1, N)
    c_matrix = k_grid.^α .+ (1-δ).*k_grid .- transpose(k_grid);
    v, policy_idx = find_maximiser(u.(c_matrix) + β*V_matrix);
    
    policy_new = k_grid[policy_idx];
    
    display(policy_new)
    display(policy)
    c_opt = k_grid.^α+(1-δ)*k_grid-policy;

    Q = spzeros(N,N);
    for i=1:N
        Q[i, policy_idx[i]] = 1;
    end

    v_new = (Matrix(I,N,N)-β*Q) \ u.(c_opt);
    diff = maximum(abs.(policy_new - policy));
    println("Iteration $iter, Error = $diff")

    v = v_new;
    policy = policy_new;

    if diff < tol
        println("Solved at Iteration $iter, Error = $diff")
        return policy, policy_idx 
        break 
    end 

end

3 posts - 2 participants

Read full topic


Viewing all articles
Browse latest Browse all 2795

Latest Images

Trending Articles



Latest Images