Saturday, September 16, 2017

Julia - Language - PlotlyJS - The standard normal distribution

For the explanation of normal distribution, refer the following video from Khan Academy:







Function
Purpose
randn()
Returns an array of randomly selected values from the - Standard Normal Distribution. The majority of values cluster around the mean of 0 and a standard deviation of 1.


First, import the following packages:

$ 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

julia> using Plots

julia> plotlyjs()
Plots.PlotlyJSBackend()

julia> using StatPlots
INFO: Precompiling module TableTraits.
INFO: Precompiling module DataValues.
INFO: Precompiling module KernelDensity.
INFO: Precompiling module Loess.

julia> using HypothesisTests
INFO: Precompiling module HypothesisTests.

julia> using DataFrames

julia> using GLM
INFO: Precompiling module GLM.

julia>

Let us get 10000 such values and attach this array to the variable arr_norm1.

julia> arr_norm1 = randn(10000);

julia> arr_norm1
10000-element Array{Float64,1}:
 -0.330113
 -0.526716
 -0.159486
 -1.50848
  0.0444939
 -2.24241
 -0.413104
  1.15788
 -0.74063
  1.3488
  0.404499
 -0.148054
  2.38449
  0.0247912
 -0.846278
 -0.116104
  1.91943
  0.0293767
  0.649929
  ⋮      
 -0.231188
 -0.243903
  0.145286
 -1.68122
  1.39924
 -2.27001
  1.67537
 -0.595119
 -1.23359
  0.249857
 -0.332047
  0.485967
 -1.65663
  0.182978
 -0.220901
 -0.412765
  1.47191
 -0.547864

julia>


We can plot this as histogram. In the example below, we will use the keyword argument bins. Setting it to 20 means that between the minimum and maximum value we create 20 equally sized ranges and count how many values occur in each range.

julia> histogram(arr_norm1,bins=20,label="Standard Normal Distribution",title="Histogram 01")






These values were selected at random. We can check how close we came to to a real mean of 0 and a standard deviation of 1.


# finding mean
julia> mean(arr_norm1)
0.014639565332684248

#standard deviation using std()
julia> std(arr_norm1)
0.9959800381020006

# variance using var()
julia> var(arr_norm1)
0.9919762362976626

julia>

No comments:

Post a Comment