Families/ adding base version of exponential family#67
Conversation
779438f to
12516ff
Compare
f67f3fd to
2d470c9
Compare
2d470c9 to
189afea
Compare
1605771 to
1d58077
Compare
83f5584 to
2500678
Compare
| if hasattr(dot, "__len__"): | ||
| dot = dot[0] | ||
|
|
||
| result = np.log(self._normalization(x)) + dot + self._log_partition(theta) |
There was a problem hiding this comment.
The formula does not correspond to the docstring and standard notation I guess.
wiki:
your code:
There was a problem hiding this comment.
Yes, but I decided that + log-partition function is easier to write and check formulas. This is described in docstring of class
| def transform( | ||
| self, | ||
| transform_function: Callable[[NumericArray], NumericArray], | ||
| ) -> ContinuousExponentialClassFamily: | ||
| """ | ||
| Transform the random variable by a monotonic, differentiable function. | ||
|
|
||
| The new density is obtained via the change‑of‑variable formula. | ||
| The sufficient statistic becomes T(transform⁻¹(x)) and the base measure | ||
| gains the Jacobian factor. | ||
|
|
||
| Args: | ||
| transform_function: Invertible, differentiable function g(x) such that | ||
| y = g(x). Must be defined on the original support. | ||
|
|
||
| Returns: | ||
| ContinuousExponentialClassFamily: A new family for the transformed variable. | ||
| """ | ||
|
|
||
| def calculate_jacobian(x: NumericArray) -> NumericArray: | ||
| if not isinstance(x, Iterable): | ||
| x = np.array([x]) | ||
|
|
||
| return np.abs(det(jacobian(transform_function, x).df)) | ||
|
|
||
| def new_support(x: NumericArray) -> bool: | ||
| return transform_function(x) in self._support | ||
|
|
||
| def new_sufficient(x: NumericArray) -> NumericArray: | ||
| return self._sufficient(transform_function(x)) | ||
|
|
||
| def new_normalization(x: NumericArray) -> Number: | ||
| return cast(np.float64, self._normalization(x) * calculate_jacobian(x)) | ||
|
|
||
| return ContinuousExponentialClassFamily( | ||
| log_partition=self._log_partition, | ||
| sufficient_statistics=new_sufficient, | ||
| normalization_constant=new_normalization, | ||
| support=SupportByPredicate(predicate=new_support), | ||
| parameter_space=self._parameter_space, | ||
| sufficient_statistics_values=self._sufficient_statistics_values, | ||
| name=f"Transformed{self._name}", | ||
| distr_type=self._distr_type, | ||
| distr_parametrizations=self.parametrization_names, | ||
| support_by_parametrization=self.support_resolver, | ||
| ) |
There was a problem hiding this comment.
AI finding
transform(...) is semantically incorrect or at least incorrectly documented: the docstring talks about the direct conversion of
No description provided.