LogLikelihood#
- class LogLikelihood[source]#
Bases:
object- static categorical(predicted=None, observed=None, negative=True, **kwargs)[source]#
Compute the log likelihood of the predicted values given the observed values for categorical data.
Categorical(y|p) = p_y
- Parameters:
- predictedarray_like
The predicted values. It must have the same shape as
observed. See Notes for more details.- observedarray_like
The observed values. It must have the same shape as
predicted. See Notes for more details.- negativebool, optional
Flag indicating whether to return the negative log likelihood.
- Returns:
floatThe log likelihood or negative log likelihood.
Notes
predictedandobservedmust have the same shape.observedis a vector of integers starting from 0 (first possible response), where each integer corresponds to the observed value. If there are two choice options, then observed would have a shape of (n, 2) and predicted would have a shape of (n, 2). On each row ofobserved, the array would have a 1 in the column corresponding to the observed value and a 0 in the other column.Examples
>>> import numpy as np >>> observed = np.array([0, 1, 0, 1]) >>> predicted = np.array([[0.7, 0.3], [0.3, 0.7], [0.6, 0.4], [0.4, 0.6]]) >>> LogLikelihood.categorical(predicted, observed) 1.7350011354094463
- static bernoulli(predicted=None, observed=None, negative=True, **kwargs)[source]#
Compute the log likelihood of the predicted values given the observed values for Bernoulli data.
Bernoulli(y|p) = p if y = 1 and 1 - p if y = 0
- Parameters:
- predictedarray_like
The predicted values. It must have the same shape as
observed. See Notes for more details.- observedarray_like
The observed values. It must have the same shape as
predicted. See Notes for more details.- negativebool, optional
Flag indicating whether to return the negative log likelihood.
- Returns:
floatThe summed log likelihood or negative log likelihood.
Notes
predictedandobservedmust have the same shape.observedis a binary variable, so it can only take the values 0 or 1.predictedmust be a value between 0 and 1. Values are clipped to avoid log(0) and log(1). If we encounter any non-finite values, we set any log likelihood to the value of np.log(1e-100).Examples
>>> import numpy as np >>> observed = np.array([1, 0, 1, 0]) >>> predicted = np.array([0.7, 0.3, 0.6, 0.4]) >>> LogLikelihood.bernoulli(predicted, observed) 1.7350011354094463
- static continuous(predicted, observed, negative=True, **kwargs)[source]#
Compute the log likelihood of the predicted values given the observed values for continuous data.
- Parameters:
- predictedarray_like
The predicted values.
- observedarray_like
The observed values.
- negativebool, optional
Flag indicating whether to return the negative log likelihood.
- Returns:
floatThe summed log likelihood or negative log likelihood.
Examples
>>> import numpy as np >>> observed = np.array([1, 0, 1, 0]) >>> predicted = np.array([0.7, 0.3, 0.6, 0.4]) >>> LogLikelihood.continuous(predicted, observed) 1.7350011354094463
- static multinomial(predicted, observed, negative=True, clip=1e-10, **kwargs)[source]#
Compute the log likelihood of the predicted values given the observed values for multinomial data.
- Parameters:
- predictedarray_like
The predicted values.
- observedarray_like
The observed values.
- negativebool, optional
Flag indicating whether to return the negative log likelihood.
- Returns:
floatThe summed log likelihood or negative summed log likelihood.
Examples
>>> # Sample data >>> predicted = np.array([[0.2, 0.5, 0.3], [0.1, 0.7, 0.2]]) >>> observed = np.array([[2, 5, 3], [1, 7, 2]])
>>> # Calculate log likelihood >>> ll_float = LogLikelihood.multinomial(predicted, observed) >>> print("Log Likelihood (multinomial):", ll) Log Likelihood (multinomial): 4.596597454123483
- static product(predicted, observed, negative=True, clip=1e-10, **kwargs)[source]#
Compute the log likelihood of the predicted values given the observed values for continuous data, according to the following equation:
likelihood = sum(observed * log(predicted))
- Parameters:
- predictedarray_like
The predicted values.
- observedarray_like
The observed values.
- negativebool, optional
Flag indicating whether to return the negative log likelihood.
- Returns:
floatThe summed log likelihood or negative log likelihood.
Examples
>>> # Sample data >>> predicted = np.array([[0.2, 0.5, 0.3], [0.1, 0.7, 0.2]]) >>> observed = np.array([[2, 5, 3], [1, 7, 2]])
>>> # Calculate log likelihood >>> ll_float = LogLikelihood.product(predicted, observed) >>> print("Log Likelihood :", ll_float) Log Likelihood : 18.314715666079106