# Set a parameter as a distribution

**URL:** <https://forum.mimiframework.org/t/set-a-parameter-as-a-distribution/106>\
**Category:** Uncategorized\
**Created:** [December 8, 2019, 8:26pm UTC](https://forum.mimiframework.org/t/set-a-parameter-as-a-distribution/106 "2019-12-08T20:26:50Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![FrankErrickson](https://yyz1.discourse-cdn.com/flex031/user_avatar/forum.mimiframework.org/frankerrickson/32/13_2.png) [@FrankErrickson](https://forum.mimiframework.org/u/FrankErrickson)\
**Post date:** [December 8, 2019, 8:26pm UTC](https://forum.mimiframework.org/t/set-a-parameter-as-a-distribution/106/1 "2019-12-08T20:26:50Z")

</div>

Some old code I had set a parameter as a distribution.

```auto
lws_distribution::Distributions.Normal{Float64} = Parameter()

```

And then I would sample it within the Mimi model equations:

```auto
v.lws_sea_level[t] = v.lws_sea_level[t-1] + rand(p.lws_distribution)

```

This code no longer works. I get `ERROR: MethodError: no method matching copy(::Normal{Float64})`. I’m assuming Mimi internally creates a copy of this distribution which no longer seems to work. Is there a workaround or alternative Mimi syntax I’m not aware of?

---

<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:** [December 14, 2019, 1:31am UTC](https://forum.mimiframework.org/t/set-a-parameter-as-a-distribution/106/2 "2019-12-14T01:31:16Z")

</div>

Hi @FrankErrickson, thanks for pointing this out. It looks like a Mimi bug, since you should be able to set a parameter as a `Distribution` type, but we hadn’t thought of that use case so we try to call `copy` and there is no `copy` method for `Distribution` (which is immutable so it doesn’t need one), as the error message shows. I can add a bug fix to this next week, if it’s time sensitive I can also give you a work around in the meantime!

Issue: [https://github.com/mimiframework/Mimi.jl/issues/640](https://github.com/mimiframework/Mimi.jl/issues/640)

---

<div class="post-metadata">

**Author:** ![FrankErrickson](https://yyz1.discourse-cdn.com/flex031/user_avatar/forum.mimiframework.org/frankerrickson/32/13_2.png) [@FrankErrickson](https://forum.mimiframework.org/u/FrankErrickson)\
**Post date:** [December 14, 2019, 7:04pm UTC](https://forum.mimiframework.org/t/set-a-parameter-as-a-distribution/106/3 "2019-12-14T19:04:59Z")

</div>

Hey @lrennels, thanks. Definitely not time sensitive, it’s just a minor component in BRICK. For now I’m just sampling outside the model and passing the uncertain values in as a Mimi parameter which works fine. Whenever it’s updated in Mimi, I can switch back to to original distribution version.

---

<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:** [December 17, 2019, 11:45pm UTC](https://forum.mimiframework.org/t/set-a-parameter-as-a-distribution/106/4 "2019-12-17T23:45:29Z")

</div>

@FrankErrickson I’m taking a look at this now and want to recreate what you were doing to make sure I get supporting it right. What did you initially set the parameter to in your model (with the `set_param` call? Something like `Distributions.Normal(0,1)`?

I’m having some trouble recreating the problem, maybe you can send me the repo or a zip folder of the model if it’s not pushed to Github?

---

<div class="post-metadata">

**Author:** ![FrankErrickson](https://yyz1.discourse-cdn.com/flex031/user_avatar/forum.mimiframework.org/frankerrickson/32/13_2.png) [@FrankErrickson](https://forum.mimiframework.org/u/FrankErrickson)\
**Post date:** [December 18, 2019, 12:21am UTC](https://forum.mimiframework.org/t/set-a-parameter-as-a-distribution/106/5 "2019-12-18T00:21:31Z")

</div>

@lrennels Cool, thanks for looking into this. In the model component, I set the parameter as:

```auto
lws_distribution::Distributions.Normal{Float64} = Parameter()

```

From what I remember and given some of the package updates, it might also be the case that the `Distributions` bit should be removed.

The equation is basically just a cummulative sum of random samples, so (after setting an initial condition) it looks like:

```auto
v.lws_sea_level[t] = v.lws_sea_level[t-1] + rand(p.lws_distribution)

```

The create model file would then have something like:

```auto
set_param!(brick, :landwater_storage, :lws_distribution, Normal(0.0003, 0.00018))

```

This code used to work, but I tried various iterations of it and it just seems like Mimi is trying to copy the distribution which throws the error.

---

<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:** [December 18, 2019, 5:37am UTC](https://forum.mimiframework.org/t/set-a-parameter-as-a-distribution/106/6 "2019-12-18T05:37:29Z")

</div>

Ok so I was oddly able to recreate your problem when using the latest tagged version of Mimi (`v0.9.4`) but not on the `master` development branch so we might have fixed this bug accidentally in the meantime. You can either try out BRICK on latest Mimi version by typing `add Mimi#master` in the pkg REPL or just wait for our next release and try it out on that one. The latter is probably easier for you, but happy to do either, I can help if you’re not sure how to do the environments!

---

<div class="post-metadata">

**Author:** ![FrankErrickson](https://yyz1.discourse-cdn.com/flex031/user_avatar/forum.mimiframework.org/frankerrickson/32/13_2.png) [@FrankErrickson](https://forum.mimiframework.org/u/FrankErrickson)\
**Post date:** [August 24, 2020, 7:02pm UTC](https://forum.mimiframework.org/t/set-a-parameter-as-a-distribution/106/7 "2020-08-24T19:02:37Z")

</div>

Hi @lrennels, just wanted to revive this with all the new Mimi updates.

I went to convert the BRICK code to set a distribution as a parameter type for the latest version of Mimi. In the component definition I have:

`lws_distribution = Parameter{Distributions.Normal{Float64}}()`

This seems to work ok if I’m just running the model files directly on my end. But if I export the BRICK `get_model` function as a module, it no longer works and throws errors saying `Distributions` aren’t recognized in this particular component.

Do you happen to know if this is either (i) an issue with Mimi and modules not communicating correctly when the parameter type comes from an external package or (ii) me just screwing up the module?

Thanks!

---

<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:** [August 24, 2020, 9:23pm UTC](https://forum.mimiframework.org/t/set-a-parameter-as-a-distribution/106/9 "2020-08-24T21:23:20Z")

</div>

Looking into this now!

---

<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:** [August 24, 2020, 9:33pm UTC](https://forum.mimiframework.org/t/set-a-parameter-as-a-distribution/106/10 "2020-08-24T21:33:33Z")

</div>

Hi Frank,

I used the dummy code below to test out this issue. It looks like due to scoping you need to put `using Distributions` in your file **both** outside the `module` **and** inside the `module` as I’ve done below. I posed this to the team in the meantime, since it is not intuitive and seems like something we should try to fix if possible.

```auto
using Distributions

module brick

using Mimi
using Distributions

@defcomp component1 begin

    savingsrate = Parameter{Distributions.Normal{Float64}}()
    pickrate = Variable(index=[time])
    function run_timestep(p, v, d, t)
        v.pickrate[t] = rand(p.savingsrate)
    end

end

function get_model()
    m = Model()
    set_dimension!(m, :time, collect(2015:5:2110))
    add_comp!(m, component1)
	set_param!(m, :component1, :savingsrate, Distributions.Normal(1.0))
    return m
end

end # module

```

---

<div class="post-metadata">

**Author:** ![FrankErrickson](https://yyz1.discourse-cdn.com/flex031/user_avatar/forum.mimiframework.org/frankerrickson/32/13_2.png) [@FrankErrickson](https://forum.mimiframework.org/u/FrankErrickson)\
**Post date:** [August 25, 2020, 4:20pm UTC](https://forum.mimiframework.org/t/set-a-parameter-as-a-distribution/106/11 "2020-08-25T16:20:39Z")

</div>

> [@lrennels](#):
>
> looks like due to scoping you need to put `using Distributions` in your file **both** outside the `module` **and** inside the `module` as I’ve done below. I posed this to the team in the meantime, sinc

Ah ok, thanks so much!

---

<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:** [August 25, 2020, 4:34pm UTC](https://forum.mimiframework.org/t/set-a-parameter-as-a-distribution/106/12 "2020-08-25T16:34:03Z")

</div>

No problem, I’m asking the team why exactly that’s true and seeing if we can change it

---

<div class="post-metadata">

**Author:** ![FrankErrickson](https://yyz1.discourse-cdn.com/flex031/user_avatar/forum.mimiframework.org/frankerrickson/32/13_2.png) [@FrankErrickson](https://forum.mimiframework.org/u/FrankErrickson)\
**Post date:** [August 25, 2020, 7:18pm UTC](https://forum.mimiframework.org/t/set-a-parameter-as-a-distribution/106/13 "2020-08-25T19:18:27Z")

</div>

Actually just set this up to have BRICK as a package. The code you shared (with `using Distributions` inside and outside the module) works when I just test the files locally on my computer.

But if I throw these updated files up onto Git and add then try to add BRICK as a package, it no longer works (I get a `UndefVarError: Distributions not defined` error and a `Failed to precompile MimiBRICK` error).

Just FYI, this isn’t a big deal. I can always just sample these outside the model and pass them in as a parameter (having the parameter be a distribution would just be slightly cleaner).

---

<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:** [August 25, 2020, 7:45pm UTC](https://forum.mimiframework.org/t/set-a-parameter-as-a-distribution/106/14 "2020-08-25T19:45:41Z")

</div>

Oh strange ok, it would still be good for me to get to the bottom of it, where’s the GitHub repo? I can try it myself and also take a look at the .toml files. If it’s not shareable no problem, I can bring it up with the team on Friday.

---

<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:** [September 11, 2020, 10:24pm UTC](https://forum.mimiframework.org/t/set-a-parameter-as-a-distribution/106/15 "2020-09-11T22:24:12Z")

</div>

@FrankErrickson I think we’ve fixed this! Sorry if I confused things before, you don’t need calls to `using` outside your module, just inside before the call to `@defcomp` like

```auto
module brick

using Mimi
using Distributions

@defcomp component1 begin

    savingsrate = Parameter{Distributions.Normal{Float64}}()
    pickrate = Variable(index=[time])
    function run_timestep(p, v, d, t)
        v.pickrate[t] = rand(p.savingsrate)
    end

end

function get_model()
    m = Model()
    set_dimension!(m, :time, collect(2015:5:2110))
    add_comp!(m, component1)
	set_param!(m, :component1, :savingsrate, Distributions.Normal(1.0))
    return m
end

```
