We can ask for random data point values for a variable to be taken from any number of discrete or continuous distributions. This greatly expands on the standard normal distribution provided by the randn() function. In this lesson we will concentrate on the continuous random variables and start with the normal distribution.
Plotting the Standard Normal Distribution:
The Normal() function from the Distribution package takes two arguments. The first is the mean and the second is the variance. We use it in conjunction with the rand() function so that we can specify how many datapoint values we want.
Refer: http://juliastats.github.io/Distributions.jl/stable/univariate.html#Distributions.Normal
$ julia
_
_ _ _(_)_ | A fresh approach to technical computing
(_) | (_) (_) | Documentation: https://docs.julialang.org
_ _ _| |_ __ _ | Type "?help" for help.
| | | | | | |/ _` | |
| | |_| | | | (_| | | Version 0.6.0 (2017-06-19 13:05 UTC)
_/ |\__'_|_|_|\__'_| | Official http://julialang.org/ release
|__/ | x86_64-pc-linux-gnu
julia> using Distributions
Let us check the Normal function
julia> Normal()
Distributions.Normal{Float64}(μ=0.0, σ=1.0)
julia> arr_norm2 = rand(Normal(0,1),10000)
1000-element Array{Float64,1}:
0.617306
0.916475
0.374644
-0.361471
-0.214228
0.528817
0.56169
1.01749
-0.787201
1.54954
1.90827
1.1112
-0.319775
-0.146447
-1.23947
-1.50368
2.18589
0.204094
1.51182
⋮
-0.424308
-0.100826
-1.95461
-0.63926
-0.899714
0.369235
0.382081
-0.118614
-0.649621
-1.3039
-0.524052
1.30383
1.00772
-0.47544
-0.62028
-0.0306563
0.269642
-0.924341
julia>
Just to expand on the point, let us use Plots to show us the theoretical normal distribution. We must import the following packages:
julia> using StatPlots
julia> using Plots
julia> plotlyjs()
Plots.PlotlyJSBackend()
Let us plot the Standard Normal Distribution
julia> plot(Normal(0,1), fillrange=0, fillalpha=0.5 , fillcolor=:blue, label="Standard Normal Distribution", title="Density")
julia>
We can also fit some data to a distribution. In the example below we use the norm1 array and fit it to the standard normal distribution.
julia> arr_norm1 = randn(1000);
julia> fit(Normal,arr_norm1)
Distributions.Normal{Float64}(μ=-0.042955933897932966, σ=1.0144309652412293)
julia>
We can plot other distributions as well. Here is the χ² distribution with different degrees of freedom.
julia> plot(Chisq(3), fillrange=0,fillalpha=0.5,fillcolor=:blue, label = "3 degrees of freedom")
julia>
Now, let us use the plot!() function. This is the plot function with a bang (!)sign. What it does is - it writes on top of the existing plot.
julia> plot!(Chisq(5), fillrange=0,fillalpha=0.25,fillcolor=:orange, label = "5 degrees of freedom")
julia>
julia> plot!(Chisq(10), fillrange=0,fillalpha=0.25,fillcolor=:deepskyblue, label = "10 degrees of freedom")
julia>
What the plot!() function does is - it plots over the existing plot.
Refer the below Links:
Plotting the Standard Normal Distribution:
The Normal() function from the Distribution package takes two arguments. The first is the mean and the second is the variance. We use it in conjunction with the rand() function so that we can specify how many datapoint values we want.
Refer: http://juliastats.github.io/Distributions.jl/stable/univariate.html#Distributions.Normal
$ julia
_
_ _ _(_)_ | A fresh approach to technical computing
(_) | (_) (_) | Documentation: https://docs.julialang.org
_ _ _| |_ __ _ | Type "?help" for help.
| | | | | | |/ _` | |
| | |_| | | | (_| | | Version 0.6.0 (2017-06-19 13:05 UTC)
_/ |\__'_|_|_|\__'_| | Official http://julialang.org/ release
|__/ | x86_64-pc-linux-gnu
julia> using Distributions
Let us check the Normal function
julia> Normal()
Distributions.Normal{Float64}(μ=0.0, σ=1.0)
julia> arr_norm2 = rand(Normal(0,1),10000)
1000-element Array{Float64,1}:
0.617306
0.916475
0.374644
-0.361471
-0.214228
0.528817
0.56169
1.01749
-0.787201
1.54954
1.90827
1.1112
-0.319775
-0.146447
-1.23947
-1.50368
2.18589
0.204094
1.51182
⋮
-0.424308
-0.100826
-1.95461
-0.63926
-0.899714
0.369235
0.382081
-0.118614
-0.649621
-1.3039
-0.524052
1.30383
1.00772
-0.47544
-0.62028
-0.0306563
0.269642
-0.924341
julia>
Just to expand on the point, let us use Plots to show us the theoretical normal distribution. We must import the following packages:
julia> using StatPlots
julia> using Plots
julia> plotlyjs()
Plots.PlotlyJSBackend()
Let us plot the Standard Normal Distribution
julia> plot(Normal(0,1), fillrange=0, fillalpha=0.5 , fillcolor=:blue, label="Standard Normal Distribution", title="Density")
julia>
We can also fit some data to a distribution. In the example below we use the norm1 array and fit it to the standard normal distribution.
julia> arr_norm1 = randn(1000);
julia> fit(Normal,arr_norm1)
Distributions.Normal{Float64}(μ=-0.042955933897932966, σ=1.0144309652412293)
julia>
We can plot other distributions as well. Here is the χ² distribution with different degrees of freedom.
julia> plot(Chisq(3), fillrange=0,fillalpha=0.5,fillcolor=:blue, label = "3 degrees of freedom")
julia>
Now, let us use the plot!() function. This is the plot function with a bang (!)sign. What it does is - it writes on top of the existing plot.
julia> plot!(Chisq(5), fillrange=0,fillalpha=0.25,fillcolor=:orange, label = "5 degrees of freedom")
julia>
julia> plot!(Chisq(10), fillrange=0,fillalpha=0.25,fillcolor=:deepskyblue, label = "10 degrees of freedom")
julia>
What the plot!() function does is - it plots over the existing plot.
Refer the below Links:
- https://juliaplots.github.io/attributes/
- http://spencerlyon.com/PlotlyJS.jl/manipulating_plots/
- https://lectures.quantecon.org/jl/julia_plots.html#plotlyjs
- https://juliaplots.github.io/examples/plotlyjs/#colors
No comments:
Post a Comment