Skip to content

Commit

Permalink
add shiftedlognormal
Browse files Browse the repository at this point in the history
  • Loading branch information
itsdfish committed Jul 4, 2024
1 parent 86b54d6 commit 428adf6
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/SequentialSamplingModels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export AbstractMLBA
export AbstractMDFT
export AbstractPoissonRace
export AbstractRDM
export AbstractShiftedLogNormal
export AbstractstDDM
export AbstractWald
export aDDM
Expand All @@ -57,6 +58,7 @@ export maaDDM
export MLBA
export MDFT
export PoissonRace
export ShiftedLogNormal
export SSM1D
export SSM2D
export stDDM
Expand Down Expand Up @@ -106,4 +108,5 @@ include("stDDM.jl")
include("MDFT.jl")
include("ClassicMDFT.jl")
include("MLBA.jl")
include("ShiftedLogNormal.jl")
end
55 changes: 55 additions & 0 deletions src/ShiftedLogNormal.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""
ShiftedLogNormal{T <: Real} <: AbstractShiftedLogNormal
A special case of the lognormal race (LNR) model for a single response. The first passage time is lognormally distributed.
# Parameters
- `ν`: mean finishing time in log-space
- `σ`: standard deviation parameter in log-space
- `τ`: a encoding-response offset
# Constructors
Two constructors are defined below. The first constructor uses positional arguments, and is therefore order dependent:
ShiftedLogNormal(ν, σ, τ)
The second constructor uses keywords with default values, and is not order dependent:
ShiftedLogNormal(; ν = -1, σ=.5, τ = .20)
# Example
```julia
using SequentialSamplingModels
dist = ShiftedLogNormal(ν = -1, σ=.5, τ = .20)
rts = rand(dist, 10)
like = pdf.(dist, rts)
loglike = logpdf.(dist, rts)
```
# References
Heathcote, A., & Bohlscheid, E. Analysis and Modeling of Response Time using the Shifted Lognormal Distribution.
"""
struct ShiftedLogNormal{T <: Real} <: AbstractShiftedLogNormal
ν::T
σ::T
τ::T
end

ShiftedLogNormal(ν, σ, τ) = ShiftedLogNormal(promote(ν, σ, τ)...)

ShiftedLogNormal(; ν = -1, σ=.5, τ = .20) = ShiftedLogNormal(ν, σ, τ)

function logpdf(dist::AbstractShiftedLogNormal, rt)
(; τ, ν, σ) = dist
return logpdf(LogNormal(ν, σ), rt - τ)
end

function rand(dist::AbstractShiftedLogNormal, n_trials::Int)
(; τ, ν, σ) = dist
return rand(LogNormal(ν, σ), n_trials) .+ τ
end

model = ShiftedLogNormal= 1, σ = 1, τ = .20)
7 changes: 7 additions & 0 deletions src/type_system.jl
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ An abstract type for the racing diffusion model.
"""
abstract type AbstractRDM <: SSM2D end

"""
AbstractShiftedLogNormal <: SSM1D
An abstract type for the shifted lognormal model.
"""
abstract type AbstractShiftedLogNormal <: SSM1D end

abstract type PDFType end

"""
Expand Down
68 changes: 68 additions & 0 deletions test/shiftedlognormaldistribution.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
@safetestset "Shifted Lognormal Distribution" begin

@safetestset "rand" begin
using Distributions
using Random
using SequentialSamplingModels
using Test

Random.seed!(5411)

lognormal = LogNormal(-1, .3)
shifted_lognormal = ShiftedLogNormal= -1, σ=.3, τ = 0.5)
mean(rand(shifted_lognormal, 10_000))
# τ is properly added
@test mean(rand(lognormal, 10_000)) - mean(rand(shifted_lognormal, 10_000)) -.5 atol = .01
end


@safetestset "pdf" begin
using Distributions
using SequentialSamplingModels
using Test

lognormal = LogNormal(-1, .3)
shifted_lognormal = ShiftedLogNormal= -1, σ=.3, τ = 0.0)
x = rand(shifted_lognormal, 100)
@test pdf.(lognormal, x) pdf.(shifted_lognormal, x)
end

@safetestset "logpdf 1" begin
using Distributions
using SequentialSamplingModels
using Test

lognormal = LogNormal(-1, .3)
shifted_lognormal = ShiftedLogNormal= -1, σ=.3, τ = 0.0)
x = rand(shifted_lognormal, 100)
@test logpdf.(lognormal, x) logpdf.(shifted_lognormal, x)
end

@safetestset "logpdf 2" begin
using Distributions
using Random
using SequentialSamplingModels
using Test

Random.seed!(2008)

parms == -1, σ=.3, τ = 0.0)
x = rand(ShiftedLogNormal(; parms...), 10_000)

νs = range(.80 * parms.ν, 1.2 * parms.ν, length = 100)
LLs = map-> sum(logpdf.(ShiftedLogNormal(; parms..., ν), x)), νs)
_,idx = findmax(LLs)
@test νs[idx] parms.ν rtol = .01

σs = range(.80 * parms.σ, 1.2 * parms.σ, length = 100)
LLs = map-> sum(logpdf.(ShiftedLogNormal(; parms..., σ), x)), σs)
_,idx = findmax(LLs)
@test σs[idx] parms.σ rtol = .01


τs = range(.80 * parms.τ, 1.2 * parms.τ, length = 100)
LLs = map-> sum(logpdf.(ShiftedLogNormal(; parms..., τ), x)), τs)
_,idx = findmax(LLs)
@test τs[idx] parms.τ rtol = .01
end
end

0 comments on commit 428adf6

Please sign in to comment.