ActiveInference

Documentation for ActiveInference.

ActiveInference.bayesian_model_averageMethod
Calculate Bayesian Model Average (BMA)

Calculates the Bayesian Model Average (BMA) which is used for the State Action Prediction Error (SAPE). It is a weighted average of the expected states for all policies weighted by the posterior over policies. The qs_pi_all should be the collection of expected states given all policies. Can be retrieved with the get_expected_states function.

qs_pi_all: Vector{Any}

q_pi: Vector{Float64}

source
ActiveInference.capped_logMethod
capped_log(x::Real)

Arguments

  • x::Real: A real number.

Return the natural logarithm of x, capped at the machine epsilon value of x.

source
ActiveInference.check_probability_distributionMethod

Check if the vector of vectors is a proper probability distribution.

Arguments

  • (Array::Vector{Vector{T}}) where T<:Real

Throws an error if the array is not a valid probability distribution:

  • The values must be non-negative.
  • The sum of the values must be approximately 1.
source
ActiveInference.check_probability_distributionMethod

Check if the vector of arrays is a proper probability distribution.

Arguments

  • (Array::Vector{<:Array{T}}) where T<:Real

Throws an error if the array is not a valid probability distribution:

  • The values must be non-negative.
  • The sum of the values must be approximately 1.
source
ActiveInference.check_probability_distributionMethod

Check if the vector is a proper probability distribution.

Arguments

  • (Vector::Vector{T}) where T<:Real : The vector to be checked.

Throws an error if the array is not a valid probability distribution:

  • The values must be non-negative.
  • The sum of the values must be approximately 1.
source
ActiveInference.construct_policiesMethod
construct_policies(n_states::Vector{T} where T <: Real; n_controls::Union{Vector{T}, Nothing} where T <: Real=nothing, 
                   policy_length::Int=1, controllable_factors_indices::Union{Vector{Int}, Nothing}=nothing)

Construct policies based on the number of states, controls, policy length, and indices of controllable state factors.

Arguments

  • n_states::Vector{T} where T <: Real: A vector containing the number of states for each factor.
  • n_controls::Union{Vector{T}, Nothing} where T <: Real=nothing: A vector specifying the number of allowable actions for each state factor.
  • policy_length::Int=1: The length of policies. (planning horizon)
  • controllable_factors_indices::Union{Vector{Int}, Nothing}=nothing: A vector of indices identifying which state factors are controllable.
source
ActiveInference.create_matrix_templatesFunction
create_matrix_templates(shapes::Vector{Int64}, template_type::String)

Creates templates based on the specified shapes vector and template type. Templates can be uniform, random, or filled with zeros.

Arguments

  • shapes::Vector{Int64}: A vector specifying the dimensions of each template to create.
  • template_type::String: The type of templates to create. Can be "uniform" (default), "random", or "zeros".

Returns

  • A vector of arrays, each corresponding to the shape given by the input vector.
source
ActiveInference.create_matrix_templatesMethod
create_matrix_templates(n_states::Vector{Int64}, n_observations::Vector{Int64}, n_controls::Vector{Int64}, policy_length::Int64, template_type::String = "uniform")

Creates templates for the A, B, C, D, and E matrices based on the specified parameters.

Arguments

  • n_states::Vector{Int64}: A vector specifying the dimensions and number of states.
  • n_observations::Vector{Int64}: A vector specifying the dimensions and number of observations.
  • n_controls::Vector{Int64}: A vector specifying the number of controls per factor.
  • policy_length::Int64: The length of the policy sequence.
  • template_type::String: The type of templates to create. Can be "uniform", "random", or "zeros". Defaults to "uniform".

Returns

  • A, B, C, D, E: The generative model as matrices and vectors.
source
ActiveInference.create_matrix_templatesMethod
create_matrix_templates(shapes::Vector{Int64})

Creates uniform templates based on the specified shapes vector.

Arguments

  • shapes::Vector{Int64}: A vector specifying the dimensions of each template to create.

Returns

  • A vector of normalized arrays.
source
ActiveInference.create_matrix_templatesMethod
create_matrix_templates(shapes::Vector{Vector{Int64}}, template_type::String)

Creates a multidimensional template based on the specified vector of shape vectors and template type. Templates can be uniform, random, or filled with zeros.

Arguments

  • shapes::Vector{Vector{Int64}}: A vector of vectors, where each vector represent a dimension of the template to create.
  • template_type::String: The type of templates to create. Can be "uniform" (default), "random", or "zeros".

Returns

  • A vector of arrays, each having the multi-dimensional shape specified in the input vector.
source
ActiveInference.create_matrix_templatesMethod
create_matrix_templates(shapes::Vector{Vector{Int64}})

Creates a uniform, multidimensional template based on the specified shapes vector.

Arguments

  • shapes::Vector{Vector{Int64}}: A vector of vectors, where each vector represent a dimension of the template to create.

Returns

  • A vector of normalized arrays (uniform distributions), each having the multi-dimensional shape specified in the input vector.
source
ActiveInference.get_expected_statesMethod
Multiple dispatch for all expected states given all policies

Multiple dispatch for getting expected states for all policies based on the agents currently inferred states and the transition matrices for each factor and action in the policy.

qs::Vector{Vector{Real}}

B: Vector{Array{<:Real}}

policy: Vector{Matrix{Int64}}

source
ActiveInference.init_aifMethod

Initialize Active Inference Agent function initaif( A, B; C=nothing, D=nothing, E = nothing, pA = nothing, pB = nothing, pD = nothing, parameters::Union{Nothing, Dict{String,Real}} = nothing, settings::Union{Nothing, Dict} = nothing, savehistory::Bool = true)

Arguments

  • 'A': Relationship between hidden states and observations.
  • 'B': Transition probabilities.
  • 'C = nothing': Prior preferences over observations.
  • 'D = nothing': Prior over initial hidden states.
  • 'E = nothing': Prior over policies. (habits)
  • 'pA = nothing':
  • 'pB = nothing':
  • 'pD = nothing':
  • 'parameters::Union{Nothing, Dict{String,Real}} = nothing':
  • 'settings::Union{Nothing, Dict} = nothing':
  • 'settings::Union{Nothing, Dict} = nothing':
source
ActiveInference.kl_divergenceMethod
kl_divergence(P::Vector{Vector{Vector{Float64}}}, Q::Vector{Vector{Vector{Float64}}})

Arguments

  • P::Vector{Vector{Vector{Real}}}
  • Q::Vector{Vector{Vector{Real}}}

Return the Kullback-Leibler (KL) divergence between two probability distributions.

source
ActiveInference.process_observationMethod
process_observation(observation::Int, n_modalities::Int, n_observations::Vector{Int})

Process a single modality observation. Returns a one-hot encoded vector.

Arguments

  • observation::Int: The index of the observed state with a single observation modality.
  • n_modalities::Int: The number of observation modalities in the observation.
  • n_observations::Vector{Int}: A vector containing the number of observations for each modality.

Returns

  • Vector{Vector{Real}}: A vector containing a single one-hot encoded observation.
source
ActiveInference.process_observationMethod
process_observation(observation::Union{Array{Int}, Tuple{Vararg{Int}}}, n_modalities::Int, n_observations::Vector{Int})

Process observation with multiple modalities and return them in a one-hot encoded format

Arguments

  • observation::Union{Array{Int}, Tuple{Vararg{Int}}}: A collection of indices of the observed states for each modality.
  • n_modalities::Int: The number of observation modalities in the observation.
  • n_observations::Vector{Int}: A vector containing the number of observations for each modality.

Returns

  • Vector{Vector{Real}}: A vector containing one-hot encoded vectors for each modality.
source
ActiveInference.Environments.bayesian_model_averageMethod
Calculate Bayesian Model Average (BMA)

Calculates the Bayesian Model Average (BMA) which is used for the State Action Prediction Error (SAPE). It is a weighted average of the expected states for all policies weighted by the posterior over policies. The qs_pi_all should be the collection of expected states given all policies. Can be retrieved with the get_expected_states function.

qs_pi_all: Vector{Any}

q_pi: Vector{Float64}

source
ActiveInference.Environments.kl_divergenceMethod
kl_divergence(P::Vector{Vector{Vector{Float64}}}, Q::Vector{Vector{Vector{Float64}}})

Arguments

  • P::Vector{Vector{Vector{Real}}}
  • Q::Vector{Vector{Vector{Real}}}

Return the Kullback-Leibler (KL) divergence between two probability distributions.

source