mixle.utils.vector module¶
Vector contains functions useful for estimation and evaluation of mixle classes.
- gammaln(x: ndarray) ndarray[source]
- gammaln(x: float) float
Return logrithm of the gamma function.
Returns np.log(.np.abs(Gamma(x)))
- sorted_merge(a, b)[source]
Performs the merge-step of merge sort on sorted np.ndarray’s a and b, returning sorted array.
- Parameters:
a (ndarray) – Sorted numpy array.
b (ndarray) – Sorted numpy array.
- Returns:
Sorted numpy array containing merge sorted a and b. Array len = len(a)+len(b).
- Return type:
- sorted_dict_merge_add(k_vec1, c_vec1, k_vec2, c_vec2)[source]
Performs a merge on two sorted arrays of dictionary keys and the counts for their respective keys.
Returns the merge sorted keys and corresponding counts.
- Parameters:
k_vec1 (ndarray) – Numpy array of sorted dictionary keys.
c_vec1 (ndarray) – Numpy array of counts for keys in vector k_vec1.
k_vec2 (ndarray) – Numpy array of sorted dictionary keys.
c_vec2 (ndarray) – Numpy array of counts for keys in vector k_vec2.
- Returns:
Tuple of numpy arrays containing the merge sorted dictionary keys and corresponding counts.
- Return type:
- make(x)[source]
Convert the array x into a numpy array.
- make_pdf(x)[source]
Takes log density values and normalizes on the log-scale, returning an ndarray that s.t. np.exp(rv).sum() == 1.0.
Arg data type for x: Union[np.ndarray, Sequence[float], List[np.ndarray]]). :param x: Array like object with float data type that can be converted to a numpy array. E.g. lists, lists :type x: See above :param of tuples: :param tuples: :param tuples of tuples: :param tuples of lists and ndarrays.:
- zeros(n)[source]
Return numpy array of shape n, with default dtype=float64.
- mat_inv(x)[source]
Computes the inverse of a square matrix x.
Arg x data type Union[List[List[Union[float, int]]],List[np.ndarray], np.ndarray]). :param x: List of List[float/int], List of np.ndarray, or 2-d np.ndarray of square matrix. :type x: See above
- dot(x, y)[source]
Performs call to numpy.dot().
- Parameters:
- Returns:
Returns float/int if x and y are both 1d vectors, returns 1d vector if x xor y is scalar, and matrix else.
- Return type:
- outer(x, y)[source]
Compute the outer product of two vectors
- Parameters:
- Return type:
Returns: (M, N) ndarray.
- diag(x)[source]
Extract a diagonal or construct a diagonal array.
Note: If x is 2-D return np.ndarray with diagonal. If x is 1-D returns 2-d diagonal matrix with x on diagonal.
See the more detailed documentation for
numpy.diagonalif you use this function to extract a diagonal and wish to write to the resulting array; whether it returns a copy or a view depends on what version of numpy you are using.
- reshape(x, sz)[source]
Gives a new shape to an array without changing its data.
- cholesky(x_mat)[source]
Compute the Cholesky decomposition of a matrix, to use in cho_solve.
Returns a matrix containing the Cholesky decomposition, x_mat = L L* or x_mat = U* U of a Hermitian positive-definite matrix x_mat. The return value can be directly used as the first parameter to cho_solve.
- cho_solve(a_mat, b)[source]
Solve the linear equations a_mat x = b, given the Cholesky factorization of a_mat.
- maximum(x, y, output=None)[source]
Element-wise maximum of array elements.
Compare two arrays and returns a new array containing the element-wise maxima. If one of the elements being compared is a NaN, then that element is returned. If both elements are NaNs then the first is returned. The latter distinction is important for complex NaNs, which are defined as at least one of the real or imaginary parts being a NaN. The net effect is that NaNs are propagated.
- Parameters:
x (array-like) – Array-like holding values to be compared. If
x.shape != y.shape, they must be broadcastable to a common shape (which becomes the shape of the output).y (array-like) – Array-like holding values to be compared. If
x.shape != y.shape, they must be broadcastable to a common shape (which becomes the shape of the output).output (float | int | ndarray | None) – Optional np.ndarray of float to output results to.
- Returns:
ndarray or scalar. The maximum of x and y, element-wise. This is a scalar if both x and y are scalars.
- Return type:
- log_sum(x)[source]
Performs log(sum(exp(x)) on 1-d numpy array. E.g. for x_i = log(y_i), log(sum(exp(x)) = log(sum(y)).
- Parameters:
x (ndarray) – Numpy array on log-scale. E.g. x_i = log(y_i).
- Returns:
Float value log(sum(exp(x)), or -np.inf if max(x) is -np.inf.
- Return type:
- weighted_log_sum(x, w)[source]
Computes numerically stable log-sum-of-exponentials with weights=exp(w), on the observation values y=exp(x), returning log(sum(exp(x)*exp(w))).
Note: The weights are on the log-scale.
- Parameters:
x (ndarray) – Numpy array on log-scale. E.g. x_i = log(y_i).
w (ndarray) – Numpy array on of weights for y_i = exp(x_i) on the log-scale. E.g. w_i = log(weight_i).
- Returns:
Float value log(sum(exp(x)*exp(w))), or -np.inf if any x or w are -np.inf. Inputs are log-densities and log-weights (<= 0); +inf terms are not supported (this is a hot EM path).
- Return type:
- log_posterior(x)[source]
Computes posterior density for vector of log-likelihood evaluated at each parameter component.
I.e. if,
x = [log(p_mat(obs_i | theta_0)), log(p_mat(obs_i | theta_1)),…, log(p_mat(obs_i | theta_{n-1}))],
then returned value is,
[log(p_mat(theta_0| obs_i)),…,log(p_mat(theta_{n-1}|obs_i))],
where,
log(p_mat(theta_j| obs_i)) = log(p_mat(obs_i| theta_j)) - log(p_mat(obs_i)).
- Parameters:
x (np.ndarray) – Numpy array of log-density values for each component/parameter value x = [log(p_mat(obs_i | theta_0)), log(p_mat(obs_i | theta_1)),…, log(p_mat(obs_i | theta_{n-1}))].
- Returns:
- Numpy array of log-posterior for each component/parameter value
[log(p_mat(theta_0| obs_i)),…,log(p_mat(theta_{n-1}|obs_i))]. Returns numpy array of [-log(len(x))] if nan or inf detected in x.
- Return type:
- posterior(log_x, out=None, log_sum=False)[source]
Computes posterior density for vector of log-likelihood evaluated at each parameter component.
I.e. if, log_x = [log(p_mat(obs_i | theta_0)), log(p_mat(obs_i | theta_1)),…, log(p_mat(obs_i | theta_{n-1}))],
then returned value is,
[p_mat(theta_0| obs_i),…,p_mat(theta_{n-1}|obs_i)],
where,
p_mat(theta_j| obs_i) = p_mat(obs_i| theta_j) / p_mat(obs_i).
- Parameters:
log_x (ndarray) – Numpy array of log-density values for each component/parameter value log_x = [log(p_mat(obs_i | theta_0)), log(p_mat(obs_i | theta_1)),…, log(p_mat(obs_i | theta_{n-1}))].
out (Optional[ndarray]) – Optional numpy array to store returned value.
log_sum (Optional[bool]) – If true returns Tuple with ([p_mat(obs_i|theta_j)], log(p_mat(obs_i))).
- Returns:
Numpy array of posterior for each component/parameter value [p_mat(theta_0| obs_i),…,p_mat(theta_{n-1}|obs_i)]. Optional tuple with ([p_mat(obs_i|theta_j)], log(p_mat(obs_i))) if log_sum true.
- Return type:
- log_posterior_sum(x)[source]
Computes posterior density for vector of log-likelihood evaluated at each parameter component.
I.e. if,
log_x = [log(p_mat(obs_i | theta_0)), log(p_mat(obs_i | theta_1)),…, log(p_mat(obs_i | theta_{n-1}))],
then returned value is a Tuple containing,
[log(p_mat(theta_0| obs_i)),…,log(p_mat(theta_{n-1}|obs_i))] and log(p_mat(obs_i)),
where, p_mat(theta_j| obs_i) = p_mat(obs_i| theta_j) / p_mat(obs_i).
- Parameters:
x (np.ndarray) – Numpy array of log-density values for each component/parameter value log_x = [log(p_mat(obs_i | theta_0)), log(p_mat(obs_i | theta_1)),…, log(p_mat(obs_i | theta_{n-1}))].
- Returns:
- Tuple of numpy array containing log-posterior for each component/parameter value
[log(p_mat(theta_0| obs_i)),…,log(p_mat(theta_{n-1}|obs_i))], and log(p_mat(obs_i))). The log-posterior value is [-log(len(x)),…,-log(len(x))] if x contains a nan or -np.inf value.
- Return type:
- weighted_log_posterior(x, w)[source]
Computes weighted posterior density for vector of log-likelihood evaluated at each parameter component.
I.e. if, x = [log(p_mat(obs_i | theta_0)), log(p_mat(obs_i | theta_1)),…, log(p_mat(obs_i | theta_{n-1}))], and w = [log(weight_0),log(weight_1),…,log(weight_{n-1})],
then returned value is a list of floats,
[log(p_mat(theta_0| obs_i))+log(weight_0),…,log(p_mat(theta_{n-1}|obs_i))+log(weight_{n-1})].
- Parameters:
x (ndarray) – Numpy array of log-density values for each component/parameter value
w (ndarray) – Numpy array of log weights for each parameter value.
- Returns:
List[float] containing log-posterior for each component/parameter value [log(p_mat(theta_0| obs_i)),…,log(p_mat(theta_{n-1}|obs_i))].
- Return type:
- weighted_log_posterior_sum(x, w)[source]
Computes weighted posterior density for vector of log-likelihood evaluated at each parameter component.
I.e. if,
x = [log(p_mat(obs_i | theta_0)), log(p_mat(obs_i | theta_1)),…, log(p_mat(obs_i | theta_{n-1}))], and w = [log(weight_0),log(weight_1),…,log(weight_{n-1})],
then returned value is a Tuple of List[float] and float, containing
[log(p_mat(theta_0| obs_i))+log(weight_0),…,log(p_mat(theta_{n-1}|obs_i))+log(weight_{n-1})], and log(p_mat(obs_i)),
where, p_mat(theta_j| obs_i) = p_mat(obs_i| theta_j) / p_mat(obs_i).
- Parameters:
x (ndarray) – (np.ndarray): numpy array of log-density values for each component/parameter value log_x = [log(p_mat(obs_i | theta_0)), log(p_mat(obs_i | theta_1)),…, log(p_mat(obs_i | theta_{n-1}))].
w (np.ndarray) – List[float] or numpy array of log weights for each parameter value.
- Returns:
Tuple of List[float] containing log-posterior for each component/parameter value [log(p_mat(theta_0| obs_i)),…,log(p_mat(theta_{n-1}|obs_i))] and log(p_mat(obs_i).
- Return type:
- matrix_log_posteriors(x, u_mat, u)[source]
- row_choice(p_mat, rng)[source]
Vectorized choice call for varying sampling weights on contained in the rows of p_mat.
N, S = p_mat.shape
Choice is called on range [0,S), where the rows of p_mat are the sample weights.
An N dim np.ndarray of ints is returned.
- Parameters:
p_mat (np.ndarray) – N by S matrix with weights
rng (Optional[RandomState]) – Set see for sampling.
- Returns:
N dim numpy array of ints.
- Return type: