Variations of utility functions in the Hierarchical Gaussian Filtering package

A lot of commonly used utility functions are collected here in an overview with examples. The following utility functions can be used:

  1. Getting Parameters
  2. Getting States
  3. Setting Parameters
  4. Giving Inputs
  5. Getting History
  6. Plotting State Trajectories
  7. Getting Predictions
  8. Getting Surprise
  9. Resetting an HGF-agent

we start by defining an agent to use

using HierarchicalGaussianFiltering

set agent

action_model = ActionModel(HGFSoftmax(; HGF = "binary_3level"))

agent = init_agent(action_model)
-- ActionModels Agent --
Action model: hgf_softmax
This agent has received 0 observations

Getting Parameters

#Let us start by defining a premade agent:

#getting all parameters
get_parameters(agent)
(action_noise = 1.0, xprob_drift = 0, xvol_autoconnection_strength = 1, xvol_initial_mean = 0, xbin_xprob_coupling_strength = 1, xprob_autoconnection_strength = 1, xvol_volatility = -2, xprob_initial_precision = 1, xprob_initial_mean = 0, xvol_drift = 0, xvol_initial_precision = 1, xprob_xvol_coupling_strength = 1, xprob_volatility = -2)

getting couplings

get_parameters(agent, :xprob_xvol_coupling_strength)
1

getting multiple parameters specify them in a vector

get_parameters(agent, (:xvol_volatility, :xvol_initial_precision))
(xvol_volatility = -2, xvol_initial_precision = 1)

Getting States

#getting all states from an agent model
get_states(agent)

#getting a single state
get_states(agent, :xprob_posterior_precision)

#getting multiple states
get_states(agent, (:xprob_posterior_precision, :xprob_effective_prediction_precision))
(xprob_posterior_precision = 1, xprob_effective_prediction_precision = 0.11920292202211755)

Setting Parameters

you can set parameters before you initialize your agent, you can set them after and change them when you wish to. Let's try an initialize a new agent with parameters. We start by choosing the premade unit square sigmoid action agent whose parameter is sigmoid action precision. We also specify our HGF and custom parameter settings:

hgf_parameters = Dict(
    ("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)

Define our agent with the HGF and agent parameter settings

action_model = ActionModel(HGFSigmoid(; HGF = hgf, action_noise = 1.0))

agent = init_agent(
    action_model,
    save_history = [:xbin_prediction_mean, :xvol_posterior_precision],
)
-- ActionModels Agent --
Action model: hgf_sigmoid
This agent has received 0 observations

Changing a single parameter

set_parameters!(agent, :xvol_initial_precision, 4)

Changing multiple parameters

set_parameters!(agent, (xvol_initial_precision = 5, xbin_xprob_coupling_strength = 2.0))

###Giving Inputs

#give single input
simulate!(agent, [0])
1-element Vector{Bool}:
 1
#reset the agent
reset!(agent)

Giving multiple inputs

inputs = [
    1,
    0,
    0,
    1,
    1,
    1,
    1,
    1,
    0,
    1,
    0,
    1,
    0,
    1,
    0,
    1,
    0,
    0,
    0,
    0,
    0,
    0,
    1,
    0,
    0,
    0,
    1,
    1,
    1,
    1,
    1,
]
simulate!(agent, inputs)
31-element Vector{Bool}:
 1
 1
 1
 0
 0
 1
 1
 1
 1
 0
 1
 1
 1
 1
 1
 1
 1
 0
 0
 0
 0
 0
 0
 1
 0
 0
 0
 1
 1
 1
 1

Getting History

#getting the history from the agent
get_history(agent)
(xbin_prediction_mean = Union{Missing, Float64}[missing, 0.5, 0.7503289698102095, 0.40912500803316554, 0.2576299032199003, 0.5556083680373223, 0.7173221091874652, 0.8019245536064432, 0.8527772539267372, 0.8861715307229966, 0.5187738328063045, 0.7152593423991487, 0.4228407813117744, 0.6485036901608904, 0.39638766699899824, 0.6277808403005352, 0.3871758132636294, 0.6211684483652263, 0.3838651010282342, 0.2553297003866363, 0.18281848000250242, 0.13753370971646614, 0.10719210485661021, 0.08583619314272904, 0.48766453386135844, 0.2785737559448291, 0.1907653709203936, 0.1412235910795823, 0.49420741757824416, 0.6988377988340833, 0.7941214745572319, 0.8486561100233567], xvol_posterior_precision = Union{Missing, Float64}[5.0, 4.972966010927402, 4.933468460315181, 4.937279980194738, 4.920674770170488, 4.930924934500309, 4.948379692483691, 4.961678259716024, 4.967166986826764, 4.884801770285868, 4.880628849813452, 4.867147563312211, 4.871780586914335, 4.875310735027138, 4.883409433519511, 4.890747023369545, 4.899202664565154, 4.907162856270055, 4.926463138442698, 4.945791368549489, 4.958355032201732, 4.962419230484382, 4.958389763090313, 4.843280628395418, 4.834600857247119, 4.846083822249297, 4.858108785733153, 4.79967883426511, 4.800362586143364, 4.8175772797894725, 4.833943067400438, 4.843362952962739])

getting history of single state

get_history(agent, :xvol_posterior_precision)
32-element Vector{Union{Missing, Float64}}:
 5.0
 4.972966010927402
 4.933468460315181
 4.937279980194738
 4.920674770170488
 4.930924934500309
 4.948379692483691
 4.961678259716024
 4.967166986826764
 4.884801770285868
 4.880628849813452
 4.867147563312211
 4.871780586914335
 4.875310735027138
 4.883409433519511
 4.890747023369545
 4.899202664565154
 4.907162856270055
 4.926463138442698
 4.945791368549489
 4.958355032201732
 4.962419230484382
 4.958389763090313
 4.843280628395418
 4.834600857247119
 4.846083822249297
 4.858108785733153
 4.79967883426511
 4.800362586143364
 4.8175772797894725
 4.833943067400438
 4.843362952962739

getting history of multiple states:

get_history(agent, (:xbin_prediction_mean, :xvol_posterior_precision))
(xbin_prediction_mean = Union{Missing, Float64}[missing, 0.5, 0.7503289698102095, 0.40912500803316554, 0.2576299032199003, 0.5556083680373223, 0.7173221091874652, 0.8019245536064432, 0.8527772539267372, 0.8861715307229966, 0.5187738328063045, 0.7152593423991487, 0.4228407813117744, 0.6485036901608904, 0.39638766699899824, 0.6277808403005352, 0.3871758132636294, 0.6211684483652263, 0.3838651010282342, 0.2553297003866363, 0.18281848000250242, 0.13753370971646614, 0.10719210485661021, 0.08583619314272904, 0.48766453386135844, 0.2785737559448291, 0.1907653709203936, 0.1412235910795823, 0.49420741757824416, 0.6988377988340833, 0.7941214745572319, 0.8486561100233567], xvol_posterior_precision = Union{Missing, Float64}[5.0, 4.972966010927402, 4.933468460315181, 4.937279980194738, 4.920674770170488, 4.930924934500309, 4.948379692483691, 4.961678259716024, 4.967166986826764, 4.884801770285868, 4.880628849813452, 4.867147563312211, 4.871780586914335, 4.875310735027138, 4.883409433519511, 4.890747023369545, 4.899202664565154, 4.907162856270055, 4.926463138442698, 4.945791368549489, 4.958355032201732, 4.962419230484382, 4.958389763090313, 4.843280628395418, 4.834600857247119, 4.846083822249297, 4.858108785733153, 4.79967883426511, 4.800362586143364, 4.8175772797894725, 4.833943067400438, 4.843362952962739])

Plotting State Trajectories

using StatsPlots
# Plotting single state:
plot(agent, ("u", "input_value"))

#Adding state trajectory on top
plot!(agent, ("xbin", "prediction"))

Plotting more individual states:

# Plot posterior of xprob
plot(agent, ("xprob", "posterior"))
# Plot posterior of xvol
plot(agent, ("xvol", "posterior"))

Getting Predictions

You can specify an HGF or an agent in the funciton.

#specify another node to get predictions from:
get_prediction(agent.model_attributes.submodel, "xprob")
(prediction_mean = 1.0140697762060709, prediction_precision = 1.3829697067465927, effective_prediction_precision = 0.3054256455896191)

Getting Purprise

#getting surprise of input node
get_surprise(agent.model_attributes.submodel, "u")
0.16410122770292793

Resetting an HGF-agent

resetting the agent with reset()

reset!(agent)

see that action state is cleared

get_history(agent)
(xbin_prediction_mean = Union{Missing, Float64}[missing], xvol_posterior_precision = Union{Missing, Float64}[5.0])

This page was generated using Literate.jl.