Skip to content

Commit

Permalink
add animation
Browse files Browse the repository at this point in the history
  • Loading branch information
DominiqueMakowski committed Jul 7, 2024
1 parent 0b34f64 commit 06bd30b
Show file tree
Hide file tree
Showing 18 changed files with 143 additions and 17 deletions.
Binary file modified content/.jupyter_cache/global.db
Binary file not shown.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion content/.quarto/cites/index.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"4_rt.qmd":["wagenmakers2008diffusion","heathcote2012linear","theriault2024check","lo2015transform","schramm2019reaction","balota2011moving","matzke2009psychological","hohle1965inferred","kieffaber2006switch","matzke2009psychological","schwarz2001ex","heathcote2004fitting","anders2016shifted"],"2_predictors.qmd":[],"index.qmd":[],"references.qmd":[],"5_individual.qmd":[],"4a_rt_descriptive.qmd":["wagenmakers2008diffusion","heathcote2012linear","theriault2024check","lo2015transform","schramm2019reaction","balota2011moving","matzke2009psychological","hohle1965inferred","kieffaber2006switch","matzke2009psychological","schwarz2001ex","heathcote2004fitting","anders2016shifted"],"3_scales.qmd":[],"1_introduction.qmd":[],"4b_rt_generative.qmd":[],"4_1_Normal.qmd":["wagenmakers2008diffusion","theriault2024check","lo2015transform","schramm2019reaction"]}
{"4_rt.qmd":["wagenmakers2008diffusion","heathcote2012linear","theriault2024check","lo2015transform","schramm2019reaction","balota2011moving","matzke2009psychological","hohle1965inferred","kieffaber2006switch","matzke2009psychological","schwarz2001ex","heathcote2004fitting","anders2016shifted"],"2_predictors.qmd":[],"4_1_Normal.qmd":["wagenmakers2008diffusion","theriault2024check","lo2015transform","schramm2019reaction"],"1_introduction.qmd":[],"3_scales.qmd":[],"index.qmd":[],"references.qmd":[],"4a_rt_descriptive.qmd":["wagenmakers2008diffusion","heathcote2012linear","theriault2024check","lo2015transform","schramm2019reaction","balota2011moving","matzke2009psychological","hohle1965inferred","kieffaber2006switch","matzke2009psychological","schwarz2001ex","heathcote2004fitting","anders2016shifted"],"5_individual.qmd":[],"4b_rt_generative.qmd":[]}
2 changes: 1 addition & 1 deletion content/.quarto/idx/4a_rt_descriptive.qmd.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion content/.quarto/idx/4b_rt_generative.qmd.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion content/.quarto/xref/15f266d2
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"entries":[],"options":{"chapters":true},"headings":["very-quick-intro-to-julia-and-turing","generate-data-from-normal-distribution","recover-distribution-parameters-with-turing","linear-models","boostrapping","hierarchical-models","bayesian-estimation","bayesian-mixed-linear-regression"]}
{"headings":["very-quick-intro-to-julia-and-turing","generate-data-from-normal-distribution","recover-distribution-parameters-with-turing","linear-models","boostrapping","hierarchical-models","bayesian-estimation","bayesian-mixed-linear-regression"],"options":{"chapters":true},"entries":[]}
2 changes: 1 addition & 1 deletion content/.quarto/xref/1a47137c
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"entries":[],"options":{"chapters":true},"headings":[]}
{"headings":[],"entries":[],"options":{"chapters":true}}
2 changes: 1 addition & 1 deletion content/.quarto/xref/26afb962
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"entries":[],"options":{"chapters":true},"headings":["categorical-predictors-condition-group","interactions","ordered-predictors-likert-scales","non-linear-relationships-polynomial-gams"]}
{"headings":["categorical-predictors-condition-group","interactions","ordered-predictors-likert-scales","non-linear-relationships-polynomial-gams"],"options":{"chapters":true},"entries":[]}
2 changes: 1 addition & 1 deletion content/.quarto/xref/26e6880e
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"entries":[],"options":{"chapters":true},"headings":["the-data","gaussian-aka-linear-model","model-specification","posterior-predictive-check","scaled-gaussian-model","solution-1-directional-effect-of-condition","solution-2-avoid-exploring-negative-variance-values","the-problem-with-linear-models","shifted-lognormal-model","prior-on-minimum-rt","model-specification-1","interpretation","exgaussian-model","conditional-tau-tau-parameter","interpretation-1","shifted-wald-model","model-specification-2","model-comparison"]}
{"options":{"chapters":true},"entries":[],"headings":["the-data","gaussian-aka-linear-model","model-specification","posterior-predictive-check","scaled-gaussian-model","solution-1-directional-effect-of-condition","solution-2-avoid-exploring-negative-variance-values","the-problem-with-linear-models","shifted-lognormal-model","prior-on-minimum-rt","model-specification-1","interpretation","exgaussian-model","conditional-tau-tau-parameter","interpretation-1","shifted-wald-model","model-specification-2","model-comparison"]}
2 changes: 1 addition & 1 deletion content/.quarto/xref/6cbe9151
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"headings":["preface","why-julia","why-bayesian","the-plan","looking-for-coauthors"],"entries":[],"options":{"chapters":true}}
{"options":{"chapters":true},"entries":[],"headings":["preface","why-julia","why-bayesian","the-plan","looking-for-coauthors"]}
2 changes: 1 addition & 1 deletion content/.quarto/xref/a408ff3e
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"headings":["evidence-accumulation","drift-diffusion-model-ddm","other-models-lba-lnr","including-random-effects","additional-resources"],"options":{"chapters":true},"entries":[]}
{"entries":[],"options":{"chapters":true},"headings":["evidence-accumulation","wald-distribution-revisited","drift-diffusion-model-ddm","other-models-lba-lnr","including-random-effects","additional-resources"]}
2 changes: 1 addition & 1 deletion content/.quarto/xref/ce37606d
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"headings":[],"entries":[],"options":{"chapters":true}}
{"entries":[],"headings":[],"options":{"chapters":true}}
2 changes: 1 addition & 1 deletion content/4a_rt_descriptive.qmd
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Descriptive Models

![](https://img.shields.io/badge/status-WIP-orange)
![](https://img.shields.io/badge/status-up_to_date-green)

## The Data

Expand Down
36 changes: 34 additions & 2 deletions content/4b_rt_generative.qmd
Original file line number Diff line number Diff line change
@@ -1,14 +1,46 @@
# Generative Models

![](https://img.shields.io/badge/status-WIP-orange)

In this chapter, we will move away from statistical models that **describe** the data to models of **data-generation processes**.

## Evidence Accumulation

This distribution appears to have been first derived in 1900 to model the time a stock reaches a certain price (a *threshold* price) for the first time, and used in 1915 by Schrödinger as the time to first passage of a threshold of a Brownian motion (i.e., a random walk).
In the previous chapter, we introduced the **Wald** distribution and its parameters, *nu* $\nu$ (drift rate) and *alpha* $\alpha$ (threshold).
This distribution appears to have been first derived in 1900 to model **the time a stock reaches a certain price** (a *threshold* price) for the first time, and used in 1915 by Schrödinger as the time to first passage of a threshold of a Brownian motion (i.e., a random walk).

A random walk describes a path consisting of a succession of random steps. It has been used by Francis Galton in 1894 to illustrate the *Central Limit Theorem*, and is now known as the **Galton Board**. The Galton Board is a physical model of a random walk, where balls are dropped from the top and bounce left or right at each peg until they reach the bottom. The distribution of the final position of the balls is a normal distribution.

![](media/rt_galtonboard.gif)

::: {.callout-caution}
TODO: Replace with my own video.
:::

In the figure below, we can see a computer simulation illustrating the same concept, with "time" being displayed on the x-axis. All iterations start at *y*=0, and change by -1 or +1 at each time step, until it reaches a threshold of *t* = 0.7 seconds.

![](media/rt_randomwalk.gif)


Random walks are used to model a wide range of phenomena, such as the movement of particules and molecules, the stock market, the behavior of animals and, crucially, **cognitive processes**.
For instance, it can be used to approximate **evidence accumulation**: the idea that a decision maker (be it a Human or any other system) accumulates evidence in a "stochastic" (i.e., random) fashion over time until a certain threshold is reached, at which point a decision is made.

## Wald Distribution (Revisited)

This is how the Wald distribution is actually **generated**. It corresponds to the distribution of times that it takes for a stochastic process to reach a certain **threshold** $\alpha$ (a certain amount of "evidence").
The twist is that the process underlying this model is a random walk with a **drift rate** $\nu$, which corresponds to the average amount of evidence accumulated per unit of time.
In other words, the **drift rate** $\nu$ is the "slope" of the evidence accumulation process, representing the **strength of the evidence** (or the **speed** by which the decision maker accumulates evidence).
The **threshold** $\alpha$ is the amount of evidence required to reach a decision ("decision" typically meaning making a response).

![](media/rt_wald2.gif)

As you can see, the Wald distribution belongs to a family of models thata do not merely attempt at describing the empirical distributions by tweaking and convolving distributions (like the ExGaussian or LogNormal models). Instead, their parameters are characterizing the **data generation process**.

::: {.callout-important}

While such "generative" models offer potential insights into the cognitive processes underlying the data, they inherently make **strong assumptions** about said underlying process (for instance, that the data of a task can be approximated by a stochastic evidence accumulation process). It is thus crucial to always keep in mind the limitations and assumptions of the models we use. Don't forget, **with great power comes great responsability.**
:::

Move from statistical models that *describe* to models that *generate* RT-like data.

## Drift Diffusion Model (DDM)

Expand Down
4 changes: 2 additions & 2 deletions content/_freeze/4a_rt_descriptive/execute-results/html.json

Large diffs are not rendered by default.

94 changes: 94 additions & 0 deletions content/media/animations_rt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -348,3 +348,97 @@ record(make_animation, fig, "rt_randomwalk.gif", frames; framerate=30)



# Walk Generation =====================================================================================


using DataFrames
using SequentialSamplingModels

α = Observable(1.5)
τ = Observable(0.05)
ν = Observable(3.0)

# Initialize the figure
fig = Figure()
ax1 = Axis(
fig[1, 1],
title=@lift("Wald(ν = $(round($ν, digits = 1)), α = $(round($α, digits = 1)), τ = $(round($τ, digits = 2)))"),
# xlabel="Time",
ylabel="Distribution",
yticksvisible=false,
xticksvisible=false,
yticklabelsvisible=false,
xticklabelsvisible=false
)
hidespines!(ax1, :b)
xlims!(ax1; low=0, high=1.5)
ylims!(ax1; low=0, high=3.5)

ax2 = Axis(
fig[2, 1],
# title="Density",
xlabel="Time",
ylabel="Evidence",
yticksvisible=false,
xticksvisible=false,
# ygridvisible=false,
# yticklabelsvisible=false,
# xticklabelsvisible=false
)
hidespines!(ax2, :t)
xlims!(ax2; low=0, high=1.5)
ylims!(ax2; low=-0.5, high=2.5)
rowgap!(fig.layout, 1, 0.1)


# Traces
function make_points=4, α=1.5, τ=0.2, max_time=1500)
trace = simulate(Wald(ν, α, τ); Δt=0.001)[2]

x = τ .+ range(0, 0.001 * length(trace), length=length(trace))
x = collect(x)

points = [(i, j) for (i, j) in zip(x, trace)]
return Point2f.(points)
end

for iter in 1:40
lines!(ax2, @lift(make_points($ν, $α, $τ)),
color=cgrad(:viridis, 40; categorical=true, alpha=0.8)[iter],
linewidth=0.5)
end

# Rest
xaxis = range(0, 1.5, length=1000)
lines!(ax1, xaxis, @lift(pdf.(Wald($ν, $α, $τ), xaxis)), color=:orange)
lines!(ax2, @lift([0, $τ]), [0, 0], color=:red, linewidth=2)
lines!(ax2, [0, 1.5], @lift([$α, $α]), color=:red, linestyle=:dash)
lines!(ax2, @lift([$τ, $τ + 1 / 4]), @lift([0, $ν / 4]), color=:black)
lines!(ax2, @lift([$τ, $τ + 1 / 4]), [0, 0], color=:black, linestyle=:dash)



fig


function make_animation(frame)
if frame < 0.15
τ[] = change_param(frame; frame_range=(0, 0.15), param_range=(0.05, 0.2))
end
if frame >= 0.25 && frame < 0.40
α[] = change_param(frame; frame_range=(0.25, 0.40), param_range=(1.5, 2.4))
end
if frame >= 0.45 && frame < 0.65
ν[] = change_param(frame; frame_range=(0.45, 0.65), param_range=(3.0, 5.0))
end
# Return to normal
if frame >= 0.7 && frame < 0.85
α[] = change_param(frame; frame_range=(0.7, 0.85), param_range=(2.4, 1.5))
τ[] = change_param(frame; frame_range=(0.7, 0.85), param_range=(0.2, 0.05))
ν[] = change_param(frame; frame_range=(0.7, 0.85), param_range=(5.0, 3.0))
end
end

# animation settings
frames = range(0, 1, length=90)
record(make_animation, fig, "rt_wald2.gif", frames; framerate=20)
Binary file added content/media/rt_galtonboard.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added content/media/rt_wald2.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 06bd30b

Please sign in to comment.