Packages
chi2fit
1.0.0-beta.9
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/wald.ex
defmodule Distribution.Wald do
# Copyright 2019 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 """
Wald or Inverse Gauss distribution.
"""
defstruct [:pars]
@type t() :: %__MODULE__{
pars: [number()] | nil,
}
end
defimpl Distribution, for: Distribution.Wald do
import Distribution.Wald
alias Distribution.Wald
@spec phi(x :: float) :: float
defp phi(x) do
d = %Distribution.Normal{pars: [0.0, 1.0]}
Distribution.cdf(d).(x)
end
@spec wald(mu::number(),lambda::number()) :: ((...) -> number)
defp wald(mu,lambda) when is_number(mu) and is_number(lambda) do
d = %Distribution.Normal{pars: [0.0,1.0]}
fn () ->
w = Distribution.random(d)
y = w*w
x = mu + mu*mu*y/2/lambda - mu/2/lambda*:math.sqrt(4*mu*lambda*y + mu*mu*y*y)
z = :rand.uniform()
if z <= mu/(mu+x), do: x, else: mu*mu/x
end
end
defp wald([avg: average],lambda), do: wald(average,lambda)
@spec waldCDF(number,number) :: (number -> number)
defp waldCDF(mu,_) when mu < 0, do: raise ArithmeticError, "Wald is only defined for positive average"
defp waldCDF(_,lambda) when lambda < 0, do: raise ArithmeticError, "Wald is only defined for positive shape"
defp waldCDF(mu,lambda) do
fn
x when x == 0 -> 0.0
x when x < 0 -> 0.0
x when x > 0 ->
phi(:math.sqrt(lambda/x) * (x/mu-1.0)) + :math.exp(2.0*lambda/mu) * phi(-:math.sqrt(lambda/x) * (x/mu+1.0))
end
end
def skewness(%Wald{pars: nil}), do: fn [k,lambda] -> 3*:math.sqrt(k/lambda) end
def kurtosis(%Wald{pars: nil}), do: fn [k,lambda] -> 15*k/lambda end
def size(%Wald{}), do: 2
def cdf(%Wald{pars: nil}), do: fn x,[k,lambda] -> waldCDF(k,lambda).(x) end
def pdf(%Wald{pars: nil}), do: fn x, [mu,lambda] -> :math.sqrt(lambda/2/:math.pi/x/x/x) * :math.exp( -lambda*(x-mu)*(x-mu)/2/x/mu/mu ) end
def random(%Wald{pars: [k,lambda]}), do: wald(k,lambda).()
def random(%Wald{pars: nil}), do: fn [k,lambda] -> wald(k,lambda).() end
end