# Random variables from one uniform distribution get added to create new rand var

**URL:** <https://discourse.julialang.org/t/random-variables-from-one-uniform-distribution-get-added-to-create-new-rand-var/71529>\
**Category:** New to Julia\
**Tags:** question, plotting, distributions, gaussian-process\
**Created:** [November 15, 2021, 11:03pm UTC](https://discourse.julialang.org/t/random-variables-from-one-uniform-distribution-get-added-to-create-new-rand-var/71529 "2021-11-15T23:03:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![RexXI](https://avatars.discourse-cdn.com/v4/letter/r/b4bc9f/32.png) [@RexXI](https://discourse.julialang.org/u/RexXI)\
**Post date:** [November 15, 2021, 11:03pm UTC](https://discourse.julialang.org/t/random-variables-from-one-uniform-distribution-get-added-to-create-new-rand-var/71529/1 "2021-11-15T23:03:13Z")

</div>

Hi, all. I’m attempting to model what happens when random variables from one  
distribution, specifically a uniform distribution, get added to create a new  
random variable.  
To begin with

```julia
begin
	ua = 2.0.*rand(10000) .- 1.0;
	histogram(ua,size=(300,300),leg=false)
end

```

Trying to do a running average of `offset` nearest elements to the right

```julia
temp[i]=ua[i+1] + ua[i+2] + ... + ua[i+offset]
	temp[i]=temp[i]/max(offset+1,1)

```

Could use function instead of another internal loop?  
When initializing `offset = 1` and plot a histogram of `temp`, i could use the values in `temp`, and then using the function `normal(x,σ)=(1/sqrt(2pi*σ^2))exp(-x^2/(2*σ^2))` for the theoretical distribution, need to superimpose the curve of the theoretical normal distribution on the histogram of `temp`.

```julia
begin
	normal(x,σ)=(1/sqrt(2pi*σ^2))exp(-x^2/(2*σ^2))
	offset=4
	temp=zeros(length(ua))
	for i =1:?
		?
		?
	end
	histogram(temp,norm=true,size=(300,300),leg=false)
	σ2 = std(temp)
	plot!(x->normal(x,σ2),-1,1)
end

```

I’ve calculated the variance of 1/12. Know the standard deviation of a uniform distribution is `(b-a)/sqrt(12)` , However, cannot figure out for the life of me how to code for the theoretical standard deviation as a function of `offset` assuming independently drawn samples.

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [November 16, 2021, 12:05am UTC](https://discourse.julialang.org/t/random-variables-from-one-uniform-distribution-get-added-to-create-new-rand-var/71529/2 "2021-11-16T00:05:25Z")

</div>

Welcome!

I’m not certain exactly what you are doing but the package Distributions might help. Here is a normal approximation to the sum of 3 uniform random variables.

```julia
using Plots, Distributions

n_vars = 3

n_samples = 10_000

dist = Uniform(0, 2)

samples = rand(dist, n_samples, n_vars)

sums = sum(samples, dims=2)

σ = sqrt(n_vars * var(dist))

μ = n_vars * mean(dist)

histogram(sums, leg=false, grid=false, norm=true)

x = 0:.1:6

dens = pdf.(Normal(μ, σ), x)

plot!(x, dens, color=:black, linestyle=:dash)

```

> [@RexXI](#):
>
> However, cannot figure out for the life of me how to code for the theoretical standard deviation as a function of `offset` assuming independently drawn samples.

You can use `var` on a distribution object to obtain the variance. Note that adding or subtracting an offset does not change the variance. You might consider looking up the linearity of expectation and variance. I hope this gets you started in the right direction.

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [November 16, 2021, 6:55am UTC](https://discourse.julialang.org/t/random-variables-from-one-uniform-distribution-get-added-to-create-new-rand-var/71529/6 "2021-11-16T06:55:19Z")

</div>

Sounds like you’re looking for `cumsum`?

---

<div class="post-metadata">

**Author:** ![RexXI](https://avatars.discourse-cdn.com/v4/letter/r/b4bc9f/32.png) [@RexXI](https://discourse.julialang.org/u/RexXI)\
**Post date:** [November 16, 2021, 8:37am UTC](https://discourse.julialang.org/t/random-variables-from-one-uniform-distribution-get-added-to-create-new-rand-var/71529/8 "2021-11-16T08:37:45Z")

</div>

You’re absolutely right. Just thinking about it logically, variance won’t change by shifting the graph. Your code works well, too, and makes sense as increasing `n_vars` changes the graph in the way expected. The way the notebook is put together doesn’t make the most sense for the Julia Language. Although, this is the first term Mathematica hasn’t been used for this course.

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [November 16, 2021, 11:08am UTC](https://discourse.julialang.org/t/random-variables-from-one-uniform-distribution-get-added-to-create-new-rand-var/71529/9 "2021-11-16T11:08:39Z")

</div>

Also, consider the excellent course on Introduction to Probabilities at the MITx: [Probability - The Science of Uncertainty and Data](https://www.edx.org/course/probability-the-science-of-uncertainty-and-data)
