|
| 1 | +using Distributions |
| 2 | + |
| 3 | +immutable Semicircle <: ContinuousUnivariateDistribution |
| 4 | + mean::Float64 |
| 5 | + radius::Float64 |
| 6 | + Semicircle(mu, r) = r > 0 ? new(float64(mu), float64(r)) : error("radius r must be positive") |
| 7 | +end |
| 8 | + |
| 9 | +#Standard (normalized) - note this is NOT the standard used in to RMT (r = 2) |
| 10 | +Semicircle(mu) = Semicircle(mu, 1.0) |
| 11 | +Semicircle() = Semicircle(0.0, 1.0) |
| 12 | + |
| 13 | +# Properties |
| 14 | +# For convenience, this is ordered like the Wikipedia infobox template ;) |
| 15 | + |
| 16 | +#Probability density function |
| 17 | +function pdf(d::Semicircle, x::Real) |
| 18 | + r, a = d.mean, d.radius |
| 19 | + return 2/(pi*r^2) * sqrt(r^2 - (x-a)^2) |
| 20 | +end |
| 21 | + |
| 22 | +#Cumulative density function |
| 23 | +function cdf(d::Semicircle, x::Real) |
| 24 | + r, a = d.mean, d.radius |
| 25 | + if x < -a |
| 26 | + return 0.0 |
| 27 | + elseif x > a |
| 28 | + return 1.0 |
| 29 | + else |
| 30 | + return 0.5 + (x-a)/(pi*r^2) * sqrt(r^2 - (x-a)^2) + 1/pi * asin((x-a)/r) |
| 31 | + end |
| 32 | +end |
| 33 | + |
| 34 | +function logpdf(d::Laplace, x::Real) |
| 35 | + log(pdf(d, x)) |
| 36 | +end |
| 37 | +function quantile(d::Laplace, p::Real) |
| 38 | + 0.0 |
| 39 | +end |
| 40 | + |
| 41 | +mean(d::Semicircle) = d.mean |
| 42 | +median(d::Semicircle) = d.mean |
| 43 | +mode(d::Semicircle) = d.mean |
| 44 | + |
| 45 | +std(d::Semicircle) = (d.radius/2) |
| 46 | +var(d::Semicircle) = (d.radius/2)^2 |
| 47 | +skewness(d::Semicircle) = 0.0 |
| 48 | +kurtosis(d::Semicircle) = 2.0 |
| 49 | +entropy(d::Semicircle) = log(pi*R) - 0.5 |
| 50 | + |
| 51 | +## Moment generating function |
| 52 | +function mgf(d::Semicircle, t::Real) |
| 53 | + r = t * d.mean |
| 54 | + 2 * besseli(1, r)/r |
| 55 | +end |
| 56 | + |
| 57 | +# Characteristic function |
| 58 | +function cf(d::Semicircle, t::Real) |
| 59 | + r = t * d.mean |
| 60 | + 2 * besselj(1, r)/r |
| 61 | +end |
| 62 | + |
| 63 | +## Common methods |
| 64 | +#XXX Use the fact that Semicircle(0.5, 0.5) = Beta(1.5, 1.5) |
| 65 | +rand(d::Semicircle) = 2*(rand(Beta(1.5, 1.5) - 0.5)) |
| 66 | +insupport(d::Normal, x::Number) = real_valued(x) & (abs(x) <= d.radius) |
0 commit comments