Skip to content

Commit c5224e1

Browse files
Merge pull request #134 from brandmaier/main
contribution of a new loss function for squared Hellinger distance
2 parents e826b68 + 46a9ad8 commit c5224e1

File tree

3 files changed

+66
-3
lines changed

3 files changed

+66
-3
lines changed

CITATION.cff

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ authors:
2121
repository-code: >-
2222
https://github.com/StructuralEquationModels/StructuralEquationModels.jl
2323
license: MIT
24+
doi: 10.5281/zenodo.6719626

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# <img src="https://github.com/StructuralEquationModels/Data/blob/main/images/logo.png" width = 100> StructuralEquationModels.jl
22

3-
| **Documentation** | **Build Status** |
4-
|:-------------------------------------------------------------------------------:|:-----------------------------------------------------------------------------------------------:|
5-
| [![Stable](https://img.shields.io/badge/docs-stable-blue.svg)](https://structuralequationmodels.github.io/StructuralEquationModels.jl/) [![Dev](https://img.shields.io/badge/docs-dev-blue.svg)](https://structuralequationmodels.github.io/StructuralEquationModels.jl/dev/) | [![Project Status: Active – The project has reached a stable, usable state and is being actively developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active) [![Github Action CI](https://github.com/StructuralEquationModels/StructuralEquationModels.jl/workflows/CI_extended/badge.svg)](https://github.com/StructuralEquationModels/StructuralEquationModels.jl/actions/) [![codecov](https://codecov.io/gh/StructuralEquationModels/StructuralEquationModels.jl/branch/main/graph/badge.svg?token=P2kjzpvM4V)](https://codecov.io/gh/StructuralEquationModels/StructuralEquationModels.jl) |
3+
| **Documentation** | **Build Status** | Citation |
4+
|:-------------------------------------------------------------------------------:|:-----------------------------------------------------------------------------------------------:|:-----------------------------------------------------------------------------------------------:|
5+
| [![Stable](https://img.shields.io/badge/docs-stable-blue.svg)](https://structuralequationmodels.github.io/StructuralEquationModels.jl/) [![Dev](https://img.shields.io/badge/docs-dev-blue.svg)](https://structuralequationmodels.github.io/StructuralEquationModels.jl/dev/) | [![Project Status: Active – The project has reached a stable, usable state and is being actively developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active) [![Github Action CI](https://github.com/StructuralEquationModels/StructuralEquationModels.jl/workflows/CI_extended/badge.svg)](https://github.com/StructuralEquationModels/StructuralEquationModels.jl/actions/) [![codecov](https://codecov.io/gh/StructuralEquationModels/StructuralEquationModels.jl/branch/main/graph/badge.svg?token=P2kjzpvM4V)](https://codecov.io/gh/StructuralEquationModels/StructuralEquationModels.jl) | [![DOI](https://zenodo.org/badge/228649704.svg)](https://zenodo.org/badge/latestdoi/228649704) |
6+
67

78
This is a package for Structural Equation Modeling.
89
It is still *in development*.

src/loss/other/hellinger.jl

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
############################################################################################
2+
### Types
3+
############################################################################################
4+
"""
5+
Squared Hellinger distance loss.
6+
7+
# Constructor
8+
9+
Hellinger()
10+
11+
# Examples
12+
```julia
13+
my_hellinger = Hellinger()
14+
```
15+
16+
# Extended help
17+
## Implementation
18+
Subtype of `SemLossFunction`.
19+
"""
20+
21+
struct Hellinger <: SemLossFunction end
22+
23+
using LinearAlgebra
24+
import StructuralEquationModels: Σ, μ, obs_cov, obs_mean, objective!
25+
26+
function objective!(hell::Hellinger, parameters, model::AbstractSem)
27+
28+
# get model-implied covariance matrices
29+
Σᵢ = Σ(imply(model))
30+
31+
# get observed covariance matrix
32+
Σₒ = obs_cov(observed(model))
33+
34+
# get model-implued mean vector
35+
μᵢ = μ(imply(model))
36+
37+
# get observed mean vector
38+
μₒ = obs_mean(observed(model))
39+
40+
Sig = (Σᵢ + Σₒ)/2
41+
42+
43+
44+
# compute the objective
45+
if isposdef(Symmetric(Σᵢ)) # is the model implied covariance matrix positive definite?
46+
47+
loss = ( det(Σᵢ)^(1/4) * det(Σₒ)^(1/4) ) / sqrt(det(Sig))
48+
49+
if !isnothing(μᵢ) & !isnothing(μₒ)
50+
μd = (μᵢ-μₒ)
51+
loss = loss * exp(0.125 * (μd)*inv(Sig)*(μd)')
52+
end
53+
54+
loss = 1 - loss
55+
56+
return loss
57+
58+
else
59+
return Inf
60+
end
61+
end

0 commit comments

Comments
 (0)