-
Notifications
You must be signed in to change notification settings - Fork 7
Moments method #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Moments method #38
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| """The module in which the moments method is presented""" | ||
|
|
||
| import numpy as np | ||
|
|
||
| from mpest import Samples | ||
| from mpest.core.distribution import Distribution | ||
| from mpest.core.mixture_distribution import MixtureDistribution | ||
| from mpest.core.problem import Problem, Result | ||
| from mpest.em.methods.abstract_steps import AMaximization | ||
| from mpest.exceptions import MStepError | ||
| from mpest.utils import ResultWithError | ||
|
|
||
| EResult = tuple[Problem, np.ndarray] | ResultWithError[MixtureDistribution] | ||
|
|
||
|
|
||
| class MomentsMStep(AMaximization[EResult]): | ||
| """ | ||
| Class which calculate new params using matrix with indicator from E step. | ||
| """ | ||
|
|
||
| def calc_order_moment_of_index_element(self, order: int, i: int, samples: Samples, indicators: np.ndarray) -> float: | ||
| """ | ||
| A function that calculates the list of n-th moments of each distribution. | ||
|
|
||
| :param order: Order of Moment. | ||
| :param i: The number of the distribution for which we count the moment. | ||
| :param samples: Ndarray with samples. | ||
| :param indicators: Matrix with indicators | ||
|
|
||
| :return: order-Moment of index element. | ||
| """ | ||
|
|
||
| sum_j_row_probabilities = np.sum(indicators[i]) | ||
|
|
||
| if sum_j_row_probabilities == 0: | ||
| return 0 | ||
|
|
||
| moment_values = samples**order | ||
|
|
||
| numerator = np.sum(moment_values * indicators[i]) | ||
|
|
||
| return numerator / sum_j_row_probabilities | ||
|
|
||
| def step(self, e_result: EResult) -> Result: | ||
| """ | ||
| A function that performs M step | ||
|
|
||
| :param e_result: Tuple with problem, new_priors and indicators. | ||
| """ | ||
|
|
||
| if isinstance(e_result, ResultWithError): | ||
| return e_result | ||
|
|
||
| problem, indicators = e_result | ||
|
|
||
| samples = problem.samples | ||
|
|
||
| mixture = problem.distributions | ||
|
|
||
| new_priors = np.sum(indicators, axis=1) / len(samples) | ||
|
|
||
| max_params_count = max(len(d.params) for d in mixture) | ||
| moments = np.zeros(shape=[len(mixture), max_params_count]) | ||
|
|
||
| for j, d in enumerate(mixture): | ||
| for r in range(len(d.params)): | ||
| moments[j][r] = self.calc_order_moment_of_index_element(r + 1, j, samples, indicators) | ||
|
|
||
| for i, d in enumerate(mixture): | ||
| if d.model.name == "WeibullExp" and (moments[i][0] * moments[i][1] < 0): | ||
| error = MStepError("The weibul distribution degenerated in the first step.") | ||
| return ResultWithError(mixture.distributions, error) | ||
|
|
||
| new_distributions = [] | ||
|
|
||
| for j, d in enumerate(mixture): | ||
| new_params = d.model.calc_moments_params(moments[j]) | ||
| new_d = Distribution(d.model, d.model.params_convert_to_model(new_params)) | ||
| new_distributions.append(new_d) | ||
|
|
||
| new_mixture = MixtureDistribution.from_distributions(new_distributions, new_priors) | ||
| return ResultWithError(new_mixture) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| from mpest.core.problem import Problem, Result | ||
| from mpest.em import EM | ||
| from mpest.em.breakpointers import ParamDifferBreakpointer, StepCountBreakpointer | ||
| from mpest.em.distribution_checkers import ( | ||
| FiniteChecker, | ||
| PriorProbabilityThresholdChecker, | ||
| ) | ||
| from mpest.em.methods.likelihood_method import BayesEStep | ||
| from mpest.em.methods.method import Method | ||
| from mpest.em.methods.moments_method import MomentsMStep | ||
|
|
||
|
|
||
| def run_test(problem: Problem, deviation: float) -> Result: | ||
| method = Method(BayesEStep(), MomentsMStep()) | ||
| em_algo = EM( | ||
| StepCountBreakpointer() + ParamDifferBreakpointer(deviation=deviation), | ||
| FiniteChecker() + PriorProbabilityThresholdChecker(), | ||
| method, | ||
| ) | ||
|
|
||
| return em_algo.solve(problem=problem) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.