# How to add/subtract probability distributions?

**URL:** <https://discourse.julialang.org/t/how-to-add-subtract-probability-distributions/75673>\
**Category:** New to Julia\
**Tags:** physics, distributions\
**Created:** [February 2, 2022, 4:25pm UTC](https://discourse.julialang.org/t/how-to-add-subtract-probability-distributions/75673 "2022-02-02T16:25:20Z")\
**Posts on this page:** 6\
**Page:** 2

<div class="post-metadata">

**Author:** ![jonniedie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonniedie/32/12842_2.png) [@jonniedie](https://discourse.julialang.org/u/jonniedie)\
**Post date:** [February 7, 2022, 5:17am UTC](https://discourse.julialang.org/t/how-to-add-subtract-probability-distributions/75673/22 "2022-02-07T05:17:57Z")

</div>

Okay cool!

Here’s how I animated it (there was a `using Printf` for the string formatting before this):

```julia
julia> anim = @animate for i in 1:1000:length(t)
           plot(F_s; label="F_s")
           plot!(f_s[i]; label=@sprintf("f_s at t = %.2f s", t[i]), ylims=(0.0,1.5))
       end;

julia> gif(anim, "Desktop/probs.gif"; fps=10)

```

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [February 7, 2022, 6:22am UTC](https://discourse.julialang.org/t/how-to-add-subtract-probability-distributions/75673/23 "2022-02-07T06:22:29Z")

</div>

> [@jonniedie](#):
>
> ` push!(f_s, dt*v_col*(F_s - f_s[it]) + f_s[it])`

This subtraction you’re doing is _not_ the one that is being described in the differential equation though is it?. It’s “construct a new distribution by repeatedly choose a random value from F(v) and a random value from f(v) and subtracting them” right? (Of course maybe that’s what the notation means, but if so it’s a truly terrible notation for the ambiguity)

---

<div class="post-metadata">

**Author:** ![CentraCep](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/centracep/32/32454_2.png) [@CentraCep](https://discourse.julialang.org/u/CentraCep)\
**Post date:** [February 7, 2022, 3:51pm UTC](https://discourse.julialang.org/t/how-to-add-subtract-probability-distributions/75673/24 "2022-02-07T15:51:26Z")

</div>

@jonniedie Would you mind if I could take a look at what your code looks likes exactly to get the animation? Here is the entirety of my code, but I am not reproducing your animation:

```julia
v_col = 1

dt = 10^(-5) # timestep
t = range(0, 1.0, step=dt)

f_s0 = Particles(Uniform(0,1))
f_s = [f_s0]
F_s = Particles(Chi(3))

for it in 1:length(t)-1
    push!(f_s, dt*v_col*(F_s - f_s[it]) + f_s[it])
end

#plot(t, f_s)

anim = @animate for i in 1:1000:length(t)
           plot(F_s; label="F_s")
           plot!(f_s[i]; label=@sprintf("f_s at t = %.2f s", t[i]), ylims=(0.0,1.5))
       end;

gif(anim, "Simple_BGK.gif"; fps=10)

```

![Simple_BGK](https://global.discourse-cdn.com/julialang/original/3X/b/8/b8290a61994b2dde9f3554d6de0314eba0c6ebe6.gif)

---

<div class="post-metadata">

**Author:** ![jonniedie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonniedie/32/12842_2.png) [@jonniedie](https://discourse.julialang.org/u/jonniedie)\
**Post date:** [February 7, 2022, 3:55pm UTC](https://discourse.julialang.org/t/how-to-add-subtract-probability-distributions/75673/25 "2022-02-07T15:55:42Z")

</div>

> [@jonniedie](#):
>
> `default(alpha=0.5, nbins=20, normalize=true)`

Oh, you might need this line to set the plot defaults

But I did everything in the REPL, so I don’t have any way of easily pulling it together.

---

<div class="post-metadata">

**Author:** ![CentraCep](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/centracep/32/32454_2.png) [@CentraCep](https://discourse.julialang.org/u/CentraCep)\
**Post date:** [February 7, 2022, 6:24pm UTC](https://discourse.julialang.org/t/how-to-add-subtract-probability-distributions/75673/26 "2022-02-07T18:24:14Z")

</div>

Thank you so much!

Quick question, the way I discretized the value right now is explicitly. If I want to create two side by side subplots (one of the explicit method, one of the implicit method) and create an animation, would it be the same way as creating two subplots regularly?

---

<div class="post-metadata">

**Author:** ![jonniedie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonniedie/32/12842_2.png) [@jonniedie](https://discourse.julialang.org/u/jonniedie)\
**Post date:** [February 7, 2022, 6:27pm UTC](https://discourse.julialang.org/t/how-to-add-subtract-probability-distributions/75673/27 "2022-02-07T18:27:50Z")

</div>

Yep! The `@animate` loop can handle subplots too.

[Previous page](https://discourse.julialang.org/t/how-to-add-subtract-probability-distributions/75673.md?page=1)
