Skip to content

Commit

Permalink
loglikelihood from paper
Browse files Browse the repository at this point in the history
  • Loading branch information
palday committed Aug 8, 2024
1 parent 615bea7 commit df77a91
Showing 1 changed file with 29 additions and 14 deletions.
43 changes: 29 additions & 14 deletions src/YeoJohnsonTransformation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -155,33 +155,48 @@ end
##### Internal methods that traits redirect to
#####

# setup linear regression
function _loglikelihood_yeojohnson::Number, X::AbstractMatrix{<:Number}, y::Vector{<:Number};

Check warning on line 159 in src/YeoJohnsonTransformation.jl

View check run for this annotation

Codecov / codecov/patch

src/YeoJohnsonTransformation.jl#L159

Added line #L159 was not covered by tests
kwargs...)
return _loglikelihood_yeojohnson!(similar(y), qr(X), X, y, λ)

Check warning on line 161 in src/YeoJohnsonTransformation.jl

View check run for this annotation

Codecov / codecov/patch

src/YeoJohnsonTransformation.jl#L161

Added line #L161 was not covered by tests
end

# do linear regression
function _loglikelihood_yeojohnson!(y_trans::Vector{<:Number}, Xqr::Factorization,

Check warning on line 165 in src/YeoJohnsonTransformation.jl

View check run for this annotation

Codecov / codecov/patch

src/YeoJohnsonTransformation.jl#L165

Added line #L165 was not covered by tests
X::Matrix{<:Number}, y::Vector{<:Number}, λ::Number;
kwargs...)
_yeojohnson!(y_trans, y, λ; kwargs...)
y_trans -= X * (Xqr \ y_trans)
return _loglikelihood_yeojohnson(y_trans)
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
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)
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, λ)

Check warning on line 183 in src/YeoJohnsonTransformation.jl

View check run for this annotation

Codecov / codecov/patch

src/YeoJohnsonTransformation.jl#L181-L183

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

function _loglikelihood_yeojohnson(y_trans::Vector{<:Number})
return -0.5 * length(y_trans) * log(sum(abs2, y_trans))
# actual likelihood computation
function _loglikelihood_yeojohnson(y_trans::Vector{<:Number}, λ::Number)
n = length(y_trans)
σ² = var(y_trans)
@info "" λ

Check warning on line 190 in src/YeoJohnsonTransformation.jl

View check run for this annotation

Codecov / codecov/patch

src/YeoJohnsonTransformation.jl#L187-L190

Added lines #L187 - L190 were not covered by tests
# we've mean centered so sum(abs2, y_trans) gives us the mean deviation
return -0.5 * n * log2π +

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
-0.5 * n * log(σ²) +
-sum(abs2, y_trans) / (2 * σ²) +
- 1) * sum(x -> copysign(log1p(abs(x)), x), y_trans)

Check warning on line 195 in src/YeoJohnsonTransformation.jl

View check run for this annotation

Codecov / codecov/patch

src/YeoJohnsonTransformation.jl#L195

Added line #L195 was not covered by tests
end

function _loglikelihood_yeojohnson::Number, X::AbstractMatrix{<:Number}, y::Vector{<:Number};
kwargs...)
return _loglikelihood_yeojohnson!(similar(y), qr(X), X, y, λ)
end

function _loglikelihood_yeojohnson::Number, ::Nothing, y::Vector{<:Number}; kwargs...)
return _loglikelihood_yeojohnson!(similar(y), y, λ)
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

# no domain restrictions here besides real values 😎
_input_check_yeojohnson(::Any) = nothing

Check warning on line 202 in src/YeoJohnsonTransformation.jl

View check run for this annotation

Codecov / codecov/patch

src/YeoJohnsonTransformation.jl#L202

Added line #L202 was not covered by tests
Expand All @@ -195,5 +210,5 @@ _input_check_yeojohnson(::Any) = nothing
_input_check(::Type{<:YeoJohnsonTransformation}) =_input_check_yeojohnson
_llfunc(::Type{<:YeoJohnsonTransformation}) = _loglikelihood_yeojohnson
_llfunc!(::Type{<:YeoJohnsonTransformation}) = _loglikelihood_yeojohnson!

Check warning on line 212 in src/YeoJohnsonTransformation.jl

View check run for this annotation

Codecov / codecov/patch

src/YeoJohnsonTransformation.jl#L210-L212

Added lines #L210 - L212 were not covered by tests
# XXX should this be identity???
_scaling(::Type{<:YeoJohnsonTransformation}) = geomean
# XXX should this be geomean like boxcox???
_scaling(::Type{<:YeoJohnsonTransformation}) = identity

0 comments on commit df77a91

Please sign in to comment.