|
| 1 | +struct Censored{P,L,U,W} <: AbstractMeasure |
| 2 | + parent::P |
| 3 | + lower::L |
| 4 | + upper::U |
| 5 | + ℓ_lower |
| 6 | + ℓ_upper |
| 7 | + |
| 8 | + function Censored(d::P, lower::L, upper::U) where {P,L,U} |
| 9 | + tails = cdf(d, lower) + ccdf(d, upper) |
| 10 | + logweight = -log1p(-tails) |
| 11 | + new{P,L,U,typeof(logweight)}(d, lower, upper, logweight) |
| 12 | + end |
| 13 | + |
| 14 | + function Censored(d::P, ::Nothing, upper::U) where {P,U} |
| 15 | + logweight = -logcdf(d, upper) |
| 16 | + new{P,Nothing,U,typeof(logweight)}(d, nothing, upper, logweight) |
| 17 | + end |
| 18 | + |
| 19 | + function Censored(d::P, lower::L, ::Nothing) where {P,L} |
| 20 | + logweight = -logccdf(d, lower) |
| 21 | + new{P,L,Nothing,typeof(logweight)}(d, lower, nothing, logweight) |
| 22 | + end |
| 23 | +end |
| 24 | + |
| 25 | +function Base.rand(rng::AbstractRNG, ::Type{T}, d::Censored) where {T} |
| 26 | + x = rand(rng, T, d.parent) |
| 27 | + clamp(x, d.lower, d.upper) |
| 28 | +end |
| 29 | + |
| 30 | +insupport(d::Censored, x) = insupport(d.parent, x) && d.lower ≤ x ≤ d.upper |
| 31 | + |
| 32 | +insupport(d::Censored{P,L,Nothing}, x) where {P,L} = insupport(d.parent, x) && d.lower ≤ x |
| 33 | + |
| 34 | +insupport(d::Censored{P,Nothing,U}, x) where {P,U} = insupport(d.parent, x) && x ≤ d.upper |
| 35 | + |
| 36 | +export censored |
| 37 | + |
| 38 | +censored(d, lower, upper) = Censored(d, lower, upper) |
| 39 | + |
| 40 | +censored(d, ::Nothing, ::Nothing) = d |
| 41 | + |
| 42 | +censored(d; lower=nothing, upper=nothing) = censored(d, lower, upper) |
| 43 | + |
| 44 | +logdensity_def(d::Censored, x) = logdensity_def(d.parent, x) |
| 45 | + |
| 46 | +basemeasure(d::Censored, x) = basemeasure(d.parent) |
0 commit comments