# Different outcomes with the same function

**URL:** <https://discourse.julialang.org/t/different-outcomes-with-the-same-function/59570>\
**Category:** Performance\
**Created:** [April 18, 2021, 11:39pm UTC](https://discourse.julialang.org/t/different-outcomes-with-the-same-function/59570 "2021-04-18T23:39:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![valperez](https://avatars.discourse-cdn.com/v4/letter/v/e79b87/32.png) [@valperez](https://discourse.julialang.org/u/valperez)\
**Post date:** [April 18, 2021, 11:39pm UTC](https://discourse.julialang.org/t/different-outcomes-with-the-same-function/59570/1 "2021-04-18T23:39:00Z")

</div>

Hi, I’m making a program in Julia that calculates the posteriori distribution of a beta binomial model.

My first model was the following:

@model betabinomial(n, z, x, y, delta, gamma) = begin  
alpha\_p ~ truncated(Normal(x, 0.1), 0, Inf)  
beta\_p ~ truncated(Normal(y, 0.1), 0, Inf)  
q\_ajustada = Vector{Real}(undef, 3)  
p = Vector{Real}(undef, 3)  
for i = 1:length(n)  
p[i] ~ Beta(alpha\_p, beta\_p)  
q\_ajustada[i] = delta_p[i] + (1-gamma)_(1-p[i])  
z[i] ~ Binomial(n[i], q\_ajustada[i])  
end  
end;

But now, instead of using the for loop, I want to use vector to achieve a better performance and less computing time so my new code looks like this:

@model betabinomial\_v2(n::Array{Int64, 1}, z::Array{Int64, 1}, x::Float64, y::Float64, delta::Float64, gamma::Float64) = begin  
#Definimos las iniciales  
alpha\_p ~ truncated(Normal(x, 0.1), 0, Inf)  
beta\_p ~ truncated(Normal(y, 0.1), 0, Inf)

```
# p se distribuye Beta
p ~ filldist(Beta(alpha_p, beta_p), length(n))

#q_ajustada sí tiene que ser así segun yo
q_ajustada = Vector{Float64}(undef, length(n))
unos = ones(length(n))
q_ajustada = delta*p + (1 - gamma)*(unos - p)

@. z ~ Binomial(n, q_ajustada)

```

end;

I defined the type of each variable and follow this publication [Vectorising Turing models](https://discourse.julialang.org/t/vectorising-turing-models/54700) for the vectorization.  
However, even when I’m using a seed, I don’t get the same results using both models. Any ideas of why and if I’m making a mistake, please let me know.

Thank you in advanced 🙂

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [April 19, 2021, 6:24pm UTC](https://discourse.julialang.org/t/different-outcomes-with-the-same-function/59570/2 "2021-04-19T18:24:40Z")

</div>

First of all, welcome!

Second, before you get too far into thinking about the new version of your code, let’s start with your premise:

> [@valperez](#):
>
> But now, instead of using the for loop, I want to use vector to achieve a better performance and less computing time

This simply isn’t true in Julia. There’s no reason to prefer “vectorized” code in this form for speed compared to code using loops. Loops in Julia are as fast or faster than vectorized code in slow languages like python or matlab. If you’re happy with your loop-based code, then you’re _done_ 🙂

That said, there are some easy things you can do to improve performance of your original code. You have:

> [@valperez](#):
>
> p = Vector{Real}(undef, 3)

which is a container (`Vector`) with an abstract type (`Real`). This is exactly what’s warned against here: [Performance Tips · The Julia Language](https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-abstract-container)

You can probably use something like `Vector{typeof(alpha_p)}` to improve performance without needing to rewrite the algorithm. Use `@code_warntype` and check out the other performance tips from that document to learn more about writing faster code.
