# Parameter structures and restrictions

**URL:** https://discourse.julialang.org/t/parameter-structures-and-restrictions/80892
**Category:** Finance and Economics
**Tags:** question, mutable-structure
**Created:** [May 11, 2022, 3:22pm UTC](https://discourse.julialang.org/t/parameter-structures-and-restrictions/80892 "2022-05-11T15:22:00Z")
**Posts on this page:** 14
**Page:** 1

<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: [May 11, 2022, 3:22pm UTC](https://discourse.julialang.org/t/parameter-structures-and-restrictions/80892/1 "2022-05-11T15:22:00Z")

</div>

Hi,

I’m using a parameter structure for use in an economic model in Julia. I’d like to know what are the best practices here, including the best way to include restrictions when generating and modifying a parameter structure. Let me give you an MWE of the structure I’m using:

```julia
using Parameters

@with_kw mutable struct Param
    β::Float64 = 0.95 # discount factor
    ϕ::Float64 = 0.12 # relative taste for housing
    γ::Float64 = 0.8 # ES parameter between consumption and housing
    βsq::Float64 = β^2 # squared discount factor
end

```

I then do `p=Param()`. Let’s say I wish to change β to 0.9. Two issues come up, that I’d like to overcome:

1. I want βsq to update accordingly, if possible.
2. I want to introduce restrictions on these variables. Say 0\<β\<1.

What are the best practices to do this (including abandoning the use of structures)?

Thanks for the help.

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [May 11, 2022, 3:38pm UTC](https://discourse.julialang.org/t/parameter-structures-and-restrictions/80892/2 "2022-05-11T15:38:54Z")

</div>

You could write a function to do this like

```julia
julia> function updateβ!(x::Param, βₙ)
           0 < βₙ < 1 || error("β has to be between 0 and 1")
           x.β = βₙ
           x.βsq = βₙ^2
       end

```

---

<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: [May 11, 2022, 3:42pm UTC](https://discourse.julialang.org/t/parameter-structures-and-restrictions/80892/3 "2022-05-11T15:42:19Z")

</div>

You can do what @nilshg suggested.

Regarding you final question, I’ve found that if I am just dealing with individual parameters, I am often better off using named tuples. I only reach for structs when I am also passing around more complicated structures.

---

<div class="post-metadata">

### Author: ![luke](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luke/32/24341_2.png) [@luke](https://discourse.julialang.org/u/luke)
#### Post date: [May 11, 2022, 3:53pm UTC](https://discourse.julialang.org/t/parameter-structures-and-restrictions/80892/4 "2022-05-11T15:53:26Z")

</div>

Given how lightweight your struct is, I’d guess the performance you might get with mutability might not be worth the hassle. An immutable approach (using simpler letters bc I’m on a phone) might look like this:

```julia
struct Params
    a::Real
    b::Real
    bsq::Real
    # mask the default constructor
    Params(a::Real = 0.5, b::Real = 0.5) = new(a, b, b^2)
end

# an "update" constructor, you can also call this `update` instead of making it a constructor
Params(old::Params; a=nothing, b=nothing) =
    Params(isnothing(a) ? old.a : a, isnothing(b) ? old.b : b)

```

---

<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: [May 11, 2022, 4:25pm UTC](https://discourse.julialang.org/t/parameter-structures-and-restrictions/80892/5 "2022-05-11T16:25:19Z")

</div>

I believe the docs would suggest not to use the types as you’ve put them. [Performance Tips · The Julia Language](https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-abstract-container)

Here is what I would do that addresses both concerns

```julia
function paramsfun(a,b)
    @assert 0 < b < 1 "b is must be between 0 and 1"
    return (; a, b, bsq=b^2)
end

```

Because you are only working with a few scalar values, creating named tuples is going to be lightweight and easy. You don’t have to worry about working “in place”. This thing will also place nicely with AD and lots of other neat tools for free.

---

<div class="post-metadata">

### Author: ![luke](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luke/32/24341_2.png) [@luke](https://discourse.julialang.org/u/luke)
#### Post date: [May 11, 2022, 5:00pm UTC](https://discourse.julialang.org/t/parameter-structures-and-restrictions/80892/6 "2022-05-11T17:00:37Z")

</div>

> I believe the docs would suggest not to use the types as you’ve put them.

Ah, yes, for better performance it should be typed like this:

```julia
struct Params {T<:Real}
    a::T
    b::T
    bsq::T
end

```

I’m interested in this lightweight approach of using a tuple instead of an immutable struct, but I can’t seem to figure out how I’d write and update function to make it easy to get a new parameters tuple with just one or two values changed.

@tbeason can you show an example of updating parameters in the `NamedTuple` approach?

---

<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: [May 11, 2022, 5:09pm UTC](https://discourse.julialang.org/t/parameter-structures-and-restrictions/80892/7 "2022-05-11T17:09:27Z")

</div>

You can use packages for that if you want an easy way to do it. In truth I never use these but now that you’ve forced me to think about it I will probably do this going forward…

> **[GitHub - jw3126/Setfield.jl: Update deeply nested immutable structs.](https://github.com/jw3126/Setfield.jl)**
>
> Update deeply nested immutable structs. Contribute to jw3126/Setfield.jl development by creating an account on GitHub.

> **[GitHub - JuliaFolds/BangBang.jl: Immutables as mutables, mutables as immutables.](https://github.com/JuliaFolds/BangBang.jl)**
>
> Immutables as mutables, mutables as immutables. Contribute to JuliaFolds/BangBang.jl development by creating an account on GitHub.

```julia
using BangBang

julia> p = paramsfun(0.8,0.95)
(a = 0.8, b = 0.95, bsq = 0.9025)

julia> p = setproperties!!(p;a=0.5)
(a = 0.5, b = 0.95, bsq = 0.9025)

```

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [May 11, 2022, 5:24pm UTC](https://discourse.julialang.org/t/parameter-structures-and-restrictions/80892/8 "2022-05-11T17:24:58Z")

</div>

> [@luke](#):
>
> ```julia
> a::Real
> b::Real
> bsq::Real
> 
> ```

if you care about performance for this type at all, don’t use abstract type here

---

<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: [May 12, 2022, 3:16pm UTC](https://discourse.julialang.org/t/parameter-structures-and-restrictions/80892/9 "2022-05-12T15:16:04Z")

</div>

Thanks for the suggestion. That seems most clean

---

<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: [May 12, 2022, 3:17pm UTC](https://discourse.julialang.org/t/parameter-structures-and-restrictions/80892/10 "2022-05-12T15:17:14Z")

</div>

In my original structure, I have something like 150 parameters, which include vectors and matrices. But it doesn’t get more complex than that for now.

---

<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: [May 12, 2022, 3:18pm UTC](https://discourse.julialang.org/t/parameter-structures-and-restrictions/80892/11 "2022-05-12T15:18:32Z")

</div>

I’ll take these suggestions into account when thinking about my framework

---

<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: [May 12, 2022, 3:21pm UTC](https://discourse.julialang.org/t/parameter-structures-and-restrictions/80892/12 "2022-05-12T15:21:34Z")

</div>

This idea makes a lot of sense to me. The original structure has 150 parameters, which includes vectors and matrices as I said previously

---

<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: [May 12, 2022, 3:22pm UTC](https://discourse.julialang.org/t/parameter-structures-and-restrictions/80892/13 "2022-05-12T15:22:15Z")

</div>

I’ll take a look at these. Thanks

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [May 12, 2022, 3:48pm UTC](https://discourse.julialang.org/t/parameter-structures-and-restrictions/80892/14 "2022-05-12T15:48:21Z")

</div>

> [@Joao\_Barata](#):
>
> I want βsq to update accordingly, if possible.

You can overload `setproperty!`, as well as your constructor(s), to enforce invariants like this.
