Bounds Error In code


I keep getting a bounds error and im not sure how to fix it

# Define a function to simulate worker paths
function simulate_worker_paths(solution, a0, y0, T, N)
    @unpack kgrid, ygrid, policy_c, policy_k, P = solution
    # Find the indices of a0 and y0 in the grids
    a0_idx = searchsortedfirst(kgrid, a0)
    y0_idx = searchsortedfirst(ygrid, y0)
    # Initialize arrays to store the paths
    a_paths = zeros(T, N)
    y_paths = zeros(T, N)
    c_paths = zeros(T, N)
    # Set the initial values
    a_paths[1, :] .= a0  
    y_paths[1, :] .= y0  
    # Simulate the paths
    for t in 2:T
        # Draw random values for y
        z = rand(N)
        y_idx = searchsortedfirst(cumsum(P[y0_idx, :]), z)
        y_paths[t, :] .= ygrid[y_idx]  
        # Update asset and consumption paths
        for n in 1:N
            # Check bounds before accessing elements
            if a0_idx > length(kgrid)
                a0_idx = length(kgrid)
            if y_idx[n] > length(ygrid)
                y_idx[n] = length(ygrid)
            a_paths[t, n] = kgrid[policy_k[a0_idx, y_idx[n]]]  
            c_paths[t, n] = policy_c[a0_idx, y_idx[n]]  

        # Update the indices for the next period
        a0_idx = searchsortedfirst(kgrid, a_paths[t, 1])  # Assuming all paths start from the same initial asset level
        y0_idx = y_idx
    return a_paths, y_paths, c_paths

# Define a function to plot asset, income, and consumption paths
function plot_worker_paths(solution, a0, y0, T, N)
    # Simulate worker paths
    a_paths, y_paths, c_paths = simulate_worker_paths(solution, a0, y0, T, N)
    # Plot the paths
    p1 = plot(legend=:outerbottom, xlabel="Period", ylabel="Asset", title="Asset Paths")
    p2 = plot(legend=:outerbottom, xlabel="Period", ylabel="Income", title="Income Paths")
    p3 = plot(legend=:outerbottom, xlabel="Period", ylabel="Consumption", title="Consumption Paths")
    for n in 1:N
        plot!(p1, 1:T, a_paths[:, n], label="Worker $n")
        plot!(p2, 1:T, y_paths[:, n], label="Worker $n")
        plot!(p3, 1:T, c_paths[:, n], label="Worker $n")
    plt = plot(p1, p2, p3, layout=(1,3), size=(900, 400))
    return plt

# Set initial conditions and simulation parameters
a0 = kgrid[Int(length(kgrid)/2)]  # Starting point at the midpoint of asset grid
y0 = exp(mean(solution.ygrid))  # Starting point at the midpoint of income grid
T = 100  # Number of model periods
N = 5  # Number of sample paths

# Plot worker paths
plot_worker_paths(solution, a0, y0, T, N)

The answer to your question is generally yes, but likely only after you’ve read

and adapted your post accordingly.

Key things:

  • Don’t post screenshots
  • Make your code reproducible (i.e. don’t use variables defined outside the code you’re posting, create dummy data if necessary)
  • Tell people what the actual problem is

AS @nilshg says - please post the code using backticks. Anyone trying to help can then cut-and-paste into the REPL.
Looking quickly at the screenshot you do not have any using statements to include packages.

I see that you’ve updated your original post to include the source code from the screenshot. Can you include the exact error message you’re getting. It’s hard to diagnose the problem if we don’t know where in this code the error gets thrown from.