Skip to content

Commit

Permalink
Merge pull request #133 from ilabcode/misc_polish
Browse files Browse the repository at this point in the history
Misc polish
  • Loading branch information
PTWaade authored Apr 15, 2024
2 parents ea3a17c + 0eea8bc commit b6f2655
Show file tree
Hide file tree
Showing 74 changed files with 4,140 additions and 4,490 deletions.
10 changes: 8 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
*.jl.*.cov
*.jl.cov
*.jl.mem
/docs/build/
.DS_Store
.vscode

testing_script*.jl
settings.json

Manifest.toml
docs/Manifest.toml
test/Manifest.toml
/docs/src/generated_markdowns/*.md

/docs/src/generated
/docs/src/index.md
/docs/build/
/build
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ authors = [ "Peter Thestrup Waade [email protected]",
"Anna Hedvig Møller [email protected]",
"Jacopo Comoglio [email protected]",
"Christoph Mathys [email protected]"]
version = "0.3.3"
version = "0.5.0"

[deps]
ActionModels = "320cf53b-cc3b-4b34-9a10-0ecb113566a3"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"

[compat]
ActionModels = "0.4"
ActionModels = "0.5"
Distributions = "0.25"
RecipesBase = "1"
julia = "1.9"
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ premade_agent("help")
### Create agent

````@example index
agent = premade_agent("hgf_binary_softmax_action")
agent = premade_agent("hgf_binary_softmax")
````

### Get states and parameters
Expand All @@ -55,10 +55,10 @@ get_parameters(agent)

![Image1](docs/src/images/readme/get_parameters.png)

Set a new parameter for initial precision of x2 and define some inputs
Set a new parameter for initial precision of xprob and define some inputs

````@example index
set_parameters!(agent, ("x2", "initial_precision"), 0.9)
set_parameters!(agent, ("xprob", "initial_precision"), 0.9)
inputs = [1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0];
nothing #hide
````
Expand All @@ -75,23 +75,23 @@ actions = give_inputs!(agent, inputs)
using StatsPlots
using Plots
plot_trajectory(agent, ("u", "input_value"))
plot_trajectory!(agent, ("x1", "prediction"))
plot_trajectory!(agent, ("x", "prediction"))
````
![Image1](docs/src/images/readme/plot_trajectory.png)

Plot state trajectory of input value, action and prediction of x1
Plot state trajectory of input value, action and prediction of x

````@example index
plot_trajectory(agent, ("u", "input_value"))
plot_trajectory!(agent, "action")
plot_trajectory!(agent, ("x1", "prediction"))
plot_trajectory!(agent, ("x", "prediction"))
````
![Image1](docs/src/images/readme/plot_trajectory_2.png)
### Fitting parameters

````@example index
using Distributions
prior = Dict(("x2", "volatility") => Normal(1, 0.5))
prior = Dict(("xprob", "volatility") => Normal(1, 0.5))
model = fit_model(agent, prior, inputs, actions, n_iterations = 20)
````
Expand Down
2 changes: 1 addition & 1 deletion docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
Glob = "c27321d9-0574-5035-807b-f59d2c89b15c"
HierarchicalGaussianFiltering = "63d42c3e-681c-42be-892f-a47f35336a79"
JuliaFormatter = "98e50ef6-434e-11e9-1051-2b60c6c9e899"
Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
StatsPlots = "f3b207a7-027a-5e70-b257-86293d7955fd"
Turing = "fce5fe82-541a-59a6-adf8-730c64b5f9a0"
14 changes: 7 additions & 7 deletions docs/src/Julia_src_files/index.jl → docs/julia_files/index.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ using ActionModels
premade_agent("help")

# ### Create agent
agent = premade_agent("hgf_binary_softmax_action")
agent = premade_agent("hgf_binary_softmax")

# ### Get states and parameters
get_states(agent)
#-
get_parameters(agent)

# Set a new parameter for initial precision of x2 and define some inputs
set_parameters!(agent, ("x2", "initial_precision"), 0.9)
# Set a new parameter for initial precision of xprob and define some inputs
set_parameters!(agent, ("xprob", "initial_precision"), 0.9)
inputs = [1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0];

# ### Give inputs to the agent
Expand All @@ -42,18 +42,18 @@ actions = give_inputs!(agent, inputs)
using StatsPlots
using Plots
plot_trajectory(agent, ("u", "input_value"))
plot_trajectory!(agent, ("x1", "prediction"))
plot_trajectory!(agent, ("xbin", "prediction"))

# Plot state trajectory of input value, action and prediction of x1
# Plot state trajectory of input value, action and prediction of xbin
plot_trajectory(agent, ("u", "input_value"))
plot_trajectory!(agent, "action")
plot_trajectory!(agent, ("x1", "prediction"))
plot_trajectory!(agent, ("xbin", "prediction"))


# ### Fitting parameters

using Distributions
prior = Dict(("x2", "volatility") => Normal(1, 0.5))
prior = Dict(("xprob", "volatility") => Normal(1, 0.5))

model = fit_model(agent, prior, inputs, actions, n_iterations = 20)

Expand Down
43 changes: 43 additions & 0 deletions docs/julia_files/tutorials/classic_JGET.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using ActionModels, HierarchicalGaussianFiltering
using CSV, DataFrames
using Plots, StatsPlots
using Distributions


# Get the path for the HGF superfolder
hgf_path = dirname(dirname(pathof(HierarchicalGaussianFiltering)))
# Add the path to the data files
data_path = hgf_path * "/docs/src/tutorials/data/"

#Load data
data = CSV.read(data_path * "classic_cannonball_data.csv", DataFrame)
inputs = data[(data.ID.==20).&(data.session.==1), :].outcome

#Create HGF
hgf = premade_hgf("JGET", verbose = false)
#Create agent
agent = premade_agent("hgf_gaussian", hgf)
#Set parameters
parameters = Dict(
"action_noise" => 1,
("u", "input_noise") => 0,
("x", "initial_mean") => first(inputs) + 2,
("x", "initial_precision") => 0.001,
("x", "volatility") => -8,
("xvol", "volatility") => -8,
("xnoise", "volatility") => -7,
("xnoise_vol", "volatility") => -2,
("x", "xvol", "coupling_strength") => 1,
("xnoise", "xnoise_vol", "coupling_strength") => 1,
)
set_parameters!(agent, parameters)
reset!(agent)

#Simulate updates and actions
actions = give_inputs!(agent, inputs);
#Plot belief trajectories
plot_trajectory(agent, "u")
plot_trajectory!(agent, "x")
plot_trajectory(agent, "xvol")
plot_trajectory(agent, "xnoise")
plot_trajectory(agent, "xnoise_vol")
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ using Distributions
# Get the path for the HGF superfolder
hgf_path = dirname(dirname(pathof(HierarchicalGaussianFiltering)))
# Add the path to the data files
data_path = hgf_path * "/docs/src/tutorials/data/"
data_path = hgf_path * "/docs/julia_files/tutorials/data/"

# Load the data
inputs = CSV.read(data_path * "classic_binary_inputs.csv", DataFrame)[!, 1];
Expand All @@ -23,59 +23,57 @@ inputs = CSV.read(data_path * "classic_binary_inputs.csv", DataFrame)[!, 1];
hgf_parameters = Dict(
("u", "category_means") => Real[0.0, 1.0],
("u", "input_precision") => Inf,
("x2", "volatility") => -2.5,
("x2", "initial_mean") => 0,
("x2", "initial_precision") => 1,
("x3", "volatility") => -6.0,
("x3", "initial_mean") => 1,
("x3", "initial_precision") => 1,
("x1", "x2", "value_coupling") => 1.0,
("x2", "x3", "volatility_coupling") => 1.0,
("xprob", "volatility") => -2.5,
("xprob", "initial_mean") => 0,
("xprob", "initial_precision") => 1,
("xvol", "volatility") => -6.0,
("xvol", "initial_mean") => 1,
("xvol", "initial_precision") => 1,
("xbin", "xprob", "coupling_strength") => 1.0,
("xprob", "xvol", "coupling_strength") => 1.0,
);

hgf = premade_hgf("binary_3level", hgf_parameters, verbose = false);

# Create an agent
agent_parameters = Dict("sigmoid_action_precision" => 5);
agent =
premade_agent("hgf_unit_square_sigmoid_action", hgf, agent_parameters, verbose = false);
agent_parameters = Dict("action_noise" => 0.2);
agent = premade_agent("hgf_unit_square_sigmoid", hgf, agent_parameters, verbose = false);

# Evolve agent and save actions
actions = give_inputs!(agent, inputs);

# Plot the trajectory of the agent
plot_trajectory(agent, ("u", "input_value"))
plot_trajectory!(agent, ("x1", "prediction"))

plot_trajectory!(agent, ("xbin", "prediction"))

# -

plot_trajectory(agent, ("x2", "posterior"))
plot_trajectory(agent, ("x3", "posterior"))
plot_trajectory(agent, ("xprob", "posterior"))
plot_trajectory(agent, ("xvol", "posterior"))

# Set fixed parameters
fixed_parameters = Dict(
"sigmoid_action_precision" => 5,
"action_noise" => 0.2,
("u", "category_means") => Real[0.0, 1.0],
("u", "input_precision") => Inf,
("x2", "initial_mean") => 0,
("x2", "initial_precision") => 1,
("x3", "initial_mean") => 1,
("x3", "initial_precision") => 1,
("x1", "x2", "value_coupling") => 1.0,
("x2", "x3", "volatility_coupling") => 1.0,
("x3", "volatility") => -6.0,
("xprob", "initial_mean") => 0,
("xprob", "initial_precision") => 1,
("xvol", "initial_mean") => 1,
("xvol", "initial_precision") => 1,
("xbin", "xprob", "coupling_strength") => 1.0,
("xprob", "xvol", "coupling_strength") => 1.0,
("xvol", "volatility") => -6.0,
);

# Set priors for parameter recovery
param_priors = Dict(("x2", "volatility") => Normal(-3.0, 0.5));
param_priors = Dict(("xprob", "volatility") => Normal(-3.0, 0.5));
#-
# Prior predictive plot
plot_predictive_simulation(
param_priors,
agent,
inputs,
("x1", "prediction_mean"),
("xbin", "prediction_mean"),
n_simulations = 100,
)
#-
Expand Down Expand Up @@ -104,6 +102,6 @@ plot_predictive_simulation(
fitted_model,
agent,
inputs,
("x1", "prediction_mean"),
("xbin", "prediction_mean"),
n_simulations = 3,
)
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ using Distributions
# Get the path for the HGF superfolder
hgf_path = dirname(dirname(pathof(HierarchicalGaussianFiltering)))
# Add the path to the data files
data_path = hgf_path * "/docs/src/tutorials/data/"
data_path = hgf_path * "/docs/julia_files/tutorials/data/"

# Load the data
inputs = Float64[]
Expand All @@ -24,20 +24,19 @@ end

#Create HGF
hgf = premade_hgf("continuous_2level", verbose = false);
agent = premade_agent("hgf_gaussian_action", hgf, verbose = false);
agent = premade_agent("hgf_gaussian", hgf, verbose = false);

# Set parameters for parameter recover
parameters = Dict(
("u", "x1", "value_coupling") => 1.0,
("x1", "x2", "volatility_coupling") => 1.0,
("x", "xvol", "coupling_strength") => 1.0,
("u", "input_noise") => -log(1e4),
("x1", "volatility") => -13,
("x2", "volatility") => -2,
("x1", "initial_mean") => 1.04,
("x1", "initial_precision") => 1 / (0.0001),
("x2", "initial_mean") => 1.0,
("x2", "initial_precision") => 1 / 0.1,
"gaussian_action_precision" => 100,
("x", "volatility") => -13,
("xvol", "volatility") => -2,
("x", "initial_mean") => 1.04,
("x", "initial_precision") => 1 / (0.0001),
("xvol", "initial_mean") => 1.0,
("xvol", "initial_precision") => 1 / 0.1,
"action_noise" => 0.01,
);

set_parameters!(agent, parameters)
Expand All @@ -59,7 +58,7 @@ plot_trajectory(
xlabel = "Trading days since 1 January 2010",
)
#-
plot_trajectory!(agent, ("x1", "posterior"), color = "red")
plot_trajectory!(agent, ("x", "posterior"), color = "red")
plot_trajectory!(
agent,
"action",
Expand All @@ -71,7 +70,7 @@ plot_trajectory!(
#-
plot_trajectory(
agent,
"x2",
"xvol",
color = "blue",
size = (1300, 500),
xlims = (0, 615),
Expand All @@ -81,27 +80,26 @@ plot_trajectory(
#-
# Set priors for fitting
fixed_parameters = Dict(
("u", "x1", "value_coupling") => 1.0,
("x1", "x2", "volatility_coupling") => 1.0,
("x1", "initial_mean") => 0,
("x1", "initial_precision") => 2000,
("x2", "initial_mean") => 1.0,
("x2", "initial_precision") => 600.0,
"gaussian_action_precision" => 100,
("x", "xvol", "coupling_strength") => 1.0,
("x", "initial_mean") => 0,
("x", "initial_precision") => 2000,
("xvol", "initial_mean") => 1.0,
("xvol", "initial_precision") => 600.0,
);

param_priors = Dict(
("u", "input_noise") => Normal(-6, 1),
("x1", "volatility") => Normal(-4, 1),
("x2", "volatility") => Normal(-4, 1),
("x", "volatility") => Normal(-4, 1),
("xvol", "volatility") => Normal(-4, 1),
"action_noise" => LogNormal(log(0.01), 1),
);
#-
# Prior predictive simulation plot
plot_predictive_simulation(
param_priors,
agent,
inputs,
("x1", "posterior_mean");
("x", "posterior_mean");
n_simulations = 100,
)
#-
Expand Down
File renamed without changes.
Loading

0 comments on commit b6f2655

Please sign in to comment.