-
Notifications
You must be signed in to change notification settings - Fork 30
style: add type hinting to variational distributions #261
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
Open
leahaeusel
wants to merge
2
commits into
queens-py:main
Choose a base branch
from
leahaeusel:add-type-hinting-to-variational-distributions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,95 +15,169 @@ | |
| """Variational Distribution.""" | ||
|
|
||
| import abc | ||
| from typing import Any, Literal, TypeAlias, TypeVar | ||
|
|
||
| import numpy as np | ||
|
|
||
| # pylint: disable=invalid-name | ||
|
|
||
| NDims = TypeVar("NDims", bound=int) | ||
| NSamples = TypeVar("NSamples", bound=int) | ||
| NParams = TypeVar("NParams", bound=int) | ||
| NParamsComponent = TypeVar("NParamsComponent", bound=int) | ||
|
|
||
| # Vectors | ||
| ArrayNDims: TypeAlias = np.ndarray[tuple[NDims], np.dtype[np.floating]] | ||
| ArrayNParams: TypeAlias = np.ndarray[tuple[NParams], np.dtype[np.floating]] | ||
| ArrayNParamsComponent: TypeAlias = np.ndarray[tuple[NParamsComponent], np.dtype[np.floating]] | ||
| ArrayNSamples: TypeAlias = np.ndarray[tuple[NSamples], np.dtype[np.floating]] | ||
|
|
||
| # Matrices | ||
| Array1XNParams: TypeAlias = np.ndarray[tuple[Literal[1], NParams], np.dtype[np.floating]] | ||
| ArrayNDimsX1: TypeAlias = np.ndarray[tuple[NDims, Literal[1]], np.dtype[np.floating]] | ||
| ArrayNDimsXNDims: TypeAlias = np.ndarray[tuple[NDims, NDims], np.dtype[np.floating]] | ||
| ArrayNParamsXNParams: TypeAlias = np.ndarray[tuple[NParams, NParams], np.dtype[np.floating]] | ||
| ArrayNParamsXNSamples: TypeAlias = np.ndarray[tuple[NParams, NSamples], np.dtype[np.floating]] | ||
| ArrayNSamplesXNDims: TypeAlias = np.ndarray[tuple[NSamples, NDims], np.dtype[np.floating]] | ||
| ArrayNSamplesXNParams: TypeAlias = np.ndarray[tuple[NSamples, NParams], np.dtype[np.floating]] | ||
|
Comment on lines
+24
to
+42
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I love this! I was always hoping we would be able to do something like this. It will help us a lot! 🙌 |
||
|
|
||
|
|
||
| class Variational: | ||
| """Base class for probability distributions for variational inference. | ||
|
|
||
| Attributes: | ||
| dimension (int): dimension of the distribution | ||
| dimension: Dimension of the distribution | ||
| n_parameters: Number of variational parameters | ||
| """ | ||
|
|
||
| def __init__(self, dimension): | ||
| """Initialize variational distribution.""" | ||
| def __init__(self, dimension: NDims, n_parameters: NParams) -> None: | ||
| """Initialize variational distribution. | ||
|
|
||
| Args: | ||
| dimension: Dimension of the variational distribution | ||
| n_parameters: Number of variational parameters | ||
| """ | ||
| self.dimension = dimension | ||
| self.n_parameters = n_parameters | ||
|
|
||
| @abc.abstractmethod | ||
| def reconstruct_distribution_parameters(self, variational_parameters): | ||
| def construct_variational_parameters(self, *args: Any) -> ArrayNParams: | ||
| """Construct variational parameters from distribution parameters. | ||
|
|
||
| Args: | ||
| args: Distribution parameters | ||
|
|
||
| Returns: | ||
| Variational parameters | ||
| """ | ||
leahaeusel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @abc.abstractmethod | ||
| def reconstruct_distribution_parameters(self, variational_parameters: ArrayNParams) -> Any: | ||
| """Reconstruct distribution parameters from variational parameters. | ||
|
|
||
| Args: | ||
| variational_parameters (np.ndarray): Variational parameters | ||
| variational_parameters: Variational parameters | ||
|
|
||
| Returns: | ||
| Distribution parameters | ||
| """ | ||
|
|
||
| @abc.abstractmethod | ||
| def draw(self, variational_parameters, n_draws=1): | ||
| def draw(self, variational_parameters: ArrayNParams, n_draws: NSamples) -> ArrayNSamplesXNDims: | ||
leahaeusel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """Draw *n_draws* samples from distribution. | ||
|
|
||
| Args: | ||
| variational_parameters (np.ndarray): variational parameters (1 x n_params) | ||
| n_draws (int): Number of samples | ||
| variational_parameters: Variational parameters | ||
| n_draws: Number of samples | ||
|
|
||
| Returns: | ||
| Drawn samples | ||
| """ | ||
|
|
||
| @abc.abstractmethod | ||
| def logpdf(self, variational_parameters, x): | ||
| """Evaluate the natural logarithm of the logpdf at sample. | ||
| def logpdf( | ||
| self, | ||
| variational_parameters: ArrayNParams, | ||
| x: ArrayNSamplesXNDims, | ||
| ) -> ArrayNSamples: | ||
| """Evaluate the natural logarithm of the PDF. | ||
|
|
||
| Args: | ||
| variational_parameters (np.ndarray): variational parameters (1 x n_params) | ||
| x (np.ndarray): Locations to evaluate (n_samples x n_dim) | ||
| variational_parameters: Variational parameters | ||
| x: Locations to evaluate | ||
|
|
||
| Returns: | ||
| Log-PDF values | ||
| """ | ||
|
|
||
| @abc.abstractmethod | ||
| def pdf(self, variational_parameters, x): | ||
| """Evaluate the probability density function (pdf) at sample. | ||
| def pdf( | ||
| self, | ||
| variational_parameters: ArrayNParams, | ||
| x: ArrayNSamplesXNDims, | ||
| ) -> ArrayNSamples: | ||
| """Evaluate the probability density function (PDF). | ||
|
|
||
| Args: | ||
| variational_parameters (np.ndarray): variational parameters (1 x n_params) | ||
| x (np.ndarray): Locations to evaluate (n_samples x n_dim) | ||
| variational_parameters: Variational parameters | ||
| x: Locations to evaluate | ||
|
|
||
| Returns: | ||
| PDF values | ||
| """ | ||
|
|
||
| @abc.abstractmethod | ||
| def grad_params_logpdf(self, variational_parameters, x): | ||
| """Logpdf gradient w.r.t. the variational parameters. | ||
| def grad_params_logpdf( | ||
| self, | ||
| variational_parameters: ArrayNParams, | ||
| x: ArrayNSamplesXNDims, | ||
| ) -> ArrayNParamsXNSamples: | ||
| """Log-PDF gradient w.r.t. the variational parameters. | ||
|
|
||
| Evaluated at samples *x*. Also known as the score function. | ||
|
|
||
| Args: | ||
| variational_parameters (np.ndarray): variational parameters (1 x n_params) | ||
| x (np.ndarray): Locations to evaluate (n_samples x n_dim) | ||
| variational_parameters: Variational parameters | ||
| x: Locations to evaluate | ||
|
|
||
| Returns: | ||
| Gradient of the log-PDF w.r.t. the variational parameters | ||
| """ | ||
|
|
||
| @abc.abstractmethod | ||
| def fisher_information_matrix(self, variational_parameters): | ||
| """Compute the fisher information matrix. | ||
| def fisher_information_matrix( | ||
| self, variational_parameters: ArrayNParams | ||
| ) -> ArrayNParamsXNParams: | ||
| """Compute the Fisher information matrix. | ||
|
|
||
| Depends on the variational distribution for the given | ||
| parameterization. | ||
|
|
||
| Args: | ||
| variational_parameters (np.ndarray): variational parameters (1 x n_params) | ||
| variational_parameters: Variational parameters | ||
|
|
||
| Returns: | ||
| Fisher information matrix | ||
| """ | ||
|
|
||
| @abc.abstractmethod | ||
| def initialize_variational_parameters(self, random=False): | ||
| def initialize_variational_parameters(self, random: bool = False) -> ArrayNParams: | ||
| """Initialize variational parameters. | ||
|
|
||
| Args: | ||
| random (bool, optional): If True, a random initialization is used. Otherwise the | ||
| default is selected | ||
| random: If True, a random initialization is used. Otherwise the default is selected. | ||
|
|
||
| Returns: | ||
| variational_parameters (np.ndarray): variational parameters (1 x n_params) | ||
| Variational parameters | ||
| """ | ||
|
|
||
| @abc.abstractmethod | ||
| def export_dict(self, variational_parameters): | ||
| def export_dict(self, variational_parameters: ArrayNParams) -> dict: | ||
| """Create a dict of the distribution based on the given parameters. | ||
|
|
||
| Args: | ||
| variational_parameters (np.ndarray): Variational parameters | ||
| variational_parameters: Variational parameters | ||
|
|
||
| Returns: | ||
| export_dict (dictionary): Dict containing distribution information | ||
| Dictionary containing distribution information | ||
| """ | ||
Oops, something went wrong.
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.