"""
```
find_threshold(Entropy(),counts,edges)
```
An algorithm for finding the threshold value for a gray-level histogram using
the entropy of the histogram.

# Output

Returns the point in the `AbstractRange` which corresponds to the threshold bin in the histogram.

# Details

This algorithm uses the entropy of a gray level histogram to produce a threshold
value.

Let ``f_1, f_2, \\ldots, f_I`` be the frequencies in the various bins of the
histogram and ``I`` the number of bins. With ``N = \\sum_{i=1}^{I}f_i``, let
``p_i = \\frac{f_i}{N}`` (``i = 1, \\ldots, I``) denote the probability
distribution of gray levels. From this distribution one derives two additional
distributions. The first defined for discrete values ``1`` to ``s`` and the
other, from ``s+1`` to ``I``. These distributions are

```math
A: \\frac{p_1}{P_s}, \\frac{p_2}{P_s}, \\ldots, \\frac{p_s}{P_s}
\\quad \\text{and} \\quad
B: \\frac{p_{s+1}}{1-P_s}, \\ldots, \\frac{p_n}{1-P_s}
\\quad \\text{where} \\quad
P_s = \\sum_{i=1}^{s}p_i.
```
The entropies associated with each distribution are as follows:

```math
H(A) = \\ln(P_s) + \\frac{H_s}{P_s}
```
```math
H(B) = \\ln(1-P_s) + \\frac{H_n-H_s}{1-P_s}
```
```math
\\quad \\text{where} \\quad
H_s = -\\sum_{i=1}^{s}p_i\\ln{p_i}
\\quad \\text{and} \\quad
H_n = -\\sum_{i=1}^{I}p_i\\ln{p_i}.
```

Combining these two entropy functions we have

```math
\\psi(s) = \\ln(P_s(1-P_s)) + \\frac{H_s}{P_s} + \\frac{H_n-H_s}{1-P_s}.
```
Finding the discrete value ``s`` which maximises the function ``\\psi(s)`` produces
the sought-after threshold value (i.e. the bin which determines the threshold).

See Section 4 of [1] for more details on the derivation of the entropy.

# Options

## Choices for `counts`
You can specify an `AbstractArray` which should be a 1D array of frequencies for
a histogram. You should submit the corresponding `edges` range for the bins of
the histogram. The function will throw an error if it detects that the `edges`
and `counts` have different lengths.

## Choices for `edges`
You can specify an `AbstractRange` which should be the corresponding range for
the bins of the histogram array passed into `counts`.

# Example

```julia

using TestImages, Images

img = testimage("cameraman")
# building a histogram with 256 bins
edges, counts = build_histogram(img, 256)
#=
  The `counts` array stores at index 0 the frequencies that were below the
  first bin edge. Since we are seeking a threshold over the interval
  partitioned by `edges` we need to discard the first bin in `counts`
  so that the dimensions of `edges` and `counts` match.
=#
find_threshold(EntropyThresholding(), counts[1:end], edges)
```

# References
[1] J. N. Kapur, P. K. Sahoo, and A. K. C. Wong, “A new method for gray-level picture thresholding using the entropy of the histogram,” *Computer Vision, Graphics, and Image Processing*, vol. 29, no. 1, p. 140, Jan. 1985.[doi:10.1016/s0734-189x(85)90156-2](https://doi.org/10.1016/s0734-189x%2885%2990156-2)
"""
function find_threshold(::Entropy, counts::AbstractArray, edges::AbstractRange)
    if length(edges) != length(counts)
        error("the lengths of edges and counts must match")
    end
    nbins = length(counts)
    pdf = counts ./ sum(counts)
    Ψₘₐₓ = 0
    Hₛ = H(pdf)
    Hₙ = -1*Hₛ[nbins]
    Pₛ = cumsum(pdf)
    # Pₛ might exceed 1 for float-point numerical stability issue (issue #35)
    Pₛ = _maybe_inplace_clamp01(Pₛ)

    # Here we use a trivial findmax implementation so as to avoid additional memory allocation.
    # Generally nbins are small number (e.g., 256) so fancy findmax strategy might give less
    # performance boost.
    threshold_bin = 1
    @inbounds for i in 1:nbins
        Pᵢ = Pₛ[i]
        Hᵢ = -1*Hₛ[i]
        Ψᵢ = log(Pᵢ*(1-Pᵢ)) + (Hᵢ/Pᵢ) + ((Hₙ-Hᵢ)/(1-Pᵢ))
        if Ψᵢ > Ψₘₐₓ
            Ψₘₐₓ = Ψᵢ
            threshold_bin = i
        end
    end
    edges[threshold_bin]
end

if VERSION > v"1.5"
    function H(pdf::AbstractArray{T}) where T
        f(x) = x == 0 ? zero(T) : convert(T, x*log(x)) # To avoid NaN when x == 0
        cumsum(f(x) for x in pdf)
    end
else
    function H(pdf::AbstractArray{T}) where T
        f(x) = x == 0 ? zero(T) : convert(T, x*log(x)) # To avoid NaN when x == 0
        cumsum(f.(pdf))
    end
end

# We can't assure that all array types support setindex!, so here we only optimize
# for the `Vector`, which is `counts[1:end]`. `SubArray` may or may not be writable.
_maybe_inplace_clamp01(A::AbstractArray{T}) where T = clamp.(A, zero(T), one(T))
_maybe_inplace_clamp01(A::Vector{T}) where T = clamp!(A, zero(T), one(T))


using Test
using ForwardDiff;
using FinancialToolbox
Dual_ = ForwardDiff.Dual
print_colored("Starting Forward Diff Dual Numbers Test\n", :green)

#Test Parameters
testToll = 1e-14;
spot = 10;
K = 10;
r = 0.02;
T = 2.0;
sigma = 0.2;
d = 0.01;
assert_(value, toll) = @test abs(value) < toll
#EuropeanCall Option
PriceCall = blsprice(spot, K, r, T, sigma, d);
DeltaCall = blsdelta(spot, K, r, T, sigma, d);
ThetaCall = blstheta(spot, K, r, T, sigma, d);
RhoCall = blsrho(spot, K, r, T, sigma, d);
LambdaCall = blslambda(spot, K, r, T, sigma, d);
VannaCall = blsvanna(spot, K, r, T, sigma, d);

#EuropeanPut Option
PricePut = blsprice(spot, K, r, T, sigma, d, false);
DeltaPut = blsdelta(spot, K, r, T, sigma, d, false);
ThetaPut = blstheta(spot, K, r, T, sigma, d, false);
RhoPut = blsrho(spot, K, r, T, sigma, d, false);
LambdaPut = blslambda(spot, K, r, T, sigma, d, false);
VannaPut = blsvanna(spot, K, r, T, sigma, d, false);

#Equals for both Options
Gamma = blsgamma(spot, K, r, T, sigma, d);
Vega = blsvega(spot, K, r, T, sigma, d);

########DUAL NUMBERS
DerToll = 1e-13;
#Function definition
#Call
FcallDual(spot) = blsprice(spot, K, r, T, sigma, d);
GcallDual(r) = blsprice(spot, K, r, T, sigma, d);
HcallDual(T) = blsprice(spot, K, r, T, sigma, d);
LcallDual(sigma) = blsprice(spot, K, r, T, sigma, d);
PcallDual(spot) = blsprice(spot, K, r, T, sigma, d) * spot.value / blsprice(spot.value, K, r, T, sigma, d);
VcallDual(sigma) = blsdelta(spot, K, r, T, sigma, d);
#Put
FputDual(spot) = blsprice(spot, K, r, T, sigma, d, false);
GputDual(r) = blsprice(spot, K, r, T, sigma, d, false);
HputDual(T) = blsprice(spot, K, r, T, sigma, d, false);
PputDual(spot) = blsprice(spot, K, r, T, sigma, d, false) * spot.value / blsprice(spot.value, K, r, T, sigma, d, false);
VPutDual(sigma) = blsdelta(spot, K, r, T, sigma, d, false);
#Input
SpotDual = Dual_(spot, 1.0);
rDual = Dual_(r, 1.0);
TDual = Dual_(T, 1.0);
SigmaDual = Dual_(sigma, 1.0);

#Automatic Differentiation Test
#TEST
print_colored("--- European Call Sensitivities: DualNumbers\n", :yellow)
print_colored("-----Testing Delta\n", :blue);
assert_(FcallDual(SpotDual).partials[1] - DeltaCall, DerToll)
print_colored("-----Testing Rho\n", :blue);
assert_(GcallDual(rDual).partials[1] - RhoCall, DerToll)
print_colored("-----Testing Theta\n", :blue);
assert_(-HcallDual(TDual).partials[1] - ThetaCall, DerToll)
print_colored("-----Testing Lambda\n", :blue);
assert_(PcallDual(SpotDual).partials[1] - LambdaCall, DerToll)
print_colored("-----Testing Vanna\n", :blue);
assert_(VcallDual(SigmaDual).partials[1] - VannaCall, DerToll)
#TEST
print_colored("--- European Put Sensitivities: DualNumbers\n", :yellow)
print_colored("-----Testing Delta\n", :blue);
assert_(FputDual(SpotDual).partials[1] - DeltaPut, DerToll)
print_colored("-----Testing Rho\n", :blue);
assert_(GputDual(rDual).partials[1] - RhoPut, DerToll)
print_colored("-----Testing Theta\n", :blue);
assert_(-HputDual(TDual).partials[1] - ThetaPut, DerToll)
print_colored("-----Testing Lambda\n", :blue);
assert_(PputDual(SpotDual).partials[1] - LambdaPut, DerToll)
print_colored("-----Testing Vanna\n", :blue);
assert_(VPutDual(SigmaDual).partials[1] - VannaPut, DerToll)

print_colored("-----Testing Vega\n", :blue);
assert_(LcallDual(SigmaDual).partials[1] - Vega, DerToll)
print_colored("Dual Numbers Test Passed\n", :green)
println("")

#TEST OF INPUT VALIDATION
print_colored("Starting Input Validation Test Dual\n", :magenta)
print_colored("----Testing Negative  Spot Price \n", :cyan)
@test_throws(ErrorException, blsprice(-SpotDual, K, r, T, sigma, d))
@test_throws(ErrorException, blkprice(-SpotDual, K, r, T, sigma))
@test_throws(ErrorException, blsdelta(-SpotDual, K, r, T, sigma, d))
@test_throws(ErrorException, blsgamma(-SpotDual, K, r, T, sigma, d))
@test_throws(ErrorException, blstheta(-SpotDual, K, r, T, sigma, d))
@test_throws(ErrorException, blsrho(-SpotDual, K, r, T, sigma, d))
@test_throws(ErrorException, blspsi(-SpotDual, K, r, T, sigma, d))
@test_throws(ErrorException, blslambda(-SpotDual, K, r, T, sigma, d))
@test_throws(ErrorException, blsvanna(-SpotDual, K, r, T, sigma, d))
@test_throws(ErrorException, blsvega(-SpotDual, K, r, T, sigma, d))

print_colored("----Testing Negative  Strike Price \n", :cyan)
KK = Dual_(K, 0);
@test_throws(ErrorException, blsprice(spot, -KK, r, T, sigma, d))
@test_throws(ErrorException, blkprice(spot, -KK, r, T, sigma))
@test_throws(ErrorException, blsdelta(spot, -KK, r, T, sigma, d))
@test_throws(ErrorException, blsgamma(spot, -KK, r, T, sigma, d))
@test_throws(ErrorException, blstheta(spot, -KK, r, T, sigma, d))
@test_throws(ErrorException, blsrho(spot, -KK, r, T, sigma, d))
@test_throws(ErrorException, blspsi(spot, -KK, r, T, sigma, d))
@test_throws(ErrorException, blslambda(spot, -KK, r, T, sigma, d))
@test_throws(ErrorException, blsvanna(spot, -KK, r, T, sigma, d))
@test_throws(ErrorException, blsvega(spot, -KK, r, T, sigma, d))

print_colored("----Testing Negative  Time to Maturity \n", :cyan)
@test_throws(ErrorException, blsprice(spot, K, r, -TDual, sigma, d))
@test_throws(ErrorException, blkprice(spot, K, r, -TDual, sigma))
@test_throws(ErrorException, blsdelta(spot, K, r, -TDual, sigma, d))
@test_throws(ErrorException, blsgamma(spot, K, r, -TDual, sigma, d))
@test_throws(ErrorException, blstheta(spot, K, r, -TDual, sigma, d))
@test_throws(ErrorException, blsrho(spot, K, r, -TDual, sigma, d))
@test_throws(ErrorException, blspsi(spot, K, r, -TDual, sigma, d))
@test_throws(ErrorException, blslambda(spot, K, r, -TDual, sigma, d))
@test_throws(ErrorException, blsvanna(spot, K, r, -TDual, sigma, d))
@test_throws(ErrorException, blsvega(spot, K, r, -TDual, sigma, d))

print_colored("----Testing Negative  Volatility \n", :cyan)
@test_throws(ErrorException, blsprice(spot, K, r, T, -SigmaDual, d))
@test_throws(ErrorException, blkprice(spot, K, r, T, -SigmaDual))
@test_throws(ErrorException, blsdelta(spot, K, r, T, -SigmaDual, d))
@test_throws(ErrorException, blsgamma(spot, K, r, T, -SigmaDual, d))
@test_throws(ErrorException, blstheta(spot, K, r, T, -SigmaDual, d))
@test_throws(ErrorException, blsrho(spot, K, r, T, -SigmaDual, d))
@test_throws(ErrorException, blspsi(spot, K, r, T, -SigmaDual, d))
@test_throws(ErrorException, blslambda(spot, K, r, T, -SigmaDual, d))
@test_throws(ErrorException, blsvanna(spot, K, r, T, -SigmaDual, d))
@test_throws(ErrorException, blsvega(spot, K, r, T, -SigmaDual, d))

print_colored("Dual Input Validation Test Passed\n", :magenta)


include("linear_regressor.jl")
include("ridge_regressor.jl")
include("bayesian_regressor.jl")
include("empirical_bayesian_regressor.jl")


struct FEMProblem
    s # system
    coords
    connec
    neumdofs
    fixeddofs
    b # Forcing term
    Fnotzerocoord
end

_homogeneize(p::FEMProblem) = _homogeneize(p.s.M, p.s.K, p.b)

# nodes of interest:
#    nodeidx_hor = 3
#    nodeidx_ver = 4
function load_lambs(; mesh=1,
                      file_matrices=joinpath(@__DIR__, "mesh$mesh", "matrices.mat"),
                      file_mesh=joinpath(@__DIR__, "mesh$mesh", "mesh.mat"),
                      file_loads=joinpath(@__DIR__, "mesh$mesh", "loads.mat"))

    # load matrices
    d = matread(file_matrices)
    K = d["KTred"]
    M = d["massMatred"]
    @assert isdiag(M)
    M = Diagonal(M) # it is diagonal
    n = size(K, 1)
    C = spzeros(n, n)
    sys = SecondOrderLinearContinuousSystem(M, C, K)

    neumdofs = d["neumdofs"]
    neumdofs = Int.(neumdofs)[:]

    # load mesh
    mesh = matread(file_mesh)
    coords = mesh["mesh"]["nodesCoords"]
    connec = mesh["mesh"]["conecCell"]
    connec = Int.(connec)

    # fixed degrees of freedom
    fixeddofs = setdiff(1:6*size(coords, 1), neumdofs)

    # load external force definition
    df = matread(file_loads)
    F = df["factorLoadsFextCell"][1, 3][:]
    F = F[neumdofs]
    Fnotzerocoord = findall(!iszero, F)

    return FEMProblem(sys, coords, connec, neumdofs, fixeddofs, F, Fnotzerocoord)
end

# ======================
# Funciones de carga
# ======================

Heaviside(t) = t >= 0 ? 1 : 0

# funcion auxiliar que devuelve el factor de carga aplicado en la coordenada coord
function factor_de_carga(m, NSTEPS; T=1.0, coord=5, func=Heaviside)
    tiempos = range(0.0, T, length=NSTEPS)

    F = Vector{Vector{Float64}}(undef, NSTEPS)
    for i in 1:NSTEPS
        aux = zeros(m)
        aux[coord] .= func(tiempos[i])

        F[i] = aux
    end
    return F
end

function step_size_lamb(p, δtfac)
    nElem = sum(p.connec[:, 2] .== 3) # number of triangular elements
    CFL = 0.125
    cP  = 3200
    h   = sqrt(2*3200^2 / nElem) * δtfac
    δt = CFL * h / cP
end


using StatsBase, Distributions, IterTools #StatsPlots

function get_freq(h::Histogram, val)
    x = searchsortedfirst.(h.edges, val)
    h.weights[x...]
end

function get_bin(h::Histogram, val)
    bin = searchsortedfirst.(h.edges, val)
    bin[1] - 1
end

function get_probability(h::Histogram, val)
    x = searchsortedfirst.(h.edges, val)
    h.weights[x...]/sum(h.weights)
end

function mutual_information(X::Vector{Vector{T}}, Y::Vector{Vector{T}}) where T <: Number
    hx = fit(Histogram, (X...,))
    hy = fit(Histogram, (Y...,))
    XY = vcat(X,Y)
    hxy = fit(Histogram, (XY...,))
    MI = 0.0
    rx = create_matrix_range(size(hx.weights))
    ry = create_matrix_range(size(hy.weights))
    for x in Iterators.product(rx...)
        p_x = get_bin_probability(hx, x...)
        for y in Iterators.product(ry...)
            p_y = get_bin_probability(hy, y...)
            index_xy = (x..., y...)
            p_xy = get_bin_probability(hxy, index_xy...)
            if p_xy != 0
                MI += p_xy*log(p_xy/(p_x*p_y))
            end
        end
    end
    MI
end

function mutual_information(p_x, p_y, p_xy)
    MI = 0.0
    for x = 1:length(p_x)
        for y = 1:length(p_y)
            prob_xy = p_xy[x, y]
            prob_log = prob_xy/(p_x[x]*p_y[y])
            if prob_xy != 0 && prob_log != NaN
                MI += prob_xy*log(prob_log)
            end
        end
    end
    MI
end

function mutual_information_unit(X::Vector{T}, Y::Vector{T}) where T <: Int
    max_X, max_Y = maximum(X), maximum(Y)
    hx = fit(Histogram, (X), 0:max_X, closed=:right)
    hy = fit(Histogram, (Y), 0:max_Y, closed=:right)
    XY = hcat([X],[Y])
    hxy = fit(Histogram, (X, Y), (0:max_X, 0:max_Y), closed=:right)
    MI = 0.0
    rx = create_matrix_range(size(hx.weights))
    ry = create_matrix_range(size(hy.weights))
    for x in Iterators.product(rx...)
        p_x = get_bin_probability(hx, x...)
        for y in Iterators.product(ry...)
            p_y = get_bin_probability(hy, y...)
            index_xy = (x..., y...)
            p_xy = get_bin_probability(hxy, index_xy...)
            if p_xy != 0
                MI += p_xy*log(p_xy/(p_x*p_y))
            end
        end
    end
    MI
end

function mutual_information_unit(X::Vector{T}, Y::Vector{T}, lim_X, lim_Y) where T <: Int
    min_X, max_X = lim_X
    min_Y, max_Y = lim_Y
    range_X = min_X:max_X
    range_Y = min_Y:max_Y
    hx = fit(Histogram, (X), range_X, closed=:right)
    hy = fit(Histogram, (Y), range_Y, closed=:right)
    XY = hcat([X],[Y])
    hxy = fit(Histogram, (X, Y), (range_X, range_Y), closed=:right)
    MI = 0.0
    rx = create_matrix_range(size(hx.weights))
    ry = create_matrix_range(size(hy.weights))
    for x in Iterators.product(rx...)
        p_x = get_bin_probability(hx, x...)
        for y in Iterators.product(ry...)
            p_y = get_bin_probability(hy, y...)
            index_xy = (x..., y...)
            p_xy = get_bin_probability(hxy, index_xy...)
            if p_xy != 0
                MI += p_xy*log(p_xy/(p_x*p_y))
            end
        end
    end
    MI
end

get_bin_probability(h::Histogram, x...) = h.weights[x...]/sum(h.weights)

function create_matrix_range(t::Tuple)
    tuple_length = length(t)
    ranges = []
    for r = 1:tuple_length
        range = 1:t[r]
        push!(ranges, range)
    end
    ranges
end

function calculate_joint_probability(X::Vector{<:Number}, Y::Vector{<:Number}, lim_X, lim_Y)
    min_X, max_X = lim_X
    min_Y, max_Y = lim_Y
    range_X = min_X:max_X
    range_Y = min_Y:max_Y
    p_x = fit(Histogram, (X), range_X, closed=:right)
    p_y = fit(Histogram, (Y), range_Y, closed=:right)
    p_xy = fit(Histogram, (X, Y), (range_X, range_Y), closed=:right)
    p_X = p_x.weights/sum(p_x.weights)
    p_Y = p_y.weights/sum(p_y.weights)
    p_XY = p_xy.weights/sum(p_xy.weights)
    return p_X, p_Y, p_XY
end

function calculate_joint_probability(X::Vector{<:Number}, Y::Vector{<:Number}, nbins)
    p_x = fit(Histogram, (X), nbins=nbins[1])
    p_y = fit(Histogram, (Y), nbins=nbins[2])
    p_xy = fit(Histogram, (X, Y), nbins=nbins)
    p_X = p_x.weights/sum(p_x.weights)
    p_Y = p_y.weights/sum(p_y.weights)
    p_XY = p_xy.weights/sum(p_xy.weights)
    return p_X, p_Y, p_XY
end

include("layer_histogram.jl")

function calculate_joint_probability(X::Vector{<:Number}, Y::Vector{<:Number}, limits_X, limits_Y, nbins)
    p_x = create_bin_histogram(X, nbins[1], limits_X[1], limits_X[2])
    p_y = create_bin_histogram(Y, nbins[2], limits_Y[1], limits_Y[2])
    p_xy = fit(Histogram, (X, Y), (p_x.edges[1], p_y.edges[1]))
    p_X = p_x.weights/sum(p_x.weights)
    p_Y = p_y.weights/sum(p_y.weights)
    p_XY = p_xy.weights/sum(p_xy.weights)
    return p_X, p_Y, p_XY
end

# Test
#=
X = randn(10000)
Y = randn(10000)

println("Mutual Information: I(X; Y) = ", mutual_information([X], [Y]))
println("Mutual Information: I(Y; X) = ", mutual_information([Y], [X]))
println("Mutual Information: I(X; (Y,X)) = ", mutual_information([X], [Y, X]))
println("Mutual Information: I(X; X) = ", mutual_information([X], [X]))
println("Mutual Information: I(Y; Y) = ", mutual_information([Y], [Y]))
=#


@testset "ClosestVector.jl" begin
    #=
    The quadratic triple QT = [Q, L, c] is an n-dimensional ellipsoid with rational coefficients.
    Where,
    Q is an nxn symmetric matrix of the quadratic form ,
    L is a rational column vector of length n &
    c is a rational number
    =#
    Q = matrix(QQ,3,3,[1 0 0; 0 1 0; 0 0 1]);
    L = matrix(QQ,3,1,[1,0,0]);
    c = fmpq(2//5);
    Lat = Zlattice(gram = matrix(QQ,3,3,[1,0,0,0,1,0,0,0,1]));
    v = convert(Array{Union{RingElem, AbstractFloat, Integer, Rational},1},[-1,0,0]);
    u = fmpq(3//5);
    @test Hecke.closest_vectors(Q,L,c)[1] == Hecke.closest_vectors(Lat, v, u)[1]

    function compare_functions(Q::MatrixElem, K::MatrixElem, d::RingElement)
        List1 = Hecke.closest_vectors(Q, K, d);
        L, v, c = Hecke.convert_type(Q, K, d)
        List2 = Hecke.closest_vectors(L, v, c);
        for i in 1:length(List1)
            if List1[i] == List2[i]
                return true
            else
                error("$i-th entry of the two lists don't match.")
            end
        end
    end

    function compare_functions(L::ZLat, v::Vector{RingElement} , upperbound::RingElement)
        List1 = Hecke.closest_vectors(L, v, upperbound);
        Q, K, d = Hecke.convert_type(L, v, upperbound);
        List2 = Hecke.closest_vectors(Q, K, d);
        for i in 1:length(List1)
            if List1[i] == List2[i]
                return true
            else
                error("$i-th entry of the two lists don't match.")
            end
        end
    end

    #EXAMPLE 1a: 3-dimensional sphere
    Q1 = matrix(QQ,3,3,[1 0 0; 0 1 0; 0 0 1]);
    L1 = matrix(QQ,3,1,[1,1,1]);
    c1 = 1;

    #EXAMPLE 1b: 4-dimensional sphere
    Q2 = matrix(QQ, 4,4,[1 0 0 0; 0 1 0 0; 0 0 1 0; 0 0 0 1]);
    L2 = matrix(QQ, 4, 1 ,[1,1,1,1]);
    c2 = 3;

    #EXAMPLE 1c: 6-dimensional sphere
    Q3 = matrix(QQ,6,6,[1 0 0 0 0 0; 0 1 0 0 0 0; 0 0 1 0 0 0; 0 0 0 1 0 0; 0 0 0 0 1 0; 0 0 0 0 0 1]);
    L3 = matrix(QQ,6,1,[1,0,1,0,2,0]);
    c3 = fmpq(4//3);

    #-----------------------------------------------------------------------------

    #Examples for closest_vectors()
    #Example 1:
    Lat1 = Zlattice(gram = matrix(QQ,3,3,[1,0,0,0,1,0,0,0,1]));
    v1 = convert(Array{Union{RingElem, AbstractFloat, Integer, Rational},1},[-1,0,0]);
    u1 = fmpq(3//5);

    #Example 1:
    Lat2 = Zlattice(gram = matrix(QQ,4,4,[1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1]));
    v2 = convert(Array{Union{RingElem, AbstractFloat, Integer, Rational},1},[-1,-1,-1,-1]);
    u2 = fmpq(41//11);

    #Example 1:
    Lat3 = Zlattice(gram = matrix(QQ,6,6,[1,0,0,0,0,0, 0,1,0,0,0,0, 0,0,1,0,0,0, 0,0,0,1,0,0, 0,0,0,0,1,0, 0,0,0,0,0,1]));
    v3 = convert(Array{Union{RingElem, AbstractFloat, Integer, Rational},1},[-1,0,-1,0,-2,0]);
    u3 = fmpq(14//3);

    @testset "Comparing closest_vectors(Q, K, d) to closest_vectors(Lat, vec, upperbound) list" begin
        @test compare_functions(Q1, L1, c1) == true
        @test compare_functions(Q2, L2, c2) == true
        @test compare_functions(Q3, L3, c3) == true
    end

    @testset "Comparing closest_vectors(Lat, vec, upperbound) list to closest_vectors(Q, K, d)" begin
        @test compare_functions(Lat1, v1, u1) == true
        @test compare_functions(Lat2, v2, u2) == true
        @test compare_functions(Lat3, v3, u3) == true
    end

    @testset "Conversion from Quadratic Triple set to Closest Vector Enumeration input parameters" begin
        Q3 = matrix(QQ,6,6,[1 0 0 0 0 0; 0 1 0 0 0 0; 0 0 1 0 0 0; 0 0 0 1 0 0; 0 0 0 0 1 0; 0 0 0 0 0 1]);
        L3 = matrix(QQ,6,1,[1,0,1,0,2,0]);
        c3 = fmpq(4//3);
        cvp = Hecke.convert_type(Q3,L3,c3);
        @test  det(basis_matrix(cvp[1])) == 1
        @test cvp[2] == [-1, 0, -1, 0, -2, 0]
        @test cvp[3] == 14//3
    end

    @testset "Conversion from Closest Vector Enumeration to Quadratic Triple set input parameters" begin
        Lat = Zlattice(gram = matrix(QQ,3,3,[1,0,0,0,1,0,0,0,1]));
        vec = convert(Array{Union{RingElem, AbstractFloat, Integer, Rational},1},[-1,-1,-1]);
        upbound = 2;
        qt = Hecke.convert_type(Lat, vec, upbound);
        @test qt[1] == matrix(QQ,3,3,[1 0 0; 0 1 0; 0 0 1])
        @test qt[2] == matrix(QQ,3,1,[1,1,1])
        @test qt[3] == 1
    end

  @testset "Quadratic Triple Tests" begin
      @testset "Projective Quadratic Triple" begin
            Q = matrix(QQ, 4,4,[1 0 0 0; 0 1 0 0; 0 0 1 0; 0 0 0 1]);
            L = matrix(QQ, 4, 1 ,[1,1,1,1]);
            c = 3;
            @test Hecke.pojective_quadratic_triple(Q, L, c, 1) == (Q[1:1, 1:1], L[1:1,1:1], 0)
            @test Hecke.pojective_quadratic_triple(Q, L, c, 4) == (Q, L, c)
        end

        @testset "Finite set of integers for a 1-dimensional quadratic triple" begin
            Q = matrix(QQ, 4,4,[1 0 0 0; 0 1 0 0; 0 0 1 0; 0 0 0 1]);
            L = matrix(QQ, 4, 1 ,[1,1,1,1]);
            c = 3;
            Q1 = Hecke.pojective_quadratic_triple(Q, L, c, 1)[1];
            L1 = Hecke.pojective_quadratic_triple(Q, L, c, 1)[2];
            c1 = Hecke.pojective_quadratic_triple(Q, L, c, 1)[3];
            @test Hecke.range_ellipsoid_dim1(Q1, L1, c1) == -2:0
        end

        @testset "Different Types of Extended Quadratic Triple" begin
            @testset "Quadratic triple extension from a 1-dimensional quadratic triple" begin
                Q = matrix(QQ, 4,4,[1 0 0 0; 0 1 0 0; 0 0 1 0; 0 0 0 1]);
                L = matrix(QQ, 4, 1 ,[1,1,1,1]);
                c = 3;
                @test Hecke.positive_quadratic_triple(fmpz(1), Q, L, c)[1] == Q[2:4, 2:4]
                @test Hecke.positive_quadratic_triple(fmpz(1), Q, L, c)[2] == L[2:4,1:1]
                @test Hecke.positive_quadratic_triple(fmpz(1), Q, L, c)[3] == 6
            end

            @testset "Quadratic triple extension from a m-dimensional quadratic triple to m+n-1 quadratic triple" begin
                Q = matrix(QQ, 4,4,[1 0 0 0; 0 1 0 0; 0 0 1 0; 0 0 0 1]);
                L = matrix(QQ, 4, 1 ,[1,1,1,1]);
                c = 3;
                @test Hecke.positive_quadratic_triple2(fmpz[-2,0,1], Q,L,c)[1][1] == 1
                @test Hecke.positive_quadratic_triple2(fmpz[-2,0,1], Q,L,c)[2][1] == 1
                @test Hecke.positive_quadratic_triple2(fmpz[-2,0,1], Q,L,c)[3] == 6
            end
        end
    end

    @testset "Finite set tests" begin
        Q = matrix(QQ, 4,4,[1 0 0 0; 0 1 0 0; 0 0 1 0; 0 0 0 1]);
        Q1 = -Q;
        L = matrix(QQ, 4, 1 ,[1,1,1,1]);
        c = 3;
        @test Hecke.closest_vectors(Q, L, c)[1] == [-2, -1, -1, -1]
        @test size(Hecke.closest_vectors(Q, L, c), 1) == 9
        @test Hecke.closest_vectors(Q, L, c)[1] == Hecke.closest_vectors(Q1,L,c)[1]

        x = matrix(QQ, 4, 1, Hecke.closest_vectors(Q, L, c)[5]);
        xt = transpose(matrix(QQ, 4, 1, Hecke.closest_vectors(Q, L, c)[5]));
        R = xt * Q * x + 2 * xt * L + c;
        @test  R[1] <= 0

        y = matrix(QQ, 4, 1, Hecke.closest_vectors(Q1, L, c)[5]);
        yt = transpose(matrix(QQ, 4, 1, Hecke.closest_vectors(Q1, L, c)[5]));
        S = yt * Q * y + 2 * yt * L + c;
        @test  S[1] <= 0
    end
end


using DiscreteFunctions, SimpleGraphs

"""
`tree2function(T::SimpleGraph)` converts a tree `T` into a
`DiscreteFunction`. We assume the vertex set of `T` is of the
form `{1,2,...,n}`. The resulting function has each vertex pointing
to the next vertex on a path to the vertex `1`.

**Warning**: I don't check if the input graph is valid. That is
it *must* be a tree with vertex set `1:n`.
"""
function tree2function(G::SimpleGraph)::DiscreteFunction
    n = NV(G)
    data = zeros(Int,n)
    data[1] = 1
    for v = 2:n
        P = find_path(G,v,1)
        data[v] = P[2]
    end
    return DiscreteFunction(data)
end

"""
`RandomTreeFunction(n)` creates a random function whose digraph
is a tree (with a single selfloop).
"""
function RandomTreeFunction(n::Int)::DiscreteFunction
    T = RandomTree(n)
    return tree2function(T)
end

"""
`is_tree_function(f::DiscreteFunction)` returns `true` exactly when the
graph of `f` is a tree (with a single loop at one vertex).
"""
function is_tree_function(f::DiscreteFunction)::Bool
    clist = cycles(f)
    if length(clist) != 1
        return false
    end
    if length(clist[1]) != 1
        return false
    end
    return true
end


module RiemannComplexNumbers

import Base.inv, Base.Complex, Base.show
import Base.+, Base.-, Base.*, Base./, Base.==, Base.hash
import Base: isinf, isnan, iszero, Complex, isequal

export RC, ComplexInf, ComplexNaN, IM

struct RC{T<:Complex} <: Number
    val::T
    nan_flag::Bool
    inf_flag::Bool
end

const ComplexNaN = RC((0+0im)/0,true,false)
const ComplexInf = RC(0+0im,false,true)
const IM = RC(im,false,false)

function RC(z::Complex)
    if isnan(z)
        return ComplexNaN
    end
    if isinf(z)
        return ComplexInf
    end
    return RC(z,false,false)
end

function Complex(a::RC)::Complex
    if isinf(a)
        return Inf + Inf*im
    end
    if isnan(a)
        return NaN
    end
    return a.val
end

RC(z::Real) = RC(z+0im)

isinf(z::RC) = z.inf_flag
isnan(z::RC) = z.nan_flag

function iszero(z::RC)
    if isnan(z) || isinf(z)
        return false
    end
    return iszero(z.val)
end

function show(io::IO, z::RC)
    if isinf(z)
        print(io,"ComplexInf")
    elseif isnan(z)
        print(io,"ComplexNaN")
    else
        sz = string(z.val)[1:end-2] * "IM"
        print(io,sz)
    end
end

function (==)(a::RC, b::RC)::Bool
    if isnan(a) || isnan(b)
        return false
    end
    if isinf(a) && isinf(b)
        return true
    end
    if isinf(a) || isinf(b)
        return false
    end
    return a.val == b.val
end

isequal(a::RC,b::Number) = isequal(promote(a,b)...)
isequal(a::Number,b::RC) = isequal(promote(a,b)...)

function isequal(a::RC, b::RC)::Bool
    # for isequal, nan's compare true
    if isnan(a) && isnan(b)
        return true
    end
    # but if only one is nan, then it's false
    if isnan(a) || isnan(b)
        return false
    end
    ## complex infinites are equal
    if isinf(a) && isinf(b)
        return true
    end
    if isinf(a) || isinf(b)
        return false
    end
    # finally, fall back on isequal for Complex
    return isequal(a.val, b.val)
end

function hash(a::RC, h::UInt=UInt(0))
    if isinf(a)
        return hash(Inf,h)
    end
    if isnan(a)
        return hash(NaN,h)
    end
    return hash(a.val,h)
end

import Base.promote_rule

promote_rule(::Type{RC{T}}, ::Type{S}) where {T,S} = RC

include("arithmetic.jl")
include("functions.jl")
end  # end of module


module FunctionZeros
import SpecialFunctions
import Roots

export besselj_zero, bessely_zero

# Asymptotic formula for zeros of Bessel J function of order nu
"""
    besselj_zero_asymptotic(nu, n)

Asymptotic formula for the `n`th zero of the the Bessel J function of order `nu`.
"""
besselj_zero_asymptotic(nu, n) = pi * (n - 1 + nu / 2 + 3//4)

"""
    bessely_zero_asymptotic(nu, n)

Asymptotic formula for the `n`th zero of the the Bessel Y function of order `nu`.
"""
bessely_zero_asymptotic(nu, n) = pi * (n + nu / 2 - 3//4)

# Use the asymptotic values as starting values.
# These find the correct zeros even for n = 1,2,...
# Order 0 is 6 times slower and 50-100 times less accurate
# than higher orders, with other parameters constant.
"""
    besselj_zero(nu, n)

`n`th zero of the Bessel J function of order `nu`,
for `n` = `1,2,...`.
"""
besselj_zero(nu, n) = Roots.fzero((x) -> SpecialFunctions.besselj(nu, x),
                                  besselj_zero_asymptotic(nu, n); order=1)

"""
    bessely_zero(nu, n)

`n`th zero of the Bessel Y function of order `nu`,
for `n` = `1,2,...`.
"""
bessely_zero(nu, n) = Roots.fzero((x) -> SpecialFunctions.bessely(nu, x),
                                 bessely_zero_asymptotic(nu, n); order=1)

end # module FunctionZeros


using ODEInterfaceDiffEq, DiffEqProblemLibrary, DiffEqBase
using Test

jac_called = false

function Lotka(du,u,p,t)
  du[1] = u[1] - u[1] * u[2] # REPL[7], line 3:
  du[2] = -3 * u[2] + 1 * u[1] * u[2]
  nothing
end

function Lotka_jac(J,u,p,t)
  global jac_called
  jac_called = true
  J[1,1] = 1.0 - u[2]
  J[1,2] = -u[1]
  J[2,1] = 1 * u[2]
  J[2,2] = -3 + u[1]
  nothing
end

prob = ODEProblem(ODEFunction(Lotka,jac=Lotka_jac),ones(2),(0.0,2.0))

sol =solve(prob,radau5();dt=1//2^(4))

@test jac_called == true


"""
    randindex(a, b) -> NTuple{4, Float64}

Compute the tuple of Rand-related indices between the clusterings `c1` and `c2`.

`a` and `b` can be either [`ClusteringResult`](@ref) instances or
assignments vectors (`AbstractVector{<:Integer}`).

Returns a tuple of indices:
  - Hubert & Arabie Adjusted Rand index
  - Rand index (agreement probability)
  - Mirkin's index (disagreement probability)
  - Hubert's index (``P(\\mathrm{agree}) - P(\\mathrm{disagree})``)

# References
> Lawrence Hubert and Phipps Arabie (1985). *Comparing partitions.*
> Journal of Classification 2 (1): 193–218

> Meila, Marina (2003). *Comparing Clusterings by the Variation of
> Information.* Learning Theory and Kernel Machines: 173–187.
"""
function randindex(a, b)
    c = counts(a, b)

    n = sum(c)
    nis = sum(abs2, sum(c, dims=2))        # sum of squares of sums of rows
    njs = sum(abs2, sum(c, dims=1))        # sum of squares of sums of columns

    t1 = binomial(n, 2)                    # total number of pairs of entities
    t2 = sum(abs2, c)                      # sum over rows & columnns of nij^2
    t3 = .5*(nis+njs)

    # Expected index (for adjustment)
    nc = (n*(n^2+1)-(n+1)*nis-(n+1)*njs+2*(nis*njs)/n)/(2*(n-1))

    A = t1+t2-t3;        # agreements count
    D = -t2+t3;          # disagreements count

    if t1 == nc
        # avoid division by zero; if k=1, define Rand = 0
        ARI = 0
    else
        # adjusted Rand - Hubert & Arabie 1985
        ARI = (A-nc)/(t1-nc)
    end

    RI = A/t1            # Rand 1971      # Probability of agreement
    MI = D/t1            # Mirkin 1970    # p(disagreement)
    HI = (A-D)/t1        # Hubert 1977    # p(agree)-p(disagree)

    return (ARI, RI, MI, HI)
end


@doc raw"""
    NegativeMultinomial(x₀::Real, p::AbstractVector)

Negative Multinomial distribution defined as
```math
    p(\boldsymbol{x}|x_0, \boldsymbol{p}) = \Gamma\left(\sum_{i=0}^M x_i \right)\frac{p_0^{x_0}}{\Gamma(x_0)}\prod_{i=1}^M \frac{p_i^{x_i}}{x_i!}
```
where $p_0= 1-\sum_{i=1}^M p_i$.

For a detailed understanding of this distribution, see "Negative multinomial distribution" - Sibuya et al. - 1964
"""
struct NegativeMultinomial{Tx₀<:Real,Tp<:AbstractVector} <:
       Distributions.DiscreteMultivariateDistribution
    x₀::Tx₀
    p::Tp
    function NegativeMultinomial(x₀::Real, p::AbstractVector)
        x₀ > 0 || throw(ArgumentError("x₀ has to be positive"))
        (all(>=(0), p) && sum(p) < 1) || throw(
            ArgumentError(
                "All p should be positive and their sum should be strictly smaller than 1",
            ),
        )
        return new{typeof(x₀),typeof(p)}(x₀, p)
    end
end

_p₀(d::NegativeMultinomial) = 1 - sum(d.p)

Distributions.params(d::NegativeMultinomial) = (d.x₀, d.p)

Base.eltype(::NegativeMultinomial) = Int

Base.length(d::NegativeMultinomial) = length(d.p)

function Distributions._rand!(
    rng::AbstractRNG, d::NegativeMultinomial, x::AbstractVector{<:Real}
)
    p₀ = _p₀(d)
    θ = rand(rng, Gamma(d.x₀, inv(p₀) - 1))
    λ = d.p * θ / (1 - p₀) # convert parameters to the scaled Poisson ones
    for i in eachindex(x)
        x[i] = rand(rng, Poisson(λ[i]))
    end
    return x
end

function Distributions._logpdf(d::NegativeMultinomial, x::AbstractVector)
    return loggamma(d.x₀ + sum(x)) + d.x₀ * log(_p₀(d)) - loggamma(d.x₀) +
           mapreduce(+, d.p, x) do pᵢ, xᵢ
        xᵢ * log(pᵢ) - logfactorial(xᵢ)
    end
end

Distributions.mean(d::NegativeMultinomial) = d.x₀ / _p₀(d) * d.p

function Distributions.var(d::NegativeMultinomial)
    p₀ = _p₀(d)
    x₀ = d.x₀
    return x₀ / p₀^2 * abs2.(d.p) + x₀ / p₀ * p
end

function Distributions.cov(d::NegativeMultinomial)
    p₀ = _p₀(d)
    x₀ = d.x₀
    return x₀ / p₀^2 * d.p * d.p' + x₀ / p₀ * Diagonal(d.p)
end

function Distributions.mgf(d::NegativeMultinomial, t::AbstractVector)
    return (_p₀(d) / (1 - dot(d.p, exp.(t))))^d.x₀
end

function Distributions.kldivergence(p::NegativeMultinomial, q::NegativeMultinomial)
    p₀ = _p₀(p)
    x₀ = p.x₀
    x₀ == q.x₀ || error(
        "KL divergence cannot be computed with different x₍", "p: $(p.x₀), q: $(q.x₀)"
    )
    return x₀ * log(p₀) - x₀ * log(_p₀(q)) +
           x₀ / p₀ * sum(1:length(p)) do i
        p.p[i] * (log(p.p[i]) - log(q.p[i]))
    end
end


using Distributions
using ConjugatePriors

using Random: seed!

using ConjugatePriors: NormalGamma, NormalInverseGamma, NormalInverseChisq
using ConjugatePriors: posterior, posterior_rand, posterior_mode, posterior_randmodel, fit_map

n = 100
w = rand(100)

@testset "Conjugates for normal distribution" begin

    @testset "Νormal - Νormal (known sigma)" begin

        pri = Normal(1.0, 5.0)

        x = rand(Normal(2.0, 3.0), n)
        p = posterior((pri, 3.0), Normal, x)
        @test isa(p, Normal)
        @test mean(p) ≈ (mean(pri) / var(pri) + sum(x) / 9.0) / (1.0 / var(pri) + n / 9.0)
        @test var(p) ≈ inv(1.0 / var(pri) + n / 9.0)

        r = posterior_mode((pri, 3.0), Normal, x)
        @test r ≈ mode(p)

        f = fit_map((pri, 3.0), Normal, x)
        @test isa(f, Normal)
        @test f.μ == r
        @test f.σ == 3.0

        p = posterior((pri, 3.0), Normal, x, w)
        @test isa(p, Normal)
        @test mean(p) ≈  (mean(pri) / var(pri) + dot(x, w) / 9.0) / (1.0 / var(pri) + sum(w) / 9.0)
        @test var(p) ≈ inv(1.0 / var(pri) + sum(w) / 9.0)

        r = posterior_mode((pri, 3.0), Normal, x, w)
        @test r ≈ mode(p)

        f = fit_map((pri, 3.0), Normal, x, w)
        @test isa(f, Normal)
        @test f.μ == r
        @test f.σ == 3.0

    end

    @testset "ΙnverseGamma - Νormal (known mu)" begin

        pri = InverseGamma(1.5, 0.5)

        x = rand(Normal(2.0, 3.0), n)
        p = posterior((2.0, pri), Normal, x)
        @test isa(p, InverseGamma)
        @test shape(p) ≈ shape(pri) + n / 2
        @test scale(p) ≈ scale(pri) + sum(abs2.(x .- 2.0)) / 2

        r = posterior_mode((2.0, pri), Normal, x)
        @test r ≈ mode(p)

        f = fit_map((2.0, pri), Normal, x)
        @test isa(f, Normal)
        @test f.μ == 2.0
        @test abs2(f.σ) ≈ r

        p = posterior((2.0, pri), Normal, x, w)
        @test isa(p, InverseGamma)
        @test shape(p) ≈ shape(pri) + sum(w) / 2
        @test scale(p) ≈ scale(pri) + dot(w, abs2.(x .- 2.0)) / 2

        r = posterior_mode((2.0, pri), Normal, x, w)
        @test r ≈ mode(p)

        f = fit_map((2.0, pri), Normal, x, w)
        @test isa(f, Normal)
        @test f.μ == 2.0
        @test abs2(f.σ) ≈ r

    end

    @testset "Gamma - Νormal (known mu)" begin

        pri = Gamma(1.5, 2.0)

        x = rand(Normal(2.0, 3.0), n)
        p = posterior((2.0, pri), Normal, x)
        @test isa(p, Gamma)
        @test shape(p) ≈ shape(pri) + n / 2
        @test scale(p) ≈ scale(pri) + sum(abs2.(x .- 2.0)) / 2

        r = posterior_mode((2.0, pri), Normal, x)
        @test r ≈ mode(p)

        f = fit_map((2.0, pri), Normal, x)
        @test isa(f, Normal)
        @test f.μ == 2.0
        @test abs2(f.σ) ≈ inv(r)

        p = posterior((2.0, pri), Normal, x, w)
        @test isa(p, Gamma)
        @test shape(p) ≈ shape(pri) + sum(w) / 2
        @test scale(p) ≈ scale(pri) + dot(w, abs2.(x .- 2.0)) / 2

        r = posterior_mode((2.0, pri), Normal, x, w)
        @test r ≈ mode(p)

        f = fit_map((2.0, pri), Normal, x, w)
        @test isa(f, Normal)
        @test f.μ == 2.0
        @test abs2(f.σ) ≈ inv(r)

    end

    @testset "NormalInverseGamma - Normal" begin

        mu_true = 2.
        sig2_true = 3.
        x = rand(Normal(mu_true, sig2_true), n)

        mu0 = 2.
        v0 = 3.
        shape0 = 5.
        scale0 = 2.
        pri = NormalInverseGamma(mu0, v0, shape0, scale0)

        post = posterior(pri, Normal, x)
        @test isa(post, NormalInverseGamma)

        @test post.mu ≈ (mu0/v0 + n*mean(x))/(1.0/v0 + n)
        @test post.v0 ≈ 1.0/(1.0/v0 + n)
        @test post.shape ≈ shape0 + 0.5*n
        @test post.scale ≈ scale0 + 0.5*(n-1)*var(x) + n./v0./(n + 1.0/v0)*0.5*(mean(x)-mu0).^2

        ps = posterior_randmodel(pri, Normal, x)

        @test isa(ps, Normal)
        @test insupport(ps,ps.μ) && ps.σ > zero(ps.σ)

        for shape in [0.1, 1., 10.]
            d = NormalInverseGamma(mu0, v0, scale0, shape)
            μ, σ2 = mode(d)
            @test pdf(d, μ, σ2) > pdf(d, μ, σ2 + 0.001)
            @test pdf(d, μ, σ2) > pdf(d, μ, σ2 - 0.001)
        end

    end

    @testset "NormalInverseChisq - Normal" begin

        mu_true = 2.
        sig2_true = 3.
        x = rand(Normal(mu_true, sig2_true), n)

        μ0 = 2.0
        σ20 = 2.0/5.0
        κ0 = 1.0/3.0
        ν0 = 10.0

        pri = NormalInverseChisq(μ0, σ20, κ0, ν0)
        pri2 = convert(NormalInverseGamma, pri)

        @test convert(NormalInverseChisq, pri2) == pri

        @test mode(pri2) == mode(pri)
        @test mean(pri2) == mean(pri)
        @test pdf(pri, mu_true, sig2_true) == pdf(pri2, mu_true, sig2_true)

        @test (seed!(1); rand(pri)) == (seed!(1); rand(pri2))

        # check that updating is consistent between NIχ2 and NIG
        post = posterior(pri, Normal, x)
        post2 = posterior(pri2, Normal, x)
        @test isa(post, NormalInverseChisq)
        @test convert(NormalInverseChisq, post2) == post

        for _ in 1:10
            x = rand(post)
            @test pdf(post, x...) ≈ pdf(post2, x...)
            @test logpdf(post, x...) ≈ logpdf(post2, x...)
        end

        for ν in [0.1, 1., 10.]
            nix = NormalInverseChisq(0., 2., 3., ν)
            μ, σ2 = mode(nix)
            @test pdf(nix, μ, σ2) > pdf(nix, μ, σ2 + 0.001)
            @test pdf(nix, μ, σ2) > pdf(nix, μ, σ2 - 0.001)
        end

    end

    @testset "NormalGamma - Normal" begin

        mu_true = 2.
        tau2_true = 3.
        x = rand(Normal(mu_true, 1.0/tau2_true), n)

        mu0 = 2.
        nu0 = 3.
        shape0 = 5.
        rate0 = 2.
        pri = NormalGamma(mu0, nu0, shape0, rate0)

        post = posterior(pri, Normal, x)
        @test isa(post, NormalGamma)

        @test post.mu ≈ (nu0*mu0 + n*mean(x))./(nu0 + n)
        @test post.nu ≈ nu0 + n
        @test post.shape ≈ shape0 + 0.5*n
        @test post.rate ≈ rate0 + 0.5*(n-1)*var(x) + n*nu0/(n + nu0)*0.5*(mean(x)-mu0).^2

        ps = posterior_randmodel(pri, Normal, x)

        @test isa(ps, Normal)
        @test insupport(ps, ps.μ) && ps.σ > zero(ps.σ)
    end

end


"""
	Obstacle

Abstract type representing obstacle.
"""
abstract type Obstacle end

"""
	CircularObstacle <: Obstacle

Circular obstacle with center c and radius r
"""
struct CircularObstacle <: Obstacle
    c::Vector{Float64}
    r::Float64
end

"""
	ControlBarrierFunction(O::CircularObstacle, α)

Construct a CBF for a ciruclar obstacle.
"""
function ControlBarrierFunction(O::CircularObstacle, α)
    h(x) = (x[1] - O.c[1])^2 + (x[2] - O.c[2])^2 - O.r^2
    return ControlBarrierFunction(h, α)
end


##########################
### Points Conversions ###
##########################

###############################################
### Conversion from LLA to ECEF coordinates ###
###############################################

function ECEF(lla::OpenStreetMapX.LLA, datum::OpenStreetMapX.Ellipsoid = OpenStreetMapX.WGS84)
    ϕdeg, λdeg, h = lla.lat, lla.lon, lla.alt
    d = datum

    sinϕ, cosϕ = sind(ϕdeg), cosd(ϕdeg)
    sinλ, cosλ = sind(λdeg), cosd(λdeg)

    N = d.a / sqrt(1 - d.e² * sinϕ^2)  # Radius of curvature (meters)

    x = (N + h) * cosϕ * cosλ
    y = (N + h) * cosϕ * sinλ
    z = (N * (1 - d.e²) + h) * sinϕ

    return OpenStreetMapX.ECEF(x, y, z)
end

###############################################
### Conversion from ECEF to LLA coordinates ###
###############################################

function LLA(ecef::OpenStreetMapX.ECEF, datum::OpenStreetMapX.Ellipsoid = OpenStreetMapX.WGS84)
    x, y, z = ecef.x, ecef.y, ecef.z
    d = datum

    p = hypot(x, y)
    θ = atan(z*d.a, p*d.b)
    λ = atan(y, x)
    ϕ = atan(z + d.e′² * d.b * sin(θ)^3, p - d.e²*d.a*cos(θ)^3)

    N = d.a / sqrt(1 - d.e² * sin(ϕ)^2)  # Radius of curvature (meters)
    h = p / cos(ϕ) - N

    return OpenStreetMapX.LLA(rad2deg(ϕ), rad2deg(λ), h)
end

###############################################
### Conversion from ECEF to ENU coordinates ###
###############################################

# Given a reference point for linarization
function ENU(ecef::OpenStreetMapX.ECEF, lla_ref::OpenStreetMapX.LLA, datum::OpenStreetMapX.Ellipsoid = OpenStreetMapX.WGS84)
    ϕdeg, λdeg = lla_ref.lat, lla_ref.lon

    ecef_ref = OpenStreetMapX.ECEF(lla_ref, datum)
    ∂x = ecef.x - ecef_ref.x
    ∂y = ecef.y - ecef_ref.y
    ∂z = ecef.z - ecef_ref.z

    # Compute rotation matrix
    sinλ, cosλ = sind(λdeg), cosd(λdeg)
    sinϕ, cosϕ = sind(ϕdeg), cosd(ϕdeg)

    # R = [     -sinλ       cosλ  0.0
    #      -cosλ*sinϕ -sinλ*sinϕ cosϕ
    #       cosλ*cosϕ  sinλ*cosϕ sinϕ]
    #
    # east, north, up = R * [∂x, ∂y, ∂z]
    east  = ∂x * -sinλ      + ∂y * cosλ       + ∂z * 0.0
    north = ∂x * -cosλ*sinϕ + ∂y * -sinλ*sinϕ + ∂z * cosϕ
    up    = ∂x * cosλ*cosϕ  + ∂y * sinλ*cosϕ  + ∂z * sinϕ

    return OpenStreetMapX.ENU(east, north, up)
end

# Given Bounds object for linearization
ENU(ecef::OpenStreetMapX.ECEF, bounds::OpenStreetMapX.Bounds{OpenStreetMapX.LLA}, datum::OpenStreetMapX.Ellipsoid = OpenStreetMapX.WGS84) = OpenStreetMapX.ENU(ecef, OpenStreetMapX.center(bounds), datum)

###############################################
### Conversion from ENU to ECEF coordinates ###
###############################################

function ECEF(enu::OpenStreetMapX.ENU, lla_ref::OpenStreetMapX.LLA, datum::OpenStreetMapX.Ellipsoid = OpenStreetMapX.WGS84)
    ϕdeg, λdeg = lla_ref.lat, lla_ref.lon

    ecef_ref = OpenStreetMapX.ECEF(lla_ref, datum)

    # Compute rotation matrix
    sinλ, cosλ = sind(λdeg), cosd(λdeg)
    sinϕ, cosϕ = sind(ϕdeg), cosd(ϕdeg)

    # R = [-sinλ -sinϕ*cosλ  cosϕ*cosλ
    #      	cosλ -sinϕ*sinλ  cosϕ*sinλ
    #        0.0       cosϕ       sinϕ]
    #
    # x,y,z = R * [east, north, up] + [ecef_ref.x, ecef_ref.y, ecef_ref.z]

    x  = -sinλ*enu.east + -sinϕ*cosλ*enu.north + cosϕ*cosλ*enu.up + ecef_ref.x
    y = cosλ*enu.east +  -sinϕ*sinλ*enu.north + cosϕ*sinλ*enu.up + ecef_ref.y
    z    = 0.0*enu.east + cosϕ*enu.north + sinϕ*enu.up + ecef_ref.z

    return OpenStreetMapX.ECEF(x, y, z)
end

# Given Bounds object for linearization
ECEF(enu::OpenStreetMapX.ENU, bounds::OpenStreetMapX.Bounds{OpenStreetMapX.LLA}, datum::OpenStreetMapX.Ellipsoid = OpenStreetMapX.WGS84) = OpenStreetMapX.ECEF(enu, OpenStreetMapX.center(bounds), datum)

##############################################
### Conversion from LLA to ENU coordinates ###
##############################################

# Given a reference point for linarization
ENU(lla::OpenStreetMapX.LLA, lla_ref::OpenStreetMapX.LLA, datum::OpenStreetMapX.Ellipsoid = OpenStreetMapX.WGS84) = OpenStreetMapX.ENU(OpenStreetMapX.ECEF(lla, datum), lla_ref, datum)

# Given Bounds object for linearization
ENU(lla::OpenStreetMapX.LLA, bounds::OpenStreetMapX.Bounds{OpenStreetMapX.LLA}, datum::OpenStreetMapX.Ellipsoid = OpenStreetMapX.WGS84) = OpenStreetMapX.ENU(OpenStreetMapX.ECEF(lla, datum), bounds, datum)

##############################################
### Conversion from ENU to LLA coordinates ###
##############################################

# Given a reference point for linarization
LLA(enu::OpenStreetMapX.ENU, lla_ref::OpenStreetMapX.LLA, datum::OpenStreetMapX.Ellipsoid = OpenStreetMapX.WGS84) = OpenStreetMapX.LLA(OpenStreetMapX.ECEF(enu,lla_ref))

# Given Bounds object for linearization
LLA(enu::OpenStreetMapX.ENU, bounds::OpenStreetMapX.Bounds{OpenStreetMapX.LLA}, datum::OpenStreetMapX.Ellipsoid = OpenStreetMapX.WGS84) = OpenStreetMapX.LLA(OpenStreetMapX.ECEF(enu,bounds))

#########################################
### Dictionaries of Nodes Conversions ###
#########################################

###############################################
### Conversion from LLA to ECEF coordinates ###
###############################################

function ECEF(nodes::Dict{Int,OpenStreetMapX.LLA}, datum::OpenStreetMapX.Ellipsoid = OpenStreetMapX.WGS84)
    r = Dict{Int,OpenStreetMapX.ECEF}()
    sizehint!(r, ceil(Int, 1.5*length(nodes)))

    for (key, node) in nodes
        r[key] = OpenStreetMapX.ECEF(node, datum)
    end

    return r
end

###############################################
### Conversion from ECEF to LLA coordinates ###
###############################################

function LLA(nodes::Dict{Int,OpenStreetMapX.ECEF}, datum::OpenStreetMapX.Ellipsoid = OpenStreetMapX.WGS84)
    r = Dict{Int,OpenStreetMapX.LLA}()
    sizehint!(r, ceil(Int, 1.5*length(nodes)))

    for (key, node) in nodes
        r[key] = OpenStreetMapX.LLA(node, datum)
    end

    return r
end

######################################################
### Conversion from LLA or ECEF to ENU coordinates ###
######################################################

# Given a reference point
function ENU(nodes::Dict{Int,T}, lla_ref::OpenStreetMapX.LLA,
            datum::OpenStreetMapX.Ellipsoid = OpenStreetMapX.WGS84) where T<:Union{OpenStreetMapX.LLA,OpenStreetMapX.ECEF}
    r = Dict{Int,OpenStreetMapX.ENU}()
    sizehint!(r, ceil(Int, 1.5*length(nodes)))

    for (key, node) in nodes
        r[key] = OpenStreetMapX.ENU(node, lla_ref, datum)
    end

    return r
end

# Given Bounds
ENU(nodes::Dict{Int,T}, bounds::OpenStreetMapX.Bounds{OpenStreetMapX.LLA}, datum::OpenStreetMapX.Ellipsoid = OpenStreetMapX.WGS84) where T<:Union{OpenStreetMapX.LLA,OpenStreetMapX.ECEF} = OpenStreetMapX.ENU(nodes, OpenStreetMapX.center(bounds), datum)

###############################################
### Conversion from ENU to ECEF coordinates ###
###############################################

function ECEF(nodes::Dict{Int,OpenStreetMapX.ENU},lla_ref::OpenStreetMapX.LLA , datum::OpenStreetMapX.Ellipsoid = OpenStreetMapX.WGS84)
    r = Dict{Int,OpenStreetMapX.ECEF}()
    sizehint!(r, ceil(Int, 1.5*length(nodes)))

    for (key, node) in nodes
        r[key] = OpenStreetMapX.ECEF(node, lla_ref, datum)
    end

    return r
end

# Given Bounds
ECEF(nodes::Dict{Int,OpenStreetMapX.ENU}, bounds::Bounds{LLA}, datum::Ellipsoid = WGS84) = ECEF(nodes, OpenStreetMapX.center(bounds), datum)

###############################################
### Conversion from ENU to LLA coordinates ###
###############################################

function LLA(nodes::Dict{Int,OpenStreetMapX.ENU},lla_ref::OpenStreetMapX.LLA , datum::OpenStreetMapX.Ellipsoid = OpenStreetMapX.WGS84)
    r = Dict{Int,OpenStreetMapX.LLA}()
    sizehint!(r, ceil(Int, 1.5*length(nodes)))

    for (key, node) in nodes
        r[key] = OpenStreetMapX.LLA(node, lla_ref, datum)
    end

    return r
end

# Given Bounds
LLA(nodes::Dict{Int,OpenStreetMapX.ENU}, bounds::OpenStreetMapX.Bounds{OpenStreetMapX.LLA}, datum::OpenStreetMapX.Ellipsoid = OpenStreetMapX.WGS84) = OpenStreetMapX.LLA(nodes, OpenStreetMapX.center(bounds), datum)


function rkrp2tn(Ũ::Vector{Matrix{Float64}},ϵ::Float64)
    # repeated Kathti-Rao product into tensor network
    # Source: Batselier et al.: Tensor network subspace identification of polynomial state space models, Algorithm 2

    N    = length(Ũ);
    M    = size(Ũ[1],1);
    P    = size(Ũ[1],2);
    U    = Vector{Array{Float64,4}}(undef,N);
    U[1] = reshape(Ũ[N],(1,M,P,1));
    r1   = 1; err2 = 0;
    δ    = ϵ / sqrt(N-1) * norm(.*(Ũ));
    for i = 1:N-1
        T = reshape(U[i],(r1*M,P));
        Ttmp = KathriRao(Ũ[N-i],T,2)

        T = reshape(Ttmp,(r1*M,M*P));
        F = svd!(T);
        r2 = length(F.S);
        sv2 = cumsum(reverse(F.S).^2);
        tr  = findfirst(sv2 .> δ^2);
        if typeof(tr) == Nothing
            tr = length(sv2)-1;
        else
            tr = Int(tr)-1;
        end
        if tr > 0
            r2 = r2 - tr;
            err2 += sv2[tr];
        end

        U[i] = reshape(F.U[:,1:r2],(r1,M,1,r2));
        U[i+1] = reshape(diagm(F.S[1:r2])*F.Vt[1:r2,:],(r2,M,P,1));
        r1 = r2;
    end
    return MPT(U), err2
end


#f(p,x)=KL(p,softmax(x))

FKLsoftmax(p::Array{Float64,2},x::Array{Float64,2})=begin DN=prod(size(x));Z=sum(exp(x),1);aux=(Z,DN); return ((sum(p.*(log(p)-x))+sum(log(Z)))/DN,aux); end

function FKLsoftmax_inplace(value,auxvalue,p::Array{Float64,2},x::Array{Float64,2})
    DN=prod(size(x));Z=sum(exp(x),1);aux=(Z,DN)
    value=((sum(p.*(log(p)-x))+sum(log(Z)))/DN,aux)
end

function DKLsoftmax(derivativeIDX,f_c,faux_c,grad_c,grad_n,x::Array{Float64,2})
    if derivativeIDX==1
        grad_c.*(1+log(p)-x)
    elseif derivativeIDX==2
        grad_c.*(exp(x)./aux[1]-p)./aux[2]
    end
end

if PROC=="GPU"
end

Derivative[FKLsoftmax]=DKLsoftmax
Inplace[FKLsoftmax]=FKLsoftmax_inplace

KLsoftmax(p::ADnode,x::ADnode)=ADnode(FKLsoftmax,[p x])

export KLsoftmax


### A Pluto.jl notebook ###
# v0.17.5

using Markdown
using InteractiveUtils

# ╔═╡ c7edc362-1d55-4556-94e2-5ccae0fce152
begin
	using LinearAlgebra
	using Statistics
	using PlutoUI
	using Plots
	gr()
end

# ╔═╡ e91395c4-e4dd-4bf7-8efa-c48d05418bb4
using Polynomials

# ╔═╡ f16d832c-67ad-11ec-3474-d9f0f2241ff9
html"""

Happy holiday! Remember to take care of yourself and your loved ones!

"""

# ╔═╡ f16d834c-67ad-11ec-05db-f726c8f8c643
md""" # 機械学習帳
ここを参考にした.[機械学習帳](https://chokkan.github.io/mlnote/regression/01sra.html)
## 1. 単回帰
### (続き)1.4. 平均二乗誤差は$a$,$b$の関数
"""

# ╔═╡ 6ce3e88a-e10d-45c2-9722-aeed933931d9
begin
	D = [
		1 3
		3 6
		6 5
		8 7]

	X = D[:,1]
	Y = D[:,2]
end;

# ╔═╡ bc133d95-c122-49be-8789-01dd2b87ac0a
function calc_msr(a, b, D)
	xm = mean(D[:,1])
	ym = mean(D[:,2])
	x2m = mean(D[:,1].^2)
	y2m = mean(D[:,2].^2)
	xym = mean(D[:,1] .* D[:,2])
	return @. x2m * a^2 + b^2 + 2*xm *a *b - 2*xym *a - 2*ym*b + y2m
end

# ╔═╡ 86db1e1c-5164-4e3f-b877-f9c6f1a535c7
begin
	N = 100
	aa = range(-10,10, N)
	bb = range(-10,10, N)
end

# ╔═╡ c47e13d4-f0e9-46b0-b746-73563344487e
J = calc_msr(aa, bb', D)

# ╔═╡ f1896c70-c8ce-4d7f-832e-11a3eede0883
let
	plot(xlim=(-10,10), ylim=(-10,10), framestyle=:box, aspect_ratio=1)
	plot!(xticks=-10:5:10, yticks=-10.0:2.5:10.0)
	plot!(xlabel="a", ylabel="b", zlabel="L̂_D(a,b)")
	heatmap!(aa, bb, J'; colorbar_scale=:log10, clim=(10^(-1),10^4),
		fillcolor=cgrad(:viridis) )
	J_levels = [1., 10.0, 100.0, 1000.0]
	contour!(aa, bb, J'; lc=:red, lw=0.5, ls=:dash, levels= J_levels)
end

# ╔═╡ 02c50675-5c7e-4662-84fd-56cf5e34e666
md""" ### 1.5. 平均二乗残差を最小化する
単回帰の目的関数(平均二乗残差)は
```math
L̂_D(a,b) = \frac{N}{1} \sum_{i=1}^{N} ϵ_i^2 = \frac{1}{N} \sum_{i=1}^{N} (y_i - ŷ_i)^2 = \frac{1}{N} \sum_{i=1}^{N} (y_i - ax_i - b)^2
```
である。
上式を $b$ で偏微分すると
```math
b = ȳ - ax̄
```
となる。 単回帰の傾き $a$ を求める式
```math
a = \frac{ \overline{xy} - (x̄)(ȳ)}{ \overline{x^2} - (x̄)^2}
	= \frac{  \rm{Cov[} \it{X, Y} \rm{]} } {\rm{Cov[} \it{X, X} \rm{]}}
    = \frac{  \rm{Cov[} \it{X, Y} \rm{]} } {\rm{Var[} \it{X} \rm{]}}
```

"""

# ╔═╡ 0367c8ba-1c50-43ea-828f-fb0cbaefe691
function set_plot()
	plot(size=(500,500), xlim=(0,10), ylim=(0,10), framestyle=:box, legend=:topleft)
	plot!(xlabel="x", ylabel="y", gridalpha=0.5)
end

# ╔═╡ 4765fa67-4e96-4cc0-85fa-266128fb8fae
function plot_y(X, Y, colormap)
	scatter!(X, Y; c=colormap,  markerstrokewidth=0, label="")
end

# ╔═╡ 8f419e32-93f9-4067-9461-e0e2f69a723a
function plot_ŷ(a, b, offset, colormap)

	offset_ŷ = offset

	a, b = a, b
	f(x, a, b) = a*x + b
	Ŷ = f.(X, a, b)
	x = [0, 10]

	a = round(a; digits=3)
	b = round(b; digits=3)

	label = (a == 1) ? "x + $(b))" : "$(a)x + $(b)"
	scatter!(D[:,1], Ŷ; c=colormap, shape=:star5, ms=6, markerstrokewidth=0, label="")
	plot!(x -> f(x, a, b); c=:black, lw=2, label=label)

	texts_ŷ = ["(x₁,ŷ₁)", "(x₂,ŷ₂)", "(x₃,ŷ₃)", "(x₄,ŷ₄)"]
	for i in 1:length(colormap)
		annotate!([(X[i],Ŷ[i]+offset_ŷ[i],text(texts_ŷ[i], 10,
			:center, colormap[i]))])
	end
	plot!()
end

# ╔═╡ 59bd7898-2dcf-4c23-815b-abf13a6954d0
function plot_error(a, b, offset, colormap)

	f(x, a, b) = a*x + b
	Ŷ = f.(X, a, b)

	offset_ϵ = offset
	x_ϵ = X + offset_ϵ
	y_ϵ = (Y + Ŷ) / 2
	texts_ϵ = ["ϵ₁", "ϵ₂", "ϵ₃", "ϵ₄"]

	for i in 1:length(colormap)
		plot!([(X[i], Ŷ[i]), (X[i], Y[i])]; c=colormap[i], ls=:dash, label="")
		annotate!([(x_ϵ[i],y_ϵ[i],text(texts_ϵ[i], 10, :center, colormap[i]))])
	end
	plot!()
end

# ╔═╡ e776c006-777f-4a6c-8a3e-a273820a0ad8
const colormap = [:red, :blue, :green, :orange]

# ╔═╡ 75364a95-c8ef-40d3-8373-6176540ceaf8
let
	# パラメータ a, b の計算
	x̄ = D[:,1] |> mean
	ȳ = D[:,2] |> mean

	Cov_XY = 1 / length(X) * sum( (X .- x̄) .* (Y .- ȳ) )
	Var_X =  1 / length(X) * sum( (X .- x̄).^2 )

	a = Cov_XY / Var_X
	b = ȳ - a* x̄

	# 可視化
	offset_y_new = [-1., 1.8, -1.2, 0.7]
	offset_ϵ_new = [0.3, -0.3, 0.3, -0.3]

	set_plot()
	plot_y(X, Y, colormap)
	plot_ŷ(a, b, offset_y_new, colormap)
	plot_error(a, b, offset_ϵ_new, colormap)

end

# ╔═╡ 792c62f0-222b-4048-ad6a-9655db74f080
md""" ### 1.6. 回帰直線の性質
"""

# ╔═╡ 0f1d5e27-f04c-47f4-bcff-bc9deea6eda2
function plot_means(X, Y)
	x̄ = D[:,1] |> mean
	ȳ = D[:,2] |> mean

	vline!([x̄]; c=:black, lw=1.5, ls=:dash, label="x̄")
	hline!([ȳ]; c=:black, lw=1.5, ls=:dot, label="ȳ")

end

# ╔═╡ 4c2841e2-2e5e-47ca-987a-0e4c5557f023
function plot_regions(X, Y)
	x̄ = D[:,1] |> mean
	ȳ = D[:,2] |> mean

	rectangle(x, y, w, h) = Shape(x .+ [0,w,w,0], y .+ [0,0,h,h])
	plot!(rectangle(0, 0, x̄, ȳ), color=:red, alpha=0.1, label="")
	plot!(rectangle(x̄, ȳ, 10-x̄, 10-ȳ), color=:red, alpha=0.1, label="")

	plot!(rectangle(0, ȳ, x̄, ȳ), color=:blue, alpha=0.1, label="")
	plot!(rectangle(x̄, 0, 10-x̄, ȳ), color=:blue, alpha=0.1, label="")

end

# ╔═╡ dde67d9a-5c75-4477-bbd0-3eefc7b5ef13
let
	set_plot()
	plot_y(X, Y, colormap)
	plot_means(X, Y)
	plot_regions(X, Y)
end

# ╔═╡ c0a1bf99-d350-47ee-9a01-763acdcdb63a
function plot_regions2(X, Y, colormap)
	x̄ = D[:,1] |> mean
	ȳ = D[:,2] |> mean

	rectangle(x, y, w, h) = Shape(x .+ [0,w,w,0], y .+ [0,0,h,h])

	xs = [D[1,1], D[2,1], x̄, x̄]
	ys = [D[1,2], D[2,2], D[3,2], ȳ]
	ws = [x̄-D[1,1], x̄-D[2,1], D[3,1]-x̄, D[4,1]-x̄]
	hs = [ȳ-D[1,2], ȳ-D[2,2], ȳ-D[3,2], D[4,2]-ȳ]

	for i in 1:length(xs)
		plot!(rectangle(xs[i], ys[i], ws[i], hs[i]), color=colormap[i],
			alpha=0.1, label="")
	end
	plot!()
end

# ╔═╡ 407a1c1b-bb2d-4188-997b-17565ef4431d
let
	set_plot()
	plot_y(X, Y, colormap)
	plot_means(X, Y)
	plot_regions2(X, Y, colormap)
end

# ╔═╡ cc5a8abc-ff3e-4693-94fe-79a73aaf2a19
md""" #### 1.6.3. 回帰直線は観測データの重心を通る
"""

# ╔═╡ 6da0bfa7-bd9e-4463-9057-bd40c56019eb
function plot_slope(x̄, ȳ, a, b)

	Cov_XY = 1 / length(X) * sum( (X .- x̄) .* (Y .- ȳ) )
	Var_X =  1 / length(X) * sum( (X .- x̄).^2 )

	a = Cov_XY / Var_X
	b = ȳ - a* x̄

	# horizontal
	plot!([0, x̄],[b, b]; lw=2, color=:brown, label="")
	annotate!([(x̄/2,b-0.3,text("x̄", 10, :center, color=:brown))])
	# vertical
	plot!([x̄, x̄], [b, ȳ]; lw=2, color=:brown, label="")
	annotate!([(x̄+0.3, b+(ȳ-b)/2,text("ax̄", 10, :center, color=:brown))])
end

# ╔═╡ f4215be2-c8af-4f7e-92cb-76b338714f1a
function plot_ŷ_only(a, b)

	a, b = a, b
	f(x, a, b) = a*x + b
	Ŷ = f.(X, a, b)
	x = [0, 10]

	a = round(a; digits=3)
	b = round(b; digits=3)

	label = (a == 1) ? "x + $(b))" : "$(a)x + $(b)"
	plot!(x -> f(x, a, b); c=:black, lw=2, label=label)

end

# ╔═╡ c1635018-bff4-4245-91ff-278fda46f3a0
let

	# パラメータ a, b の計算
	x̄ = D[:,1] |> mean
	ȳ = D[:,2] |> mean

	Cov_XY = 1 / length(X) * sum( (X .- x̄) .* (Y .- ȳ) )
	Var_X =  1 / length(X) * sum( (X .- x̄).^2 )

	a = Cov_XY / Var_X
	b = ȳ - a* x̄

	# ----- 可視化 -----
	# 初期化
	set_plot()
	# データ点をプロット
	plot_y(X, Y, colormap)
	# xとyの平均の直線をプロット
	plot_means(X, Y)
	# +(赤)、 -(青)になる領域をプロット
	plot_regions(X, Y)
	# スロープ部分(茶色)をプロット
	plot_slope(x̄, ȳ, a, b)
	# 推定値 ŷ をプロット
	plot_ŷ_only(a, b)
end

# ╔═╡ fcd91d11-e8e0-4419-a992-47f82b39b393
md""" #### 1.8. 単回帰の実施例
"""

# ╔═╡ e2ae904b-c4aa-4f3c-ad18-58b0fabb6877
begin
	mmt = [
		9.1, 11.2, 12.3, 18.9, 22.2, 26. , 30.9, 31.2, 28.8, 23. , 18.3,
       11.1,  8.3,  9.1, 12.5, 18.5, 23.6, 24.8, 30.1, 33.1, 29.8, 23. ,
       16.3, 11.2,  9.6, 10.3, 16.4, 19.2, 24.1, 26.5, 31.4, 33.2, 28.8,
	   23. , 17.4, 12.1, 10.6,  9.8, 14.5, 19.6, 24.7, 26.9, 30.5, 31.2,
	   26.9, 23. , 17.4, 11. , 10.4, 10.4, 15.5, 19.3, 26.4, 26.4, 30.1,
	   30.5, 26.4, 22.7, 17.8, 13.4, 10.6, 12.2, 14.9, 20.3, 25.2, 26.3,
	   29.7, 31.6, 27.7, 22.6, 15.5, 13.8, 10.8, 12.1, 13.4, 19.9, 25.1,
	   26.4, 31.8, 30.4, 26.8, 20.1, 16.6, 11.1,  9.4, 10.1, 16.9, 22.1,
	   24.6, 26.6, 32.7, 32.5, 26.6, 23. , 17.7, 12.1, 10.3, 11.6, 15.4,
	   19. , 25.3, 25.8, 27.5, 32.8, 29.4, 23.3, 17.7, 12.6, 11.1, 13.3,
	   16. , 18.2, 24. , 27.5, 27.7, 34.1, 28.1, 21.4, 18.6, 12.3
	]

	spend = [
	  463.,  360.,  380.,  584.,  763.,  886., 1168., 1325.,  847.,
      542.,  441.,  499.,  363.,  327.,  414.,  545.,  726.,  847.,
     1122., 1355.,  916.,  571.,  377.,  465.,  377.,  362.,  518.,
      683.,  838., 1012., 1267., 1464., 1000.,  629.,  448.,  466.,
      404.,  343.,  493.,  575.,  921., 1019., 1149., 1303.,  805.,
      739.,  587.,  561.,  486.,  470.,  564.,  609.,  899.,  946.,
     1295., 1325.,  760.,  667.,  564.,  633.,  478.,  450.,  567.,
      611.,  947.,  962., 1309., 1307.,  930.,  668.,  496.,  650.,
      506.,  423.,  531.,  672.,  871.,  986., 1368., 1319.,  924.,
      716.,  651.,  708.,  609.,  535.,  717.,  890., 1054., 1077.,
     1425., 1378.,  900.,  725.,  554.,  542.,  561.,  459.,  604.,
      745., 1105.,  973., 1263., 1533., 1044.,  821.,  621.,  601.,
      549.,  572.,  711.,  819., 1141., 1350., 1285., 1643., 1133.,
      784.,  682.,  587.
	]
end;

# ╔═╡ 37558fb1-0ea2-4aa5-bdae-d1b54d237812
function plot_temp_vs_spend()
	plot(title="MMT vs. Spend for Icecream", framestyle=:box)
	plot!(xlabel="month mean maximum temperature [̇Degree]", ylabel="spend [Yen]")
	plot!(xlim=(0,35), ylim=(-250,2000), yticks=-250:250:2000, gridalpha=0.5)
	scatter!(mmt, spend; marker=:dot, ms=3, label="")
end

# ╔═╡ 569e9185-fb79-4a0b-b32d-b6e20dee29e8
let
	ŷ = fit(mmt, spend, 1)
	x = range(0, 35, length=100)
	plot_temp_vs_spend()
	plot!(x, ŷ.(x); lw=2, color=:red, label="")
end

# ╔═╡ e802792b-052f-4bd5-976a-2f5983fe7e06

# ╔═╡ 00000000-0000-0000-0000-000000000001
PLUTO_PROJECT_TOML_CONTENTS = """
[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
PlutoUI = "7f904dfe-b85e-4ff6-b463-dae2292396a8"
Polynomials = "f27b6e38-b328-58d1-80ce-0feddd5e7a45"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"

[compat]
Plots = "~1.25.3"
PlutoUI = "~0.7.27"
Polynomials = "~2.0.22"
"""

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

julia_version = "1.7.1"
manifest_format = "2.0"

[[deps.AbstractPlutoDingetjes]]
deps = ["Pkg"]
git-tree-sha1 = "8eaf9f1b4921132a4cff3f36a1d9ba923b14a481"
uuid = "6e696c72-6542-2067-7265-42206c756150"
version = "1.1.4"

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

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

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

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

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

[[deps.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 = "4b859a208b2397a7a623a03449e4636bdb17bcf2"
uuid = "83423d85-b0ee-5818-9007-b63ccbeb887a"
version = "1.16.1+1"

[[deps.ChainRulesCore]]
deps = ["Compat", "LinearAlgebra", "SparseArrays"]
git-tree-sha1 = "4c26b4e9e91ca528ea212927326ece5918a04b47"
uuid = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
version = "1.11.2"

[[deps.ChangesOfVariables]]
deps = ["ChainRulesCore", "LinearAlgebra", "Test"]
git-tree-sha1 = "bf98fa45a0a4cee295de98d4c1462be26345b9a1"
uuid = "9e997f8a-9a97-42d5-a9f1-ce6bfc15e2c0"
version = "0.1.2"

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

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

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

[[deps.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 = "44c37b4636bc54afac5c574d2d02b625349d6582"
uuid = "34da2185-b29b-5c13-b0c7-acf172513d20"
version = "3.41.0"

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

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

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

[[deps.DataStructures]]
deps = ["Compat", "InteractiveUtils", "OrderedCollections"]
git-tree-sha1 = "3daef5523dd2e769dad2365274f760ff5f282c7d"
uuid = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
version = "0.18.11"

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

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

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

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

[[deps.DocStringExtensions]]
deps = ["LibGit2"]
git-tree-sha1 = "b19534d1895d702889b219c382a6e18010797f0b"
uuid = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
version = "0.8.6"

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

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

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

[[deps.ExprTools]]
git-tree-sha1 = "b7e3d17636b348f005f11040025ae8c6f645fe92"
uuid = "e2ba6199-217a-4e67-a87a-7c52f15ade04"
version = "0.1.6"

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

[[deps.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"

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

[[deps.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"

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

[[deps.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"

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

[[deps.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"

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

[[deps.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 = "f97acd98255568c3c9b416c5a3cf246c1315771b"
uuid = "d2c73de3-f751-5644-a686-071e5b155ba9"
version = "0.63.0+0"

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

[[deps.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"

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

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

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

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

[[deps.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"

[[deps.Hyperscript]]
deps = ["Test"]
git-tree-sha1 = "8d511d5b81240fc8e6802386302675bdf47737b9"
uuid = "47d2ed2b-36de-50cf-bf87-49c2cf4b8b91"
version = "0.0.4"

[[deps.HypertextLiteral]]
git-tree-sha1 = "2b078b5a615c6c0396c77810d92ee8c6f470d238"
uuid = "ac1192a8-f4b3-4bfe-ba22-af5b92cd3ab2"
version = "0.9.3"

[[deps.IOCapture]]
deps = ["Logging", "Random"]
git-tree-sha1 = "f7be53659ab06ddc986428d3a9dcc95f6fa6705a"
uuid = "b5f81e59-6552-4d32-b1f0-c071b021bf89"
version = "0.2.2"

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

[[deps.InlineStrings]]
deps = ["Parsers"]
git-tree-sha1 = "8d70835a3759cdd75881426fced1508bb7b7e1b6"
uuid = "842dd82b-1e85-43dc-bf29-5d0ee9dffc48"
version = "1.1.1"

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

[[deps.Intervals]]
deps = ["Dates", "Printf", "RecipesBase", "Serialization", "TimeZones"]
git-tree-sha1 = "323a38ed1952d30586d0fe03412cde9399d3618b"
uuid = "d8418881-c3e1-53bb-8760-2df7ec849ed5"
version = "1.5.0"

[[deps.InverseFunctions]]
deps = ["Test"]
git-tree-sha1 = "a7254c0acd8e62f1ac75ad24d5db43f5f19f3c65"
uuid = "3587e190-3f89-42d0-90ee-14403ec27112"
version = "0.1.2"

[[deps.IrrationalConstants]]
git-tree-sha1 = "7fd44fd4ff43fc60815f8e764c0f352b83c49151"
uuid = "92d709cd-6900-40b7-9082-c6be49f344b6"
version = "0.1.1"

[[deps.IterTools]]
git-tree-sha1 = "fa6287a4469f5e048d763df38279ee729fbd44e5"
uuid = "c8e1da08-722c-5040-9ed9-7db0dc04731e"
version = "1.4.0"

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

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

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

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

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

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

[[deps.LaTeXStrings]]
git-tree-sha1 = "f2355693d6778a178ade15952b7ac47a4ff97996"
uuid = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f"
version = "1.3.0"

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

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

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

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

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

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

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

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

[[deps.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"

[[deps.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"

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

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

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

[[deps.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"

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

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

[[deps.LogExpFunctions]]
deps = ["ChainRulesCore", "ChangesOfVariables", "DocStringExtensions", "InverseFunctions", "IrrationalConstants", "LinearAlgebra"]
git-tree-sha1 = "e5718a00af0ab9756305a0392832c8952c7426c1"
uuid = "2ab3a3ac-af41-5b50-aa03-7779005ae688"
version = "0.3.6"

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

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

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

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

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

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

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

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

[[deps.Mocking]]
deps = ["Compat", "ExprTools"]
git-tree-sha1 = "29714d0a7a8083bba8427a4fbfb00a540c681ce7"
uuid = "78c3b35d-d492-501b-9361-3d52fe80e533"
version = "0.7.3"

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

[[deps.MutableArithmetics]]
deps = ["LinearAlgebra", "SparseArrays", "Test"]
git-tree-sha1 = "73deac2cbae0820f43971fad6c08f6c4f2784ff2"
uuid = "d8a4904e-b15c-11e9-3269-09a3773c0cb0"
version = "0.3.2"

[[deps.NaNMath]]
git-tree-sha1 = "f755f36b19a5116bb580de457cda0c140153f283"
uuid = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3"
version = "0.3.6"

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

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

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

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

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

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

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

[[deps.Parsers]]
deps = ["Dates"]
git-tree-sha1 = "d7fa6237da8004be601e19bd6666083056649918"
uuid = "69de0a69-1ddd-5017-9359-2bf0b02dc9f0"
version = "2.1.3"

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

[[deps.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"

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

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

[[deps.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", "UnicodeFun", "Unzip"]
git-tree-sha1 = "7eda8e2a61e35b7f553172ef3d9eaa5e4e76d92e"
uuid = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
version = "1.25.3"

[[deps.PlutoUI]]
deps = ["AbstractPlutoDingetjes", "Base64", "ColorTypes", "Dates", "Hyperscript", "HypertextLiteral", "IOCapture", "InteractiveUtils", "JSON", "Logging", "Markdown", "Random", "Reexport", "UUIDs"]
git-tree-sha1 = "fed057115644d04fba7f4d768faeeeff6ad11a60"
uuid = "7f904dfe-b85e-4ff6-b463-dae2292396a8"
version = "0.7.27"

[[deps.Polynomials]]
deps = ["Intervals", "LinearAlgebra", "MutableArithmetics", "RecipesBase"]
git-tree-sha1 = "a2515a5baa0b6954e96e10a8dc3f6855b4dedb97"
uuid = "f27b6e38-b328-58d1-80ce-0feddd5e7a45"
version = "2.0.22"

[[deps.Preferences]]
deps = ["TOML"]
git-tree-sha1 = "2cf929d64681236a2e074ffafb8d568733d2e6af"
uuid = "21216c6a-2e73-6563-6e65-726566657250"
version = "1.2.3"

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

[[deps.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"

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

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

[[deps.RecipesBase]]
git-tree-sha1 = "6bf3f380ff52ce0832ddd3a2a7b9538ed1bcca7d"
uuid = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
version = "1.2.1"

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

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

[[deps.Requires]]
deps = ["UUIDs"]
git-tree-sha1 = "8f82019e525f4d5c669692772a6f4b0a58b06a6a"
uuid = "ae029012-a4dd-5104-9daa-d747884805df"
version = "1.2.0"

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

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

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

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

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

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

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

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

[[deps.StaticArrays]]
deps = ["LinearAlgebra", "Random", "Statistics"]
git-tree-sha1 = "3c76dde64d03699e074ac02eb2e8ba8254d428da"
uuid = "90137ffa-7385-5640-81b9-e52037218182"
version = "1.2.13"

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

[[deps.StatsAPI]]
git-tree-sha1 = "d88665adc9bcf45903013af0982e2fd05ae3d0a6"
uuid = "82ae8749-77ed-4fe6-ae5f-f523153014b0"
version = "1.2.0"

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

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

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

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

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

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

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

[[deps.TimeZones]]
deps = ["Dates", "Downloads", "InlineStrings", "LazyArtifacts", "Mocking", "Printf", "RecipesBase", "Serialization", "Unicode"]
git-tree-sha1 = "ce5aab0b0146b81efefae52f13002e19c2af57ac"
uuid = "f269a46b-ccf7-5d73-abea-4c690281aa53"
version = "1.7.0"

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

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

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

[[deps.UnicodeFun]]
deps = ["REPL"]
git-tree-sha1 = "53915e50200959667e78a92a418594b428dffddf"
uuid = "1cfade01-22cf-5700-b092-accc4b62d6e1"
version = "0.4.1"

[[deps.Unzip]]
git-tree-sha1 = "34db80951901073501137bdbc3d5a8e7bbd06670"
uuid = "41fe7b60-77ed-43a1-b4f0-825fd5a5650d"
version = "0.1.2"

[[deps.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"

[[deps.Wayland_protocols_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "66d72dc6fcc86352f01676e8f0f698562e60510f"
uuid = "2381bf8a-dfd0-557d-9999-79630e7b1b91"
version = "1.23.0+0"

[[deps.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"

[[deps.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"

[[deps.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"

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

[[deps.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"

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

[[deps.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"

[[deps.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"

[[deps.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"

[[deps.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"

[[deps.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"

[[deps.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"

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

[[deps.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"

[[deps.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"

[[deps.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"

[[deps.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"

[[deps.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"

[[deps.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"

[[deps.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"

[[deps.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"

[[deps.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"

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

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

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

[[deps.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"

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

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

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

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

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

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

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

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

[[deps.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:
# ╟─f16d832c-67ad-11ec-3474-d9f0f2241ff9
# ╠═c7edc362-1d55-4556-94e2-5ccae0fce152
# ╠═f16d834c-67ad-11ec-05db-f726c8f8c643
# ╠═6ce3e88a-e10d-45c2-9722-aeed933931d9
# ╠═bc133d95-c122-49be-8789-01dd2b87ac0a
# ╠═86db1e1c-5164-4e3f-b877-f9c6f1a535c7
# ╠═c47e13d4-f0e9-46b0-b746-73563344487e
# ╠═f1896c70-c8ce-4d7f-832e-11a3eede0883
# ╠═02c50675-5c7e-4662-84fd-56cf5e34e666
# ╠═0367c8ba-1c50-43ea-828f-fb0cbaefe691
# ╠═4765fa67-4e96-4cc0-85fa-266128fb8fae
# ╠═8f419e32-93f9-4067-9461-e0e2f69a723a
# ╠═59bd7898-2dcf-4c23-815b-abf13a6954d0
# ╠═e776c006-777f-4a6c-8a3e-a273820a0ad8
# ╠═75364a95-c8ef-40d3-8373-6176540ceaf8
# ╠═792c62f0-222b-4048-ad6a-9655db74f080
# ╠═0f1d5e27-f04c-47f4-bcff-bc9deea6eda2
# ╠═4c2841e2-2e5e-47ca-987a-0e4c5557f023
# ╠═dde67d9a-5c75-4477-bbd0-3eefc7b5ef13
# ╠═c0a1bf99-d350-47ee-9a01-763acdcdb63a
# ╠═407a1c1b-bb2d-4188-997b-17565ef4431d
# ╠═cc5a8abc-ff3e-4693-94fe-79a73aaf2a19
# ╠═6da0bfa7-bd9e-4463-9057-bd40c56019eb
# ╠═f4215be2-c8af-4f7e-92cb-76b338714f1a
# ╠═c1635018-bff4-4245-91ff-278fda46f3a0
# ╠═fcd91d11-e8e0-4419-a992-47f82b39b393
# ╠═e2ae904b-c4aa-4f3c-ad18-58b0fabb6877
# ╠═37558fb1-0ea2-4aa5-bdae-d1b54d237812
# ╠═e91395c4-e4dd-4bf7-8efa-c48d05418bb4
# ╠═569e9185-fb79-4a0b-b32d-b6e20dee29e8
# ╠═e802792b-052f-4bd5-976a-2f5983fe7e06
# ╟─00000000-0000-0000-0000-000000000001
# ╟─00000000-0000-0000-0000-000000000002


## Marker style

#md # ![FILE_NAME.png](images/FILE_NAME.png)

## ref https://jp.mathworks.com/matlabcentral/fileexchange/35231-matlab-plot-gallery-standard-plot-markers?focused=6792907&tab=example

using SpecialFunctions

x = 0:0.2:10;
y0 = besselj.(0,x);
y1 = besselj.(1,x);
y2 = besselj.(2,x);
y3 = besselj.(3,x);
y4 = besselj.(4,x);

plot(x, [y0 y1 y2 y3 y4],
    marker=[:circle :rect :star5 :diamond :hexagon],
    line=false)

#-

Plots.supported_markers()


@testset "Cycle Basis" begin

    function evaluate(x,y)
        x_sorted = sort(sort.(x))
        y_sorted = sort(sort.(y))
        @test x_sorted == y_sorted
    end

    # No Edges
    ex = Graph(1)
    expected_cyclebasis = Array{Int64,1}[]
    @testset "no edges" for g in testgraphs(ex)
        ex_cyclebasis = @inferred cycle_basis(g)
        @test isempty(ex_cyclebasis)
    end

    # Only one self-edge
    elist = [(1,1)]
    ex = Graph(SimpleEdge.(elist))
    expected_cyclebasis = Array{Int64,1}[[1]]
    @testset "one self-edge" for g in testgraphs(ex)
        ex_cyclebasis = cycle_basis(g)
        evaluate(ex_cyclebasis,expected_cyclebasis)
    end

    # Graph with one cycle
    elist = [(1,2),(2,3),(3,4),(4,1),(1,5)]
    ex = Graph(SimpleEdge.(elist))
    expected_cyclebasis = Array{Int64,1}[
        [1,2,3,4] ]
    @testset "one cycle" for g in testgraphs(ex)
        ex_cyclebasis = cycle_basis(g)
        evaluate(ex_cyclebasis,expected_cyclebasis)
    end

    # Graph with 2 of 3 cycles forming a basis
    elist = [(1,2),(1,3),(2,3),(2,4),(3,4)]
    ex = Graph(SimpleEdge.(elist))
    expected_cyclebasis = Array{Int64,1}[
        [2,3,4],
        [2,1,3] ]
    @testset "2 of 3 cycles w/ basis" for g in testgraphs(ex)
        ex_cyclebasis = cycle_basis(g)
        evaluate(ex_cyclebasis,expected_cyclebasis)
    end

    # Testing root argument
    elist = [(1,2),(1,3),(2,3),(2,4),(3,4),(1,5),(5,6),(6,4)]
    ex = Graph(SimpleEdge.(elist))
    expected_cyclebasis = Array{Int64,1}[
        [2, 4, 3],
        [1, 5, 6, 4, 3],
        [1, 2, 3] ]
    @testset "root argument" for g in testgraphs(ex)
        ex_cyclebasis = @inferred cycle_basis(g,3)
        evaluate(ex_cyclebasis,expected_cyclebasis)
    end

    @testset "two isolated cycles" begin
        ex = blockdiag(cycle_graph(3), cycle_graph(4))
        expected_cyclebasis = [[1, 2, 3], [4, 5, 6, 7]]
        for g in testgraphs(ex)
            found_cyclebasis = @inferred cycle_basis(g)
            evaluate(expected_cyclebasis, found_cyclebasis)
        end
    end
end


export gradtest, gradtest_assembleStiffAndForce

@doc raw"""
    gradtest(f::Function, x0::Array{Float64}; scale::Float64 = 1.0)

Testing the gradients of a vector function `f`.
Here `x0` is $n$-dimensional vector, `f` takes an $n$-dimensional vector as inputs, and
outputs a $m$ dimensional vector. The function tests the second order convergence of `f`
```math
\|f(x_0+\gamma c) - f(x_0) - \gamma \nabla f(x_0) c\|_2 = \mathcal{O}(\gamma^2)
```
"""
function gradtest(f::Function, x0::Array{Float64}; scale::Float64 = 1.0)
    v0 = rand(Float64,size(x0))
    γs = scale ./10 .^(1:5)
    err2 = []
    err1 = []
    f0, J = f(x0)
    for i = 1:5
        f1, _ = f(x0+γs[i]*v0)
        push!(err1, norm(f1-f0))
        # @show f1, f0, 2γs[i]*J*v0
        push!(err2, norm(f1-f0-γs[i]*J*v0))
        # push!(err2, norm((f1-f2)/(2γs[i])-J*v0))
        # #@show "test ", f1, f2, f1-f2
    end
    close("all")
    loglog(γs, err2, label="Automatic Differentiation")
    loglog(γs, err1, label="Finite Difference")
    loglog(γs, γs.^2 * 0.5*abs(err2[1])/γs[1]^2, "--",label="\$\\mathcal{O}(\\gamma^2)\$")
    loglog(γs, γs * 0.5*abs(err1[1])/γs[1], "--",label="\$\\mathcal{O}(\\gamma)\$")
    plt.gca().invert_xaxis()
    legend()
    println("Finite difference: $err1")
    println("Automatic differentiation: $err2")
    return err1, err2
end

function gradtest_assembleStiffAndForce(domain::Domain; scale::Float64 = 1.0)
    function f(s)
        domain.state[domain.dof_to_eq] = s
        rhs, K = assembleStiffAndForce(domain, 0.0)
        return rhs, K
    end
    gradtest(f, rand(domain.neqs), scale=scale)
end