# Value function backward induction

**URL:** <https://discourse.julialang.org/t/value-function-backward-induction/45097>\
**Category:** Finance and Economics\
**Created:** [August 17, 2020, 9:52am UTC](https://discourse.julialang.org/t/value-function-backward-induction/45097 "2020-08-17T09:52:18Z")\
**Posts on this page:** 4\
**Page:** 2

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [August 22, 2020, 3:58pm UTC](https://discourse.julialang.org/t/value-function-backward-induction/45097/21 "2020-08-22T15:58:14Z")

</div>

The way I’ve done this (or similar things) before is to compute a set of indices 1:N such that each index corresponds to a point in the state space. So you would distribute over that 1:N, and then construct which point it corresponds to within the loop.

Eg instead of

```julia
nb = 12
nz = 8

@sync @distributed for ib = 1:nb
      for z = 1:nz
              vf = solve_problem(params,ib,z,age)
      end
end

```

you would do something like

```julia
nb = 12
nz = 8
N = nb*nz
@sync @distributed for i = 1:N
    ib,iz = indicesfrom(i,nb,nz)  
    vf = solve_problem(params,ib,iz,age)
end

```

where you just need to figure out what that `indicesfrom` function should look like. `CartesianIndices` should help.

---

<div class="post-metadata">

**Author:** ![Joao\_Barata](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joao_barata/32/10082_2.png) [@Joao\_Barata](https://discourse.julialang.org/u/Joao_Barata)\
**Post date:** [August 22, 2020, 4:25pm UTC](https://discourse.julialang.org/t/value-function-backward-induction/45097/22 "2020-08-22T16:25:55Z")

</div>

Thank you very much for your suggestion. It makes a lot of sense

---

<div class="post-metadata">

**Author:** ![RJDennis](https://avatars.discourse-cdn.com/v4/letter/r/90db22/32.png) [@RJDennis](https://discourse.julialang.org/u/RJDennis)\
**Post date:** [August 22, 2020, 4:49pm UTC](https://discourse.julialang.org/t/value-function-backward-induction/45097/23 "2020-08-22T16:49:36Z")

</div>

CartesianIndices((nb,nz))[i]

---

<div class="post-metadata">

**Author:** ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)\
**Post date:** [August 22, 2020, 4:57pm UTC](https://discourse.julialang.org/t/value-function-backward-induction/45097/24 "2020-08-22T16:57:21Z")

</div>

I thought `@distributed` did that automatically, but looking at the internals that’s not the case. I will have to fix that in my code [https://github.com/amrods/vfi-fh-dp/blob/master/VFI-alg-parallel.jl](https://github.com/amrods/vfi-fh-dp/blob/master/VFI-alg-parallel.jl) 🤷‍♂️

[Previous page](https://discourse.julialang.org/t/value-function-backward-induction/45097.md?page=1)
