Previous topic

fem.continuous package

Next topic

fortran_module extension

fem.discrete package

Submodules

fem.discrete.combinatorics module

fem.discrete.combinatorics.binomial_coefficients(n, k)[source]

Return a list of binomial coefficients.

Parameters:
  • n (int) – the first argument to the choose operation
  • k (int) – the maximum second argument to the choose operation
Returns:

a list of binomial coefficients

Return type:

ndarray

Examples

Compute the binomial coefficients \({4\choose 0}\), \({4\choose 1}\), and \({4\choose 2}\)

>>> from fem.discrete.combinatorics import binomial_coefficients
>>> binomial_coefficients(4, 2)
array([1, 4, 6])
fem.discrete.combinatorics.mixed_radix_to_base_10(x, b)[source]

Convert the mixed radix integer with digits x and bases b to base 10.

Parameters:
  • x (list) – a list of digits ordered by increasing place values
  • b (list) – a list of bases corresponding to the digits

Examples

Generally, the base 10 representation of the mixed radix number \(x_n\ldots x_1\) where \(x_i\) is a digit in place value \(i\) with base \(b_i\) is

\[\sum_{i=1}^nx_i\prod_{j=i+1}^nb_j = x_n + b_nx_{n-1} + b_nb_{n-1}x_{n-2} + \cdots + b_n\cdots b_2x_1\]

Convert 111 with bases \((b_1,b_2,b_3)=(2,3,4)\) to base 10:

>>> from fem.discrete.combinatorics import mixed_radix_to_base_10
>>> mixed_radix_to_base_10([1,1,1], [2,3,4])
17
fem.discrete.combinatorics.multiindices(n, k)[source]

Return an ordered list of distinct tuples of the first \(n\) nonnegative integers.

Parameters:
  • n (int) – The least integer not included in the list of distinct tuples
  • k (int) – The length of each tuple
Returns:

a list of distinct tupes of shape \({n\choose k}\) by \(k\)

Return type:

ndarray

Examples

>>> from fem.discrete.combinatorics import multiindices
>>> multiindices(4, 2)
array([[0, 1], [0, 2], [0, 3], [1, 2], [1, 3], [2, 3]])

fem.discrete.fit module

This module implements a class that can fit the Potts model to data and make predictions with the model. FEM for discrete data

fem.discrete.fit.categorize(x)[source]

Convert symbolic x data to integer data. x contains a list of lists that are samples of different variables. Each list of samples is mapped to the first number of unique values detected of nonnegative integers.

Parameters:x (list) – A list where each element is a list of samples of a different variable
Returns:The integer data and a list of dictionaries that map symbol data to integer data for each row of x
Return type:(list, list)

Examples

>>> from fem.discrete import categorize
>>> x = [['a', 'b', 'a', 'a'], [10, 11, 12, 10, 11]]
>>> x_int, cat_x = categorize(x)
>>> x_int
[array([0, 1, 0, 0]), array([0, 1, 2, 0, 1])]
>>> cat_x
[{'a': 0, 'b': 1}, {10: 0, 11: 1, 12: 2}]
class fem.discrete.fit.model(degs=[1])[source]

Bases: object

This class implements a Potts model that can be fit to data and used to make predictions.

degs

list – A list of model degrees

n_x

int – The number of input variables

n_y

int – The number of output variables

m_x

ndarray – An array of length n_x listing the number of values that each input variable takes

m_y

ndarray

m_x_cumsum

ndarray

m_y_cumsum

ndarray

x_int

ndarray – The input variable data x mapped to nonnegative integers

y_int

ndarray – The output variable data y mapped to nonnegative integers

cat_x

list – A list of per-row dicts that map the input x symbol data to the integer data in x_int

cat_y

list

cat_x_inv

list – A list of dicts that are the inverses of the list of dicts in cat_x

cat_y_inv

list

w

dict – A dictionary of length len(degs) keyed by degs. w[deg] are the model parameters for degree deg

disc

list – A list of length n_x of the running discrepancies per variable computed during the model fit

impute

bool – If true, the model will map each combination of hold-one-out input variables to the held out variable

Examples

>>> import fem
>>> n, m = 10, 3
>>> fem.discrete.time_series(fem.discrete.model_parameters(n, m), n, m)
>>> model = fem.discrete.model()
>>> model.fit(x[:, :-1], x[:, 1:])
>>> model.predict(x[:, -1])
energy(x)[source]
fit(x, y=None, iters=100, overfit=True, impute=None, svd='approx')[source]

Fit the Potts model to the data

Parameters:
  • x (ndarray) –
  • y (ndarray) –
  • iters (int) –
  • overfit (bool) –
  • impute (bool) –
  • svd (str) –
Returns:

The fitted model parameters and the running discrepancies

Return type:

(dict, list)

predict(x)[source]

Predict the state of the output variable given input variable x

Parameters:x (ndarray) –
Returns:prediction and probability
Return type:(ndarray, ndarray)
fem.discrete.fit.one_hot(x, m, degs=[1])[source]

Compute the one-hot encoding of x

Parameters:
  • x (ndarray) – n by l array of nonnegative integers to be converted to one-hot encoding
  • m (ndarray) – length n array storing the number of possible integers in each row of x
  • degs (list) – list of degrees of one-hot-encodings to compute
Returns:

sparse matrix that is the one-hot encoding of x. The degrees of one-hot encodings are vertically stacked.

Return type:

csc_matrix

Examples

The one-hot encoding of a discrete variable records the state of the variable by a boolean vector with a 1 at the index which is the state of the variable. The one-hot encoding of a system of discrete variables is the concatenation of the one-hot encodings of each variable. For example if the system of discrete variables \((x_1, x_2, x_3)\) has state \((x_1,x_2,x_3)=(1,1,2)\) and the variables may take values from the first \(m=(3,3,4)\) nonnegative integers, i.e. \(x_1,x_2\in\{0,1,2\}\) and \(x_3\in\{0,1,2,3\}\), then the one-hot encoding of \((x_1, x_2, x_3)^T\) is \(\sigma=(0,1,0,0,1,0,0,0,1,0)^T\).

>>> from fem.discrete import one_hot
>>> x = [1,1,2]
>>> m = [3,3,4]
>>> x_oh = one_hot(x, m)
>>> print x_oh.todense()
matrix([[0.],
        [1.],
        [0.],
        [0.],
        [1.],
        [0.],
        [0.],
        [0.],
        [1.],
        [0.]])
fem.discrete.fit.svd_pinv(x)[source]

Compute the SVD-based pseudoinverse matrices

Parameters:x (csc_matrix) – The matrix for which to compute the pseudoinverse
Returns:If the SVD of x is \(USV^T\), this function returns \(V\), \(S^+\), and \(U^T\)
Return type:(csc_matrix, ndarray, csc_matrix)

fem.discrete.simulate module

fem.discrete.simulate.model_parameters(n, m, degs=[1], dist=None, dist_par=None)[source]

Draw random model parameters

Parameters:
  • n (int) –
  • m (int) –
  • degs (list) –
  • dist (callable) –
  • dist_par (tuple) –
Returns:

keys degs

Return type:

dict

fem.discrete.simulate.mutations(w, n, m, l=None, o=1.0)[source]
fem.discrete.simulate.time_series(w, n, m, l=None, o=1.0)[source]

Simulate discrete time series data

Parameters:
  • w (dict) –
  • n (int) –
  • m (int) –
  • l (int) –
  • o (float) –
Returns:

time series data

Return type:

ndarray

Module contents