inferpy.models package

Submodules

inferpy.models.deterministic module

The Deterministic distribution

class inferpy.models.deterministic.Deterministic(loc=None, dim=None, observed=False, name='Determ')[source]

Bases: inferpy.models.random_variable.RandomVariable

Class implementing a Deterministic variable.

This allows to encapsulate any Tensor object of Edward variable. Moreover, variables of this type can be intilially empty and later initialized. When operating with InferPy random variables, the result is always a deterministic variable.

An example of use:

import inferpy as inf
import edward as ed
import tensorflow as tf


# empty initialized variable
x = inf.models.Deterministic()
x.dist = ed.models.Normal(0.,1.)


# deterministic variable with returning a tensor

x = inf.models.Deterministic()
x.base_object = tf.constant(1.)


# deterministic variable returning an InferPy variable

a = inf.models.Normal(0,1)
b = inf.models.Normal(2,2)

x = a + b

type(x)  # <class 'inferpy.models.deterministic.Deterministic'>

__init__(loc=None, dim=None, observed=False, name='Determ')[source]

Constructor for the Deterministic distribution

loc

Distribution parameter for the mean.

inferpy.models.factory module

This module implements all the methods for definition of each type of random variable. For further details about all the supported distributions, see Guide to Building Probabilistic Models .

class inferpy.models.factory.Bernoulli(*args, **kwargs)[source]

Bases: inferpy.models.random_variable.RandomVariable

PARAMS = ['logits', 'probs']
__init__(*args, **kwargs)[source]

constructor for Bernoulli

logits

property for logits

probs

property for probs

class inferpy.models.factory.Beta(*args, **kwargs)[source]

Bases: inferpy.models.random_variable.RandomVariable

PARAMS = ['concentration1', 'concentration0']
__init__(*args, **kwargs)[source]

constructor for Beta

concentration0

property for concentration0

concentration1

property for concentration1

class inferpy.models.factory.Categorical(*args, **kwargs)[source]

Bases: inferpy.models.random_variable.RandomVariable

PARAMS = ['logits', 'probs']
__init__(*args, **kwargs)[source]

constructor for Categorical

logits

property for logits

probs

property for probs

class inferpy.models.factory.Dirichlet(*args, **kwargs)[source]

Bases: inferpy.models.random_variable.RandomVariable

PARAMS = ['concentration']
__init__(*args, **kwargs)[source]

constructor for Dirichlet

concentration

property for concentration

class inferpy.models.factory.Exponential(*args, **kwargs)[source]

Bases: inferpy.models.random_variable.RandomVariable

PARAMS = ['rate']
__init__(*args, **kwargs)[source]

constructor for Exponential

rate

property for rate

class inferpy.models.factory.Gamma(*args, **kwargs)[source]

Bases: inferpy.models.random_variable.RandomVariable

PARAMS = ['concentration', 'rate']
__init__(*args, **kwargs)[source]

constructor for Gamma

concentration

property for concentration

rate

property for rate

class inferpy.models.factory.InverseGamma(*args, **kwargs)[source]

Bases: inferpy.models.random_variable.RandomVariable

PARAMS = ['concentration', 'rate']
__init__(*args, **kwargs)[source]

constructor for InverseGamma

concentration

property for concentration

rate

property for rate

class inferpy.models.factory.Laplace(*args, **kwargs)[source]

Bases: inferpy.models.random_variable.RandomVariable

PARAMS = ['loc', 'scale']
__init__(*args, **kwargs)[source]

constructor for Laplace

loc

property for loc

scale

property for scale

class inferpy.models.factory.Multinomial(*args, **kwargs)[source]

Bases: inferpy.models.random_variable.RandomVariable

PARAMS = ['total_count', 'logits', 'probs']
__init__(*args, **kwargs)[source]

constructor for Multinomial

logits

property for logits

probs

property for probs

total_count

property for total_count

class inferpy.models.factory.MultivariateNormalDiag(*args, **kwargs)[source]

Bases: inferpy.models.random_variable.RandomVariable

PARAMS = ['loc', 'scale_diag']
__init__(*args, **kwargs)[source]

constructor for MultivariateNormalDiag

loc

property for loc

scale_diag

property for scale_diag

class inferpy.models.factory.Normal(*args, **kwargs)[source]

Bases: inferpy.models.random_variable.RandomVariable

PARAMS = ['loc', 'scale']
__init__(*args, **kwargs)[source]

constructor for Normal

loc

property for loc

scale

property for scale

class inferpy.models.factory.Poisson(*args, **kwargs)[source]

Bases: inferpy.models.random_variable.RandomVariable

PARAMS = []
__init__(*args, **kwargs)[source]

constructor for Poisson

class inferpy.models.factory.Uniform(*args, **kwargs)[source]

Bases: inferpy.models.random_variable.RandomVariable

PARAMS = ['low', 'high']
__init__(*args, **kwargs)[source]

constructor for Uniform

high

property for high

low

property for low

inferpy.models.factory.def_random_variable(var)[source]

inferpy.models.prob_model module

Module with the probabilistic model functionality.

class inferpy.models.prob_model.ProbModel(varlist=None)[source]

Bases: object

Class implementing a probabilistic model

A probabilistic model defines a joint distribution over observed and latent variables. This class encapsulates all the functionality for making inference (and learning) in these models.

An example of use:

import inferpy as inf
from inferpy.models import Normal



with inf.ProbModel() as m:

    x = Normal(loc=1., scale=1., name="x", observed=True)
    y = Normal(loc=x, scale=1., dim=3, name="y")


# print the list of variables
print(m.varlist)
print(m.latent_vars)
print(m.observed_vars)

# get a sample

m_sample = m.sample()



# compute the log_prob for each element in the sample
print(m.log_prob(m_sample))

# compute the sum of the log_prob
print(m.sum_log_prob(m_sample))




### alternative definition

x2 = Normal(loc=1., scale=1., name="x2", observed=True)
y2 = Normal(loc=x, scale=1., dim=3, name="y2")

m2 = inf.ProbModel(varlist=[x2,y2])

This class can be used, for instance, for infering the parameters of some observed data:

import edward as ed
import inferpy as inf


#### learning a 1-dim parameter from 1-dim data


N = 50
sampling_mean = [30.]
sess = ed.util.get_session()


with inf.ProbModel() as m:

    theta = inf.models.Normal(loc=0., scale=1.)

    with inf.replicate(size=N):
        x = inf.models.Normal(loc=theta, scale=1., observed=True)

m.compile()

x_train = inf.models.Normal(loc=sampling_mean, scale=1.).sample(N)
data = {x.name : x_train}


m.fit(data)

m.posterior(theta).loc[0]  #29.017122


#### learning a 2 parameters of 1-dim from 2-dim data

N = 50
sampling_mean = [30., 10.]
sess = ed.util.get_session()


with inf.ProbModel() as m:

    theta1 = inf.models.Normal(loc=0., scale=1., dim=1)
    theta2 = inf.models.Normal(loc=0., scale=1., dim=1)


    with inf.replicate(size=N):
        x = inf.models.Normal(loc=[theta1, theta2], scale=1., observed=True)




m.compile()


x_train = inf.models.Normal(loc=sampling_mean, scale=1.).sample(N)
data = {x.name : x_train}


m.fit(data)

m.posterior(theta1).loc
m.posterior(theta2).loc

__init__(varlist=None)[source]

Initializes a probabilistic model

Parameters:varlist – optional list with the variables in the model
add_var(v)[source]

Method for adding a new random variable. After use, the model should be re-compiled

static compatible_var(v)[source]
compile(infMethod='KLqp', Q=None, proposal_vars=None)[source]

This method initializes the structures for making inference in the model.

copy(swap_dict=None)[source]
fit(*args, **kwargs)[source]

Assings data to the observed variables

static get_active_model()[source]

Return the active model defined with the construct ‘with’

get_config()[source]
get_copy_from(original_var)[source]
get_parents(v)[source]
get_var(name)[source]

Get a varible in the model with a given name

get_vardict()[source]
get_vardict_rev()[source]
static is_active()[source]

Check if a replicate construct has been initialized

Returns:True if the method is inside a construct ProbModel (of size different to 1). Otherwise False is return
is_compiled()[source]

Determines if the model has been compiled

latent_vars

list of latent (i.e., non-observed) variables in the model

log_prob(*args, **kwargs)[source]

Computes the log probabilities of a (set of) sample(s)

no_parents()[source]
observed_vars

list of observed variabels in the model

posterior(*args, **kwargs)[source]

Return the posterior distribution of some latent variables

Parameters:latent_var – a single or a set of latent variables in the model
Returns:Random variable(s) of the same type than the prior distributions
predict(target, data, reset_tf_vars=False)[source]
predict_old(target_var, observations)[source]
reset_compilation()[source]

Clear the structues created during the compilation of the model

sample(*args, **kwargs)[source]

Generates a sample for eache variable in the model

sum_log_prob(*args, **kwargs)[source]

Computes the sum of the log probabilities of a (set of) sample(s)

summary()[source]
to_json()[source]
varlist

list of variables (observed and latent)

inferpy.models.random_variable module

Module implementing the shared functionality across all the variable types

class inferpy.models.random_variable.RandomVariable(base_object=None, observed=False)[source]

Bases: object

Base class for random variables.

__init__(base_object=None, observed=False)[source]

Constructor for the RandomVariable class

Parameters:
  • base_object – encapsulated Edward object (optional).
  • observed (bool) – specifies if the random variable is observed (True) or observed (False).
base_object

Underlying Tensorflow object

batches

Number of batches of the variable

bind
copy(swap_dict=None, observed=False)[source]

Build a new random variable with the same values. The underlying tensors or edward objects are copied as well.

Parameters:
  • swap_dict – random variables, variables, tensors, or operations to swap with.
  • observed – determines if the new variable is observed or not.
dim

Dimensionality of variable

dist

Underlying Edward object

equal(other)

documentation for equal

event_shape
static get_key_from_var(*args, **kwargs)[source]
get_local_hidden()[source]

Returns a list with all the local hidden variables w.r.t. this one. Local hidden variables are those latent variables which are in the same replicate construct.

get_replicate_list()[source]

Returns a list with all the replicate constructs that this variable belongs to.

static get_var_with_key(*args, **kwargs)[source]
is_generic_variable()[source]

Determines if this is a generic variable, i.e., an Edward variable is not encapsulated.

log_prob(*args, **kwargs)[source]

Method for computing the log probability of a sample v (or a set of samples)

mean(*args, **kwargs)[source]

Method for obaining the mean of this random variable

name

name of the variable

observed

boolean property that determines if a variable is observed or not

prob(*args, **kwargs)[source]

Method for computing the probability of a sample v (or a set of samples)

prod_prob(*args, **kwargs)[source]

Method for computing the joint probability of a sample v (or a set of samples)

sample(*args, **kwargs)[source]

Method for obaining a samples

Parameters:size – scalar or matrix of integers indicating the shape of the matrix of samples.
Returns:Matrix of samples. Each element in the output matrix has the same shape than the variable.
shape

shape of the variable, i.e. (batches, dim)

stddev(*args, **kwargs)[source]

Method for obataining the standard deviation of this random variable

sum_log_prob(*args, **kwargs)[source]

Method for computing the sum of the log probability of a sample v (or a set of samples)

variance(*args, **kwargs)[source]

Method for obataining the variance of this random variable

inferpy.models.replicate module

Module with the replication functionality.

class inferpy.models.replicate.replicate(size)[source]

Class implementing the Plateau notation

The plateau notation is used to replicate the random variables contained within this construct. Every replicated variable is conditionally idependent given the previous random variables (if any) defined outside the with statement. The with inf.replicate(size = N) sintaxis is used to replicate N times the contained definitions. For example:

import inferpy as inf


with inf.replicate(size=50):
    # Define some random variables here
    print("Variable replicated " + str(inf.replicate.get_total_size()) + " times")


with inf.replicate(size=10):
    # Define some random variables here
    print("Variable replicated " + str(inf.replicate.get_total_size()) + " times")

    with inf.replicate(size=2):
        # Define some random variables here
        print("Variable replicated " + str(inf.replicate.get_total_size()) + " times")
        print(inf.replicate.in_replicate())

# Define some random variables here
print("Variable replicated " + str(inf.replicate.get_total_size()) + " times")

The number of times that indicated with input argument size. Note that nested replicate constructs can be defined as well. At any moment, the product of all the nested replicate constructs can be obtained by invoking the static method get_total_size().

Note

Defining a variable inside the construct replicate with size equal to 1, that is, inf.replicate(size=1) is equivalent to defining outside any replicate construct.

__init__(size)[source]

Initializes the replicate construct

Parameters:size (int) – number of times that the variables contained are replicated.
static get_active_replicate()[source]

Return the active replicate defined with the construct ‘with’

static get_all_replicate()[source]
static get_total_size()[source]

Static method that returns the product of the sizes of all the nested replicate constructs

Returns:Integer with the product of sizes
static in_replicate()[source]

Check if a replicate construct has been initialized

Returns:True if the method is inside a construct replicate (of size different to 1). Otherwise False is return
static print_total_size()[source]

Static that prints the total size