Skip to content

Commit

Permalink
Fixes ilabcode#106, allow external samplers.
Browse files Browse the repository at this point in the history
  • Loading branch information
zeyus committed May 11, 2024
1 parent bb44028 commit 2c00dac
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
9 changes: 4 additions & 5 deletions src/fitting/fit_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Use Turing to fit the parameters of an agent to a set of inputs and correspondin
- 'inputs:Array': array of inputs. Each row is a timestep, and each column is a single input value.
- 'actions::Array': array of actions. Each row is a timestep, and each column is a single action.
- 'fixed_parameters::Dict = Dict()': dictionary containing parameter values for parameters that are not fitted. Keys are parameter names, values are priors. For parameters not specified here and without priors, the parameter values of the agent are used instead.
- 'sampler::Union{Missing, DynamicPPL.Sampler} = missing': specify the type of Turing sampler. Defaults to `NUTS(-1, 0.65; adtype=AutoReverseDiff(true))`
- 'sampler::Union{DynamicPPL.AbstractSampler, Turing.Inference.InferenceAlgorithm}': specify the type of Turing sampler. Defaults to `NUTS(-1, 0.65; adtype=AutoReverseDiff(true))`
- 'n_cores = 1': set number of cores to use for parallelization. If set to 1, no parallelization is used.
- 'n_iterations = 1000': set number of iterations per chain.
- 'n_chains = 2': set number of amount of chains.
Expand Down Expand Up @@ -46,7 +46,7 @@ function fit_model(
input_cols::Vector = [:input],
action_cols::Vector = [:action],
fixed_parameters::Dict = Dict(),
sampler::Turing.Inference.InferenceAlgorithm = NUTS(
sampler::Union{DynamicPPL.AbstractSampler, Turing.Inference.InferenceAlgorithm} = NUTS(
-1,
0.65;
adtype = AutoReverseDiff(true),
Expand Down Expand Up @@ -103,7 +103,6 @@ function fit_model(
## Store whether there are multiple inputs and actions ##
multiple_inputs = length(input_cols) > 1
multiple_actions = length(action_cols) > 1
multilevel = length(multilevel_group_cols) > 0

## Structure multilevel parameter information ##
general_parameters_info = extract_structured_parameter_info(;
Expand Down Expand Up @@ -307,7 +306,7 @@ Use Turing to fit the parameters of an agent to a set of inputs and correspondin
- 'inputs:Array': array of inputs. Each row is a timestep, and each column is a single input value.
- 'actions::Array': array of actions. Each row is a timestep, and each column is a single action.
- 'fixed_parameters::Dict = Dict()': dictionary containing parameter values for parameters that are not fitted. Keys are parameter names, values are priors. For parameters not specified here and without priors, the parameter values of the agent are used instead.
- 'sampler::Union{Missing, DynamicPPL.Sampler} = missing': specify the type of Turing sampler, defaults to `NUTS(-1, 0.65; adtype=AutoReverseDiff(true))`
- 'sampler::Union{DynamicPPL.AbstractSampler, Turing.Inference.InferenceAlgorithm}: specify the type of Turing sampler, defaults to `NUTS(-1, 0.65; adtype=AutoReverseDiff(true))`
- 'n_cores = 1': set number of cores to use for parallelization. If set to 1, no parallelization is used.
- 'n_iterations = 1000': set number of iterations per chain.
- 'n_chains = 2': set number of amount of chains.
Expand All @@ -334,7 +333,7 @@ function fit_model(
inputs::Array,
actions::Array;
fixed_parameters::Dict = Dict(),
sampler::Turing.Inference.InferenceAlgorithm = NUTS(
sampler::Union{DynamicPPL.AbstractSampler, Turing.Inference.InferenceAlgorithm} = NUTS(
-1,
0.65;
adtype = AutoReverseDiff(true),
Expand Down
2 changes: 2 additions & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
[deps]
AdvancedMH = "5b7e9947-ddc0-4b3f-9b55-0d8042f74170"
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
Glob = "c27321d9-0574-5035-807b-f59d2c89b15c"
MicroCanonicalHMC = "234d2aa0-2291-45f7-9047-6fa6f316b0a8"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
StatsPlots = "f3b207a7-027a-5e70-b257-86293d7955fd"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Expand Down
29 changes: 28 additions & 1 deletion test/testsuite/fitting_tests.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@

using AdvancedMH
using ActionModels
using Test
using Distributions
using DataFrames
using Plots
using StatsPlots

using Turing: externalsampler


@testset "simulate actions and fit" begin
Expand Down Expand Up @@ -183,3 +184,29 @@ end
@test get_parameters(agent) == initial_parameters

end


@testset "Make sure fitting allows using a custom sampler" begin

agent = premade_agent("binary_rescorla_wagner_softmax", verbose = false)

param_priors = Dict("learning_rate" => Uniform(0, 1))

inputs = [1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0]

actions = give_inputs!(agent, inputs)

rwmh = externalsampler(AdvancedMH.RWMH(10))

chains = fit_model(
agent,
param_priors,
inputs,
actions,
n_chains = 1,
n_iterations = 10,
verbose = false,
sampler = ext_sampler,
)

end

0 comments on commit 2c00dac

Please sign in to comment.