# Using conditionals in @defsim, and add\_RV! for vectors

**URL:** <https://forum.mimiframework.org/t/using-conditionals-in-defsim-and-add-rv-for-vectors/237>\
**Category:** Uncategorized\
**Created:** [January 27, 2023, 4:28pm UTC](https://forum.mimiframework.org/t/using-conditionals-in-defsim-and-add-rv-for-vectors/237 "2023-01-27T16:28:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jrising](https://yyz1.discourse-cdn.com/flex031/user_avatar/forum.mimiframework.org/jrising/32/7_2.png) [@jrising](https://forum.mimiframework.org/u/jrising)\
**Post date:** [January 27, 2023, 4:28pm UTC](https://forum.mimiframework.org/t/using-conditionals-in-defsim-and-add-rv-for-vectors/237/1 "2023-01-27T16:28:43Z")

</div>

I am setting up a Monte Carlo simulation, but I have a lot of different cases where different components may or may not be included or set to vary.

My first thought was just to include conditional statements in the `@defsim`, but the macro does not appear to be able to handle these.

So, I have used `add_RV!`, and that works for most of my cases. The problem is for vectors of random variables, like

```auto
OMH_uniforms[:] = Uniform(0, 1)

```

It looks like I ought to be able to use a statement like

```auto
add_RV!(sim, :OMH_uniforms, ReshapedDistribution([dim_count(model, :time)], Uniform(0, 1)))

```

but doing so throws a `MethodError: no method matching length(::ReshapedDistribution)` error. What’s the right way to do this?

---

<div class="post-metadata">

**Author:** ![lrennels](https://yyz1.discourse-cdn.com/flex031/user_avatar/forum.mimiframework.org/lrennels/32/6_2.png) [@lrennels](https://forum.mimiframework.org/u/lrennels)\
**Post date:** [January 27, 2023, 5:35pm UTC](https://forum.mimiframework.org/t/using-conditionals-in-defsim-and-add-rv-for-vectors/237/2 "2023-01-27T17:35:01Z")

</div>

Hi @jrising this is an excellent question that I also struggled with while working on the GIVE mcs specification, and the same as you ended up creating a (almost) blank `SimulationDefinition` and then used pairs of `add_RV!` and `add_transform!`. In this case I was almost always applying one RV per element, as opposed to many elements to the same RV, which can be done I a similar but slightly different way. I think you also are trying to do the former.

Our GIVE one is here: [MimiGIVE.jl/main\_mcs.jl at main · rffscghg/MimiGIVE.jl · GitHub](https://github.com/rffscghg/MimiGIVE.jl/blob/main/src/main_mcs.jl) but this is something I’m happy to talk about further since I know it’s complicated and I would like to improve this aspect of Mimi … `@defsim` is quite restrictive.

---

<div class="post-metadata">

**Author:** ![lrennels](https://yyz1.discourse-cdn.com/flex031/user_avatar/forum.mimiframework.org/lrennels/32/6_2.png) [@lrennels](https://forum.mimiframework.org/u/lrennels)\
**Post date:** [January 27, 2023, 5:40pm UTC](https://forum.mimiframework.org/t/using-conditionals-in-defsim-and-add-rv-for-vectors/237/3 "2023-01-27T17:40:09Z")

</div>

My general approach was to loop over the individual elements, which were sometimes countries (like this: [MimiGIVE.jl/main\_mcs.jl at eaa9413d70dab7ad339ade0beb8ec734eb27a741 · rffscghg/MimiGIVE.jl · GitHub](https://github.com/rffscghg/MimiGIVE.jl/blob/eaa9413d70dab7ad339ade0beb8ec734eb27a741/src/main_mcs.jl#L204)) and sometimes years (like this: [MimiGIVE.jl/main\_mcs.jl at eaa9413d70dab7ad339ade0beb8ec734eb27a741 · rffscghg/MimiGIVE.jl · GitHub](https://github.com/rffscghg/MimiGIVE.jl/blob/eaa9413d70dab7ad339ade0beb8ec734eb27a741/src/main_mcs.jl#L135)) or even both in horrid cases 🙂

The important part there is that I’m assigning one RV per element, not every element to the same RV. That second one is an option too with similar techniques (like what happens inside the macro when we use these techniques: [3 Monte Carlo + SA · Mimi.jl](https://www.mimiframework.org/Mimi.jl/stable/howto/howto_3/#Apply-RVs-to-model-parameters:-Assigning-to-array-slices-1) - Assigning to array slices and Assigning a vector of distributions) but I don’t think that’s your intention.

Now I admit I did this about 6 months ago, and I am now very curious to know if that’s really the best way or more efficient way, so I am now going to look at the macro code to think about if there’s a better way which I am sure there is … I just need to dig into the Mimi code a bit and the way the macro splices teh calls out into calls of `add_RV!` and `add_transform!`.

---

<div class="post-metadata">

**Author:** ![lrennels](https://yyz1.discourse-cdn.com/flex031/user_avatar/forum.mimiframework.org/lrennels/32/6_2.png) [@lrennels](https://forum.mimiframework.org/u/lrennels)\
**Post date:** [January 27, 2023, 5:51pm UTC](https://forum.mimiframework.org/t/using-conditionals-in-defsim-and-add-rv-for-vectors/237/4 "2023-01-27T17:51:49Z")

</div>

`ReshapedDistribution` seems like a good alternative approach to me … a Mimi developer (not me) created that type in Mimi and probably just didn’t define `length` so it’s a method I’d think we could just define. I’m embarrassed to say I forgot about that type as an option but think it does look promising. I will dig into this more today. I recreated your error with the code below

```auto
using Mimi, Distributions

@defcomp mycomp begin
    p = Parameter(index=[countries])
    function run_timestep(p,v,d,t)
    end
end

m = Model()
set_dimension!(m, :time, 2000:2010)
set_dimension!(m, :countries, [:a, :b, :c, :d, :e])
add_comp!(m, mycomp)
update_param!(m, :mycomp, :p, fill(1., 5))

sd = @defsim begin end
Mimi.add_RV!(sd, :rv_p, ReshapedDistribution([10], Uniform(0,1)))
Mimi.add_transform!(sd, :mycomp, :p, :(=), :rv_p)

run(sd, m, 10)

```

and will dig into it, but my gut would say that type isn’t very robust to different use cases and it might be preferable to try to mirror the methods I used ie. use loops to add one RV and one transform per vector element for now, although there may be a more efficient way that I can absolutely look into!
