Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,43 @@ jobs:
with:
version: ${{ matrix.version }}
arch: ${{ matrix.arch }}
- name: Isolate the test workspace from documentation dependencies
run: |
julia -e '
using TOML
project = TOML.parsefile("Project.toml")
project["workspace"]["projects"] = ["test"]
open(io -> TOML.print(io, project), "Project.toml", "w")'
- uses: julia-actions/julia-downgrade-compat@v2
if: ${{ matrix.version == '1.12' }}
with:
projects: '.,test'
- uses: julia-actions/cache@v1
- name: Resolve test workspace dependencies
run: julia --project=. -e 'using Pkg; Pkg.Registry.add(); Pkg.resolve()'
env:
JULIA_PKG_PRECOMPILE_AUTO: '0'
- uses: julia-actions/julia-buildpkg@v1
- uses: julia-actions/julia-runtest@v1
- uses: julia-actions/julia-processcoverage@v1
- uses: coverallsapp/github-action@v2
with:
file: lcov.info
- name: Upload coverage to Coveralls
env:
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
COVERALLS_SOURCE_HEADER: github-action
run: |
# v0.6.22: use the asset API to avoid failing release-page redirects.
reporter_dir="${RUNNER_TEMP}/coveralls-v0.6.22"
mkdir -p "$reporter_dir"
curl --fail --location --silent --show-error --retry 5 --retry-all-errors \
--connect-timeout 15 --max-time 60 \
--header 'Accept: application/octet-stream' \
--output "$reporter_dir/coveralls.tar.gz" \
https://api.github.com/repos/coverallsapp/coverage-reporter/releases/assets/521399808
printf '%s %s\n' \
cd042b63bcb2c03fd0fd3372c040507331f5013a997828071e2ab7f9584fc7d6 \
"$reporter_dir/coveralls.tar.gz" | sha256sum --check
tar -xzf "$reporter_dir/coveralls.tar.gz" -C "$reporter_dir"
"$reporter_dir/coveralls" report lcov.info
docs:
name: Documentation
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/benchmark_pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- uses: actions/checkout@v2
- uses: julia-actions/setup-julia@v2
with:
version: "1.12"
version: "1.13"
- uses: julia-actions/cache@v2
- name: Extract Package Name from Project.toml
id: extract-package-name
Expand Down
8 changes: 4 additions & 4 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,16 @@ Distributions = "^0.25.88"
DocStringExtensions = " ^0.9"
EffectSizes = "^1.0.1"
ElasticArrays = "1.2.12"
FittedItemBanks = "^0.7.6"
ForwardDiff = "1"
FittedItemBanks = "^0.7.7"
ForwardDiff = "1.1"
HypothesisTests = "^0.10.12, ^0.11.0"
LinearAlgebra = "^1.12"
LogarithmicNumbers = "1"
LogarithmicNumbers = "1.4.1"
Mmap = "^1.11"
PrecompileTools = "1.2.1"
PrettyPrinting = "0.4.2"
PrettyTables = "3"
PsychometricsBazaarBase = "^0.8.7"
PsychometricsBazaarBase = "^0.8.11"
QuickHeaps = "0.2.2"
Random = "^1.11"
Reexport = "1"
Expand Down
3 changes: 1 addition & 2 deletions docs/src/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,7 @@ the posterior peak: stable normalization does not correct quadrature error.
`AbilityIntegrator(grid, tracker)` infers the distribution from the tracker and
selects the tracked adapter. It supports fixed, preallocated and iterative
equal-weight grids, including multidimensional grids and vector/matrix moments.
The scalar preallocated path applies speculative probabilities during its
integration pass. Shared trackers remain shared after `preallocate`.
Shared trackers remain shared after `preallocate`.

Both grid trackers require the estimator to declare the multiplicative density
contract via `response_delta_style(dist) == MultiplicativeResponseDelta()`:
Expand Down
6 changes: 4 additions & 2 deletions src/Aggregators/Aggregators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ using PsychometricsBazaarBase.Integrators: Integrators,
IntReturnType,
IntValue, Integrator,
PreallocatedFixedGridIntegrator,
normdenom
normdenom, LogGridWeights,
normalize_grid_weights!, weighted_grid_mean,
weighted_grid_sum, weighted_grid_moments,
grid_mass, scale_grid_value
using PsychometricsBazaarBase.Optimizers: OneDimOptimOptimizer, Optimizer, Optimizers
using PsychometricsBazaarBase.ConstDistributions: std_normal, std_mv_normal
using PsychometricsBazaarBase.IndentWrappers: indent
Expand Down Expand Up @@ -282,7 +285,6 @@ include("./riemann.jl")
include("./ability_estimator.jl")
include("./ability_tracker.jl")
include("./tracked.jl")
include("./log_grid_weights.jl")
include("./log_grid.jl")
include("./log_function.jl")
include("./configuration.jl")
Expand Down
16 changes: 12 additions & 4 deletions src/Aggregators/ability_trackers/grid.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const EqualWeightGridIntegrator = Union{FixedGridIntegrator,
PreallocatedFixedGridIntegrator, Integrators.IterativeFixedGridIntegrator}
const ScalarGridIntegrator = Union{FixedGridIntegrator{<:AbstractVector{Float64}},
PreallocatedFixedGridIntegrator{<:AbstractVector{Float64}},
Integrators.IterativeFixedGridIntegrator{<:AbstractVector{Float64}}}
const TabulatedDichotomousBank = Union{FittedItemBanks.DichotomousPointsItemBank,
FittedItemBanks.DichotomousPointsWithLogsItemBank}

Expand All @@ -13,6 +16,12 @@ function grid_linear_values(density::IntegralCoeffs.PriorApply, grid)
grid_linear_values(density.func, grid) .* pdf.(Ref(density.prior.dist), grid)
end

# Keep analytic delta evaluation lazy so the tracker can broadcast directly into
# its destination. Tabulated banks require their grid adapter, not pointwise resp.
grid_delta_values(likelihood, grid) = Base.Broadcast.broadcasted(likelihood, grid)
grid_delta_values(likelihood::AbilityLikelihood{<:TabulatedDichotomousBank}, grid) =
grid_linear_values(likelihood, grid)

"""
$(TYPEDEF)

Expand Down Expand Up @@ -70,7 +79,7 @@ function track_append!(responses::TrackedResponses, tracker::GriddedAbilityTrack
tracker.density = nothing
delta = response_view(responses.responses, length(responses):length(responses))
likelihood = AbilityLikelihood(responses.item_bank, delta)
density .*= grid_linear_values(likelihood, Integrators.get_grid(tracker.integrator))
density .*= grid_delta_values(likelihood, Integrators.get_grid(tracker.integrator))
tracker.density = density
tracker
end
Expand All @@ -81,9 +90,8 @@ end

function grid_delta_density!(scratch::Vector{Float64}, tracker::GriddedAbilityTracker,
density, responses, start)
copyto!(scratch, density)
start > length(responses) && return scratch
start > length(responses) && return copyto!(scratch, density)
delta = response_view(responses.responses, start:length(responses))
likelihood = AbilityLikelihood(responses.item_bank, delta)
scratch .*= grid_linear_values(likelihood, Integrators.get_grid(tracker.integrator))
scratch .= density .* grid_delta_values(likelihood, Integrators.get_grid(tracker.integrator))
end
31 changes: 27 additions & 4 deletions src/Aggregators/log_grid.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ function grid_log_values(density::LogPosteriorDensity, grid)
grid_log_values(density.likelihood, grid) .+ logpdf.(Ref(density.prior), grid)
end

grid_delta_values(likelihood::AbilityLogLikelihood{<:AbilityLikelihood{<:TabulatedDichotomousBank}}, grid) =
grid_log_values(likelihood, grid)

function grid_density(est, responses, integrator)
LogGridWeights(grid_log_values(logpdf(est, responses), Integrators.get_grid(integrator)))
end
Expand Down Expand Up @@ -85,11 +88,14 @@ initialize_grid_tracker!(responses, tracker::LogGridAbilityTracker) = track!(res

function grid_delta_density!(scratch::LogGridWeights, tracker::LogGridAbilityTracker,
density, responses, start)
copyto!(scratch.log_values, density.log_values)
start > length(responses) && return LogGridWeights(scratch.log_values,
scratch.weights, density.log_scale, density.scaled_sum)
if start > length(responses)
copyto!(scratch.log_values, density.log_values)
copyto!(scratch.weights, density.weights)
return LogGridWeights(scratch.log_values,
scratch.weights, density.log_scale, density.scaled_sum)
end
delta = response_view(responses.responses, start:length(responses))
scratch.log_values .+= grid_log_values(
scratch.log_values .= density.log_values .+ grid_delta_values(
AbilityLogLikelihood(responses.item_bank, delta), Integrators.get_grid(tracker.integrator))
normalize_grid_weights!(scratch.log_values, scratch.weights)
end
Expand Down Expand Up @@ -182,6 +188,23 @@ function expectation(rett::IntReturnType, f::F, ncomp, integrator::LogGridIntegr
rett(BareIntegrationResult(value * correction))
end

function variance(integrator::LogGridIntegrator{<:ScalarGridIntegrator},
est::DistributionAbilityEstimator, responses::TrackedResponses, denom = nothing)
density = grid_density(est, responses, integrator)
# The public denominator is an absolute mass; the upstream log-state
# summary accepts a divisor in normalized-weight units.
divisor = denom === nothing ? nothing : unlog(denom / grid_mass(density))
if divisor !== nothing && (iszero(divisor) || !isfinite(divisor))
# A valid absolute denominator can have an unrepresentable ratio to
# the mass. Preserve the generic log-space expectation scaling then.
return invoke(variance,
Tuple{AbilityIntegrator, DistributionAbilityEstimator, TrackedResponses, Any},
integrator, est, responses, denom)
end
weighted_grid_moments(Integrators.get_grid(integrator.integrator), density;
denominator = divisor).variance
end

function (est::MeanAbilityEstimator{E, I})(::DiscreteIndexableDomain,
rett::IntReturnType, responses::TrackedResponses) where {E, I <: LogGridIntegrator}
expectation(rett, IntegralCoeffs.id, 0, est.integrator, est.dist_est, responses)
Expand Down
59 changes: 0 additions & 59 deletions src/Aggregators/log_grid_weights.jl

This file was deleted.

46 changes: 17 additions & 29 deletions src/Aggregators/tracked.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,6 @@ function (product::FunctionProduct)(x::T) where {T}
return product.f(x) * product.lh_function(x)
end

struct FunctionArgProduct{F}
f::F
end

function (product::FunctionArgProduct)(x::A, y::B) where {A, B}
product.f(x) * y
end

struct TrackedLikelihoodIntegrator{IntegratorT <: EqualWeightGridIntegrator,
TrackerT <: GriddedAbilityTracker} <: AbilityIntegrator
integrator::IntegratorT
Expand All @@ -38,28 +30,13 @@ function power_summary(io::IO, integrator::TrackedLikelihoodIntegrator)
power_summary(indent(io, 2), integrator.integrator)
end

# The backend's weighted preallocated overload works for scalar grids/results.
# Other shapes reuse the same grid reduction as log-space integration.
# Reduce the cached weights directly, using the same weighted reducer as
# log-space integration. The backend's broadcast path can box scalar coefficient
# evaluations even with a preallocated buffer (notably nested broadcasts in SqDev).
function integrate_grid_weights(backend, f::F, weights, ncomp) where {F}
BareIntegrationResult(weighted_grid_sum(f, Integrators.get_grid(backend), weights))
end

@inline function integrate_grid_weights(
backend::Union{FixedGridIntegrator{Vector{Float64}},
PreallocatedFixedGridIntegrator{Vector{Float64}}},
f::F,
weights,
ncomp) where {F}
if ncomp == 0
# The backend also has an array-valued branch for positive ncomp.
# Pin its scalar result here so that branch cannot widen all moments.
result = @inline backend(FunctionArgProduct(f), weights, 0)
result::BareIntegrationResult{Float64}
else
BareIntegrationResult(weighted_grid_sum(f, Integrators.get_grid(backend), weights))
end
end

function (integrator::TrackedLikelihoodIntegrator)(f::F, ncomp) where {F}
integrator.tracker.density === nothing &&
throw(ArgumentError("Call track! before integrating a cache without a response history"))
Expand All @@ -80,14 +57,25 @@ function (integrator::TrackedLikelihoodIntegrator)(f::F,
est,
tracked_responses::TrackedResponses;
kwargs...) where {F}
density = grid_density(est, tracked_responses, integrator)
integrate_grid_weights(integrator.integrator, f, density, ncomp)
end

function grid_density(est, tracked_responses, integrator::TrackedLikelihoodIntegrator)
tracker = integrator.tracker
density = speculative_density(tracker, tracked_responses.ability_tracker)
if density !== nothing && est === tracker.ability_estimator
return integrate_grid_weights(integrator.integrator, f, density, ncomp)
return density
end
# Uninitialized or mismatched estimators use a temporary full density.
weights = grid_linear_values(pdf(est, tracked_responses), Integrators.get_grid(integrator.integrator))
integrate_grid_weights(integrator.integrator, f, weights, ncomp)
grid_linear_values(pdf(est, tracked_responses), Integrators.get_grid(integrator.integrator))
end

function variance(integrator::TrackedLikelihoodIntegrator{<:ScalarGridIntegrator},
est::DistributionAbilityEstimator, responses::TrackedResponses, denom = nothing)
density = grid_density(est, responses, integrator)
weighted_grid_moments(Integrators.get_grid(integrator.integrator), density;
denominator = denom).variance
end

function (est::MeanAbilityEstimator{E, I})(::DiscreteIndexableDomain,
Expand Down
4 changes: 4 additions & 0 deletions src/NextItemRules/NextItemRules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ using PsychometricsBazaarBase: Differentiation
using PsychometricsBazaarBase.IndentWrappers: indent
import PsychometricsBazaarBase.IntegralCoeffs
using FittedItemBanks: AbstractItemBank, DiscreteDomain, DomainType,
TransferItemBank, SlopeInterceptTransferItemBank,
CdfMirtItemBank, SlopeInterceptMirtItemBank, GuessAndSlipItemBank,
ResponseType, BooleanResponse,
ItemResponse, OneDimContinuousDomain, domdims, item_params,
resp, resp_vec, log_resp, log_resp_vec, responses, subset_view
using ..Aggregators
Expand Down Expand Up @@ -94,6 +97,7 @@ include("./combinators/likelihood.jl")
include("./criteria/item/information.jl")
include("./criteria/item/urry.jl")
include("./criteria/state/ability_variance.jl")
include("./combinators/grid_mepv.jl")
include("./criteria/pointwise/information_special.jl")
include("./criteria/pointwise/information_support.jl")
include("./criteria/pointwise/information.jl")
Expand Down
7 changes: 7 additions & 0 deletions src/NextItemRules/combinators/expectation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ struct PointResponseExpectation{
ability_estimator::PointAbilityEstimatorT
end

preallocate(expectation::PointResponseExpectation) =
PointResponseExpectation(preallocate(expectation.ability_estimator))

function Aggregators.response_expectation(
point_response_expectation::PointResponseExpectation,
tracked_responses,
Expand All @@ -53,6 +56,10 @@ struct DistributionResponseExpectation{
integrator::AbilityIntegratorT
end

preallocate(expectation::DistributionResponseExpectation) =
DistributionResponseExpectation(expectation.ability_estimator,
preallocate(expectation.integrator))

function power_summary(io::IO, expectation::DistributionResponseExpectation)
println(io, "Expected response over the ability distribution")
indent_io = indent(io, 2)
Expand Down
Loading
Loading