#------------------------------------------------------------------------------
#  Example.jl
#
#  Test of LSTAR code used in "Carry Trade" paper.
#
#
#
#
#
#
#
#
#  Paul.Soderlind@unisg.ch   18 January 2013, to Julia Nov 2015
#------------------------------------------------------------------------------

using Statistics, Dates, DelimitedFiles, LinearAlgebra, Optim, ForwardDiff

include("jlFiles/FindNoNaNPs.jl")
include("jlFiles/NewEst3Ps.jl")
include("jlFiles/OlsPs.jl")
include("jlFiles/OlsLStar3Ps.jl")
include("jlFiles/printmat.jl")
#------------------------------------------------------------------------------

x     = readdlm("CTData.csv",',')      #date, FXV, Return_CT, SP, Ty

FXV   = x[:,2]                         #FX volatility
Re_CT = x[:,3]                         #carry trade return
SP    = x[:,4]                         #S&P return
Ty    = x[:,5]                         #treasury return

T = size(Re_CT,1)

gM = range(1,stop=4,length=15)
cM = range(-2,stop=3,length=21)

z = (FXV .- mean(FXV))/std(FXV)        #standardised regime variable
#------------------------------------------------------------------------------

gcKeep = [NaN;NaN]                       #(gamma,c) set to NaN if estimated in NLS, otherwise imposed
#gcKeep = [3.0;NaN]                      #try to uncomment this to restrict gamma to 3
#gcKeep = [NaN;1.3]                      #or this

w = zeros(T,0)                           #no regressors without "regimes": empty vector
(theta,Stdtheta,fnOutput) = OlsLStar3Ps(Re_CT,[ones(T) SP Ty],w,true,z,gM,cM,gcKeep)

if all(isnan.(gcKeep))
  println("\n","theta is [g;c;b_low;b_high;(slopes without regimes, if any)]")
  rowNames = ["γ";"c";string.("b",1:3,"_L");string.("b",1:3,"_H")]
elseif isnan.(gcKeep) == [true;false]
  println("\n","theta is [g;b_low;b_high;(slopes without regimes, if any)]")
  rowNames = ["γ";string.("b",1:3,"_L"); string.("b",1:3,"_H")]
elseif isnan.(gcKeep) == [false;true]
  println("\n","theta is [c;b_low;b_high;(slopes without regimes, if any)]")
  rowNames = ["c";string.("b",1:3,"_L");string.("b",1:3,"_H")]
end

println("Estimates and t-stats")
printTable([theta Stdtheta],["coef","t-stat"],rowNames)

println("\n","difference of slope (b, high minus low state)")
printTable(fnOutput.slopeDiff,["coef","t-stat"],string.("b",1:3))
#------------------------------------------------------------------------------

#Plot of the loss function. Comment out this if you do not have PyPlot installed.
using PyPlot
#PyPlot.svg(true)           #for ipynb notebooks

using3D()                   #active 3D plotting
fig = figure(figsize=(12,8))
ax = gca(projection="3d")
  surf(gM,cM,fnOutput.sseM',rstride=2,cstride=2,cmap=ColorMap("summer"),alpha=0.8)
  scatter(fnOutput.gcHat[1],fnOutput.gcHat[2],zs=minimum(fnOutput.sseM),s=200,color="k")
  ax.view_init(elev=20.0, azim=10)
  title("Sum of squared residuals (optimum at the dot)")
  xlabel(L"$\gamma$")
  ylabel(L"$c$")
  #display(gcf())          #uncomment in Atom/Juno
#------------------------------------------------------------------------------


### A Pluto.jl notebook ###
# v0.17.2

using Markdown
using InteractiveUtils

# ╔═╡ 61f4e85b-3b4e-4906-85a6-fd4a08056532
using LinearAlgebra

# ╔═╡ 898608b9-09c8-4e6d-a0ee-29454d5ce0c9
using FFTW

# ╔═╡ 5ce3d306-5a65-4f07-86eb-c417d1959d4e
using BlockArrays

# ╔═╡ 7a6a3ee1-4fc8-4b1f-ba8a-5312fb2b4a1a
using Plots

# ╔═╡ 46dda2a7-9ca8-4c2e-b00e-e6aa95761f65
md"
# 3D problem

This debugging notebook compares Lorenzo's solver for 2D unit cells (in progress)
against Luke's solver for 1D unit cells.
"

# ╔═╡ aefce721-b5f9-4008-963a-898ac77aec66
md"
## Incident wave parameters
"

# ╔═╡ b75a1f3b-e5f6-4cb5-b2a9-550e07976f44
kᵢ = 10.0 # wavenumber k²=ω²μ₀ϵ₀ of incident wave

# ╔═╡ 0c5978ca-6b44-4bf0-b6e7-467bfa98ec60
θⁱ = π/2.0 # incidence angle (measured with respect to the x axis in xz plane)

# ╔═╡ a07150dd-1230-425e-b773-76cc0b03ba58
αᵢ = kᵢ * cos(θⁱ) # component of kᵢ along x (sheet)

# ╔═╡ a25e6d0f-eedd-40e9-ba76-984b72b19563
βᵢ = kᵢ * sin(θⁱ) # component of kᵢ along y (transmiision axis)

# ╔═╡ d0138354-2ce6-4d8c-b222-992f5df51b57
md"
## Metasurface impedance parameters

Using the metasurface parameters described in
[Pérez-Arancibia et al., 2018, Sideways adiabaticity: beyond ray optics for slowly varying metasurfaces](https://doi.org/10.1364/OE.26.030202)
which result in an incident wave being transmitted at an angle of choice,
while the reflection is attenuated.
"

# ╔═╡ 517389c5-8ea6-4406-9726-06d6438d9ceb
k = kᵢ / 1.1 # wavenumber designed for metasurface
# do not choose k = kᵢ (a.k.a Wood anomaly)

# ╔═╡ 2f1ab012-2b32-4cff-a6e7-1154a07a890a
θᵗ = -π/3    # transmitted field angle

# ╔═╡ e166637c-5160-41bc-b4d0-924d3da59985
d  = cos(θᵗ)-cos(θⁱ) # metasurface design parameter

# ╔═╡ 863dca2b-d32a-4cb1-8ba2-bb051bcd0b6b
L = (2*π)/(k*abs(d))  # Unit cell width

# ╔═╡ c2ea899f-29cf-44d2-b5f0-108b1b9bbb4c
ρₑˣˣ(x⃗) = 0.5sin(abs(θⁱ)) * (1 - exp.(im*k*d*x⃗[1]))

# ╔═╡ b551098c-c1c3-45c0-9129-99f555acbf4f
ρₑˣʸ(x⃗) = zero(eltype(x⃗[1]))

# ╔═╡ 34c99ebe-8429-401a-a101-c18d70ac2543
ρₑʸˣ(x⃗) = zero(eltype(x⃗[1]))

# ╔═╡ 4dc98adb-729d-4458-a6cc-9ee2bc2b73e6
ρₑʸʸ(x⃗) = zero(eltype(x⃗[1]))

# ╔═╡ dea64f1e-e74f-452e-9b04-2e8de9e0199c
σₘˣˣ(x⃗) = zero(eltype(x⃗[1]))

# ╔═╡ 5d492cf3-a5e9-4f17-8dee-ed14393ef003
σₘˣʸ(x⃗) = zero(eltype(x⃗[1]))

# ╔═╡ d6a00833-a98e-4f58-bb72-d12f5c0bf7d3
σₘʸˣ(x⃗) = zero(eltype(x⃗[1]))

# ╔═╡ ead74c7d-b9cf-40fc-b2d8-9f6357599d63
σₘʸʸ(x⃗) = 2sin(abs(θⁱ)) * (1 + exp(im*k*d*x⃗[1]))

# ╔═╡ 5f608c76-a381-468a-824b-5be3f1a1c009
md"
## Discretization parameters
"

# ╔═╡ 513288b8-a444-44f1-9e64-1eeff0b0da6c
Nx = 25 # number of discretization points / modes along unit cell

# ╔═╡ dfdcfa71-2876-4e3f-be1a-27730fcfc47c
Ny = 1

# ╔═╡ 4216696f-8958-4691-9f77-c9b77998ea55
Nxy = Nx * Ny

# ╔═╡ b6ee3a1c-790a-46d6-877f-6fd17a52c0f9
Lx = L # Unit cell width (integer multiple of period of metasurface)

# ╔═╡ b0869bb2-25a7-437a-900a-17ccef7162ba
Ly = L

# ╔═╡ 952ad83c-39ae-4f2c-beed-6124b1b8f3ac
dx = Lx/Nx

# ╔═╡ 6328f2b3-71eb-4a14-8868-36dee395b562
dy = Ly/Ny

# ╔═╡ 7a0d472a-705c-4699-8097-86894476c147
x⃗ = range(0, step=dx, length=Nx)

# ╔═╡ dbf104bd-4baf-4319-8aab-d9c1cffc1369
y⃗ = range(0, step=dy, length=Ny)

# ╔═╡ 4025b58f-f538-4492-8ce7-0b1ce5a41095
r⃗ = Iterators.product(x⃗, y⃗)

# ╔═╡ 18b42581-4df0-48ef-b2b2-c74332421b05
z⃗ = range(-L, L, length=2Nx)

# ╔═╡ 50a8c6d5-e45e-4393-8cb7-88235603908a
z⃗₁ = z⃗[z⃗ .< 0]

# ╔═╡ 88a871b9-4533-49f0-ab10-2544463c11f8
z⃗₂ = z⃗[z⃗ .> 0]

# ╔═╡ b6ca58f3-a916-4c80-b188-58fd07db8461
k⃗ˣ = 2*pi*fftfreq(Nx, 1/dx)

# ╔═╡ 16f10984-4df6-4c92-925e-a220aae156a3
k⃗ʸ = 2*pi*fftfreq(Ny, 1/dy)

# ╔═╡ 39e29cf4-e8ba-4350-b382-d9ee32e3963e
k⃗ = Iterators.product(k⃗ˣ, k⃗ʸ)

# ╔═╡ 202161a1-8f80-4d74-aeda-8433fbfe7378
β⃗ˣ = @. sqrt(Complex(kᵢ^2 - k⃗ˣ^2))

# ╔═╡ 2f205787-8ea0-419b-8972-a6aed1557e63
β⃗ˣʸ = map(e⃗ -> sqrt(Complex(kᵢ^2 - mapreduce(abs2, +, e⃗, init=zero(kᵢ)))), k⃗)

# ╔═╡ 7dcb06d9-8e27-4182-97b2-77eecf851e6e
i = findfirst(k⃗ˣ .- αᵢ .< eps()) # mode index of incident wave

# ╔═╡ 56a1ed22-f069-4926-abd9-1ef4c7e4e0ae
@assert i == findfirst(real.(β⃗ˣ .- βᵢ) .< eps())

# ╔═╡ 545c10ff-fa96-4807-80a8-1b065fb5e244
md"
## Helper functions
"

# ╔═╡ 68f9cc5b-d55b-42e7-a31f-b85b8ebc3820
"""
    diagFT(A::AbstractArray{<:Number, N}) where N
Accepts a diagonal endomorphism in N-D position space and returns the Fourier
transformed rank-2N tensor reshaped to a matrix with flattened indices
"""
function diagFT(A::AbstractArray{<:Number, ndim}) where ndim
    n⃗ = size(A)
    n = prod(n⃗)
    Ã = Array(reshape(Diagonal(reshape(A, n)), (n⃗..., n⃗...)))
    Ã = fft(ifft(Ã, 1:ndim), (ndim+1):2ndim)
    reshape(Ã, (n, n))
end

# ╔═╡ 5020f47a-61f9-4622-82a6-80bbd5e1c833
md"
## Luke's solver
"

# ╔═╡ d1a60c40-d46f-4ac9-9293-cd09946e4209
begin
	β̂ˣ = Diagonal(β⃗ˣ)
    ρ̃ₑˣˣ = diagFT(ρₑˣˣ.(Iterators.product(x⃗)))
    σ̃ₘʸʸ = diagFT(σₘʸʸ.(Iterators.product(x⃗)))
    A = [
        0.5β̂ˣ+kᵢ*ρ̃ₑˣˣ    -0.5β̂ˣ-kᵢ*ρ̃ₑˣˣ;
        2β̂ˣ+kᵢ*σ̃ₘʸʸ     2β̂ˣ+kᵢ*σ̃ₘʸʸ
    ]
    B = [
        0.5β̂ˣ-kᵢ*ρ̃ₑˣˣ    -0.5β̂ˣ+kᵢ*ρ̃ₑˣˣ;
        2β̂ˣ-kᵢ*σ̃ₘʸʸ     2β̂ˣ-kᵢ*σ̃ₘʸʸ
    ]
	S = A\B
	if θⁱ < 0
		I₁ = zeros(Nx)
		I₂ = [n==i ? 1 : 0 for n in 1:Nx]
	else
		I₁ = [n==i ? 1 : 0 for n in 1:Nx]
		I₂ = zeros(Nx)
	end
	inc = [I₁; I₂]
	scat = S*inc
	O₁ = scat[1:Nx]
	O₂ = scat[Nx+1:2Nx]
end;

# ╔═╡ eb3dd186-3741-42ee-9515-ace5b7126a86
begin
	# quick plotting
	BO₁ = rotr90(bfft(exp.(-β⃗ˣ * transpose(im * z⃗₁)) .* O₁, 1))
	BI₂ = rotr90(bfft(exp.(-β⃗ˣ * transpose(im * z⃗₂)) .* I₂, 1))
	BI₁ = rotr90(bfft(exp.( β⃗ˣ * transpose(im * z⃗₁)) .* I₁, 1))
	BO₂ = rotr90(bfft(exp.( β⃗ˣ * transpose(im * z⃗₂)) .* O₂, 1))
	fields = cat(BI₁ + BO₁, BI₂ + BO₂; dims=1)
	heatmap(x⃗, z⃗, real.(fields), xguide="x", yguide="z", aspect_ratio=:equal,color=:RdBu,clim=(-1.0,1.0))
end

# ╔═╡ 47bde8a6-2106-4baa-b472-8636e035baef
md"
## Lorenzo's solver
"

# ╔═╡ 8006c599-9451-4d85-b18e-adf2cd4eca56
R = [
	0I(Nxy)	-I(Nxy);
	1I(Nxy)	0I(Nxy)
]

# ╔═╡ 36e2b2e0-c5f4-4d9c-92f9-779828c44e2f
K = [
	Diagonal(vec(map(e⃗ -> e⃗[1]^2, k⃗)))	Diagonal(vec(map(e⃗ -> e⃗[1]*e⃗[2], k⃗)));
	Diagonal(vec(map(e⃗ -> e⃗[1]*e⃗[2], k⃗)))	Diagonal(vec(map(e⃗ -> e⃗[2]^2, k⃗)))
]

# ╔═╡ edf27dee-85ea-42e6-a489-a8fea95d3075
ρ̃ₑ = [
	diagFT(ρₑˣˣ.(r⃗))	diagFT(ρₑˣʸ.(r⃗));
	diagFT(ρₑʸˣ.(r⃗))	diagFT(ρₑʸʸ.(r⃗))
]

# ╔═╡ 9159265a-1858-42c8-8ab0-e48c3cfa86eb
ρ̃′ₑ = R' * ρ̃ₑ * R

# ╔═╡ 78eb0b10-c309-4da4-8bfc-75d1642b90f7
σ̃ₘ = [
	diagFT(σₘˣˣ.(r⃗))	diagFT(σₘˣʸ.(r⃗));
	diagFT(σₘʸˣ.(r⃗))	diagFT(σₘʸʸ.(r⃗))
]

# ╔═╡ b34d564a-7c80-4fa9-aa6d-14d30887d894
β̂ˣʸ = [
	Diagonal(vec(β⃗ˣʸ))	0.0im*I(Nxy);
	0.0im*I(Nxy) Diagonal(vec(β⃗ˣʸ))
]

# ╔═╡ 97bc0901-86ba-4f1d-b217-f25069977395
k⃗² = kᵢ^2*I(2Nxy)

# ╔═╡ da289e3e-283b-41b3-82cc-8342b7417cf4
AA = [
	(-(K - k⃗²) * ρ̃′ₑ + 0.5kᵢ*β̂ˣʸ)	((K - k⃗²) * ρ̃′ₑ - 0.5kᵢ*β̂ˣʸ);
	(-(K - k⃗²) * σ̃ₘ + 2kᵢ*β̂ˣʸ)		(-(K - k⃗²) * σ̃ₘ + 2kᵢ*β̂ˣʸ)
	# (-(R * K * R' - k⃗²) + 0.5kᵢ*β̂ˣʸ * σ̃ₘ)		((R * K * R' - k⃗²) - 0.5kᵢ*β̂ˣʸ * σ̃ₘ)
]

# ╔═╡ 111d9973-91bb-481c-b33e-067cfe11d0e1
BB = [
	((K - k⃗²) * ρ̃′ₑ + 0.5kᵢ*β̂ˣʸ)	(-(K - k⃗²) * ρ̃′ₑ - 0.5kᵢ*β̂ˣʸ);
	((K - k⃗²) * σ̃ₘ + 2kᵢ*β̂ˣʸ)		((K - k⃗²) * σ̃ₘ + 2kᵢ*β̂ˣʸ)
	# ((R * K * R' - k⃗²) + 0.5kᵢ*β̂ˣʸ * σ̃ₘ)		(-(R * K * R' - k⃗²) - 0.5kᵢ*β̂ˣʸ * σ̃ₘ)
]

# ╔═╡ 8a686e69-6b69-4e68-8b79-23140b5ee4dd
SS = AA\BB

# ╔═╡ 1d3b118a-a8b0-4dad-aecb-a75e9ea3161b
begin
	### Define equivalent 2D inputs
	I₁ˣ = [n==1 ? 0 : 0 for n in 1:Nxy]
	I₂ˣ = [n==1 ? 0 : 0 for n in 1:Nxy]
	I₁ʸ = [n==1 ? 1 : 0 for n in 1:Nxy]
	I₂ʸ = [n==1 ? 0 : 0 for n in 1:Nxy]
end;

# ╔═╡ e2892425-ca03-4823-89be-8ebc4439206b
out = SS * vcat(I₁ˣ, I₁ʸ, I₂ˣ, I₂ʸ)

# ╔═╡ 877eca8b-9e16-4a7d-ba56-8d44569ffaa3
begin
	O₁ˣ = out[1:Nxy]
	O₁ʸ = out[Nxy+1:2Nxy]
	O₂ˣ = out[2Nxy+1:3Nxy]
	O₂ʸ = out[3Nxy+1:4Nxy]
end;

# ╔═╡ 209f25c0-aecd-4983-888e-ddaa4b513e0e
begin
	# quick plotting
	CO₁ = rotr90(bfft(exp.(-β⃗ˣ * transpose(im * z⃗₁)) .* O₁ʸ[1:Nx], 1))
	CI₂ = rotr90(bfft(exp.(-β⃗ˣ * transpose(im * z⃗₂)) .* I₂ʸ[1:Nx], 1))
	CI₁ = rotr90(bfft(exp.( β⃗ˣ * transpose(im * z⃗₁)) .* I₁ʸ[1:Nx], 1))
	CO₂ = rotr90(bfft(exp.( β⃗ˣ * transpose(im * z⃗₂)) .* O₂ʸ[1:Nx], 1))
	heatmap(x⃗, z⃗, real.(cat(CI₁ + CO₁, CI₂ + CO₂; dims=1)), xguide="x", yguide="z", aspect_ratio=:equal,color=:RdBu,clim=(-1.0,1.0))
end

# ╔═╡ 00000000-0000-0000-0000-000000000001
PLUTO_PROJECT_TOML_CONTENTS = """
[deps]
BlockArrays = "8e7c35d0-a365-5155-bbbb-fb81a777f24e"
FFTW = "7a1cc6ca-52ef-59f5-83cd-3a7055c09341"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"

[compat]
BlockArrays = "~0.16.7"
FFTW = "~1.4.5"
Plots = "~1.22.2"
"""

# ╔═╡ 00000000-0000-0000-0000-000000000002
PLUTO_MANIFEST_TOML_CONTENTS = """
# This file is machine-generated - editing it directly is not advised

[[AbstractFFTs]]
deps = ["LinearAlgebra"]
git-tree-sha1 = "485ee0867925449198280d4af84bdb46a2a404d0"
uuid = "621f4979-c628-5d54-868e-fcf4e3e8185c"
version = "1.0.1"

[[Adapt]]
deps = ["LinearAlgebra"]
git-tree-sha1 = "84918055d15b3114ede17ac6a7182f68870c16f7"
uuid = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
version = "3.3.1"

[[ArgTools]]
uuid = "0dad84c5-d112-42e6-8d28-ef12dabb789f"

[[ArrayLayouts]]
deps = ["FillArrays", "LinearAlgebra", "SparseArrays"]
git-tree-sha1 = "623a32b87ef0b85d26320a8cc7e57ded707aef64"
uuid = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"
version = "0.7.5"

[[Artifacts]]
uuid = "56f22d72-fd6d-98f1-02f0-08ddc0907c33"

[[Base64]]
uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"

[[BlockArrays]]
deps = ["ArrayLayouts", "FillArrays", "LinearAlgebra"]
git-tree-sha1 = "57a7b84893e2e11739e345ecfef14e43d7ed1143"
uuid = "8e7c35d0-a365-5155-bbbb-fb81a777f24e"
version = "0.16.7"

[[Bzip2_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "19a35467a82e236ff51bc17a3a44b69ef35185a2"
uuid = "6e34b625-4abd-537c-b88f-471c36dfa7a0"
version = "1.0.8+0"

[[Cairo_jll]]
deps = ["Artifacts", "Bzip2_jll", "Fontconfig_jll", "FreeType2_jll", "Glib_jll", "JLLWrappers", "LZO_jll", "Libdl", "Pixman_jll", "Pkg", "Xorg_libXext_jll", "Xorg_libXrender_jll", "Zlib_jll", "libpng_jll"]
git-tree-sha1 = "f2202b55d816427cd385a9a4f3ffb226bee80f99"
uuid = "83423d85-b0ee-5818-9007-b63ccbeb887a"
version = "1.16.1+0"

[[ColorSchemes]]
deps = ["ColorTypes", "Colors", "FixedPointNumbers", "Random"]
git-tree-sha1 = "9995eb3977fbf67b86d0a0a0508e83017ded03f2"
uuid = "35d6a980-a343-548e-a6ea-1d62b119f2f4"
version = "3.14.0"

[[ColorTypes]]
deps = ["FixedPointNumbers", "Random"]
git-tree-sha1 = "024fe24d83e4a5bf5fc80501a314ce0d1aa35597"
uuid = "3da002f7-5984-5a60-b8a6-cbb66c0b333f"
version = "0.11.0"

[[Colors]]
deps = ["ColorTypes", "FixedPointNumbers", "Reexport"]
git-tree-sha1 = "417b0ed7b8b838aa6ca0a87aadf1bb9eb111ce40"
uuid = "5ae59095-9a9b-59fe-a467-6f913c188581"
version = "0.12.8"

[[Compat]]
deps = ["Base64", "Dates", "DelimitedFiles", "Distributed", "InteractiveUtils", "LibGit2", "Libdl", "LinearAlgebra", "Markdown", "Mmap", "Pkg", "Printf", "REPL", "Random", "SHA", "Serialization", "SharedArrays", "Sockets", "SparseArrays", "Statistics", "Test", "UUIDs", "Unicode"]
git-tree-sha1 = "1a90210acd935f222ea19657f143004d2c2a1117"
uuid = "34da2185-b29b-5c13-b0c7-acf172513d20"
version = "3.38.0"

[[CompilerSupportLibraries_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "e66e0078-7015-5450-92f7-15fbd957f2ae"

[[Contour]]
deps = ["StaticArrays"]
git-tree-sha1 = "9f02045d934dc030edad45944ea80dbd1f0ebea7"
uuid = "d38c429a-6771-53c6-b99e-75d170b6e991"
version = "0.5.7"

[[DataAPI]]
git-tree-sha1 = "cc70b17275652eb47bc9e5f81635981f13cea5c8"
uuid = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a"
version = "1.9.0"

[[DataStructures]]
deps = ["Compat", "InteractiveUtils", "OrderedCollections"]
git-tree-sha1 = "7d9d316f04214f7efdbb6398d545446e246eff02"
uuid = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
version = "0.18.10"

[[DataValueInterfaces]]
git-tree-sha1 = "bfc1187b79289637fa0ef6d4436ebdfe6905cbd6"
uuid = "e2d170a0-9d28-54be-80f0-106bbe20a464"
version = "1.0.0"

[[Dates]]
deps = ["Printf"]
uuid = "ade2ca70-3891-5945-98fb-dc099432e06a"

[[DelimitedFiles]]
deps = ["Mmap"]
uuid = "8bb1440f-4735-579b-a4ab-409b98df4dab"

[[Distributed]]
deps = ["Random", "Serialization", "Sockets"]
uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b"

[[Downloads]]
deps = ["ArgTools", "LibCURL", "NetworkOptions"]
uuid = "f43a241f-c20a-4ad4-852c-f6b1247861c6"

[[EarCut_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "3f3a2501fa7236e9b911e0f7a588c657e822bb6d"
uuid = "5ae413db-bbd1-5e63-b57d-d24a61df00f5"
version = "2.2.3+0"

[[Expat_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "b3bfd02e98aedfa5cf885665493c5598c350cd2f"
uuid = "2e619515-83b5-522b-bb60-26c02a35a201"
version = "2.2.10+0"

[[FFMPEG]]
deps = ["FFMPEG_jll"]
git-tree-sha1 = "b57e3acbe22f8484b4b5ff66a7499717fe1a9cc8"
uuid = "c87230d0-a227-11e9-1b43-d7ebe4e7570a"
version = "0.4.1"

[[FFMPEG_jll]]
deps = ["Artifacts", "Bzip2_jll", "FreeType2_jll", "FriBidi_jll", "JLLWrappers", "LAME_jll", "Libdl", "Ogg_jll", "OpenSSL_jll", "Opus_jll", "Pkg", "Zlib_jll", "libass_jll", "libfdk_aac_jll", "libvorbis_jll", "x264_jll", "x265_jll"]
git-tree-sha1 = "d8a578692e3077ac998b50c0217dfd67f21d1e5f"
uuid = "b22a6f82-2f65-5046-a5b2-351ab43fb4e5"
version = "4.4.0+0"

[[FFTW]]
deps = ["AbstractFFTs", "FFTW_jll", "LinearAlgebra", "MKL_jll", "Preferences", "Reexport"]
git-tree-sha1 = "463cb335fa22c4ebacfd1faba5fde14edb80d96c"
uuid = "7a1cc6ca-52ef-59f5-83cd-3a7055c09341"
version = "1.4.5"

[[FFTW_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "c6033cc3892d0ef5bb9cd29b7f2f0331ea5184ea"
uuid = "f5851436-0d7a-5f13-b9de-f02708fd171a"
version = "3.3.10+0"

[[FillArrays]]
deps = ["LinearAlgebra", "Random", "SparseArrays", "Statistics"]
git-tree-sha1 = "7f6ad1a7f4621b4ab8e554133dade99ebc6e7221"
uuid = "1a297f60-69ca-5386-bcde-b61e274b549b"
version = "0.12.5"

[[FixedPointNumbers]]
deps = ["Statistics"]
git-tree-sha1 = "335bfdceacc84c5cdf16aadc768aa5ddfc5383cc"
uuid = "53c48c17-4a7d-5ca2-90c5-79b7896eea93"
version = "0.8.4"

[[Fontconfig_jll]]
deps = ["Artifacts", "Bzip2_jll", "Expat_jll", "FreeType2_jll", "JLLWrappers", "Libdl", "Libuuid_jll", "Pkg", "Zlib_jll"]
git-tree-sha1 = "21efd19106a55620a188615da6d3d06cd7f6ee03"
uuid = "a3f928ae-7b40-5064-980b-68af3947d34b"
version = "2.13.93+0"

[[Formatting]]
deps = ["Printf"]
git-tree-sha1 = "8339d61043228fdd3eb658d86c926cb282ae72a8"
uuid = "59287772-0a20-5a39-b81b-1366585eb4c0"
version = "0.4.2"

[[FreeType2_jll]]
deps = ["Artifacts", "Bzip2_jll", "JLLWrappers", "Libdl", "Pkg", "Zlib_jll"]
git-tree-sha1 = "87eb71354d8ec1a96d4a7636bd57a7347dde3ef9"
uuid = "d7e528f0-a631-5988-bf34-fe36492bcfd7"
version = "2.10.4+0"

[[FriBidi_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "aa31987c2ba8704e23c6c8ba8a4f769d5d7e4f91"
uuid = "559328eb-81f9-559d-9380-de523a88c83c"
version = "1.0.10+0"

[[GLFW_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Libglvnd_jll", "Pkg", "Xorg_libXcursor_jll", "Xorg_libXi_jll", "Xorg_libXinerama_jll", "Xorg_libXrandr_jll"]
git-tree-sha1 = "0c603255764a1fa0b61752d2bec14cfbd18f7fe8"
uuid = "0656b61e-2033-5cc2-a64a-77c0f6c09b89"
version = "3.3.5+1"

[[GR]]
deps = ["Base64", "DelimitedFiles", "GR_jll", "HTTP", "JSON", "Libdl", "LinearAlgebra", "Pkg", "Printf", "Random", "Serialization", "Sockets", "Test", "UUIDs"]
git-tree-sha1 = "c2178cfbc0a5a552e16d097fae508f2024de61a3"
uuid = "28b8d3ca-fb5f-59d9-8090-bfdbd6d07a71"
version = "0.59.0"

[[GR_jll]]
deps = ["Artifacts", "Bzip2_jll", "Cairo_jll", "FFMPEG_jll", "Fontconfig_jll", "GLFW_jll", "JLLWrappers", "JpegTurbo_jll", "Libdl", "Libtiff_jll", "Pixman_jll", "Pkg", "Qt5Base_jll", "Zlib_jll", "libpng_jll"]
git-tree-sha1 = "ef49a187604f865f4708c90e3f431890724e9012"
uuid = "d2c73de3-f751-5644-a686-071e5b155ba9"
version = "0.59.0+0"

[[GeometryBasics]]
deps = ["EarCut_jll", "IterTools", "LinearAlgebra", "StaticArrays", "StructArrays", "Tables"]
git-tree-sha1 = "58bcdf5ebc057b085e58d95c138725628dd7453c"
uuid = "5c1252a2-5f33-56bf-86c9-59e7332b4326"
version = "0.4.1"

[[Gettext_jll]]
deps = ["Artifacts", "CompilerSupportLibraries_jll", "JLLWrappers", "Libdl", "Libiconv_jll", "Pkg", "XML2_jll"]
git-tree-sha1 = "9b02998aba7bf074d14de89f9d37ca24a1a0b046"
uuid = "78b55507-aeef-58d4-861c-77aaff3498b1"
version = "0.21.0+0"

[[Glib_jll]]
deps = ["Artifacts", "Gettext_jll", "JLLWrappers", "Libdl", "Libffi_jll", "Libiconv_jll", "Libmount_jll", "PCRE_jll", "Pkg", "Zlib_jll"]
git-tree-sha1 = "74ef6288d071f58033d54fd6708d4bc23a8b8972"
uuid = "7746bdde-850d-59dc-9ae8-88ece973131d"
version = "2.68.3+1"

[[Graphite2_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "344bf40dcab1073aca04aa0df4fb092f920e4011"
uuid = "3b182d85-2403-5c21-9c21-1e1f0cc25472"
version = "1.3.14+0"

[[Grisu]]
git-tree-sha1 = "53bb909d1151e57e2484c3d1b53e19552b887fb2"
uuid = "42e2da0e-8278-4e71-bc24-59509adca0fe"
version = "1.0.2"

[[HTTP]]
deps = ["Base64", "Dates", "IniFile", "Logging", "MbedTLS", "NetworkOptions", "Sockets", "URIs"]
git-tree-sha1 = "60ed5f1643927479f845b0135bb369b031b541fa"
uuid = "cd3eb016-35fb-5094-929b-558a96fad6f3"
version = "0.9.14"

[[HarfBuzz_jll]]
deps = ["Artifacts", "Cairo_jll", "Fontconfig_jll", "FreeType2_jll", "Glib_jll", "Graphite2_jll", "JLLWrappers", "Libdl", "Libffi_jll", "Pkg"]
git-tree-sha1 = "129acf094d168394e80ee1dc4bc06ec835e510a3"
uuid = "2e76f6c2-a576-52d4-95c1-20adfe4de566"
version = "2.8.1+1"

[[IniFile]]
deps = ["Test"]
git-tree-sha1 = "098e4d2c533924c921f9f9847274f2ad89e018b8"
uuid = "83e8ac13-25f8-5344-8a64-a9f2b223428f"
version = "0.5.0"

[[IntelOpenMP_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "d979e54b71da82f3a65b62553da4fc3d18c9004c"
uuid = "1d5cc7b8-4909-519e-a0f8-d0f5ad9712d0"
version = "2018.0.3+2"

[[InteractiveUtils]]
deps = ["Markdown"]
uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240"

[[IterTools]]
git-tree-sha1 = "05110a2ab1fc5f932622ffea2a003221f4782c18"
uuid = "c8e1da08-722c-5040-9ed9-7db0dc04731e"
version = "1.3.0"

[[IteratorInterfaceExtensions]]
git-tree-sha1 = "a3f24677c21f5bbe9d2a714f95dcd58337fb2856"
uuid = "82899510-4779-5014-852e-03e436cf321d"
version = "1.0.0"

[[JLLWrappers]]
deps = ["Preferences"]
git-tree-sha1 = "642a199af8b68253517b80bd3bfd17eb4e84df6e"
uuid = "692b3bcd-3c85-4b1f-b108-f13ce0eb3210"
version = "1.3.0"

[[JSON]]
deps = ["Dates", "Mmap", "Parsers", "Unicode"]
git-tree-sha1 = "8076680b162ada2a031f707ac7b4953e30667a37"
uuid = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
version = "0.21.2"

[[JpegTurbo_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "d735490ac75c5cb9f1b00d8b5509c11984dc6943"
uuid = "aacddb02-875f-59d6-b918-886e6ef4fbf8"
version = "2.1.0+0"

[[LAME_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "f6250b16881adf048549549fba48b1161acdac8c"
uuid = "c1c5ebd0-6772-5130-a774-d5fcae4a789d"
version = "3.100.1+0"

[[LZO_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "e5b909bcf985c5e2605737d2ce278ed791b89be6"
uuid = "dd4b983a-f0e5-5f8d-a1b7-129d4a5fb1ac"
version = "2.10.1+0"

[[LaTeXStrings]]
git-tree-sha1 = "c7f1c695e06c01b95a67f0cd1d34994f3e7db104"
uuid = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f"
version = "1.2.1"

[[Latexify]]
deps = ["Formatting", "InteractiveUtils", "LaTeXStrings", "MacroTools", "Markdown", "Printf", "Requires"]
git-tree-sha1 = "a4b12a1bd2ebade87891ab7e36fdbce582301a92"
uuid = "23fbe1c1-3f47-55db-b15f-69d7ec21a316"
version = "0.15.6"

[[LazyArtifacts]]
deps = ["Artifacts", "Pkg"]
uuid = "4af54fe1-eca0-43a8-85a7-787d91b784e3"

[[LibCURL]]
deps = ["LibCURL_jll", "MozillaCACerts_jll"]
uuid = "b27032c2-a3e7-50c8-80cd-2d36dbcbfd21"

[[LibCURL_jll]]
deps = ["Artifacts", "LibSSH2_jll", "Libdl", "MbedTLS_jll", "Zlib_jll", "nghttp2_jll"]
uuid = "deac9b47-8bc7-5906-a0fe-35ac56dc84c0"

[[LibGit2]]
deps = ["Base64", "NetworkOptions", "Printf", "SHA"]
uuid = "76f85450-5226-5b5a-8eaa-529ad045b433"

[[LibSSH2_jll]]
deps = ["Artifacts", "Libdl", "MbedTLS_jll"]
uuid = "29816b5a-b9ab-546f-933c-edad1886dfa8"

[[Libdl]]
uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb"

[[Libffi_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "0b4a5d71f3e5200a7dff793393e09dfc2d874290"
uuid = "e9f186c6-92d2-5b65-8a66-fee21dc1b490"
version = "3.2.2+1"

[[Libgcrypt_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Libgpg_error_jll", "Pkg"]
git-tree-sha1 = "64613c82a59c120435c067c2b809fc61cf5166ae"
uuid = "d4300ac3-e22c-5743-9152-c294e39db1e4"
version = "1.8.7+0"

[[Libglvnd_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Xorg_libX11_jll", "Xorg_libXext_jll"]
git-tree-sha1 = "7739f837d6447403596a75d19ed01fd08d6f56bf"
uuid = "7e76a0d4-f3c7-5321-8279-8d96eeed0f29"
version = "1.3.0+3"

[[Libgpg_error_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "c333716e46366857753e273ce6a69ee0945a6db9"
uuid = "7add5ba3-2f88-524e-9cd5-f83b8a55f7b8"
version = "1.42.0+0"

[[Libiconv_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "42b62845d70a619f063a7da093d995ec8e15e778"
uuid = "94ce4f54-9a6c-5748-9c1c-f9c7231a4531"
version = "1.16.1+1"

[[Libmount_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "9c30530bf0effd46e15e0fdcf2b8636e78cbbd73"
uuid = "4b2f31a3-9ecc-558c-b454-b3730dcb73e9"
version = "2.35.0+0"

[[Libtiff_jll]]
deps = ["Artifacts", "JLLWrappers", "JpegTurbo_jll", "Libdl", "Pkg", "Zlib_jll", "Zstd_jll"]
git-tree-sha1 = "340e257aada13f95f98ee352d316c3bed37c8ab9"
uuid = "89763e89-9b03-5906-acba-b20f662cd828"
version = "4.3.0+0"

[[Libuuid_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "7f3efec06033682db852f8b3bc3c1d2b0a0ab066"
uuid = "38a345b3-de98-5d2b-a5d3-14cd9215e700"
version = "2.36.0+0"

[[LinearAlgebra]]
deps = ["Libdl", "libblastrampoline_jll"]
uuid = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

[[Logging]]
uuid = "56ddb016-857b-54e1-b83d-db4d58db5568"

[[MKL_jll]]
deps = ["Artifacts", "IntelOpenMP_jll", "JLLWrappers", "LazyArtifacts", "Libdl", "Pkg"]
git-tree-sha1 = "5455aef09b40e5020e1520f551fa3135040d4ed0"
uuid = "856f044c-d86e-5d09-b602-aeab76dc8ba7"
version = "2021.1.1+2"

[[MacroTools]]
deps = ["Markdown", "Random"]
git-tree-sha1 = "5a5bc6bf062f0f95e62d0fe0a2d99699fed82dd9"
uuid = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
version = "0.5.8"

[[Markdown]]
deps = ["Base64"]
uuid = "d6f4376e-aef5-505a-96c1-9c027394607a"

[[MbedTLS]]
deps = ["Dates", "MbedTLS_jll", "Random", "Sockets"]
git-tree-sha1 = "1c38e51c3d08ef2278062ebceade0e46cefc96fe"
uuid = "739be429-bea8-5141-9913-cc70e7f3736d"
version = "1.0.3"

[[MbedTLS_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "c8ffd9c3-330d-5841-b78e-0817d7145fa1"

[[Measures]]
git-tree-sha1 = "e498ddeee6f9fdb4551ce855a46f54dbd900245f"
uuid = "442fdcdd-2543-5da2-b0f3-8c86c306513e"
version = "0.3.1"

[[Missings]]
deps = ["DataAPI"]
git-tree-sha1 = "bf210ce90b6c9eed32d25dbcae1ebc565df2687f"
uuid = "e1d29d7a-bbdc-5cf2-9ac0-f12de2c33e28"
version = "1.0.2"

[[Mmap]]
uuid = "a63ad114-7e13-5084-954f-fe012c677804"

[[MozillaCACerts_jll]]
uuid = "14a3606d-f60d-562e-9121-12d972cd8159"

[[NaNMath]]
git-tree-sha1 = "bfe47e760d60b82b66b61d2d44128b62e3a369fb"
uuid = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3"
version = "0.3.5"

[[NetworkOptions]]
uuid = "ca575930-c2e3-43a9-ace4-1e988b2c1908"

[[Ogg_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "7937eda4681660b4d6aeeecc2f7e1c81c8ee4e2f"
uuid = "e7412a2a-1a6e-54c0-be00-318e2571c051"
version = "1.3.5+0"

[[OpenBLAS_jll]]
deps = ["Artifacts", "CompilerSupportLibraries_jll", "Libdl"]
uuid = "4536629a-c528-5b80-bd46-f80d51c5b363"

[[OpenSSL_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "15003dcb7d8db3c6c857fda14891a539a8f2705a"
uuid = "458c3c95-2e84-50aa-8efc-19380b2a3a95"
version = "1.1.10+0"

[[Opus_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "51a08fb14ec28da2ec7a927c4337e4332c2a4720"
uuid = "91d4177d-7536-5919-b921-800302f37372"
version = "1.3.2+0"

[[OrderedCollections]]
git-tree-sha1 = "85f8e6578bf1f9ee0d11e7bb1b1456435479d47c"
uuid = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
version = "1.4.1"

[[PCRE_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "b2a7af664e098055a7529ad1a900ded962bca488"
uuid = "2f80f16e-611a-54ab-bc61-aa92de5b98fc"
version = "8.44.0+0"

[[Parsers]]
deps = ["Dates"]
git-tree-sha1 = "9d8c00ef7a8d110787ff6f170579846f776133a9"
uuid = "69de0a69-1ddd-5017-9359-2bf0b02dc9f0"
version = "2.0.4"

[[Pixman_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "b4f5d02549a10e20780a24fce72bea96b6329e29"
uuid = "30392449-352a-5448-841d-b1acce4e97dc"
version = "0.40.1+0"

[[Pkg]]
deps = ["Artifacts", "Dates", "Downloads", "LibGit2", "Libdl", "Logging", "Markdown", "Printf", "REPL", "Random", "SHA", "Serialization", "TOML", "Tar", "UUIDs", "p7zip_jll"]
uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"

[[PlotThemes]]
deps = ["PlotUtils", "Requires", "Statistics"]
git-tree-sha1 = "a3a964ce9dc7898193536002a6dd892b1b5a6f1d"
uuid = "ccf2f8ad-2431-5c83-bf29-c5338b663b6a"
version = "2.0.1"

[[PlotUtils]]
deps = ["ColorSchemes", "Colors", "Dates", "Printf", "Random", "Reexport", "Statistics"]
git-tree-sha1 = "2537ed3c0ed5e03896927187f5f2ee6a4ab342db"
uuid = "995b91a9-d308-5afd-9ec6-746e21dbc043"
version = "1.0.14"

[[Plots]]
deps = ["Base64", "Contour", "Dates", "Downloads", "FFMPEG", "FixedPointNumbers", "GR", "GeometryBasics", "JSON", "Latexify", "LinearAlgebra", "Measures", "NaNMath", "PlotThemes", "PlotUtils", "Printf", "REPL", "Random", "RecipesBase", "RecipesPipeline", "Reexport", "Requires", "Scratch", "Showoff", "SparseArrays", "Statistics", "StatsBase", "UUIDs"]
git-tree-sha1 = "457b13497a3ea4deb33d273a6a5ea15c25c0ebd9"
uuid = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
version = "1.22.2"

[[Preferences]]
deps = ["TOML"]
git-tree-sha1 = "00cfd92944ca9c760982747e9a1d0d5d86ab1e5a"
uuid = "21216c6a-2e73-6563-6e65-726566657250"
version = "1.2.2"

[[Printf]]
deps = ["Unicode"]
uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7"

[[Qt5Base_jll]]
deps = ["Artifacts", "CompilerSupportLibraries_jll", "Fontconfig_jll", "Glib_jll", "JLLWrappers", "Libdl", "Libglvnd_jll", "OpenSSL_jll", "Pkg", "Xorg_libXext_jll", "Xorg_libxcb_jll", "Xorg_xcb_util_image_jll", "Xorg_xcb_util_keysyms_jll", "Xorg_xcb_util_renderutil_jll", "Xorg_xcb_util_wm_jll", "Zlib_jll", "xkbcommon_jll"]
git-tree-sha1 = "ad368663a5e20dbb8d6dc2fddeefe4dae0781ae8"
uuid = "ea2cea3b-5b76-57ae-a6ef-0a8af62496e1"
version = "5.15.3+0"

[[REPL]]
deps = ["InteractiveUtils", "Markdown", "Sockets", "Unicode"]
uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"

[[Random]]
deps = ["SHA", "Serialization"]
uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

[[RecipesBase]]
git-tree-sha1 = "44a75aa7a527910ee3d1751d1f0e4148698add9e"
uuid = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
version = "1.1.2"

[[RecipesPipeline]]
deps = ["Dates", "NaNMath", "PlotUtils", "RecipesBase"]
git-tree-sha1 = "7ad0dfa8d03b7bcf8c597f59f5292801730c55b8"
uuid = "01d81517-befc-4cb6-b9ec-a95719d0359c"
version = "0.4.1"

[[Reexport]]
git-tree-sha1 = "45e428421666073eab6f2da5c9d310d99bb12f9b"
uuid = "189a3867-3050-52da-a836-e630ba90ab69"
version = "1.2.2"

[[Requires]]
deps = ["UUIDs"]
git-tree-sha1 = "4036a3bd08ac7e968e27c203d45f5fff15020621"
uuid = "ae029012-a4dd-5104-9daa-d747884805df"
version = "1.1.3"

[[SHA]]
uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce"

[[Scratch]]
deps = ["Dates"]
git-tree-sha1 = "0b4b7f1393cff97c33891da2a0bf69c6ed241fda"
uuid = "6c6a2e73-6563-6170-7368-637461726353"
version = "1.1.0"

[[Serialization]]
uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b"

[[SharedArrays]]
deps = ["Distributed", "Mmap", "Random", "Serialization"]
uuid = "1a1011a3-84de-559e-8e89-a11a2f7dc383"

[[Showoff]]
deps = ["Dates", "Grisu"]
git-tree-sha1 = "91eddf657aca81df9ae6ceb20b959ae5653ad1de"
uuid = "992d4aef-0814-514b-bc4d-f2e9a6c4116f"
version = "1.0.3"

[[Sockets]]
uuid = "6462fe0b-24de-5631-8697-dd941f90decc"

[[SortingAlgorithms]]
deps = ["DataStructures"]
git-tree-sha1 = "b3363d7460f7d098ca0912c69b082f75625d7508"
uuid = "a2af1166-a08f-5f64-846c-94a0d3cef48c"
version = "1.0.1"

[[SparseArrays]]
deps = ["LinearAlgebra", "Random"]
uuid = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"

[[StaticArrays]]
deps = ["LinearAlgebra", "Random", "Statistics"]
git-tree-sha1 = "3240808c6d463ac46f1c1cd7638375cd22abbccb"
uuid = "90137ffa-7385-5640-81b9-e52037218182"
version = "1.2.12"

[[Statistics]]
deps = ["LinearAlgebra", "SparseArrays"]
uuid = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"

[[StatsAPI]]
git-tree-sha1 = "1958272568dc176a1d881acb797beb909c785510"
uuid = "82ae8749-77ed-4fe6-ae5f-f523153014b0"
version = "1.0.0"

[[StatsBase]]
deps = ["DataAPI", "DataStructures", "LinearAlgebra", "Missings", "Printf", "Random", "SortingAlgorithms", "SparseArrays", "Statistics", "StatsAPI"]
git-tree-sha1 = "8cbbc098554648c84f79a463c9ff0fd277144b6c"
uuid = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
version = "0.33.10"

[[StructArrays]]
deps = ["Adapt", "DataAPI", "StaticArrays", "Tables"]
git-tree-sha1 = "2ce41e0d042c60ecd131e9fb7154a3bfadbf50d3"
uuid = "09ab397b-f2b6-538f-b94a-2f83cf4a842a"
version = "0.6.3"

[[TOML]]
deps = ["Dates"]
uuid = "fa267f1f-6049-4f14-aa54-33bafae1ed76"

[[TableTraits]]
deps = ["IteratorInterfaceExtensions"]
git-tree-sha1 = "c06b2f539df1c6efa794486abfb6ed2022561a39"
uuid = "3783bdb8-4a98-5b6b-af9a-565f29a5fe9c"
version = "1.0.1"

[[Tables]]
deps = ["DataAPI", "DataValueInterfaces", "IteratorInterfaceExtensions", "LinearAlgebra", "TableTraits", "Test"]
git-tree-sha1 = "1162ce4a6c4b7e31e0e6b14486a6986951c73be9"
uuid = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
version = "1.5.2"

[[Tar]]
deps = ["ArgTools", "SHA"]
uuid = "a4e569a6-e804-4fa4-b0f3-eef7a1d5b13e"

[[Test]]
deps = ["InteractiveUtils", "Logging", "Random", "Serialization"]
uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[[URIs]]
git-tree-sha1 = "97bbe755a53fe859669cd907f2d96aee8d2c1355"
uuid = "5c2747f8-b7ea-4ff2-ba2e-563bfd36b1d4"
version = "1.3.0"

[[UUIDs]]
deps = ["Random", "SHA"]
uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"

[[Unicode]]
uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5"

[[Wayland_jll]]
deps = ["Artifacts", "Expat_jll", "JLLWrappers", "Libdl", "Libffi_jll", "Pkg", "XML2_jll"]
git-tree-sha1 = "3e61f0b86f90dacb0bc0e73a0c5a83f6a8636e23"
uuid = "a2964d1f-97da-50d4-b82a-358c7fce9d89"
version = "1.19.0+0"

[[Wayland_protocols_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Wayland_jll"]
git-tree-sha1 = "2839f1c1296940218e35df0bbb220f2a79686670"
uuid = "2381bf8a-dfd0-557d-9999-79630e7b1b91"
version = "1.18.0+4"

[[XML2_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Libiconv_jll", "Pkg", "Zlib_jll"]
git-tree-sha1 = "1acf5bdf07aa0907e0a37d3718bb88d4b687b74a"
uuid = "02c8fc9c-b97f-50b9-bbe4-9be30ff0a78a"
version = "2.9.12+0"

[[XSLT_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Libgcrypt_jll", "Libgpg_error_jll", "Libiconv_jll", "Pkg", "XML2_jll", "Zlib_jll"]
git-tree-sha1 = "91844873c4085240b95e795f692c4cec4d805f8a"
uuid = "aed1982a-8fda-507f-9586-7b0439959a61"
version = "1.1.34+0"

[[Xorg_libX11_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Xorg_libxcb_jll", "Xorg_xtrans_jll"]
git-tree-sha1 = "5be649d550f3f4b95308bf0183b82e2582876527"
uuid = "4f6342f7-b3d2-589e-9d20-edeb45f2b2bc"
version = "1.6.9+4"

[[Xorg_libXau_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "4e490d5c960c314f33885790ed410ff3a94ce67e"
uuid = "0c0b7dd1-d40b-584c-a123-a41640f87eec"
version = "1.0.9+4"

[[Xorg_libXcursor_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Xorg_libXfixes_jll", "Xorg_libXrender_jll"]
git-tree-sha1 = "12e0eb3bc634fa2080c1c37fccf56f7c22989afd"
uuid = "935fb764-8cf2-53bf-bb30-45bb1f8bf724"
version = "1.2.0+4"

[[Xorg_libXdmcp_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "4fe47bd2247248125c428978740e18a681372dd4"
uuid = "a3789734-cfe1-5b06-b2d0-1dd0d9d62d05"
version = "1.1.3+4"

[[Xorg_libXext_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Xorg_libX11_jll"]
git-tree-sha1 = "b7c0aa8c376b31e4852b360222848637f481f8c3"
uuid = "1082639a-0dae-5f34-9b06-72781eeb8cb3"
version = "1.3.4+4"

[[Xorg_libXfixes_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Xorg_libX11_jll"]
git-tree-sha1 = "0e0dc7431e7a0587559f9294aeec269471c991a4"
uuid = "d091e8ba-531a-589c-9de9-94069b037ed8"
version = "5.0.3+4"

[[Xorg_libXi_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Xorg_libXext_jll", "Xorg_libXfixes_jll"]
git-tree-sha1 = "89b52bc2160aadc84d707093930ef0bffa641246"
uuid = "a51aa0fd-4e3c-5386-b890-e753decda492"
version = "1.7.10+4"

[[Xorg_libXinerama_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Xorg_libXext_jll"]
git-tree-sha1 = "26be8b1c342929259317d8b9f7b53bf2bb73b123"
uuid = "d1454406-59df-5ea1-beac-c340f2130bc3"
version = "1.1.4+4"

[[Xorg_libXrandr_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Xorg_libXext_jll", "Xorg_libXrender_jll"]
git-tree-sha1 = "34cea83cb726fb58f325887bf0612c6b3fb17631"
uuid = "ec84b674-ba8e-5d96-8ba1-2a689ba10484"
version = "1.5.2+4"

[[Xorg_libXrender_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Xorg_libX11_jll"]
git-tree-sha1 = "19560f30fd49f4d4efbe7002a1037f8c43d43b96"
uuid = "ea2f1a96-1ddc-540d-b46f-429655e07cfa"
version = "0.9.10+4"

[[Xorg_libpthread_stubs_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "6783737e45d3c59a4a4c4091f5f88cdcf0908cbb"
uuid = "14d82f49-176c-5ed1-bb49-ad3f5cbd8c74"
version = "0.1.0+3"

[[Xorg_libxcb_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "XSLT_jll", "Xorg_libXau_jll", "Xorg_libXdmcp_jll", "Xorg_libpthread_stubs_jll"]
git-tree-sha1 = "daf17f441228e7a3833846cd048892861cff16d6"
uuid = "c7cfdc94-dc32-55de-ac96-5a1b8d977c5b"
version = "1.13.0+3"

[[Xorg_libxkbfile_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Xorg_libX11_jll"]
git-tree-sha1 = "926af861744212db0eb001d9e40b5d16292080b2"
uuid = "cc61e674-0454-545c-8b26-ed2c68acab7a"
version = "1.1.0+4"

[[Xorg_xcb_util_image_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Xorg_xcb_util_jll"]
git-tree-sha1 = "0fab0a40349ba1cba2c1da699243396ff8e94b97"
uuid = "12413925-8142-5f55-bb0e-6d7ca50bb09b"
version = "0.4.0+1"

[[Xorg_xcb_util_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Xorg_libxcb_jll"]
git-tree-sha1 = "e7fd7b2881fa2eaa72717420894d3938177862d1"
uuid = "2def613f-5ad1-5310-b15b-b15d46f528f5"
version = "0.4.0+1"

[[Xorg_xcb_util_keysyms_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Xorg_xcb_util_jll"]
git-tree-sha1 = "d1151e2c45a544f32441a567d1690e701ec89b00"
uuid = "975044d2-76e6-5fbe-bf08-97ce7c6574c7"
version = "0.4.0+1"

[[Xorg_xcb_util_renderutil_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Xorg_xcb_util_jll"]
git-tree-sha1 = "dfd7a8f38d4613b6a575253b3174dd991ca6183e"
uuid = "0d47668e-0667-5a69-a72c-f761630bfb7e"
version = "0.3.9+1"

[[Xorg_xcb_util_wm_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Xorg_xcb_util_jll"]
git-tree-sha1 = "e78d10aab01a4a154142c5006ed44fd9e8e31b67"
uuid = "c22f9ab0-d5fe-5066-847c-f4bb1cd4e361"
version = "0.4.1+1"

[[Xorg_xkbcomp_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Xorg_libxkbfile_jll"]
git-tree-sha1 = "4bcbf660f6c2e714f87e960a171b119d06ee163b"
uuid = "35661453-b289-5fab-8a00-3d9160c6a3a4"
version = "1.4.2+4"

[[Xorg_xkeyboard_config_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Xorg_xkbcomp_jll"]
git-tree-sha1 = "5c8424f8a67c3f2209646d4425f3d415fee5931d"
uuid = "33bec58e-1273-512f-9401-5d533626f822"
version = "2.27.0+4"

[[Xorg_xtrans_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "79c31e7844f6ecf779705fbc12146eb190b7d845"
uuid = "c5fb5394-a638-5e4d-96e5-b29de1b5cf10"
version = "1.4.0+3"

[[Zlib_jll]]
deps = ["Libdl"]
uuid = "83775a58-1f1d-513f-b197-d71354ab007a"

[[Zstd_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "cc4bf3fdde8b7e3e9fa0351bdeedba1cf3b7f6e6"
uuid = "3161d3a3-bdf6-5164-811a-617609db77b4"
version = "1.5.0+0"

[[libass_jll]]
deps = ["Artifacts", "Bzip2_jll", "FreeType2_jll", "FriBidi_jll", "HarfBuzz_jll", "JLLWrappers", "Libdl", "Pkg", "Zlib_jll"]
git-tree-sha1 = "5982a94fcba20f02f42ace44b9894ee2b140fe47"
uuid = "0ac62f75-1d6f-5e53-bd7c-93b484bb37c0"
version = "0.15.1+0"

[[libblastrampoline_jll]]
deps = ["Artifacts", "Libdl", "OpenBLAS_jll"]
uuid = "8e850b90-86db-534c-a0d3-1478176c7d93"

[[libfdk_aac_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "daacc84a041563f965be61859a36e17c4e4fcd55"
uuid = "f638f0a6-7fb0-5443-88ba-1cc74229b280"
version = "2.0.2+0"

[[libpng_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Zlib_jll"]
git-tree-sha1 = "94d180a6d2b5e55e447e2d27a29ed04fe79eb30c"
uuid = "b53b4c65-9356-5827-b1ea-8c7a1a84506f"
version = "1.6.38+0"

[[libvorbis_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Ogg_jll", "Pkg"]
git-tree-sha1 = "c45f4e40e7aafe9d086379e5578947ec8b95a8fb"
uuid = "f27f6e37-5d2b-51aa-960f-b287f2bc3b7a"
version = "1.3.7+0"

[[nghttp2_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "8e850ede-7688-5339-a07c-302acd2aaf8d"

[[p7zip_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "3f19e933-33d8-53b3-aaab-bd5110c3b7a0"

[[x264_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "4fea590b89e6ec504593146bf8b988b2c00922b2"
uuid = "1270edf5-f2f9-52d2-97e9-ab00b5d0237a"
version = "2021.5.5+0"

[[x265_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "ee567a171cce03570d77ad3a43e90218e38937a9"
uuid = "dfaa095f-4041-5dcd-9319-2fabd8486b76"
version = "3.5.0+0"

[[xkbcommon_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Wayland_jll", "Wayland_protocols_jll", "Xorg_libxcb_jll", "Xorg_xkeyboard_config_jll"]
git-tree-sha1 = "ece2350174195bb31de1a63bea3a41ae1aa593b6"
uuid = "d8fb68d0-12a3-5cfd-a85a-d49703b185fd"
version = "0.9.1+5"
"""

# ╔═╡ Cell order:
# ╟─46dda2a7-9ca8-4c2e-b00e-e6aa95761f65
# ╠═61f4e85b-3b4e-4906-85a6-fd4a08056532
# ╠═898608b9-09c8-4e6d-a0ee-29454d5ce0c9
# ╠═5ce3d306-5a65-4f07-86eb-c417d1959d4e
# ╠═7a6a3ee1-4fc8-4b1f-ba8a-5312fb2b4a1a
# ╟─aefce721-b5f9-4008-963a-898ac77aec66
# ╠═b75a1f3b-e5f6-4cb5-b2a9-550e07976f44
# ╠═0c5978ca-6b44-4bf0-b6e7-467bfa98ec60
# ╠═a07150dd-1230-425e-b773-76cc0b03ba58
# ╠═a25e6d0f-eedd-40e9-ba76-984b72b19563
# ╟─d0138354-2ce6-4d8c-b222-992f5df51b57
# ╠═517389c5-8ea6-4406-9726-06d6438d9ceb
# ╠═2f1ab012-2b32-4cff-a6e7-1154a07a890a
# ╠═e166637c-5160-41bc-b4d0-924d3da59985
# ╠═863dca2b-d32a-4cb1-8ba2-bb051bcd0b6b
# ╠═c2ea899f-29cf-44d2-b5f0-108b1b9bbb4c
# ╠═b551098c-c1c3-45c0-9129-99f555acbf4f
# ╠═34c99ebe-8429-401a-a101-c18d70ac2543
# ╠═4dc98adb-729d-4458-a6cc-9ee2bc2b73e6
# ╠═dea64f1e-e74f-452e-9b04-2e8de9e0199c
# ╠═5d492cf3-a5e9-4f17-8dee-ed14393ef003
# ╠═d6a00833-a98e-4f58-bb72-d12f5c0bf7d3
# ╠═ead74c7d-b9cf-40fc-b2d8-9f6357599d63
# ╟─5f608c76-a381-468a-824b-5be3f1a1c009
# ╠═513288b8-a444-44f1-9e64-1eeff0b0da6c
# ╠═dfdcfa71-2876-4e3f-be1a-27730fcfc47c
# ╠═4216696f-8958-4691-9f77-c9b77998ea55
# ╠═b6ee3a1c-790a-46d6-877f-6fd17a52c0f9
# ╠═b0869bb2-25a7-437a-900a-17ccef7162ba
# ╠═952ad83c-39ae-4f2c-beed-6124b1b8f3ac
# ╠═6328f2b3-71eb-4a14-8868-36dee395b562
# ╠═7a0d472a-705c-4699-8097-86894476c147
# ╠═dbf104bd-4baf-4319-8aab-d9c1cffc1369
# ╠═4025b58f-f538-4492-8ce7-0b1ce5a41095
# ╠═18b42581-4df0-48ef-b2b2-c74332421b05
# ╠═50a8c6d5-e45e-4393-8cb7-88235603908a
# ╠═88a871b9-4533-49f0-ab10-2544463c11f8
# ╠═b6ca58f3-a916-4c80-b188-58fd07db8461
# ╠═16f10984-4df6-4c92-925e-a220aae156a3
# ╠═39e29cf4-e8ba-4350-b382-d9ee32e3963e
# ╠═202161a1-8f80-4d74-aeda-8433fbfe7378
# ╠═2f205787-8ea0-419b-8972-a6aed1557e63
# ╠═7dcb06d9-8e27-4182-97b2-77eecf851e6e
# ╠═56a1ed22-f069-4926-abd9-1ef4c7e4e0ae
# ╟─545c10ff-fa96-4807-80a8-1b065fb5e244
# ╠═68f9cc5b-d55b-42e7-a31f-b85b8ebc3820
# ╟─5020f47a-61f9-4622-82a6-80bbd5e1c833
# ╠═d1a60c40-d46f-4ac9-9293-cd09946e4209
# ╠═eb3dd186-3741-42ee-9515-ace5b7126a86
# ╟─47bde8a6-2106-4baa-b472-8636e035baef
# ╠═8006c599-9451-4d85-b18e-adf2cd4eca56
# ╠═36e2b2e0-c5f4-4d9c-92f9-779828c44e2f
# ╠═edf27dee-85ea-42e6-a489-a8fea95d3075
# ╠═9159265a-1858-42c8-8ab0-e48c3cfa86eb
# ╠═78eb0b10-c309-4da4-8bfc-75d1642b90f7
# ╠═b34d564a-7c80-4fa9-aa6d-14d30887d894
# ╠═97bc0901-86ba-4f1d-b217-f25069977395
# ╠═da289e3e-283b-41b3-82cc-8342b7417cf4
# ╠═111d9973-91bb-481c-b33e-067cfe11d0e1
# ╠═8a686e69-6b69-4e68-8b79-23140b5ee4dd
# ╠═1d3b118a-a8b0-4dad-aecb-a75e9ea3161b
# ╠═e2892425-ca03-4823-89be-8ebc4439206b
# ╠═877eca8b-9e16-4a7d-ba56-8d44569ffaa3
# ╠═209f25c0-aecd-4983-888e-ddaa4b513e0e
# ╟─00000000-0000-0000-0000-000000000001
# ╟─00000000-0000-0000-0000-000000000002


"""
ARD Transform
```
    v = rand(3)
    tr = ARDTransform(v)
```
Multiply every vector of observation by `v` element-wise
"""
struct ARDTransform{T,N} <: Transform
    v::Vector{T}
end

function ARDTransform(s::T,dims::Integer) where {T<:Real}
    @check_args(ARDTransform, s, s > zero(T), "s > 0")
    ARDTransform{T,dims}(fill(s,dims))
end

function ARDTransform(v::AbstractVector{T}) where {T<:Real}
    @check_args(ARDTransform, v, all(v.>zero(T)), "v > 0")
    ARDTransform{T,length(v)}(v)
end

function set!(t::ARDTransform{T},ρ::AbstractVector{T}) where {T<:Real}
    @assert length(ρ) == dim(t) "Trying to set a vector of size $(length(ρ)) to ARDTransform of dimension $(dim(t))"
    t.v .= ρ
end

dim(t::ARDTransform) = length(t.v)

function apply(t::ARDTransform,X::AbstractMatrix{<:Real};obsdim::Int = defaultobs)
    @boundscheck if dim(t) != size(X,feature_dim(obsdim))
        throw(DimensionMismatch("Array has size $(size(X,!Bool(obsdim-1)+1)) on dimension $(!Bool(obsdim-1)+1)) which does not match the length of the scale transform length , $(dim(t)).")) #TODO Add test
    end
    _transform(t,X,obsdim)
end
apply(t::ARDTransform,x::AbstractVector{<:Real};obsdim::Int=defaultobs) = t.v .* x
_transform(t::ARDTransform,X::AbstractMatrix{<:Real},obsdim::Int=defaultobs) = obsdim == 1 ? t.v'.*X : t.v .* X

Base.isequal(t::ARDTransform,t2::ARDTransform) = isequal(t.v,t2.v)


"""
    UniformContexts(limits::Matrix{Float64})

Construct a callable object to generate uniform contexts.
"""
struct UniformContexts <: AbstractContextSampler
    limits::Matrix{Float64}

    function UniformContexts(limits::Matrix{Float64})
        check_limits(limits)
        return new(limits)
    end
end

function (sampler::UniformContexts)(n::Int64)
    d = size(sampler.limits, 1)
    X = zeros(d, n)
    for i in 1:d
        X[i, :] = rand(Uniform(sampler.limits[i, 1], sampler.limits[i, 2]), n)
    end
    return X
end


"""
    classification_report(i, yhat, y)

    Returns multiple classification metrics based on predictions yhat and
    ground truth values y, for run i.
 """
function classification_report(i, yhat, y)
    P′ = [pdf(yhat[j], 1.0) for j ∈ 1:size(yhat)[1]]
    yₚ = [p >= 0.5 for p ∈ P′]
    out = DataFrame(
        Run = i,
        TN  = true_negative(yₚ, y),
        FP  = false_positive(yₚ, y),
        FN  = false_negative(yₚ, y),
        TP  = true_positive(yₚ, y),
        Sensitivity = sensitivity(yₚ, y),
        Specificity = specificity(yₚ, y),
        PPV = ppv(yₚ, y),
        NPV = npv(yₚ, y),
        Accuracy = accuracy(yₚ, y),
        AUC = auc(yhat, y),
        MCC = mcc(yₚ, y),
        F1  = f1score(yₚ, y),
        Brier = mean((yₚ .- float(y)).^2)
    )
    return out
end

"""
    classification_predictions(fold, train_sites, test_sites, ytrue, ypred)

    Returns a table with individual level predictions
"""
function classification_predictions(fold, train_site, test_sites, ytrue, ypred)
    return DataFrame(
        fold = fold,
        train_site = train_site,
        test_site = vec(Matrix(test_sites)),
        ytrue = int(ytrue, type=Int64) .- 1,
        ypred = map(y->pdf(y, 1.0), ypred))
end

"""
    compute_reliability_curve(predictions, n_bins)

"""
function compute_reliability_curve(
    predictions::DataFrame,
    n_bins::Int64=10)

    sort!(predictions, :ypred)
    N = size(predictions)[1]
    increment = convert(Int, ceil(N/n_bins))
    indices = collect(1:increment:N)
    push!(indices, N)
    fₚ = []; fₒ = []
    for i ∈ 2:length(indices)
        pred_i = predictions[indices[i-1]:indices[i],:]
        push!(fₚ, pred_i[:,:ypred] |> mean)
        push!(fₒ, pred_i[:,:ytrue] |> mean)
    end

    return fₚ, fₒ
end

"""
    plot_reliability_curves(predictions; color, alpha, title, xlabel, ylabel, figsize)
"""
function plot_reliability_curves(
    predictions::DataFrame;
    color::Symbol=:black,
    alpha::Float64=1.0,
    title::Union{String, LaTeXString}="Reliability",
    xlabel::Union{String, LaTeXString}=L"f_p",
    ylabel::Union{String, LaTeXString}=L"f_o",
    figsize::Tuple=(250,250))
    fig = plot([0,1], [0,1], c=:black,
        ls=:dash, label=nothing, size=figsize,
        title=title, xlabel=xlabel, ylabel=ylabel)
    for i ∈ unique(predictions.fold)
        fₚ, fₒ = compute_reliability_curve(predictions[predictions.fold .== i,:])
        fig = plot!(fₚ, fₒ, c=color, alpha=alpha, label=nothing)
    end
    return fig
end

"""
    plot_roc_curves(roc_curves; color, alpha, title, xlabel, ylabel, figsize)

    Plots ROC curves.
"""
function plot_roc_curves(
    roc_curves::Vector;
    color::Symbol=:black,
    alpha::Float64=0.5,
    title::Union{String, LaTeXString}="ROC Curve",
    xlabel::Union{String, LaTeXString}=L"FPR",
    ylabel::Union{String, LaTeXString}=L"TPR",
    figsize::Tuple{Int64,Int64}=(250,250))

    fig = plot([0,1], [0,1], c=color, ls=:dash,
                label=nothing, title=title,
                ylabel=ylabel, xlabel=xlabel, size=figsize)
    K = size(roc_curves)[1]
    for i ∈ 1:K
        plot!(roc_curves[i][1], roc_curves[i][2],
        c=color, alpha=alpha, label=nothing)
    end
    return fig
end

"""
    store_classification_predictions(df, fold, train_sites, test_sites, ytrue, ypred)

    Returns a table with individual level predictions
"""
function store_classification_predictions(df, fold, train_site, test_sites, ytrue, ypred)
    return [df; classification_predictions(fold, train_site, test_sites, ytrue, ypred)]
end

"""
    store_classification_results(df, fold, yhat, ytrue)

    This function takes the results dataframe and adds to it a row for the
    classification results in the current fold.
"""
function store_classification_results(df, fold, yhat, ytrue)
    return [df; classification_report(fold, yhat, coerce(ytrue, OrderedFactor))]
end

"""
    summarize_classification_results(results, sigdigits)

    Returns a formatted version of the results with a column showing mean and 95%ci
    rounded to `sigdigits`.
"""
function summarize_classification_results(results::DataFrame, sigdigits::Int64=4)
    summary_res = describe(results, :mean, :std)
    summary_res[:, :mean] = round.(summary_res[:, :mean]; sigdigits=sigdigits)
    summary_res[:, :se] = round.(summary_res[:, :std] ./ sqrt(K); sigdigits=sigdigits)
    summary_res[:, :lci] = round.(summary_res[:, :mean] .+ quantile(Normal(), 0.025).*summary_res[:, :se]; sigdigits=sigdigits)
    summary_res[:, :uci] = round.(summary_res[:, :mean] .+ quantile(Normal(), 1-0.025).*summary_res[:, :se]; sigdigits=sigdigits)
    summary_res[:, :Result] = map((x,y,z) -> "$x ($y, $z)", summary_res.mean, summary_res.lci, summary_res.uci)
    return summary_res
end


# Test reduction

using NumericFuns
using NumericExtensions
using Base.Test

import NumericExtensions: _Max, _Min, NonnegMax

### safe (but slow) reduction functions for result verification

import NumericExtensions: safe_sum, safe_max, safe_min

function rsize(x::Array, dim::Int)
    if 1 <= dim <= ndims(x)
        siz = size(x)
        rsiz_v = [siz...]
        rsiz_v[dim] = 1
        tuple(rsiz_v...)
    else
        size(x)
    end
end

### full reduction ###

x = randn(3, 4)

# foldl & foldr

@test foldl(Subtract(), 10, [4, 7, 9]) === (10 - 4 - 7 - 9)
@test foldr(Subtract(), 10, [4, 7, 9]) === 4 - (7 - (9 - 10))

@test foldl(Subtract(), [4, 7, 9]) === (4 - 7 - 9)
@test foldr(Subtract(), [4, 7, 9]) === (4 - (7 - 9))

# reduce

@test reduce(Add(), Int[]) == 0
@test reduce(Add(), [1, 2, 3]) == 6
@test reduce(_Max(), Int[]) == typemin(Int)
@test reduce(_Max(), [1, 2, 3]) == 3
@test reduce(_Min(), Int[]) == typemax(Int)
@test reduce(_Min(), [1, 2, 3]) == 1
@test reduce(NonnegMax(), Int[]) == 0
@test reduce(NonnegMax(), [1, 2, 3]) == 3

@test mapreduce(Abs2Fun(), Add(), Int[]) == 0
@test mapreduce(Abs2Fun(), Add(), [1, 2, 3]) == 14

@test mapreduce(Multiply(), Add(), Int[], Int[]) == 0
@test mapreduce(Multiply(), Add(), [1, 2, 3], [2, 3, 4]) == 20
@test mapreduce(Multiply(), Add(), [1, 2, 3], 2) == 12
@test mapreduce(Multiply(), Add(), 2, [1, 2, 3]) == 12

@test mapreduce(FMA(), Add(), [1, 2, 3], [4, 5, 6], [7, 8, 9]) == 128
@test mapreduce(FMA(), Add(), 4, [4, 5, 6], [7, 8, 9]) == 134
@test mapreduce(FMA(), Add(), [1, 2, 3], 2, [7, 8, 9]) == 54
@test mapreduce(FMA(), Add(), [1, 2, 3], [4, 5, 6], 2) == 36

# sum

@test sum(Bool[]) === 0
@test sum([false]) === 0
@test sum([true]) === 1
@test sum([true, false, true]) === 2

@test sum(Int[]) === 0
@test sum([5]) === 5
@test sum([2, 3]) === 5
@test sum([2, 3, 4]) === 9
@test sum([2, 3, 4, 5]) === 14
@test sum([2, 3, 4, 5, 6, 7]) === 27

@test_approx_eq sum(x) safe_sum(x)

# mean

@test isnan(mean(Int[]))
@test isnan(mean(Float64[]))
@test mean([1, 2]) == 1.5

@test_approx_eq mean(x) safe_sum(x) / length(x)

# maximum & minimum

@test_throws VERSION < v"0.4-"? ErrorException : ArgumentError maximum(Int[])
@test_throws VERSION < v"0.4-"? ErrorException : ArgumentError minimum(Int[])

@test maximum([4, 5, 2, 3]) === 5
@test minimum([4, 5, 2, 3]) === 2

@test maximum([NaN, 3.0, NaN, 2.0, NaN]) === 3.0
@test minimum([NaN, 3.0, NaN, 2.0, NaN]) === 2.0

@test maximum(x) == safe_max(x)
@test minimum(x) == safe_min(x)

# small sample testing (for coverage)

@test sumsq([2]) == 4
@test sumsq([2,3]) == 13
@test sumsq([2,3,4]) == 29
@test sumsq([2,3,4,5]) == 54
@test sumsq([2,3,4,5,6]) == 90

@test meansq([2]) == 4
@test meansq([2,3]) == 13 / 2
@test meansq([2,3,4]) == 29 / 3
@test meansq([2,3,4,5]) == 54 / 4
@test meansq([2,3,4,5,6]) == 90 / 5

@test NumericExtensions.maxabs([3, -5, 4, -2]) == 5
@test NumericExtensions.minabs([3, -5, 4, -2]) == 2

@test dot([1, 2, 3], [4, 5, 6]) === 32

# large random sample testing

x = randn(3, 4)
y = randn(3, 4)
z = randn(3, 4)
p = rand(3, 4)
q = rand(3, 4)

@test_approx_eq NumericExtensions.sumabs(x) sum(abs(x))
@test_approx_eq NumericExtensions.maxabs(x) maximum(abs(x))
@test_approx_eq NumericExtensions.minabs(x) minimum(abs(x))
@test_approx_eq meanabs(x) mean(abs(x))

@test_approx_eq sumsq(x) sum(abs2(x))
@test_approx_eq meansq(x) mean(abs2(x))

@test_approx_eq dot(x, y) sum(x .* y)

@test_approx_eq sumabsdiff(x, y)  sum(abs(x - y))
@test_approx_eq maxabsdiff(x, y)  maximum(abs(x - y))
@test_approx_eq minabsdiff(x, y)  minimum(abs(x - y))
@test_approx_eq meanabsdiff(x, y) mean(abs(x - y))

@test_approx_eq sumsqdiff(x, y)  sum(abs2(x - y))
@test_approx_eq meansqdiff(x, y) mean(abs2(x - y))

@test_approx_eq sumabsdiff(x, 1.5)  sum(abs(x .- 1.5))
@test_approx_eq maxabsdiff(x, 1.5)  maximum(abs(x .- 1.5))
@test_approx_eq minabsdiff(x, 1.5)  minimum(abs(x .- 1.5))
@test_approx_eq meanabsdiff(x, 1.5) mean(abs(x .- 1.5))

@test_approx_eq sumsqdiff(x, 1.5) sum(abs2(x .- 1.5))
@test_approx_eq meansqdiff(x, 1.5) mean(abs2(x .- 1.5))

@test_approx_eq sumabsdiff(1.5, x)  sum(abs(x .- 1.5))
@test_approx_eq maxabsdiff(1.5, x)  maximum(abs(x .- 1.5))
@test_approx_eq minabsdiff(1.5, x)  minimum(abs(x .- 1.5))
@test_approx_eq meanabsdiff(1.5, x) mean(abs(x .- 1.5))

@test_approx_eq sumsqdiff(1.5, x)  sum(abs2(x .- 1.5))
@test_approx_eq meansqdiff(1.5, x) mean(abs2(x .- 1.5))

@test_approx_eq sumxlogx(p) sum(p .* log(p))
@test_approx_eq sumxlogy(p, q) sum(p .* log(q))
@test_approx_eq entropy(p) -sum(p .* log(p))

# generic & ternary

@test_approx_eq sum(FMA(), x, y, z) sum(x .+ y .* z)
@test_approx_eq sum(FMA(), x, y, 2.) sum(x .+ y .* 2)
@test_approx_eq sum(FMA(), x, 2., y) sum(x .+ 2 .* y)
@test_approx_eq sum(FMA(), 2., x, y) sum(2. .+ x .* y)
@test_approx_eq sum(FMA(), x, 2., 3.) sum(x .+ 6.)
@test_approx_eq sum(FMA(), 2., x, 3.) sum(2. .+ x * 3.)
@test_approx_eq sum(FMA(), 2., 3., x) sum(2. .+ 3. * x)

@test_approx_eq mean(FMA(), x, y, z) mean(x .+ y .* z)
@test_approx_eq mean(FMA(), x, y, 2.) mean(x .+ y .* 2)
@test_approx_eq mean(FMA(), x, 2., y) mean(x .+ 2 .* y)
@test_approx_eq mean(FMA(), 2., x, y) mean(2. .+ x .* y)
@test_approx_eq mean(FMA(), x, 2., 3.) mean(x .+ 6.)
@test_approx_eq mean(FMA(), 2., x, 3.) mean(2. .+ x * 3.)
@test_approx_eq mean(FMA(), 2., 3., x) mean(2. .+ 3. * x)

@test_approx_eq maximum(FMA(), x, y, z) maximum(x .+ y .* z)
@test_approx_eq maximum(FMA(), x, y, 2.) maximum(x .+ y .* 2)
@test_approx_eq maximum(FMA(), x, 2., y) maximum(x .+ 2 .* y)
@test_approx_eq maximum(FMA(), 2., x, y) maximum(2. .+ x .* y)
@test_approx_eq maximum(FMA(), x, 2., 3.) maximum(x .+ 6.)
@test_approx_eq maximum(FMA(), 2., x, 3.) maximum(2. .+ x * 3.)
@test_approx_eq maximum(FMA(), 2., 3., x) maximum(2. .+ 3. * x)

@test_approx_eq minimum(FMA(), x, y, z) minimum(x .+ y .* z)
@test_approx_eq minimum(FMA(), x, y, 2.) minimum(x .+ y .* 2)
@test_approx_eq minimum(FMA(), x, 2., y) minimum(x .+ 2 .* y)
@test_approx_eq minimum(FMA(), 2., x, y) minimum(2. .+ x .* y)
@test_approx_eq minimum(FMA(), x, 2., 3.) minimum(x .+ 6.)
@test_approx_eq minimum(FMA(), 2., x, 3.) minimum(2. .+ x .* 3.)
@test_approx_eq minimum(FMA(), 2., 3., x) minimum(2. .+ 3. .* x)

# folding

@test_approx_eq foldl(Subtract(), 10, Abs2Fun(), [1,2,3,4]) (-20)
@test_approx_eq foldr(Subtract(), 10, Abs2Fun(), [1,2,3,4]) 0

@test_approx_eq foldl(Subtract(), 10, Multiply(), [1,2,3,4], [1,2,3,4]) (-20)
@test_approx_eq foldr(Subtract(), 10, Multiply(), [1,2,3,4], [1,2,3,4]) 0

@test_approx_eq foldl_fdiff(Subtract(), 10, Abs2Fun(), [1,2,3,4], zeros(Int,4)) (-20)
@test_approx_eq foldr_fdiff(Subtract(), 10, Abs2Fun(), [1,2,3,4], zeros(Int,4)) 0

@test_approx_eq foldl(Subtract(), Abs2Fun(), [1,2,3,4]) (-28)
@test_approx_eq foldr(Subtract(), Abs2Fun(), [1,2,3,4]) (-10)

@test_approx_eq foldl(Subtract(), Multiply(), [1,2,3,4], [1,2,3,4]) (-28)
@test_approx_eq foldr(Subtract(), Multiply(), [1,2,3,4], [1,2,3,4]) (-10)

@test_approx_eq foldl_fdiff(Subtract(), Abs2Fun(), [1,2,3,4], zeros(Int,4)) (-28)
@test_approx_eq foldr_fdiff(Subtract(), Abs2Fun(), [1,2,3,4], zeros(Int,4)) (-10)


using OrdinaryDiffEq, ParameterizedFunctions, Plots, DiffEqDevTools, Sundials, LSODA
gr() #gr(fmt=:png)
f = @ode_def Orego begin
  dy1 = p1*(y2+y1*(1-p2*y1-y2))
  dy2 = (y3-(1+y1)*y2)/p1
  dy3 = p3*(y1-y3)
end p1 p2 p3

p = [77.27,8.375e-6,0.161]
prob = ODEProblem(f,[1.0,2.0,3.0],(0.0,30.0),p)
sol = solve(prob,Rodas5(),abstol=1/10^14,reltol=1/10^14)
test_sol = TestSolution(sol)

# group 1
setups = [Dict(:alg=>Rosenbrock23()),
          Dict(:alg=>TRBDF2()),
          Dict(:alg=>CVODE_BDF()),
          Dict(:alg=>lsoda())]

# High Tolerance
abstols = 1 ./ 10 .^ (5:8)
reltols = 1 ./ 10 .^ (1:4);

wp = WorkPrecisionSet(prob,abstols,reltols,setups;dense = false,verbose=false,
                      appxsol=test_sol,maxiters=Int(1e5),error_estimate=:l2)
DIAGRAMS["Orego-HighTol-Group1"] = plot(wp)

# group 2
setups = [Dict(:alg=>Rodas4()),
          Dict(:alg=>Rodas5()),
          Dict(:alg=>Rodas5P()),
          Dict(:alg=>KenCarp4()),
          Dict(:alg=>RadauIIA5()),
          Dict(:alg=>CVODE_BDF()),
          Dict(:alg=>lsoda())]

# Low Tolerance
abstols = 1 ./ 10 .^ (7:13)
reltols = 1 ./ 10 .^ (4:10)

wp = WorkPrecisionSet(prob,abstols,reltols,setups;verbose=false,
                      dense=false,appxsol=test_sol,maxiters=Int(1e5),error_estimate=:l2)
DIAGRAMS["Orego-LowTol-Group2"] = plot(wp)


module ParetoSets

export dominates, naive_pareto
export ParetoSet, pareto_ids, pareto_ys, dominated_ids, dominated_ys, all_ys, ispareto

"""
    dominates(y, y´)

Returns true if y dominates y´, false otherwise.
"""
dominates(y::Vector{Float64}, y´::Vector{Float64}) = all(y .≤ y´) && any(y .< y´)

"""
    naive_pareto{T}(ys::AbstractVector{Vector{T}})

Returns the pareto set of a collection of points ys as a vector of indices into ys.  Uses naive O(n^2) algorithm.
"""
function naive_pareto(ys::AbstractVector{Vector{Float64}})
    pareto_ids = Int[]
    for (i,y) in enumerate(ys)
        if !any(dominates(y´,y) for y´ in ys)
            push!(pareto_ids, i)
        end
    end
    return pareto_ids
end

mutable struct ParetoSet
    ys::Vector{Vector{Float64}}  #collection of all points
    pareto_ids::Vector{Int}  #vector of indices into full containing pareto set
end
ParetoSet() = ParetoSet(Vector{Float64}[], Int[])

function Base.push!(p::ParetoSet, y::Vector{Float64})
    push!(p.ys, y)
    yi = length(p.ys)
    ispareto(p, y) && push!(p.pareto_ids, yi)
    ids = find(y´->dominates(y,y´), pareto_ys(p))
    deleteat!(p.pareto_ids, ids)
end

pareto_ids(p::ParetoSet) = p.pareto_ids
dominated_ids(p::ParetoSet) = setdiff(1:length(p.ys), p.pareto_ids)
pareto_ys(p::ParetoSet) = view(p.ys, p.pareto_ids)
dominated_ys(p::ParetoSet) = view(p.ys, dominated_ids(p))
all_ys(p::ParetoSet) = p.ys

Base.getindex(p::ParetoSet, i) = p.ys[i]
function Base.append!(ps::ParetoSet, ys)
    for y in ys
        push!(ps, y)
    end
end

ispareto(p::ParetoSet, y::Vector{Float64}) = !any(dominates(y´, y) for y´ in pareto_ys(p))

end # module


# test_GLS.jl

# Write tests corresponding to GLS here.

using PDESolver
using AdvectionEquationMod
using Input
using Base.Test
using ODLCommonTools
using ArrayViews

fname = "input_vals_GLS.jl"
opts = read_input(fname)
@testset "--- Check input arguments applied correctly ---" begin
  @test ( opts["use_GLS"] )== true
  @test ( opts["Q_transpose"] )== true
end

mesh, sbp, eqn, opts = solvePDE(fname)

@testset "--- Check functions in ../src/solver/advection/GLS.jl ---" begin

  @testset "Checking shape function derivatives" begin
    sbp2 = getTriSBPGamma{Float64}()
    shapefuncderiv = zeros(Tsol, sbp2.numnodes, sbp2.numnodes, Tdim)
    calcShapefuncDeriv(sbp2, shapefuncderiv)
    @test isapprox(shapefuncderiv[:,:,1] -  [-0.5  0.5  0.0
                                             -0.5  0.5  0.0
                                             -0.5  0.5  0.0]) atol=1e-13
    @test isapprox(shapefuncderiv[:,:,2] -  [-0.5  0.0  0.5
                                             -0.5  0.0  0.5
                                             -0.5  0.0  0.5]) atol=1e-13
  end

  @testset "Check calcAxidxi" begin
    Tdim = 2
    alpha_x = ones(Tsol, mesh.numNodesPerElement)
    alpha_y = ones(alpha_x)
    dxidx = sview(mesh.dxidx,:,:,:,1)
    shapefuncderiv = zeros(Tsol, sbp.numnodes, sbp.numnodes, Tdim)
    calcShapefuncDeriv(sbp, shapefuncderiv)
    AxiDxi = calcAxiDxi(mesh, dxidx, alpha_x, alpha_y, shapefuncderiv)
    @test isapprox(AxiDxi -[-0.5 0.25 0.25
                            -0.5 0.25 0.25
                            -0.5 0.25 0.25]) atol=1e-13
  end

  @testset "Check calcTau" begin
    fill!(eqn.params.alpha_x, 1.0)
    fill!(eqn.params.alpha_y, 1.0)
    tau = zeros(Tsol,mesh.numNodesPerElement, mesh.numEl)
    calcTau(mesh, sbp, eqn, tau)
    @test ( mesh.numEl )== 1
    @test isapprox( tau[:,1], [1.0,1.0,1.0])
  end

  @testset "Check GLS" begin
    fill!(eqn.params.alpha_x, 1.0)
    fill!(eqn.params.alpha_y, 1.0)
    fill!(eqn.res, 0.0)
    eqn.q[1,:,1] = eqn.q_vec
    GLS(mesh, sbp, eqn)
    @test isapprox( eqn.res[1,:,1], [8.0 -4.0 -4.0])

  end

end  # end facts


module GameOfLife

import REPL
using REPL.TerminalMenus
export Gol, game, game2, glidergun

mutable struct Gol
    state::Matrix{Int8}
    isperiodic::Bool
    generations::Int
end

function Gol(n::Int,
             m::Int = n;
             isperiodic::Bool = true,
             generations::Int = 100)
    state = rand(Int8[0,1], n, m)

    return Gol(state, isperiodic, generations)
end

"count the number of live neighbors"
function determine_neighbor_state(d::Gol, i::Integer, j::Integer)
    n, m = size(d.state)
    neighbor = [(p,q) for p in -1:1 for q in -1:1 if !(p == 0 && q == 0)]
    total = Int8(0)
    if d.isperiodic # periodic
        for idx in 1:8
            total += d.state[mod1(i + neighbor[idx][1], n), mod1(j + neighbor[idx][2], m)]
        end
    else # non-periodic
        for idx in 1:8
            if !(i + neighbor[idx][1] <= 0 || j + neighbor[idx][2] <= 0 || i + neighbor[idx][1] > n || j + neighbor[idx][2] > m)
                total += d.state[i + neighbor[idx][1], j + neighbor[idx][2]]
            end
        end
    end

    return total
end

"update cells"
function next_generation(d::Gol)
    n, m = size(d.state)
    next_state = zeros(Int8, n, m)
    for col in 1:m
        for row in 1:n
            if d.state[row, col] == one(d.state[row, col]) # live
                if determine_neighbor_state(d, row, col) in Int8[2, 3]
                    next_state[row, col] = Int8(1)
                end
            else # dead
                if determine_neighbor_state(d, row, col) == Int8(3)
                    next_state[row, col] = Int8(1)
                end
            end
        end
    end
    return next_state
end

"show state"
function Base.show(d::Gol)
    n, m = size(d.state)
    for i in 1:n
        for j in 1:m
            if d.state[i,j] == zero(d.state[i,j])
                print(" ")
            else
                printstyled("X", color=:light_blue, bold=true)
            end
        end
        println()
    end
end

"""
    game(d::Gol; tstep=0.1)
simulation start
"""
function game(d::Gol; tstep=0.1)
    sleep(0.1)
    show(d);
    n = length(string(d.generations))
    for iter in 1:d.generations
       d.state = next_generation(d)
       sleep(tstep)
       run(`clear`)
       show(d)
       print("\nGeneration: ", lpad(iter, n))
       sum(d.state) == 0 && break
    end
end

"""
    game(;t=0.1, gen=100)
"""
function game(;t=0.1, gen=100)
    row, col = displaysize(stdout)
    d = Gol(row, col, isperiodic=true, generations=gen)
    game(d, tstep=t)
end

"""
    glidergun(n::Integer = 20, m::Integer = 36; generations=250)
glider gun sample
"""
function glidergun(n::Integer = 20, m::Integer = 36; generations=250)
    d = Gol(n, m, isperiodic=false, generations=generations)
    d.state = zeros(Int8, n, m)
    gliderpos = Tuple{Int8,Int8}[(5,1),(6,1),(5,2),(6,2),(5,11),(6,11),(7,11),(4,12),(8,12),(3,13),(9,13),(3,14),(9,14),(6,15),(4,16),(8,16),(5,17),(6,17),(7,17),(6,18),(3,21),(4,21),(5,21),(3,22),(4,22),(5,22),(2,23),(6,23),(1,25),(2,25),(6,25),(7,25),(3,35),(4,35),(3,36),(4,36)]
    for pos in gliderpos
        d.state[pos...] = Int8(1)
    end
    return d
end

boardsize = ["(20, 20)", "(20, 40)", "(30, 30)", "(30, 40)", "(40, 40)", "(40, 80)", "(100, 200)"]
boardsize_dict = Dict(zip(1:length(boardsize), eval.(Meta.parse.(boardsize))))
menu = RadioMenu(boardsize, pagesize=4)
function game2()
    choice = request("Choose your favorite field size (row, column):", menu)
    col, row = boardsize_dict[choice]
    println()
    periodic_condition = request("Choose boundary condition:",
                                RadioMenu(["Periodic", "Non periodic"],
                                pagesize=2))
    condition = ifelse(isone(periodic_condition), true, false)

    print("\nThe number of generations: ")
    num_generation = Meta.parse(readline())
    @assert isinteger(num_generation)

    game(Gol(col, row, isperiodic=condition, generations=num_generation))
    return nothing
end

end # module


# Assumes the space dimension and field dimension are the same.  In other words, when the
# space coordinate indices are (i,j,k), then the field has three vector components.
# Therefore, for the output field array G[i,j,k,w], we assume w = 1:3.
export create_grad

# Wrapper to create the discrete gradient by default
#
# Note that when the positional arguments are SVector's, this function may call the concrete
# implementation directly instead of the next wrapper function taking ∆l⁻¹ even if the
# keyword arguments are not SVector's, because methods are dispatched based only on the
# types of positional arguments.  Therefore, we convert the keyword arguments into SVector's.
create_grad(isfwd::AbsVecBool,  # isfwd[w] = true|false: ∂w is forward|backward difference
            N::AbsVecInteger,  # size of grid
            ∆l⁻¹::Tuple{Vararg{Number}}=ntuple(x->1.0,length(isfwd)),  # ∆l⁻¹[w]: inverse of uniform distance between grid planes in w-direction
            isbloch::AbsVecBool=fill(true,length(isfwd)),  # boundary conditions in x, y, z
            e⁻ⁱᵏᴸ::AbsVecNumber=ones(length(isfwd));  # Bloch phase factor in x, y, z
            order_cmpfirst::Bool=true) =  # true to use Cartesian-component-major ordering for more tightly banded matrix
    (K = length(isfwd); create_grad(isfwd, fill.(∆l⁻¹,(N...,)), isbloch, e⁻ⁱᵏᴸ; order_cmpfirst))

# Wrapper to convert AbstractVector's to SVec's
create_grad(isfwd::AbsVecBool,  # isfwd[w] = true|false: ∂w is forward|backward difference
            ∆l⁻¹::NTuple{K,AbsVecNumber},  # ∆l⁻¹[w]: inverse of distances between grid planes in w-direction
            isbloch::AbsVecBool=fill(true,K),  # boundary conditions in x, y, z
            e⁻ⁱᵏᴸ::AbsVecNumber=ones(K);  # Bloch phase factor in x, y, z
            order_cmpfirst::Bool=true  # true to use Cartesian-component-major ordering for more tightly banded matrix
            ) where {K} =
    # I should not cast e⁻ⁱᵏᴸ into a complex vector, because then the entire curl matrix
    # becomes a complex matrix.  Sometimes I want to keep it real (e.g., when no PML and
    # Bloch phase factors are used).  In fact, this is the reason why I accept e⁻ⁱᵏᴸ instead
    # of constructing it from k and L as exp.(-im .* k .* L), which is always complex even
    # if k = 0.
    #
    # I should not cast ∆l⁻¹ to a vector of any specific type (e.g., Float, ComplexF), either,
    # because sometimes I would want to even create an integral curl operator.
    create_grad(SBool{K}(isfwd), ∆l⁻¹, SBool{K}(isbloch), SVec{K}(e⁻ⁱᵏᴸ); order_cmpfirst)

function create_grad(isfwd::SBool{K},  # isfwd[w] = true|false: ∂w is forward|backward difference
                     ∆l⁻¹::NTuple{K,AbsVecNumber},  # ∆l⁻¹[w]: inverse of distances between grid planes in w-direction
                     isbloch::SBool{K},  # boundary conditions in K dimensions
                     e⁻ⁱᵏᴸ::SNumber{K};  # Bloch phase factors in K dimensions
                     order_cmpfirst::Bool=true  # true to use Cartesian-component-major ordering for more tightly banded matrix
                     ) where {K}
    T = promote_type(eltype.(∆l⁻¹)..., eltype(e⁻ⁱᵏᴸ))  # eltype(eltype(∆l⁻¹)) can be Any if ∆l⁻¹ is inhomogeneous
    N = SVec(length.(∆l⁻¹))
    M = prod(N)
    KM = K * M

    # Below, create_∂info() is called K times in the double for loops, and each call returns
    # I, J, V of length 2M, so we preallocate Itot, Jtot, Vtot of length K×2M = 2KM.
    Itot = VecInt(undef, 2KM)
    Jtot = VecInt(undef, 2KM)
    Vtot = Vector{T}(undef, 2KM)

    for nw = 1:K  # direction of differentiation
        I, J, V = create_∂info(nw, isfwd[nw], N, ∆l⁻¹[nw], isbloch[nw], e⁻ⁱᵏᴸ[nw])

        nv = nw  # component of output field generated by ∂w (row index of matrix block)
        istr, ioff = order_cmpfirst ? (K, nv-K) : (1, M*(nv-1))  # (row stride, row offset)
        @. I = istr * I + ioff

        # For some reason, using .= below is slower because it uses 1 allocatiotn.  On the
        # other hand, using = does not use allocation and therefore faster.
        indₛ, indₑ = (nw-1)*2M + 1, nw*2M
        Itot[indₛ:indₑ] = I
        Jtot[indₛ:indₑ] = J
        Vtot[indₛ:indₑ] = V
    end

    return dropzeros!(sparse(Itot, Jtot, Vtot, KM, M))
end


## Test pvalue adjustment methods ##
module Test_pval_pi0_adjustment

using MultipleTesting
using Base.Test

pval1 = [0.0, 0.0001, 0.001, 0.01, 0.05, 0.1, 0.2, 0.4, 0.75, 1.0]
pi0 = 0.4

## compared against:
## qvalue::qvalue(p, pi0, pfdr = TRUE, pi0.method = "bootstrap")

## benjamini_hochberg
m = benjamini_hochberg
t = BenjaminiHochbergAdaptive
ref0 = [0.0, 0.0005, 0.003333333, 0.025, 0.1, 0.166666667, 0.285714286, 0.5, 0.833333333, 1.0]
ref = ref0 .* pi0
println(" ** ", m)
@test_throws MethodError m()
## no integers as input
@test_throws MethodError m([0, 1])
## no valid p-values as input
@test_throws DomainError m([-1.0, 0.5], pi0)
@test_throws DomainError m([0.5, 1.5], pi0)
@test_throws DomainError m([0.5, 0.7], -1.0)
@test_throws DomainError m([0.5, 0.7], 1.5)
## single p-value is returned unchanged
pval = rand(1)
@test m(pval, pi0) == pval .* pi0
@test adjust(pval, t(pi0)) == pval .* pi0
## compare with reference values
@test_approx_eq_eps m(pval1, pi0) ref 1e-6
@test_approx_eq_eps adjust(pval1, t(pi0)) ref 1e-6
## BH Adaptive same as BH for π0 missing or 1
@test_approx_eq_eps m(pval1, 1.0) ref0 1e-6
@test_approx_eq_eps adjust(pval1, t(1.0)) ref0 1e-6
@test_approx_eq_eps adjust(pval1, t()) ref0 1e-6
@test_approx_eq_eps adjust(pval1, t(0.0)) zeros(ref0) 1e-6

# adaptive with pi0 estimator == oracle with estimated pi0
@test adjust(pval1, t(Storey())) == adjust(pval1, t(estimate_pi0(pval1, Storey())))
@test adjust(pval1, t(Storey(0.3))) == adjust(pval1, t(estimate_pi0(pval1, Storey(0.3))))
@test adjust(pval1, t(LeastSlope())) == adjust(pval1, t(estimate_pi0(pval1, LeastSlope())))

## qvalue
m = qValues
ref = [0.0, 0.0002, 0.00133333, 0.01, 0.04, 0.0666667, 0.114286, 0.2, 0.333333, 0.4]
println(" ** ", m)
@test_throws MethodError m()
## no integers as input
@test_throws MethodError m([0, 1])
## no valid p-values as input
@test_throws DomainError m([-1.0, 0.5], pi0)
@test_throws DomainError m([0.5, 1.5], pi0)
@test_throws DomainError m([0.5, 0.7], -1.0)
@test_throws DomainError m([0.5, 0.7], 1.5)
## single p-value is returned unchanged
pval = rand(1)
@test m(pval, pi0) == pval .* pi0
## compare with reference values
@test_approx_eq_eps m(pval1, pi0) ref 1e-6
@test_approx_eq_eps m(pval1, pi0, false) ref 1e-6

## qvalue pfdr
m = qValues
ref = [0.099685, 0.099685, 0.099685, 0.099685, 0.099685, 0.102356, 0.128033, 0.201217, 0.333333, 0.4]
println(" ** ", m)
@test_throws MethodError m()
## no integers as input
@test_throws MethodError m([0, 1])
## no valid p-values as input
@test_throws DomainError m([-1.0, 0.5], pi0)
@test_throws DomainError m([0.5, 1.5], pi0)
@test_throws DomainError m([0.5, 0.7], -1.0)
@test_throws DomainError m([0.5, 0.7], 1.5)
## single p-value is returned unchanged
pval = rand(1)
@test m(pval, pi0) == pval .* pi0
## compare with reference values
@test_approx_eq_eps m(pval1, pi0, true) ref 1e-6

end


"""
    unroll(moving_average::Vector{Float64}, window::Int64; initial_conditions::U = nothing, assert_natural::Bool = false) where { U <: Union{ Tuple{Vararg{Union{Int64,Float64}}},Nothing} }

Retrive original time series (i.e. unroll) from its moving average `moving_average`.
# Arguments
- `moving_average::Vector{Float64}`: the time series representing the moving average to unroll ;
- `window:::Int64`: the width of the moving average ;
- `initial_conditions::U = nothing`: the initial values of the original time series to be recovered. It may be a `Tuple` of `window-1` float or integer values, or `nothing` if initial conditions are unknown ;
- `assert_natural::Bool = false` default boolean argument. If true, then the pipeline will try to recover a time series of natural numbers only. More then one acceptable time series (where "acceptable" means that it reproduces `moving_average`) may be found and returned .

NB: If ```isnothing(initial_conditions) && !assert_natural``` , then only an approximate method may be used, see this [StackExchange post](https://stats.stackexchange.com/a/68002).
"""
function unroll(moving_average::Vector{Float64}, window::Int64; initial_conditions::U = nothing, assert_natural::Bool = false) where { U <: Union{ Tuple{Vararg{Union{Int64,Float64}}},Nothing} }

    reconstructed_time_series = assert_natural ? Vector{Int64}[] : Vector{Float64}[]

    if isnothing(initial_conditions)
        if assert_natural
            reconstructed_time_series = unroll_iterative(moving_average, window)
        else
            push!(reconstructed_time_series, unroll_linear_approximation(moving_average, window))
        end
    elseif length(initial_conditions) == window-1
        push!(reconstructed_time_series, unroll_recursive(moving_average, window,initial_conditions))
    else
        error("`initial_conditions` type must be either Nothing or a NTuple{window-1,Union{Float64,Int64}}")
    end

end

"""
    unroll_iterative(moving_average::Vector{Float64}, window::Int64)

Unroll `moving_average` (interpreting it as a moving average whose window width is `window`), returning the original time series assuming it is composed of only natural numbers.

The methodology is as follows:
1. Consider the minimum of `moving_average`, that we will name `minimum_average` ;
2. Produce all possible sets of `n₋ + n₊ + 1` naturals that could have `minimum_average` as mean. This is perfomed by obtaining all permutations of all the partititions of `minimum_average*(n₋ + n₊ + 1)` via Combinatorics.jl, and we will refer to each of the resulting array as a "possibility". These are irganized in an array of iterators `possibilities`;
3. For each for each `possibility` and for each `element` in `moving_average[(minimum_index +1):end]` compute the natural `x` (zero included) to be pushed to `possibility`'s end so that `sum(possibility[(i+1):(i+1 + n₋ + n₊ - 1)]) == minimum_average*(n₋ + n₊ + 1)`. if there is such `x`, push it to the `possibility`'s end and go to the next possibility, else remove the `possibility`. When this loop finishes, perform the same loops backward for each `element in reverse(moving_average[1:(minimum_index - 1)])`, this time pushing the `x`s to the `possibility`'s top. This allows for obtaining the set of all possible time series ;
4. Return the remaining possibilities.
"""
function unroll_iterative(moving_average::Vector{Float64}, window::Int64) #  n₋::Int64, n₊::Int64, window = n₋ + n₊ + 1

    # compute linear approximation
    # mean_vec::Vector{Float64} = repeat([1/ window ], window ) #n₋ + n₊ + 1
    # mean_mat::Matrix{Float64} = hcat([pad_left_right(mean_vec, i, length(moving_average) - i - 1) for i in 0:(length(moving_average) - 1)]...)'
    # linear_approximation::Vector{Float64} = pinv(mean_mat) * moving_average #mean_mat \ moving_average

    # find minimum element of the moving average, and compute the numerator of the such average
    minimum_index::Int64 = argmin(moving_average)
    minimum_window_total_cases::Int64 = round(Int64,moving_average[minimum_index] * window) #n₋ + n₊ + 1
    #println("minimum_window_total_cases = ", minimum_window_total_cases)

    # if minimum_window_total_cases == 0, set collected_partitions manually since Combinatorics would return an "undefined reference"
    collected_partitions = Vector{Int64}[]
    if minimum_window_total_cases == 0
        collected_partitions = [[0]]
    # Else, compute it using Combinatorics.jl
    else
        collected_partitions = collect(partitions(minimum_window_total_cases))
    end

    # Get all permutations of partitions of such numerator. organize them as the column of a matrix.
    possibilities::Vector{Combinatorics.Permutations{Vector{Int64}}} = permutations.([pad!(partition,window) for partition in collected_partitions if length(partition) <= window ])

    # Initialize arrays that will contain the index of the columns (possibilities) to keep (`to_be_kept`) and the new row to be added after each iteration (see for loop below)
    to_be_kept = Set{Vector{Int64}}()

    # Pre-compute the numerators of all the averages
    numerators::Vector{Int64} = round.(Ref(Int64),moving_average .* window)

    #l::Int64 = length(possibilities)
    # loop over possibilities, and store the ones that reproduce the moving average
    for perm_it in possibilities
        # if k%1000 == 0
        #     println("doing $k \\ $l")
        # end
        for possibility in perm_it
            valid::Bool = true
            # go forward
            for (i,numerator) in enumerate(@view(numerators[(minimum_index +1):end]))
                diff::Int64 = numerator - sum(@view(possibility[(i+1):(i+1 + window -1 - 1)]))
                if diff < 0
                    valid = false
                    break
                else
                    push!(possibility,diff)
                end
            end

            # go backward
            if valid
                for numerator in reverse(@view(numerators[1:(minimum_index - 1)]))
                diff::Int64 = numerator - sum( @view(possibility[1:(window-1)]))
                if diff < 0
                    valid = false
                    break
                else
                    pushfirst!(possibility,diff)
                end
            end

            if valid
                push!(to_be_kept,possibility)
            end

        end

        end
    end
    # println("unroll_rolling_mean_of_natural_series_iterators. Returning...")
    # println("length(to_be_kept) = ", length(to_be_kept))
    # return accepted possibilities
    return collect(to_be_kept)

end

"""
    unroll_linear_approximation(moving_average::Vector{Float64}, window::Int64)

Compute the linear approximation of the time series whose moving average of window `window` is `moving_average`. For details, please refer to https://stats.stackexchange.com/a/68002 .
"""
function unroll_linear_approximation(moving_average::Vector{Float64}, window::Int64)

 # compute linear approximation
 mean_vec::Vector{Float64} = repeat([1/ window ], window )
 mean_mat::Matrix{Float64} = hcat([pad_left_right(mean_vec, i, length(moving_average) - i - 1) for i in 0:(length(moving_average) - 1)]...)'
 linear_approximation::Vector{Float64} = pinv(mean_mat) * moving_average # equivalently, mean_mat \ moving_average

 return linear_approximation

end

"""
    unroll_recursive(moving_average::Vector{Float64}, window::Int64 , initial_conditions::Tuple{Vararg{Union{Int64,Float64}}} )

Return the raw time series of which `moving_average` is the moving average, where `moving_average[i]` is the moving average of the `[(i-n₋):(i+n₊)]` slice of the raw time series to be returned. Assume the raw time series is composed of only natural numbers.

The methodology is as follow:
1. Initialize `deaveraged = collect(initial)` ;
2. for each `i ∈ eachindex(moving_average) `, set `deaveraged[i+window-1] = round(Int64,window*moving_average[i] - sum(@view(deaveraged[i:(i+window-1-1)])))` ;
3. Return `deaveraged` .
"""
function unroll_recursive(moving_average::Vector{Float64}, window::Int64 , initial_conditions::Tuple{Vararg{Union{Int64,Float64}}}) #NTuple{n₋+n₊,Int64} n₋::Int64, n₊::Int64

    # check that initial conditions are of the correct size
    if length(initial_conditions) != window - 1
        error("initial_conditions must have length equal to (window-1) = ", window-1)
    end
     # initialize raw time series using provided `initial_conditions`
    deaveraged::Vector{Union{Int64,Float64}} = collect(initial_conditions)
    # pre-resize `deaveraged` to accomodate all the reconstructed time series elements
    resize!(deaveraged, length(initial_conditions) + length(moving_average))

    # loop over moving_average
    for i ∈ eachindex(moving_average) #in 1:(length(moving_average))
        deaveraged[i+window-1] = round(Int64,window*moving_average[i] - sum(@view(deaveraged[i:(i+window-1-1)])))
    end

    # return deaveraged time series
    return deaveraged

end

#######################################

# function unroll_iterative(moving_average::Vector{Float64}, n₋::Int64, n₊::Int64) #  n₋::Int64, n₊::Int64, window = n₋ + n₊ + 1

#     # compute linear approximation
#     mean_vec::Vector{Float64} = repeat([1/(n₋ + n₊ + 1 )], n₋ + n₊ + 1 ) #n₋ + n₊ + 1
#     mean_mat::Matrix{Float64} = hcat([pad_left_right(mean_vec, i, length(moving_average) - i - 1) for i in 0:(length(moving_average) - 1)]...)'
#     linear_approximation::Vector{Float64} = pinv(mean_mat) * moving_average #mean_mat \ moving_average

#     # find minimum element of the moving average, and compute the numerator of the such average
#     minimum_index::Int64 = argmin(moving_average)
#     minimum_window_total_cases::Int64 = round(Int64,moving_average[minimum_index] * (n₋ + n₊ + 1 ))
#     println("minimum_window_total_cases = ", minimum_window_total_cases)

#     # compute the numeratir of the frst average
#     # first_window_total_cases::Int64 = round(Int64,moving_average[1] * (n₋ + n₊ + 1 ))
#     # if first_window_total_cases == 0, set collected_partitions manually since Combinatorics would return a "indefined reference"
#     #println("first_window_total_cases  = $first_window_total_cases ")
#     collected_partitions = Int64[]
#     if minimum_window_total_cases == 0
#         collected_partitions = [[0]]
#     # else, compute it using Combinatorics.jl
#     else
#         collected_partitions = collect(partitions(minimum_window_total_cases))
#     end

#     # get all permutations of partitions of such numerator. organize them as the column of a matrix.
#     possibilities::Vector{Combinatorics.Permutations{Vector{Int64}}} = permutations.([pad!(partition, n₋ + n₊ + 1) for partition in collected_partitions if length(partition) <= (n₋ + n₊ + 1) ])
#     #for partition in second_collected_partitions if length(partition) <= (n₋ + n₊ + 1) ])
#     # initialize arrays that will contain the index of the columns (possibilities) to keep (`to_be_kept`) and the new row to be added after each iteration (see for loop below)
#     to_be_kept = Set{Vector{Int64}}()

#     numerators::Vector{Int64} = round.(Ref(Int64),moving_average .* (n₋ + n₊ + 1))

#     #l::Int64 = length(possibilities)
#     for (k,perm_it) in enumerate(possibilities)
#         # if k%1000 == 0
#         #     println("doing $k \\ $l")
#         # end
#         for possibility in perm_it
#             valid::Bool = true
#             # go forward
#             for (i,numerator) in enumerate(@view(numerators[(minimum_index +1):end])) # enumerate(moving_average[(minimum_index +1):end]) #(n₋ + n₊ + 1)
#                 diff::Int64 =  numerator  - sum( @view(possibility[(i+1):(i+1 + n₋ + n₊ - 1)]) ) # round(Int64,average*(n₋ + n₊ + 1 ))
#                 if diff < 0
#                     valid = false
#                     break
#                 else
#                     push!(possibility,diff)
#                 end
#             end

#             # go backward
#             if valid
#                 for numerator in reverse(@view(numerators[1:(minimum_index - 1)])) #reverse(moving_average[1:(minimum_index - 1)])
#                 diff::Int64 = numerator - sum( @view(possibility[1:(n₋ + n₊)]) ) # round(Int64,average*(n₋ + n₊ + 1 ))
#                 if diff < 0
#                     valid = false
#                     break
#                 else
#                     pushfirst!(possibility,diff)
#                 end
#             end

#             if valid
#                 push!(to_be_kept,possibility) #[1:(n₋ + n₊ )] #@view(
#             end

#         end

#         end
#     end

#     println("unroll_rolling_mean_of_natural_series_iterators. Returning...")

#     # end

#     println("length(to_be_kept) = ", length(to_be_kept))

#     # For every initial conditions, which are all slices `possibility[1:(n₋ + n₊)]` for the remaining possibilities, apply `deaverage(moving_average, n₋, n₊, Tuple(possibility[1:(n₋ + n₊ )])  )` and keep the resulting reconstructed raw series iff it contains no negative numbers. Return all possible reconstructed time series, togheter with a linear approximation.
#     #return [pred_poss for pred_poss in deaverage.(Ref(moving_average), Ref(n₋), Ref(n₊), Tuple.(collect(to_be_kept))  ) if all(pred_poss .>= 0)] , linear_approximation

#     return collect(to_be_kept), linear_approximation

# end

###############################################################################


using OrdinaryDiffEq
using DiffEqCallbacks
using RecipesBase
using SimulationLogs
using Suppressor
using Test

function lorenz!(du, u, p, t)
    @log a = u[2]-u[1]
    @log b u[3] + a

    @log c du[1] = p[1]*a
    @log p1 p[1]
    du[2] = u[1]*(p[2]-u[3]) - u[2]
    du[3] = u[1]*u[2] - p[3]*u[3]

    accum = 0
    for i in 1:5
        accum += u[1]
        @log d accum
        if t>50
            @log e u[2]
        end
    end

    return nothing
end

p = [10.0, 28.0, 8/3]
u0 = [1.0, 0.0, 0.0]
tspan = (0.0, 100.0)

prob = ODEProblem(lorenz!, u0, tspan, p)

@testset "Lorenz example" begin
    sol = solve(prob, Tsit5())

    out = get_log(sol)

    a = [u[2]-u[1] for u in sol.u]
    b = [u[3]+(u[2]-u[1]) for u in sol.u]
    c = [p[1]*(u[2]-u[1]) for u in sol.u]
    d = [n*u[1] for u in sol.u, n in 1:5]

    @testset "Get" begin
        @test out.a == a
        @test out.b == b
        @test out.c == c
        @test out.d == d
        @test all(out.p1 .== p[1])

        @test out.a == out[:a]
    end

    @testset "Variable types" begin
        @test typeof(out.a) === typeof(a)
        @test typeof(out.b) === typeof(b)
        @test typeof(out.c) === typeof(c)
        @test typeof(out.d) === typeof(d)
    end

    @testset "Plot recipes" begin
        using SimulationLogs: Scope
        using RecipesBase: apply_recipe

        AnyDict = Dict{Symbol, Any}
        t = range(tspan..., length=2000)
        out = get_log(sol, t)

        rec = apply_recipe(AnyDict(), Scope((sol, :a)))
        @test only(rec).plotattributes == AnyDict(:xguide=>"t", :label=>"a", :seriestype=>:path)
        @test only(rec).args == (t, out.a)

        rec = apply_recipe(AnyDict(), Scope((sol, [:b, :c])))
        @test rec[1].plotattributes == AnyDict(:xguide=>"t", :label=>"b", :seriestype=>:path)
        @test rec[1].args == (t, out.b)
        @test rec[2].plotattributes == AnyDict(:xguide=>"t", :label=>"c", :seriestype=>:path)
        @test rec[2].args == (t, out.c)

        rec = apply_recipe(AnyDict(), Scope((sol, (:a, :c))))
        @test only(rec).plotattributes == AnyDict(:xguide=>"t", :label=>"(a, c)", :seriestype=>:path)
        @test only(rec).args == (out.a, out.c)

        rec = apply_recipe(AnyDict(), Scope((sol, :d)))
        @test only(rec).plotattributes == AnyDict(:xguide=>"t", :label=>["d[:,$i]" for i in (1:5)'], :seriestype=>:path)
        @test only(rec).args == (t, out.d)
    end

    @testset "Printing" begin
        @test @capture_out(print(out)) ==
        """
        SimulationLog with signals:
          a :: $(typeof(out.a))
          b :: $(typeof(out.b))
          d :: $(typeof(out.d))
          c :: $(typeof(out.c))
          p1 :: $(typeof(out.p1))
        """
    end

    @testset "Miscellaneous" begin
        @test Set(propertynames(out)) == Set((:a, :b, :c, :d, :p1))

        @test is_active(out) === false

        @test_logs () get_log(sol)
    end
end

@testset "Callbacks" begin
    d_callback = PresetTimeCallback(Int(tspan[1]):Int(tspan[2]), integrator->(integrator.p[1] += 0.01))
    callback_set = CallbackSet(d_callback)
    prob.p[1] = 10.0
    sol = logged_solve(prob, Tsit5(); callback=callback_set)
    prob.p[1] = 10.0
    out = get_log(sol)

    @test out.c[1] == p[1]*(u0[2]-u0[1])
    @test !all(out.c .≈ [p[1]*(u[2]-u[1]) for u in sol.u])
    @test !all(==(p[1]), out.p1)
    @test out.p1[end] ≈ 11
end

@testset "Logged solution" begin
    include("lotka_test.jl")
end

@testset "Global log" begin
    using SimulationLogs: activate!, deactivate!, reset!

    glog = SimulationLogs.GLOBAL_LOG

    @test propertynames(glog) === ()

    @test collect(keys(glog)) == Symbol[]

    @test is_active(glog) === false
    activate!()
    @test is_active(glog) === true
    deactivate!()

    @test isempty(value_dict(glog))
    glog[:a] = zeros(5)
    @test glog.a == zeros(5)
    @test glog[:a] == zeros(5)
    reset!()
    @test_throws KeyError glog.a
    @test_throws KeyError glog[:a]
    @test isempty(value_dict(glog))
end


# Misfit functions

## Predicates ##

@doc """`isstable(sys)`

Returns `true` if `sys` is stable, else returns `false`.""" ->
function isstable(sys::LTISystem)
    if sys.Ts == 0
        if any(real(pole(sys)).>=0)
            return false
        end
    else
        if any(abs(pole(sys)).>=1)
            return false
        end
    end
    return true
end

@doc """`iscontinuous(sys)`

Returns `true` if `sys` is continuous, else returns `false`.""" ->
function iscontinuous(sys::LTISystem)
    return sys.Ts == 0
end

@doc """`issiso(sys)`

Returns `true` if `sys` is SISO, else returns `false`.""" ->
function issiso(sys::LTISystem)
    return sys.ny == 1 && sys.nu == 1
end

@doc """`isproper(tf)`

Returns `true` if the `TransferFunction` is proper. This means that order(den)
\>= order(num))""" ->
function isproper(t::TransferFunction)
    for s in t.matrix
        if length(num(s)) > length(den(s))
            return false
        end
    end
    return true
end

## Helpers (not public) ##

# Convert the argument to a Matrix{Float64}
function float64mat(A::Vector)
    A = reshape(A, size(A, 1), 1)
    return float64mat(A)
end
float64mat(A::Matrix) = map(Float64,A)
float64mat(A::Matrix{Float64}) = A
float64mat(A::Real) = float64mat([A])

# Ensures the metadata for an LTISystem is valid
function validate_names(kwargs, key, n)
    names = get(kwargs, key, "")
    if names == ""
        return fill(String(""), n)
    elseif isa(names, Vector) && eltype(names) <: AbstractString
        return String[names[i] for i = 1:n]
    elseif isa(names, AbstractString)
        return String[names * "$i" for i = 1:n]
    else
        error("$key must be of type `AbstractString` or Vector{AbstractString}")
    end
end

# Format the metadata for printing
function format_names(names::Vector{String}, default::AbstractString, unknown::AbstractString)
    n = size(names, 1)
    if all(names .== "")
        return String[default * string(i) for i=1:n]
    else
        for (i, n) in enumerate(names)
            names[i] = String((n == "") ? unknown : n)
        end
        return names
    end
end

function unwrap!(M::Array, dim=1)
    alldims(i) = [ n==dim ? i : (1:size(M,n)) for n in 1:ndims(M) ]
    for i = 2:size(M, dim)
        #This is a copy of slicedim from the JuliaLang but enables us to write to it
        #The code (with dim=1) is equivalent to
        # d = M[i,:,:,...,:] - M[i-1,:,...,:]
        # M[i,:,:,...,:] -= floor((d+π) / (2π)) * 2π
        d = M[alldims(i)...] - M[alldims(i-1)...]
        M[alldims(i)...] -= floor((d+π) / 2π) * 2π
    end
    return M
end

#Collect will create a copy and collect the elements
unwrap(m::AbstractArray, args...) = unwrap!(collect(m), args...)
unwrap(x::Number) = x

numpoly(G::TransferFunction) = map(numpoly, G.matrix)
denpoly(G::TransferFunction) = map(denpoly, G.matrix)

poly2vec(p::Poly) = p.a[1:end]

"""
outs = index2range(ind1, ind2)
Helper function to convert indexes with scalars to ranges. Used to avoid dropping dimensions
"""
index2range(ind1, ind2) = (index2range(ind1), index2range(ind2))
index2range{T<:Number}(ind::T) = ind:ind
index2range{T<:AbstractArray}(ind::T) = ind
index2range(ind::Colon) = ind


# \sum_{i, j} A_{i, j} min(w_i, w_j)
function minplus_quad(A::AbstractMatrix{T},
                      w::AbstractVector{T}) where T
    length(w) == size(A, 1) == size(A, 2) ||
        throw(DimensionMismatch("A $(size(A)) and w ($(length(w))) size mismatch"))
    isempty(w) && return zero(T)
    res = zero(T)
    @inbounds for i in eachindex(w)
        wi = w[i]
        ioffset = (i-1)*size(A, 1)
        resi = zero(T)
        if zero(T) < wi < one(T)
            for j in eachindex(w)
                resi += A[ioffset+j]*min(wi, w[j])
            end
        elseif wi == one(T) # wi >= maxw
            for j in eachindex(w)
                resi += A[ioffset+j] * w[j] # FIXME dotprod() faster?
            end
        end
        res += resi
    end
    return res
end

take2(_, v) = v

# calculate res_i = fold(res_i, \sum_{j = 1} A_{i, j} min(u_i, v_j)), i = 1..N
function minplus_bilinear!(foldl::Function,
                           res::AbstractVector{T},
                           A::AbstractMatrix{T},
                           u::AbstractVector{T},
                           v::AbstractVector{T}) where T
    length(res) == length(u) == size(A, 2) ||
        throw(DimensionMismatch("res ($(length(res))), u ($(length(u))) or A $(size(A)) size mismatch"))
    length(v) == size(A, 1) ||
        throw(DimensionMismatch("v ($(length(v))) and A $(size(A)) size mismatch"))
    n = length(res)
    @inbounds for i in axes(A, 2)
        ui = u[i]
        ioffset = (i-1)*size(A, 1)
        x = zero(T)
        if zero(T) < ui < one(T) # if ui == 0, nothing is done
            for j in eachindex(v)
                x += A[ioffset+j] * min(ui, v[j])
            end
        elseif ui == one(T)
            for j in eachindex(v)
                x += A[ioffset+j] * v[j] # FIXME dotprod() faster?
            end
        end
        res[i] = foldl(res[i], x)
    end
    return res
end

function maxplus_linear(v::AbstractVector, A::SparseMatrixCSC)
    length(v) == size(A, 1) ||
        throw(DimensionMismatch("v ($(length(v))) and A $(size(A)) size mismatch"))
    Arows = rowvals(A)
    Avals = nonzeros(A)
    res = 0.0
    @inbounds for i in axes(A, 2)
        wmax = 0.0
        for j in nzrange(A, i)
            jr = Arows[j]
            wmax = max(wmax, v[jr] * Avals[j])
        end
        res += wmax
    end
    return res
end

function maxplus_linear!(res::AbstractVector, A::SparseMatrixCSC, u::AbstractVector)
    length(res) == size(A, 1) ||
        throw(DimensionMismatch("result ($(length(res))) and A $(size(A)) size mismatch"))
    length(u) == size(A, 2) ||
        throw(DimensionMismatch("u ($(length(u))) and A $(size(A)) size mismatch"))
    Arows = rowvals(A)
    Avals = nonzeros(A)
    fill!(res, 0.0)
    @inbounds for i in eachindex(u)
        ((ui = u[i]) > 0) || continue
        for j in nzrange(A, i)
            jr = Arows[j]
            res[jr] = max(res[jr], ui * Avals[j])
        end
    end
    return res
end