Skip to content

Commit

Permalink
Implement likelihood (#45)
Browse files Browse the repository at this point in the history
* implement likelihood functions

* export likelihood and loglikelihood

* add tests

* bump version

* add docs
  • Loading branch information
p-gw authored Jul 22, 2024
1 parent ee624d7 commit 2298687
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ItemResponseFunctions"
uuid = "18e85bec-f2a0-4122-b3a6-37651333b522"
authors = ["Philipp Gewessler <[email protected]>"]
version = "0.1.6"
version = "0.1.7"

[deps]
AbstractItemResponseModels = "0ab3451c-659c-47cd-a7a9-a2d579e209dd"
Expand Down
15 changes: 4 additions & 11 deletions docs/src/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,13 @@ expected_score
information
```
### Utilities
#### Scoring functions
```@docs
partial_credit
```

#### Derivatives
```@docs
ItemParameters
derivative_theta
derivative_theta!
likelihood
loglikelihood
partial_credit
second_derivative_theta
second_derivative_theta!
```

#### Item parameters
```@docs
ItemParameters
```
5 changes: 4 additions & 1 deletion src/ItemResponseFunctions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ export DichotomousItemResponseModel,
derivative_theta,
derivative_theta!,
second_derivative_theta,
second_derivative_theta!
second_derivative_theta!,
likelihood,
loglikelihood

include("model_types.jl")
include("item_parameters.jl")
Expand All @@ -56,6 +58,7 @@ include("expected_score.jl")
include("information.jl")
include("scoring_functions.jl")
include("derivatives.jl")
include("likelihood.jl")

include("precompile.jl")

Expand Down
35 changes: 35 additions & 0 deletions src/likelihood.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""
$(SIGNATURES)
Evaluate the likelihood of an item response model `M` at `theta`, given item parameters
`betas` and response pattern `responses`.
Items are assumed to be independent. Then, the likelihood function is defined as,
``
L(\\boldsymbol{u} | \\theta, \\boldsymbol{\\beta}) = \\prod_{j=1}^J P(Y_j = y | \\theta, \\boldsymbol{\\beta}_j)
``
See also [`loglikelihood`](@ref).
"""
function likelihood(M::Type{<:ItemResponseModel}, theta, betas, responses)
L = one(theta)

for (beta, y) in zip(betas, responses)
L *= irf(M, theta, beta, y)
end

return L
end

"""
$(SIGNATURES)
Evaluate the log-likelihood of an item response model `M` at `theta`, given item parameters
`betas` and response pattern `responses`,
See also [`likelihood`](@ref).
"""
function loglikelihood(M::Type{<:ItemResponseModel}, theta, betas, responses)
return log(likelihood(M, theta, betas, responses))
end
11 changes: 11 additions & 0 deletions test/likelihood.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@testset "likelihood" begin
betas = [(; b = 0.0) for _ in 1:4]
@test likelihood(OnePL, Inf, betas, ones(length(betas))) == 1.0
@test likelihood(OnePL, -Inf, betas, ones(length(betas))) == 0.0

@test likelihood(OnePL, Inf, betas, zeros(length(betas))) == 0.0
@test likelihood(OnePL, -Inf, betas, zeros(length(betas))) == 1.0

@test loglikelihood(OnePL, Inf, betas, ones(length(betas))) == 0.0
@test loglikelihood(OnePL, -Inf, betas, ones(length(betas))) == -Inf
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ using ForwardDiff: Dual
include("models/rating_scale_model.jl")
include("models/generalized_rating_scale_model.jl")
include("derivatives.jl")
include("likelihood.jl")
end

2 comments on commit 2298687

@p-gw
Copy link
Member Author

@p-gw p-gw commented on 2298687 Jul 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/111491

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.7 -m "<description of version>" 2298687bc9bc1e932455172e75dd3953cec687ac
git push origin v0.1.7

Please sign in to comment.