Skip to content

Commit

Permalink
loglikelihood
Browse files Browse the repository at this point in the history
  • Loading branch information
palday committed Aug 9, 2024
1 parent df77a91 commit 32e72d0
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions src/YeoJohnsonTransformation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -170,34 +170,37 @@ function _loglikelihood_yeojohnson!(y_trans::Vector{<:Number}, Xqr::Factorizatio
return _loglikelihood_yeojohnson(y_trans, λ)

Check warning on line 170 in src/YeoJohnsonTransformation.jl

View check run for this annotation

Codecov / codecov/patch

src/YeoJohnsonTransformation.jl#L168-L170

Added lines #L168 - L170 were not covered by tests
end

# setup mean centering
# setup marginal distrbution
function _loglikelihood_yeojohnson::Number, ::Nothing, y::Vector{<:Number}; kwargs...)
return _loglikelihood_yeojohnson!(similar(y), y, λ)

Check warning on line 175 in src/YeoJohnsonTransformation.jl

View check run for this annotation

Codecov / codecov/patch

src/YeoJohnsonTransformation.jl#L174-L175

Added lines #L174 - L175 were not covered by tests
end

# mean center (makes life easier and more consistent)
# do marginal distribution
function _loglikelihood_yeojohnson!(y_trans::Vector{<:Number}, y::Vector{<:Number}, λ::Number;

Check warning on line 179 in src/YeoJohnsonTransformation.jl

View check run for this annotation

Codecov / codecov/patch

src/YeoJohnsonTransformation.jl#L179

Added line #L179 was not covered by tests
kwargs...)
_yeojohnson!(y_trans, y, λ; kwargs...)
y_trans .-= mean(y_trans)
return _loglikelihood_yeojohnson(y_trans, λ)
return _loglikelihood_yeojohnson(y_trans, y, λ)

Check warning on line 182 in src/YeoJohnsonTransformation.jl

View check run for this annotation

Codecov / codecov/patch

src/YeoJohnsonTransformation.jl#L181-L182

Added lines #L181 - L182 were not covered by tests
end

# actual likelihood computation
function _loglikelihood_yeojohnson(y_trans::Vector{<:Number}, λ::Number)
function _loglikelihood_yeojohnson(y_trans::Vector{<:Number}, y::Vector{<:Number}, λ::Number)
n = length(y_trans)
σ² = var(y_trans)
@info "" λ
# we've mean centered so sum(abs2, y_trans) gives us the mean deviation
return -0.5 * n * log2π +
-0.5 * n * log(σ²) +
-sum(abs2, y_trans) / (2 * σ²) +
- 1) * sum(x -> copysign(log1p(abs(x)), x), y_trans)
σ² = var(y_trans; corrected=false)
penalty =- 1) * sum(y) do x
return copysign(log1p(abs(x)), x)

Check warning on line 190 in src/YeoJohnsonTransformation.jl

View check run for this annotation

Codecov / codecov/patch

src/YeoJohnsonTransformation.jl#L186-L190

Added lines #L186 - L190 were not covered by tests
end
return -0.5 * n * (1 + log2π + log(σ²)) + penalty

Check warning on line 192 in src/YeoJohnsonTransformation.jl

View check run for this annotation

Codecov / codecov/patch

src/YeoJohnsonTransformation.jl#L192

Added line #L192 was not covered by tests
end

# plants = [6.1, -8.4, 1.0, 2.0, 0.7, 2.9, 3.5, 5.1, 1.8, 3.6, 7.0, 3.0, 9.3, 7.5, -6.0]
# λ = 1.305, μ = 4.570, σ² = 29.876, lrt compared to lamba=1 is 3.873, p=0.0499

# λ = 1.305, μ = 4.570, σ² = 29.876, lrt compared to lamba=1 is, 3.873 p=0.0499
# yt0 = YeoJohnsonTransformation(; λ=1, X=nothing, y=plants)
# yt1 = YeoJohnsonTransformation(; λ=1.305, X=nothing, y=plants)
# yt0.(plants) ≈ plants
# isapprox(mean(yt1.(plants)), 4.570; atol=0.005)
# isapprox(var(yt1.(plants); corrected=false), 29.876; rtol=0.005)
# lrt = 2 * abs(loglikelihood(yt0) - loglikelihood(yt1))
# isapprox(lrt, 3.873; rtol=0.005)
# no domain restrictions here besides real values 😎
_input_check_yeojohnson(::Any) = nothing

Check warning on line 205 in src/YeoJohnsonTransformation.jl

View check run for this annotation

Codecov / codecov/patch

src/YeoJohnsonTransformation.jl#L205

Added line #L205 was not covered by tests

Expand Down

0 comments on commit 32e72d0

Please sign in to comment.