Packages
chi2fit
3.0.0
3.1.0
3.0.1
3.0.0
2.1.7
2.1.6
2.1.5
retired
2.1.4
retired
2.1.3
retired
2.1.2
retired
2.1.1
retired
2.1.0
retired
2.0.2
2.0.1
2.0.0
1.3.0
1.1.0
1.0.3
1.0.2
1.0.1
1.0.0
1.0.0-beta.11
1.0.0-beta.10
1.0.0-beta.9
1.0.0-beta.8
1.0.0-beta.7
1.0.0-beta.6
1.0.0-beta.5
1.0.0-beta.4
1.0.0-beta.3
1.0.0-beta.2
1.0.0-beta.1
1.0.0-beta
1.0.0-alpha
0.9.5
0.9.4
0.9.3
0.9.2
0.9.1
0.9.0
0.8.11
0.8.10
0.8.9
0.8.8
0.8.7-alpha.2
0.8.7-alpha
0.8.6
0.8.5
0.8.4
0.8.3
0.8.2
0.8.1
0.8.0
0.7.8
0.7.7
0.7.6
0.7.5
0.7.4
0.7.3
0.7.3-1
0.7.2
0.7.1
0.7.0
0.6.7
0.6.6
0.6.5
0.6.3
0.6.0
0.5.2
0.5.1
0.5.0
0.4.0
0.3.1
0.3.0
0.2.0
Provides functions for fast matrix inversion, creation of empirical CDF from sample data including handling of asymmetric errors, and fitting to a funtion using chi-squared. The fitting procedure return the full covariance matrix describing the fitted parameters.
Current section
Files
Jump to
Current section
Files
lib/distributions/trimodal.ex
defmodule Chi2fit.Distribution.TriModal do
# Copyright 2020 Pieter Rijken
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
@moduledoc """
Bimodal distribution.
"""
defstruct [:weights, :distribs, name: "trimodal"]
@type t() :: %__MODULE__{
weights: [number()] | nil,
distribs: [Chi2fit.Distribution.t()] | nil,
name: String.t
}
end
defimpl Chi2fit.Distribution, for: Chi2fit.Distribution.TriModal do
alias Chi2fit.Distribution, as: D
import D.TriModal
alias D.TriModal
def skewness(%TriModal{distribs: nil}), do: raise(ArithmeticError, "Skewness not supported for TriModal distribution")
def kurtosis(%TriModal{distribs: nil}), do: raise(ArithmeticError, "Kurtosis not supported for TriModal distribution")
def size(%TriModal{distribs: distribs}), do: 2 + (distribs|>Enum.map(&D.size(&1))|>Enum.sum)
def cdf(%TriModal{weights: nil, distribs: distribs}) do
fn x,[w1,w2|parameters] ->
distribs
|> Enum.map(&{&1,D.size(&1)})
|> Enum.reduce({[],parameters},fn {d,size},{result,rest} -> {[{d,Enum.take(rest,size)}|result],Enum.drop(rest,size)} end)
|> elem(0)
|> Enum.reverse()
|> Enum.zip([w1,(1-w1)*w2,(1-w1)*(1-w2)])
|> Enum.map(fn {tup,p} -> Tuple.to_list(tup) ++ [p] end)
|> Enum.map(fn [d,pars,p] -> p*D.cdf(d).(x,pars) end)
|> Enum.sum
end
end
def pdf(%TriModal{weights: nil, distribs: distribs}) do
fn x,[w1,w2|parameters] ->
distribs
|> Enum.map(&{&1,D.size(&1)})
|> Enum.reduce({[],parameters},fn {d,size},{result,rest} -> {[{d,Enum.take(rest,size)}|result],Enum.drop(rest,size)} end)
|> elem(0)
|> Enum.reverse()
|> Enum.zip([w1,(1-w1)*w2,(1-w1)*(1-w2)])
|> Enum.map(fn {tup,p} -> Tuple.to_list(tup) ++ [p] end)
|> Enum.map(fn [d,pars,p] -> p*D.pdf(d).(x,pars) end)
|> Enum.sum
end
end
def random(%TriModal{weights: [w1,w2], distribs: distribs}) do
distribs
|> Enum.zip([w1,(1-w1)*w2,(1-w1)*(1-w2)])
|> Enum.map(fn {d,p} -> p*D.random(d) end)
|> Enum.sum
end
def name(model), do: model.name
end
defimpl Inspect, for: Chi2fit.Distribution.TriModal do
import Inspect.Algebra
def inspect(dict, opts) do
case {dict.weights,dict.distribs} do
{_,nil} ->
"#TriModal<>"
{nil,[d1,d2,d3]} ->
concat ["#TriModal<", to_doc([d1,d2,d3], opts), ">"]
{[w1,w2],[d1,d2,d3]} ->
concat ["#TriModal<", "weights=(#{w1},#{(1-w1)*w2},#{(1-w1)*(1-w2)})", "distribs=", to_doc([d1,d2,d3], opts), ">"]
end
end
end