Skip to content
This repository has been archived by the owner on Nov 8, 2024. It is now read-only.

Commit

Permalink
Move images to Makie (#72)
Browse files Browse the repository at this point in the history
* added .JuliaFormatter.toml

* makie 02_bayes_stats.jl

* makie 03_prob_dist

* format 01_why_Julia

* format everything

* makie conversion

* using stuff multiline

* makie 04_Turing

* remove plots stuff from Project.toml

* animations and makie 05_MCMC

* fix setrange in 12_Turing_tricks
  • Loading branch information
storopoli authored Dec 26, 2022
1 parent 18fb675 commit 670d2c7
Show file tree
Hide file tree
Showing 15 changed files with 686 additions and 553 deletions.
1 change: 1 addition & 0 deletions .JuliaFormatter.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
style = "blue"
4 changes: 1 addition & 3 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0"
CategoricalArrays = "324d7699-5711-5eae-9e2f-1d82baa6b597"
Chain = "8be319e6-bccf-4806-a6f7-6fae938471bc"
Colors = "5ae59095-9a9b-59fe-a467-6f913c188581"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
DifferentialEquations = "0c46a032-eb83-5123-abaf-570d42b7fbaa"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
Expand All @@ -14,15 +15,12 @@ FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b"
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
Franklin = "713c75ef-9fc9-4b05-94a9-213340da978e"
HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3"
LaTeXStrings = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f"
LazyArrays = "5078a376-72f3-5289-bfd5-ec5146d43c02"
Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306"
MCMCChains = "c7f686f2-ff18-58e9-bc7b-31028e88f75d"
NodeJS = "2bd173c7-0d6d-553b-b6af-13a54713934c"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
StatsFuns = "4c63d2b9-4356-54db-8cca-17b64c39e42c"
StatsPlots = "f3b207a7-027a-5e70-b257-86293d7955fd"
Turing = "fce5fe82-541a-59a6-adf8-730c64b5f9a0"

[compat]
Expand Down
18 changes: 13 additions & 5 deletions _literate/01_why_Julia.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@
# Here is Julia:

# ```julia
# using Random, StatsBase, DataFrames, BenchmarkTools, Chain
# using Random
# using StatsBase
# using DataFrames
# using BenchmarkTools
# using Chain
# Random.seed!(123)
#
# n = 10_000
Expand Down Expand Up @@ -474,12 +478,16 @@
# to define the action that one type `Pet` takes when it meets another `Pet`:

abstract type Pet end
struct Dog <: Pet name::String end
struct Cat <: Pet name::String end
struct Dog <: Pet
name::String
end
struct Cat <: Pet
name::String
end

function encounter(a::Pet, b::Pet)
verb = meets(a, b)
println("$(a.name) meets $(b.name) and $verb")
return println("$(a.name) meets $(b.name) and $verb")
end

meets(a::Dog, b::Dog) = "sniffs";
Expand Down Expand Up @@ -639,7 +647,7 @@ using BenchmarkTools
# with a simple column selection. We do this by defining a new method of the `*` function (multiplier function)
# of `Base` Julia. Additionally we also create a new optimized method of `inner()` for dealing with `OneHotVector`:

import Base:*
import Base: *

*(A::AbstractMatrix, v::OneHotVector) = A[:, v.ind]
inner(v::OneHotVector, A, w::OneHotVector) = A[v.ind, w.ind]
Expand Down
54 changes: 28 additions & 26 deletions _literate/02_bayes_stats.jl
Original file line number Diff line number Diff line change
Expand Up @@ -416,24 +416,23 @@
# and the 75% percentile of the probability density of $\theta$. In this example, MLE leads to estimated values that are not
# consistent with the actual probability density of the value of $\theta$.

using Plots, StatsPlots, Distributions, LaTeXStrings
using CairoMakie
using Distributions

d = LogNormal(0, 2)
range_d = 0:0.001:4
q25 = quantile(d, 0.25)
q75 = quantile(d, 0.75)
plot((range_d, pdf.(d, range_d)),
leg=false,
xlims=(-0.2, 4.2),
lw=3,
xlabel=L"\theta",
ylabel="Density")
scatter!((mode(d), pdf(d, mode(d))), mc=:green, ms=5)
plot!(range(q25, stop=q75, length=100),
x -> pdf(d, x),
lc=false, fc=:blues,
fill=true, fa=0.5)
savefig(joinpath(@OUTPUT, "lognormal.svg")); # hide
credint = range(q25; stop=q75, length=100)
f, ax, l = lines(
range_d,
pdf.(d, range_d);
linewidth=3,
axis=(; limits=(-0.2, 4.2, nothing, nothing), xlabel=L"\theta", ylabel="Density"),
)
scatter!(ax, mode(d), pdf(d, mode(d)); color=:green, markersize=12)
band!(ax, credint, 0.0, pdf.(d, credint); color=(:steelblue, 0.5))
save(joinpath(@OUTPUT, "lognormal.svg"), f); # hide

# \fig{lognormal}
# \center{_**Log-Normal**: Maximum Likelihood Estimate vs Credible Intervals_} \\
Expand All @@ -454,19 +453,22 @@ range_d = -2:0.01:14
sim_d = rand(d, 10_000)
q25 = quantile(sim_d, 0.25)
q75 = quantile(sim_d, 0.75)
plot((range_d, pdf.(d, range_d)),
leg=false,
xlims=(-2, 14),
xticks=[0, 5, 10],
lw=3,
xlabel=L"\theta",
ylabel="Density")
scatter!((mode(d2), pdf(d, mode(d2))), mc=:green, ms=5)
plot!(range(q25, stop=q75, length=100),
x -> pdf(d, x),
lc=false, fc=:blues,
fill=true, fa=0.5)
savefig(joinpath(@OUTPUT, "mixture.svg")); # hide
credint = range(q25; stop=q75, length=100)

f, ax, l = lines(
range_d,
pdf.(d, range_d);
linewidth=3,
axis=(;
limits=(-2, 14, nothing, nothing),
xticks=[0, 5, 10],
xlabel=L"\theta",
ylabel="Density",
),
)
scatter!(ax, mode(d2), pdf(d, mode(d2)); color=:green, markersize=12)
band!(ax, credint, 0.0, pdf.(d, credint); color=(:steelblue, 0.5))
save(joinpath(@OUTPUT, "mixture.svg"), f); # hide

# \fig{mixture}
# \center{_**Mixture**: Maximum Likelihood Estimate vs Credible Intervals_} \\
Expand Down
Loading

0 comments on commit 670d2c7

Please sign in to comment.