|
| 1 | +module DerivedMeasures |
| 2 | + |
| 3 | +import PsychometricsBazaarBase: power_summary, GridSummary |
| 4 | +using ..Aggregators: TrackedResponses, |
| 5 | + Aggregators, |
| 6 | + AbilityIntegrator, |
| 7 | + AbilityOptimizer, |
| 8 | + AbilityEstimator, |
| 9 | + ModeAbilityEstimator, |
| 10 | + MeanAbilityEstimator, |
| 11 | + LikelihoodAbilityEstimator, |
| 12 | + DistributionAbilityEstimator, |
| 13 | + get_integrator, |
| 14 | + expectation |
| 15 | +using FittedItemBanks: domdims |
| 16 | +using ..NextItemRules: AbilityVariance, compute_criteria, compute_criterion, best_item |
| 17 | +using PsychometricsBazaarBase.Integrators: AnyGridIntegrator, get_grid, normdenom |
| 18 | +using PsychometricsBazaarBase.IndentWrappers: indent |
| 19 | +using PsychometricsBazaarBase: IntegralCoeffs |
| 20 | +using PsychometricsBazaarBase: Differentiation |
| 21 | + |
| 22 | +export PointAndSpreadEstimator, MeanAndStdDevEstimator, LaplaceApproxEstimator, SpreadEstimator |
| 23 | + |
| 24 | +abstract type PointAndSpreadEstimator end |
| 25 | + |
| 26 | +# TODO: These all recalculate everything at the moment, but they should reuse the results generated during the CAT |
| 27 | + |
| 28 | +struct MeanAndStdDevEstimator{ |
| 29 | + DistEstT <: DistributionAbilityEstimator, |
| 30 | + IntegratorT <: AbilityIntegrator |
| 31 | +} <: PointAndSpreadEstimator |
| 32 | + dist_est::DistEstT |
| 33 | + integrator::IntegratorT |
| 34 | +end |
| 35 | + |
| 36 | +MeanAndStdDevEstimator(ability_estimator::MeanAbilityEstimator) = MeanAndStdDevEstimator(ability_estimator.dist_est, ability_estimator.integrator) |
| 37 | +MeanAndStdDevEstimator(ability_variance::AbilityVariance) = MeanAndStdDevEstimator(ability_variance.dist_est, ability_variance.integrator) |
| 38 | + |
| 39 | +function (est::MeanAndStdDevEstimator)(tracked_responses::TrackedResponses) |
| 40 | + denom = normdenom(est.integrator, |
| 41 | + est.dist_est, |
| 42 | + tracked_responses) |
| 43 | + mean = expectation(IntegralCoeffs.id, |
| 44 | + domdims(tracked_responses.item_bank), |
| 45 | + est.integrator, |
| 46 | + est.dist_est, |
| 47 | + tracked_responses, |
| 48 | + denom) |
| 49 | + return ( |
| 50 | + mean, |
| 51 | + sqrt(expectation(IntegralCoeffs.SqDev(mean), |
| 52 | + domdims(tracked_responses.item_bank), |
| 53 | + est.integrator, |
| 54 | + est.dist_est, |
| 55 | + tracked_responses, |
| 56 | + denom)) |
| 57 | + ) |
| 58 | +end |
| 59 | + |
| 60 | +function power_summary(io::IO, est::MeanAndStdDevEstimator) |
| 61 | + println(io, "Mean and standard deviation estimator") |
| 62 | + indent_io = indent(io, 2) |
| 63 | + power_summary(indent_io, est.dist_est) |
| 64 | + power_summary(indent_io, est.integrator) |
| 65 | +end |
| 66 | + |
| 67 | +show(io::IO, ::MIME"text/plain", est::MeanAndStdDevEstimator) = power_summary(io, est) |
| 68 | + |
| 69 | +struct LaplaceApproxEstimator{ |
| 70 | + DistEstT <: DistributionAbilityEstimator, |
| 71 | + OptimizerT <: AbilityOptimizer |
| 72 | +} <: PointAndSpreadEstimator |
| 73 | + dist_est::DistEstT |
| 74 | + optimizer::OptimizerT |
| 75 | +end |
| 76 | + |
| 77 | +LaplaceApproxEstimator(ability_estimator::ModeAbilityEstimator) = LaplaceApproxEstimator(ability_estimator.dist_est, ability_estimator.optim) |
| 78 | + |
| 79 | +function (est::LaplaceApproxEstimator)(tracked_responses::TrackedResponses) |
| 80 | + # TODO: Numerical stability: Should directly access the log-pdf here |
| 81 | + mode = est.optimizer(IntegralCoeffs.one, est.dist_est, tracked_responses) |
| 82 | + return ( |
| 83 | + mode, |
| 84 | + -Differentiation.double_derivative((ability -> log(pdf(est, tracked_responses))), mode) |
| 85 | + ) |
| 86 | +end |
| 87 | + |
| 88 | +function power_summary(io::IO, est::LaplaceApproxEstimator) |
| 89 | + println(io, "Laplace approximation based mean and standard deviation estimator") |
| 90 | + indent_io = indent(io, 2) |
| 91 | + power_summary(indent_io, est.dist_est) |
| 92 | + power_summary(indent_io, est.optimizer) |
| 93 | +end |
| 94 | + |
| 95 | +struct SpreadEstimator{InnerT <: PointAndSpreadEstimator} |
| 96 | + inner::InnerT |
| 97 | +end |
| 98 | + |
| 99 | +function (est::SpreadEstimator)(tracked_responses::TrackedResponses) |
| 100 | + _, stddev = est.inner(tracked_responses) |
| 101 | + return stddev |
| 102 | +end |
| 103 | + |
| 104 | +struct DistributionSampler{ |
| 105 | + DistEst <: DistributionAbilityEstimator, |
| 106 | + IntegratorT <: AbilityIntegrator, |
| 107 | + ContainerT <: Union{Vector{Float64}, Vector{Vector{Float64}}} |
| 108 | +} |
| 109 | + dist_est::DistEst |
| 110 | + integrator::IntegratorT |
| 111 | + points::ContainerT |
| 112 | +end |
| 113 | + |
| 114 | +_get_estimator_and_integrator(ability_estimator::MeanAbilityEstimator) = (ability_estimator.dist_est, ability_estimator.integrator) |
| 115 | +_get_estimator_and_integrator(ability_variance::AbilityVariance) = (ability_variance.dist_est, ability_variance.integrator) |
| 116 | + |
| 117 | +function DistributionSampler(composite::Union{MeanAbilityEstimator, AbilityVariance}, points=nothing) |
| 118 | + dist_est, integrator = _get_estimator_and_integrator(composite) |
| 119 | + return DistributionSampler(dist_est, integrator, points) |
| 120 | +end |
| 121 | + |
| 122 | +function DistributionSampler(dist_est::DistributionAbilityEstimator, integrator::Union{AbilityIntegrator, Nothing}=nothing, points::Nothing=nothing) |
| 123 | + @info "DistributionSampler" dist_est integrator points |
| 124 | + if isnothing(integrator) |
| 125 | + return nothing |
| 126 | + end |
| 127 | + inner_integrator = get_integrator(integrator) |
| 128 | + if !isnothing(points) |
| 129 | + return DistributionSampler(dist_est, integrator, points) |
| 130 | + elseif inner_integrator isa AnyGridIntegrator |
| 131 | + return DistributionSampler(dist_est, integrator, get_grid(inner_integrator)) |
| 132 | + else |
| 133 | + return nothing |
| 134 | + end |
| 135 | +end |
| 136 | + |
| 137 | +function eachmatcol(xs::AbstractMatrix) |
| 138 | + eachcol(xs) |
| 139 | +end |
| 140 | + |
| 141 | +function eachmatcol(xs::AbstractVector) |
| 142 | + xs |
| 143 | +end |
| 144 | + |
| 145 | +function (est::DistributionSampler)(tracked_responses::TrackedResponses) |
| 146 | + num = Aggregators.pdf.( |
| 147 | + est.dist_est, |
| 148 | + tracked_responses, |
| 149 | + eachmatcol(est.points) |
| 150 | + ) |
| 151 | + denom = normdenom(est.integrator, est.dist_est, tracked_responses) |
| 152 | + return num ./ denom |
| 153 | +end |
| 154 | + |
| 155 | +function power_summary(io::IO, est::DistributionSampler) |
| 156 | + println(io, "Distribution sampler") |
| 157 | + indent_io = indent(io, 2) |
| 158 | + power_summary(indent_io, est.dist_est) |
| 159 | + power_summary(indent_io, est.integrator) |
| 160 | + power_summary(indent_io, GridSummary(est.points)) |
| 161 | +end |
| 162 | + |
| 163 | +end |
0 commit comments