package scientific.stats.continuous
import std.math.*
import std.unittest.*
import std.unittest.testmacro.*
import scientific.numbers.*
import scientific.stats.random.*
/*
* Log of Probability density function
*/
public func maxwellLogPDF(x: Float64, loc!: Float64 = 0.0, scale!: Float64 = 1.0): Float64 {
let y = (x - loc) / scale
if (y < 0.0) {
throw IllegalArgumentException("maxwellLogPDF: input value out of bound.")
}
let res = 0.5 * (log(2.0) - log(Float64.getPI())) + 2.0 * log(y) - 0.5 * y * y
return res - log(scale)
}
/*
* Probability density function
*/
public func maxwellPDF(x: Float64, loc!: Float64 = 0.0, scale!: Float64 = 1.0): Float64 {
let y = (x - loc) / scale
if (y < 0.0) {
throw IllegalArgumentException("maxwellPDF: input value out of bound.")
}
let temp = maxwellLogPDF(x, loc: loc, scale: scale)
return exp(temp)
}
/*
* Cumulative probability density function
*/
public func maxwellCDF(x: Float64, loc!: Float64 = 0.0, scale!: Float64 = 1.0): Float64 {
let y = (x - loc) / scale
if (y < 0.0) {
throw IllegalArgumentException("maxwellCDF: input value out of bound.")
}
let res = igam(1.5, 0.5 * y * y)
return res
}
/*
* Cumulative probability density function
*/
public func maxwellLogCDF(x: Float64, loc!: Float64 = 0.0, scale!: Float64 = 1.0): Float64 {
let y = (x - loc) / scale
if (y < 0.0) {
throw IllegalArgumentException("maxwellLogCDF: input value out of bound.")
}
let temp = maxwellCDF(x, loc: loc, scale: scale)
if (temp < 0.000001) {
throw IllegalArgumentException("maxwellLogCDF: return-value too small.")
}
return log(temp)
}
/*
* PPF
*/
public func maxwellPPF(q: Float64, loc!: Float64 = 0.0, scale!: Float64 = 1.0): Float64 {
if (q <= 0.0 || q >= 1.0) {
throw IllegalArgumentException("maxwellPPF: quantile out of bound.")
}
let temp = igami(1.5, 1.0 - q)
let res = sqrt(2.0 * temp)
return res * scale + loc
}
/*
* compute the mean
*/
public func maxwellMean(loc!: Float64 = 0.0, scale!: Float64 = 1.0): Float64 {
let res = 2.0 * sqrt(2.0 / Float64.getPI())
return res * scale + loc
}
/*
* compute the var
*/
public func maxwellVar(loc!: Float64 = 0.0, scale!: Float64 = 1.0): Float64 {
let res = 3.0 - 8.0 / Float64.getPI()
return res * scale * scale
}
/*
* compute the std
*/
public func maxwellStd(loc!: Float64 = 0.0, scale!: Float64 = 1.0): Float64 {
let temp = halfnormVar(loc: loc, scale: scale)
if (temp < 0.000001) {
throw IllegalArgumentException("maxwellStd: return-value too small.")
}
return sqrt(temp)
}
@Test
public class TestMaxwell {
@TestCase
func testMaxwell(): Unit {
@Assert(approxEqual(maxwellLogPDF(3.0, loc: 2.0, scale: 1.0), -0.7257913526447275, atol:1e-13))
@Assert(approxEqual(maxwellLogCDF(3.0, loc: 2.0, scale: 1.0), -1.6157173715399462, atol:1e-13))
@Assert(approxEqual(maxwellPPF(0.7, loc: 2.0, scale: 1.0), 3.9143852232950183, atol:1e-13))
}
}