# First actuarial example

**URL:** <https://discourse.julialang.org/t/first-actuarial-example/81863>\
**Category:** New to Julia\
**Tags:** question, distributions, random\
**Created:** [May 29, 2022, 12:32pm UTC](https://discourse.julialang.org/t/first-actuarial-example/81863 "2022-05-29T12:32:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sbacelar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sbacelar/32/4359_2.png) [@sbacelar](https://discourse.julialang.org/u/sbacelar)\
**Post date:** [May 29, 2022, 12:32pm UTC](https://discourse.julialang.org/t/first-actuarial-example/81863/1 "2022-05-29T12:32:52Z")

</div>

I found this little example in an actuarial book using `R`.

```julia
set.seed(1)
X <- rexp(200, rate=1/100)
print(X[1:5])

```

I know that `rexp` generates random deviates.

How can I replicate this example in `Julia`? Using `Distributions.jl` but then what?

---

<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 29, 2022, 12:54pm UTC](https://discourse.julialang.org/t/first-actuarial-example/81863/2 "2022-05-29T12:54:43Z")

</div>

I think you are looking for

```julia
julia> using Distributions

julia> rand(Exponential(100), 100)

```

although double check the second parameter - I just guessed that it is the inverse of what R is using based on the name of the kwarg in R and the docstring for `Exponential`:

```julia
help?> Exponential

  Exponential(θ)

  The Exponential distribution with scale parameter θ has probability density function

  f(x; \theta) = \frac{1}{\theta} e^{-\frac{x}{\theta}}, \quad x > 0

  Exponential() # Exponential distribution with unit scale, i.e. Exponential(1)
  Exponential(θ) # Exponential distribution with scale θ
  
  params(d) # Get the parameters, i.e. (θ,)
  scale(d) # Get the scale parameter, i.e. θ
  rate(d) # Get the rate parameter, i.e. 1 / θ

```

---

<div class="post-metadata">

**Author:** ![sbacelar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sbacelar/32/4359_2.png) [@sbacelar](https://discourse.julialang.org/u/sbacelar)\
**Post date:** [May 29, 2022, 1:14pm UTC](https://discourse.julialang.org/t/first-actuarial-example/81863/3 "2022-05-29T13:14:10Z")

</div>

I tried with:

```julia
using Distributions
Random.seed!(1)
X = rand(Exponential(200), 100)
X[1:5]

```

but the results are different in `R`:  
`[1] 75.51818 118.16428 14.57067 13.97953 43.60686`

---

<div class="post-metadata">

**Author:** ![Alec\_Loudenback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alec_loudenback/32/278_2.png) [@Alec\_Loudenback](https://discourse.julialang.org/u/Alec_Loudenback)\
**Post date:** [May 29, 2022, 2:28pm UTC](https://discourse.julialang.org/t/first-actuarial-example/81863/4 "2022-05-29T14:28:21Z")

</div>

Random.numbers are mostly just that - random. You can create a bit of consistentcy by setting a seed like you did but in general the random numbers will be implementation-specific. How Julia and R do random numbers is likely different and therefore you are seeing different output despite setting the same seed.

You could:

- generate and store a series of random numbers from R and then use that wherever you need it
- actually call R from Julia with [RCall](https://juliainterop.github.io/RCall.jl/stable/gettingstarted/)

P.S. welcome to Julia! Do check out [JuliaActuary.org](https://juliaactuary.org/) which has some examples and tutorials too.

---

<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 29, 2022, 2:40pm UTC](https://discourse.julialang.org/t/first-actuarial-example/81863/5 "2022-05-29T14:40:54Z")

</div>

Sorry you misread my post, but I made it easy for you to do so.

```julia
rand(Exponential(theta), n)

```

Draws n random numbers from an Exponential distribution with scale parameter theta. I interpreted Rs `rate` kwarg to be the inverse of the scale parameter in Distributions.

So when you do `Exponential(200)` you are likely sampling from a distribution that’s the equivalent of `rate=1/200` in R. You should do `rand(Exponential(100), 200)`

(this is independent to Alex’s good point that random number generators are generally not comparably across programming languages so you shouldn’t expect to get the exact same numbers back from Julia and R even with the same random seed)
