Skip to content

Commit

Permalink
Merge pull request #107 from ilabcode/dev
Browse files Browse the repository at this point in the history
version 0.5.1
  • Loading branch information
PTWaade authored May 1, 2024
2 parents 09812f3 + f68fbaf commit 4a487cc
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ authors = ["Peter Thestrup Waade ptw@cas.au.dk",
"Anna Hedvig Møller hedvig.2808@gmail.com",
"Jacopo Comoglio jacopo.comoglio@gmail.com",
"Christoph Mathys chmathys@cas.au.dk"]
version = "0.5.0"
version = "0.5.1"

[deps]
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Expand Down
34 changes: 24 additions & 10 deletions src/fitting/create_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -143,24 +143,38 @@ Create a Turing model object used for fitting an ActionModels agent.
#If there is only a single action
if !multiple_actions

#If the action isn't missing, or if missing actions are to be imputed
if !ismissing(actions[group][timestep]) || impute_missing_actions
#Sample the action from the probability distribution
actions[group][timestep] ~ action_distribution
# If missing actions are to be imputed, we do not care if the action exists
if !impute_missing_actions
# If we should not impute missing actions, we need to check if the action exists
@inbounds action_exists = !ismissing(actions[group][timestep])
# if the action doesn't exist, we skip this timestep
if !action_exists
continue
end
end

#Sample the action from the probability distribution
actions[group][timestep] ~ action_distribution


#If there are multiple actions
else
#Go through each separate action
for (action_idx, single_distribution) in enumerate(action_distribution)

#If the action isn't missing, or if missing actions are to be imputed
if !ismissing(actions[group][timestep, action_idx]) ||
impute_missing_actions

#Sample the action from the probability distribution
actions[group][timestep, action_idx] ~ single_distribution
# If missing actions are to be imputed, we do not care if the action exists
if !impute_missing_actions
# If we should not impute missing actions, we need to check if the action exists
@inbounds action_exists = !ismissing(actions[group][timestep, action_idx])
# if the action doesn't exist, we skip this timestep
if !action_exists
continue
end
end

#Sample the action from the probability distribution
@inbounds actions[group][timestep, action_idx] ~ single_distribution

end
end
end
Expand Down
9 changes: 3 additions & 6 deletions src/fitting/fit_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function fit_model(
input_cols::Vector = [:input],
action_cols::Vector = [:action],
fixed_parameters::Dict = Dict(),
sampler::Union{Missing, DynamicPPL.Sampler} = missing,
sampler::Turing.Inference.InferenceAlgorithm = NUTS(-1, 0.65; adtype=AutoReverseDiff(true)),
n_cores::Integer = 1,
n_iterations::Integer = 1000,
n_chains::Integer = 2,
Expand All @@ -56,10 +56,7 @@ function fit_model(
sampler_kwargs...,
)
### SETUP ###
# If no sampler has been specified, use NUTS
if ismissing(sampler)
sampler = NUTS(-1, 0.65; adtype=AutoReverseDiff(true))
end

#Convert column names to symbols
independent_group_cols = Symbol.(independent_group_cols)
multilevel_group_cols = Symbol.(multilevel_group_cols)
Expand Down Expand Up @@ -326,7 +323,7 @@ function fit_model(
inputs::Array,
actions::Array;
fixed_parameters::Dict = Dict(),
sampler::Union{Missing, DynamicPPL.Sampler} = missing,
sampler::Turing.Inference.InferenceAlgorithm = NUTS(-1, 0.65; adtype=AutoReverseDiff(true)),
n_cores::Integer = 1,
n_iterations::Integer = 1000,
n_chains = 2,
Expand Down
20 changes: 20 additions & 0 deletions test/testsuite/fitting_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,23 @@ end
)

end


@testset "ensure parameters are reset after fitting" begin

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

initial_parameters = get_parameters(agent)

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)

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

@test get_parameters(agent) == initial_parameters

end

2 comments on commit 4a487cc

@PTWaade
Copy link
Collaborator Author

@PTWaade PTWaade commented on 4a487cc May 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/105956

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.5.1 -m "<description of version>" 4a487cc702bcdefb3b024f462ad1ba7aec526e89
git push origin v0.5.1

Please sign in to comment.