Scruff.SFuncs

Scruff.SFuncs.ApplyType
Apply{J, O} <: SFunc{Tuple{SFunc{J, O}, J}, O}

Apply represents an sfunc that takes two groups of arguments. The first group is a single argument, which is an sfunc to apply to the second group of arguments.

Additional supported operators

  • support
  • support_quality
  • sample
  • logcpdf
  • compute_pi
  • send_lambda

Type parameters

  • J: the input type of the sfunc that may be applied; that sfunc is the input type of the Apply
  • O: the output type of the sfunc that may be applied, which is also the output type of the Apply
source
Scruff.SFuncs.CatType
mutable struct Cat{O} <: Dist{O, Vector{Real}}

Cat defines an sfunc that represents a set of categorical output values that are not conditioned on any input. Its parameters are always of type Vector{Real}, which is the probability of each output value.

Supported operations

  • support
  • support_quality
  • sample
  • cpdf
  • bounded_probs
  • compute_pi
  • f_expectation

Type parameters

  • O: the output type of the Cat
source
Scruff.SFuncs.ChainType
struct Chain{I, J, K, O} <: Conditional{I, J, K, O, Nothing, Q, SFunc{J, O, Nothing}}

A Conditional that chains its input I through a given function that returns an SFunc{J,O}.

source
Scruff.SFuncs.ConditionalType
abstract type Conditional{I <: Tuple, J <: Tuple, K <: Tuple, O, S <: SFunc{J, O}} <: SFunc{K, O}

Conditional sfuncs represent the generation of an sfunc depending on the values of parents. An subtype of Conditional must provide a gensf method that takes an I and returns an SFunc{J,O} (important the generated SFunc must not appear outside the Conditional. It should not be a parent).

Additional supported operators

  • support
  • support_quality
  • sample
  • logcpdf
  • make_factors
  • compute_pi
  • send_lambda

Type parameters

  • I: the type of data used to generate the Conditional's sfunc
  • J: a tuple that represents the input types (the I) of the Conditional's generated sfunc
  • K: the input types of the Conditional; this is a tuple of types constructed from I,

and J using extend_tuple_types

  • O: the output type(s) of both the Conditional and the Conditional's generated sfunc
  • S: the type of the Conditional's generated sfunc
source
Scruff.SFuncs.ConstantType
mutable struct Constant{O} <: Dist{O,Nothing}

Constant represents an sfunc that always produces the same value. It has no inputs and no parameters.

Additional supported operators

  • support
  • support_quality
  • sample
  • logcpdf
  • bounded_probs
  • compute_pi

Type parameters

  • O: the output type(s) of the Constant
source
Scruff.SFuncs.DetType
abstract type Det{I, O} <: SFunc{I, O}

Det defines an sfunc that represents a deterministic function I -> O. When a Det is subtyped, a function apply(d::Det, i::I)::O or apply(d::Det, i::I...)::O must also be implemented. It has no parameters.

Additional supported operators

  • support
  • support_quality
  • sample
  • logcpdf
  • bounded_probs
  • make_factors
  • compute_pi
  • send_lambda

Type parameters

  • I: the input type(s) of the Det
  • O: the output type(s) of the Det
source
Scruff.SFuncs.ExpanderType
mutable struct Expander{I,O} <: SFunc{I,O}

An Expander represents a model defined by a function that returns a network. For a given value of an input, the conditional probability distribution is provided by the network produced by the function on that input.

For each such network, the expander manages a runtime to reason about it. Expanders are lazy and do not evaluate the function until they have to.

As a result, there is state associated with Expanders. This is analysis state rather than world state, i.e., it is the state of Scruff's reasoning about the Expander. In keeping with Scruff design, Expanders are immutable and all state associated with reasoning is stored in the runtime that contains the expander. To support this, a runtime has three fields of global state:

  • :subnets: the expansions of all Expanders managed by the runtime
  • :subruntimes: all the subruntimes recursively managed by this runtime through Expanders, keyed by the networks
  • :depth: the depth to which Expanders in this runtime should be expanded

Type parameters

  • I: the input type(s) of the Expander
  • O: the output type(s) of the Expander
source
Scruff.SFuncs.ExplicitDetType
struct ExplicitDet{I, O} <: Det{I, O}

ExplicitDet is a Det that contains a field f::Function. It also has an apply method that simply delegates to the ExplicitDet's function:

    apply(d::ExplicitDet, i...) = d.f(i...)
julia> d = ExplicitDet{Tuple{Vararg{Real}},Real}(sum)
ExplicitDet{Tuple{Vararg{Real, N} where N}, Real}(sum)
source
Scruff.SFuncs.ExtendType
struct Extend{I<:Tuple{Any},J,O} <: SFunc{J,O}

Extend defines an sfunc that extend the input of another sfunc. Useful for defining Separable SFuncs.

Additional supported operators

  • support
  • support_quality
  • sample
  • logcpdf
  • bounded_probs
  • make_factors
  • compute_pi
  • send_lambda

Type parameters

  • I: the input type(s) of the extended sfunc; it must be a tuple of length 1
  • J: the input type(s) of the Extend
  • O: the output type(s) of both the Extend and the extended sfunc
source
Scruff.SFuncs.GenerateType
Generate{O} <: SFunc{Tuple{Dist{O}}, O}

Generate a value from its Dist argument.

This helps in higher-order programming. A typical pattern will be to create an sfunc that produces a Dist, and then generate many observations from the Dist using Generate.

Additional supported operators

  • support
  • support_quality
  • sample
  • logcpdf
  • compute_pi
  • send_lambda
  • make_factors
source
Scruff.SFuncs.InvertibleType
struct Invertible{I,O} <: SFunc{Tuple{I},O}

An invertible sfunc, with both a forward and a inverse function provided.

Additional supported operators

  • support
  • support_quality
  • sample
  • logcpdf
  • bounded_probs
  • make_factors
  • compute_pi
  • send_lambda
source
Scruff.SFuncs.LinearGaussianType
mutable struct LinearGaussian{I <: Tuple{Vararg{Float64}}} <: 
    Conditional{I, Tuple{}, I, Float64, Normal}

LinearGaussian defines an sfunc whose mean is a linear function of its parents. A LinearGaussian's output type is a Float, its parameter type is Tuple{I, Float64, Float64}, and it's contained sfunc is a Normal mean 0.0.

Type parameters

  • I: the input type(s) of the LinearGaussian

See also: Conditional, Normal

source
Scruff.SFuncs.MixtureType
mutable struct Mixture{I,O} <: SFunc{I,O}

Mixture defines an sfunc representing mixtures of other sfuncs. It contains a vector of sfuncs and a vector of probabilities that those sfuncs are selected, which indices are keys associating the two. The output type of a Mixture is defined by the output type of its internal components. The parameters of a Mixture are its probabilities followed by parameters of all its internal components, in order.

Additional supported operators

  • support
  • support_quality
  • sample
  • cpdf
  • expectation
  • compute_pi
  • send_lambda

Type parameters

  • I: the input type(s) of the Mixture
  • O: the shared output type(s) of its internal components and the output type(s) of the Mixture
source
Scruff.SFuncs.NetworkSFuncType
struct NetworkSFunc{I,O} <: SFunc{I,O}

An sfunc that combines multiple sfuncs in a network structure.

Arguments

input_placeholders A vector of placeholders indicating the types of network inputs. The type parameter `I` is computed from these.
sfuncs The sfuncs to combine.
parents A `Dict` that maps sfuncs to their parent sfuncs. Note that this parallels networks, 
     except that we are mapping sfuncs to lists of sfuncs directly rather than variables to vectors of variables.
output A vector of output sfuncs, determining the `O` type parameter.

Additional supported operators

  • sample
  • sample_logcpdf
  • logcpdf
source
Scruff.SFuncs.SerialType
struct Serial{I,O} <: SFunc{I,O}

A sequence of sfuncs in series.

Although this could be implemented as a special case of NetworkSFunc, the serial composition allows an easier and more efficient implementation of operations. All but the first sfunc in the sequence will have a single input; the output of each sfunc feeds into the input of the next sfunc.

To work properly, most of the operations on Serial need the support of the intermediate sfuncs, given an input range. Rather than compute this each time, and to avoid having the non-support operations take a size argument, support is memoized, and must be called before other operations like logcpdf are called. The support_memo is a dictionary whose keys are tuples of parent ranges and whose values are the support computed for those parent ranges, along with the target size for which they were computed. Storing the target size enables refinement algorithms that increase the size and improve the support.

Additional supported operators

  • support
  • support_quality
  • sample
  • cpdf
  • bounded_probs
  • make_factors
  • compute_pi
  • send_lambda

Type parameters

  • I the input type of the first sfunc
  • O the output type of the last sfunc
source
Scruff.SFuncs.SwitchType
abstract type Switch{N, I, K, O} <: Det{K, O}

Switch defines an sfunc that represents choosing between multiple incoming (parent) Sfuncs based on a test. A subtype of Switch must provide a choose function that takes the switch and an i and returns an integer between 1 and N. This is an index into a 'parent array'.

K must be a flat tuple type consisting of I and N occurrences of O: for example, if I is Int

    K = extend_tuple_type(Tuple{Int}, NTuple{N, O})

If the subtype'd sfunc is not in the Scruff.SFuncs module, the system must import Scruff.SFuncs: choose.

Additional supported operators

  • support
  • support_quality
  • compute_pi
  • send_lambda

Type parameters

  • N: the count of sfuncs from which to choose
  • I: the type of the second argument of the choose function defined for the Switch
  • K: the input type of the Switch; see above
  • O: the output type of the Switch

See also: choose, extend_tuple_type

source
Scruff.SFuncs.CLGMethod
CLG(paramdict::Dict)

Constructs an*sfunc representing a Conditional linear Gaussian. These sfuncs may have both discrete and continuous parents. For each combination of discrete parents, there is a LinearGaussian that depends on the continuous parents.

CLGs are implemented as a Table with a LinearGaussian.

The paramdict parameter defines the discrete and continuous parents, and the linear gaussean values where the length of a key is the count of the discrete inputs, the length of the tuple in a value is the count of continuous inputs, and the rest of the values are used to build the parameters for CLG itself. For example,

    Dict((:x,1) => ((-1.0, 1.0, 2.0), 3.0, 1.0), 
         (:x,2) => ((-2.0, 4.0, 2.0), 3.0, 1.0),
         (:x,3) => ((-3.0, 2.0, 2.0), 3.0, 1.0), 
         (:y,1) => ((-4.0, 5.0, 2.0), 3.0, 1.0),
         (:y,2) => ((-5.0, 3.0, 2.0), 3.0, 1.0), 
         (:y,3) => ((-6.0, 6.0, 2.0), 3.0, 1.0))
- the keys define two(2) discrete parents, with values `[:x,:y]` and `[1,2,3]`
- in the values, the first tuple defines three(3) continuous parents for each
  underlying `LinearGausian`, with values `-1.0:-6.0`, `1.0:6.0`, and `2.0`
- the values `3.0` and `1.0` are mean/stddev of the underlying `LinearGaussian`

See also: Table, LinearGaussian

source
Scruff.SFuncs.DiscreteCPTMethod
function DiscreteCPT(range::Vector{O}, paramdict::Dict{I, Vector{Float64}}) where {I <: Tuple, O}

Constructs an sfunc that represents a Discrete Conditional Probability table.

DiscreteCPTs are implemented as a Table with a Cat.

The range parameter defines all the possible outputs of the DiscreteCPT. The paramdict parameter defines the input(s) and the actual CPT. For example,

    range = [1, 2]
    paramdict = Dict((1,1) => [0.3, 0.7], (1,2) => [0.6, 0.4], (2,1) =>[0.4, 0.6],
    (2,2) => [0.7, 0.3], (3,1) => [0.5, 0.5], (3,2) => [0.8, 0.2])

can create a DiscreteCPT which has two(2) inputs (the length of the key) and, given each input as defined by the key, selects either 1 or 2 (the range) with the given probability. So, if the input is (2,1), 1 is selected with probability 0.4 and 2 is selected with probability 0.6.

See also: Table, Cat

source
Scruff.SFuncs.FlipMethod
Flip(p)

Constructs a very simple sfunc corresponding to a Bernoulli distribution, represented by a Cat. The output is true with probability p, and false with probability 1-p.

See also: Cat

source
Scruff.SFuncs.SeparableMethod
function Separable(range::Vector{O}, probabilities :: Vector{Float64}, compparams :: SepCPTs) where O

Constructs an sfunc representing separable models, defined by additive decompositon of a conditional probability distribution into separate distributions depending on each of the parents.

Separables are implemented as a Mixture of Extend sfuncs that extend DiscreteCPTs.

To construct a Separable, this method is passed the range of output values, the probabilities of each of the underlying DiscreteCPT (which are the internal sfuncs of the Mixture), and the parameters for each of the DiscreteCPTs. For example,

alphas = [0.2, 0.3, 0.5]
cpd1 = Dict((1,) => [0.1, 0.9], (2,) => [0.2, 0.8])
cpd2 = Dict((1,) => [0.3, 0.7], (2,) => [0.4, 0.6], (3,) => [0.5, 0.5])
cpd3 = Dict((1,) => [0.6, 0.4], (2,) => [0.7, 0.3])
cpds :: Array{Dict{I,Array{Float64,1}} where I,1} = [cpd1, cpd2, cpd3]
s = Separable([1, 2], alphas, cpds)

See also: Mixture, Extend, DiscreteCPT, Table

source
Scruff.SFuncs.SoftScoreMethod
SoftScore(vs::Vector{I}, ss::Vector{Float64})

Return a LogScore of the log values in ss vector for the associated keys in vs.

source
Scruff.SFuncs.chooseFunction

choose interface. For every subtype of Switch, an implementation of this method must be created, whose first parameter is the subtype, and the second parameter is of type I for the parameter type in Switch.

For example, the definition of the If sfunc is as follows, where choose returns either index 1 or index 2.

struct If{O} <: Switch{2, Bool, Tuple{Bool, O, O}, O} end
choose(::If, b::Bool) = b ? 1 : 2
source
Scruff.SFuncs.extend_tuple_typeMethod
extend_tuple_type(T1, T2)

Given two types T1 and T2, concatenate the types into a single tuple type.

Arguments

  • T1: Any type
  • T2: A tuple type

Returns

  • If T1 is a tuple type, a tuple with the concatenation of the types in T1 and T2
  • If T1 is not a tuple type, a tuple with T1 prepended to the types in T2

Examples

julia> extend_tuple_type(Int64, Tuple{Float64})
Tuple{Int64, Float64}

julia> extend_tuple_type(Tuple{Int64}, Tuple{Float64})
Tuple{Int64, Float64}

julia> extend_tuple_type(Tuple{Vector{Float64}}, Tuple{Symbol,Symbol})     
Tuple{Vector{Float64}, Symbol, Symbol}
source