Two types of packages
In [ ]:
# Pkg.add("Distributions")
In [ ]:
# Pkg.rm("Distributions")
In [ ]:
Pkg.rm("PlotlyJS")
In [1]:
Pkg.clone("https://github.com/spencerlyon2/PlotlyJS.jl.git")
In [ ]:
Packages can be updated to their most recent version by using Pkg.update()
Running this command will update:
METADATA
, which tracks all versions of registered packagesDoesn't update "dirty" packages (git status
$\neq$ clean)
In [3]:
using Distributions
In [ ]:
ncud = length(subtypes(Distributions.ContinuousUnivariateDistribution))
ntd = length(subtypes(Distributions.Distribution))
println("There are $(ncud) Continuous Univariate Distributions")
println("There are $(ntd) Total Distributions")
Our example will not demonstrate everything that Distributions.jl
can do.
It does much much more, but it will give you an idea of the types of things that you can do with it -- you can read the docs for more information.
You can find a more complete list of possible methods in the documentation, but we list a few of the methods below to give you an idea of what is included:
params
, scale
, shape
, dof
mean
, median
, std
, skewness
, kurtosis
, entropy
, mgf
insupport
, pdf
, cdf
, likelihood
, quantile
Below we create two distributions that we will play with
In [ ]:
nrv = Normal(0.0, 1.0)
tdist = TDist(5)
In [ ]:
for f in (:mean, :median, :std, :skewness, :kurtosis, :entropy)
ftup = (eval(f)(nrv), eval(f)(tdist))
println("Normal and T ", f, " are ", ftup)
end
In [ ]:
for f in (mean, median, std, skewness, kurtosis, entropy)
ftup = (f(nrv), f(tdist))
println("Normal and T ", f, " are ", ftup)
end
In [ ]:
for f in (:pdf, :logpdf, :cdf, :logcdf, :quantile)
ftup = (eval(f)(nrv, 0.25), eval(f)(tdist, 0.25))
println("Normal and T ", f, " are ", ftup)
end
We will only cover a fraction of what this library can do.
For more information see: The examples page and the plot attribute page
Other good plotting options include: PyPlot.jl
, Gadfly
, and Plots.jl
In [2]:
using PlotlyJS
In [4]:
using Distributions
function plot_distribution(d::Distribution)
p_001, p_999 = quantile(d, 1e-3), quantile(d, 1-1e-3)
x = collect(linspace(p_001, p_999, 100))
y = pdf(d, x)
t1 = scatter(;x=x, y=y, showlegend=false)
return t1
end
Out[4]:
In [5]:
plot(plot_distribution(Normal(0, 1)))
Out[5]:
In [6]:
function hist_distribution(d::Distribution, N=10_000)
y = rand(d, N)
t2 = histogram(;x=y, histnorm="probability density",
showlegend=false, nbinsx=250, opacity=0.6)
return t2
end
Out[6]:
In [7]:
plot(hist_distribution(Normal(0, 1)))
Out[7]:
In [8]:
function multiple_surface()
z1 = Vector[[8.83, 8.89, 8.81, 8.87, 8.9, 8.87],
[8.89, 8.94, 8.85, 8.94, 8.96, 8.92],
[8.84, 8.9, 8.82, 8.92, 8.93, 8.91],
[8.79, 8.85, 8.79, 8.9, 8.94, 8.92],
[8.79, 8.88, 8.81, 8.9, 8.95, 8.92],
[8.8, 8.82, 8.78, 8.91, 8.94, 8.92],
[8.75, 8.78, 8.77, 8.91, 8.95, 8.92],
[8.8, 8.8, 8.77, 8.91, 8.95, 8.94],
[8.74, 8.81, 8.76, 8.93, 8.98, 8.99],
[8.89, 8.99, 8.92, 9.1, 9.13, 9.11],
[8.97, 8.97, 8.91, 9.09, 9.11, 9.11],
[9.04, 9.08, 9.05, 9.25, 9.28, 9.27],
[9, 9.01, 9, 9.2, 9.23, 9.2],
[8.99, 8.99, 8.98, 9.18, 9.2, 9.19],
[8.93, 8.97, 8.97, 9.18, 9.2, 9.18]]
z2 = map(x->x+1, z1)
z3 = map(x->x-1, z1)
trace1 = surface(z=z1)
trace2 = surface(z=z2, showscale=false, opacity=0.9)
trace3 = surface(z=z3, showscale=false, opacity=0.9)
plot([trace1, trace2, trace3])
end
Out[8]:
In [9]:
multiple_surface()
Out[9]:
We often want to add titles, labels, or other information to a plot.
Here it makes sense to mention that PlotlyJS
constructs figures in two parts.
trace
s: Stores plot data and how it should be displayedLayout
: Figure wide settingsLet's write another function that combines two traces from the previous functions and adds layout information.
In [6]:
function full_plot_distribution(d::Distribution, N=10000;
xlim=(quantile(d, 1e-3), quantile(d, 1-1e-3)))
# Create multiple traces which will go on plot
t1 = plot_distribution(d)
t2 = hist_distribution(d, N)
# Create layout
l = Layout(;title="$(typeof(d))",
xaxis_range=xlim, xaxis_title="x",
yaxis_title="Probability Density of x",
xaxis_showgrid=true, yaxis_showgrid=true,
legend_y=1.15, legend_x=0.7)
return plot([t1, t2], l)
end
Out[6]:
In [7]:
full_plot_distribution(Normal(0, 1))
Out[7]:
In [ ]:
p1 = full_plot_distribution(Normal(0, 1), xlim=(-3, 3))
p2 = full_plot_distribution(TDist(5), xlim=(-3, 3))
[p1 p2]
It is important to interpolate. Interpolations.jl
is an extremely fast interpolation package that is based around using splines.
Have a look at their benchmarks
In [ ]:
using Interpolations
There are multiple types of interpolators. We will focus on BSplines()
.
See the docs for information on the other types.
In [ ]:
x = linspace(-1.0, 1.0, 50)
y = sin(collect(x))
itp = interpolate(y, BSpline(Linear()), OnGrid())
diff = maxabs([itp[i] for i in 1:50] - y)
println("The max absolute difference is: ", diff)
In [ ]:
itp_scaled = scale(itp, x)
diff_scaled = maxabs([itp_scaled[el] for el in x] - y)
println("The max absolute difference is: ", diff_scaled)
In [ ]:
gradient(itp_scaled, 0.0)
Here are some other recommended packages
Some are useful, others fun
They appear in no particular order
In [ ]: