- 
                Notifications
    You must be signed in to change notification settings 
- Fork 2.1k
Implement Ordered distribution factory #7603
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
          
     Draft
      
      
            ricardoV94
  wants to merge
  1
  commit into
  pymc-devs:main
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
ricardoV94:ordered
  
      
      
   
  
    
  
  
  
 
  
      
    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.
          
          
  
     Draft
                    Changes from all commits
      Commits
    
    
  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
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| # Copyright 2024 The PyMC Developers | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| import pytensor.tensor as pt | ||
|  | ||
| from pytensor.tensor.random.op import RandomVariable | ||
| from pytensor.tensor.random.utils import normalize_size_param | ||
| from pytensor.tensor.variable import TensorVariable | ||
|  | ||
| from pymc.distributions.distribution import ( | ||
| Distribution, | ||
| SymbolicRandomVariable, | ||
| _support_point, | ||
| ) | ||
| from pymc.distributions.shape_utils import change_dist_size, get_support_shape_1d, rv_size_is_none | ||
| from pymc.distributions.transforms import _default_transform, ordered | ||
|  | ||
|  | ||
| class OrderedRV(SymbolicRandomVariable): | ||
| inline_logprob = True | ||
| extended_signature = "(x)->(x)" | ||
| _print_name = ("Ordered", "\\operatorname{Ordered}") | ||
|  | ||
| @classmethod | ||
| def rv_op(cls, dist, *, size=None): | ||
| # We don't allow passing `rng` because we don't fully control the rng of the components! | ||
|  | ||
| size = normalize_size_param(size) | ||
|  | ||
| if not rv_size_is_none(size): | ||
| core_shape = tuple(dist.shape)[-1] | ||
| shape = (*tuple(size), core_shape) | ||
| dist = change_dist_size(dist, shape) | ||
|  | ||
| sorted_rv = pt.sort(dist, axis=-1) | ||
|  | ||
| return OrderedRV( | ||
| inputs=[dist], | ||
| outputs=[sorted_rv], | ||
| )(dist) | ||
|  | ||
|  | ||
| class Ordered(Distribution): | ||
| r"""Univariate IID Ordered distribution. | ||
|  | ||
| The pdf of the oredered distribution is | ||
|  | ||
| .. math:: | ||
| f(x_1, ..., x_n) = n!\prod_{i=1}^n f(x_{(i)}), | ||
| where x_1 <= x2 <= ... <= x_n | ||
|  | ||
| Parameters | ||
| ---------- | ||
| dist: unnamed_distribution | ||
| Univariate IID distribution which will be sorted. | ||
|  | ||
| .. warning:: dist will be cloned, rendering it independent of the one passade as input | ||
|  | ||
| Examples | ||
| -------- | ||
| .. code-block:: python | ||
| import pymc as pm | ||
|  | ||
| with pm.Model(): | ||
| x = pm.Normal.dist(mu=0, sigma=1) # Must be IID | ||
| ordered_x = pm.Ordered("ordered_x", dist=x, shape=(3,)) | ||
|  | ||
| pm.draw(ordered_x, random_seed=52) # array([0.05172346, 0.43970706, 0.91500416]) | ||
| """ | ||
|  | ||
| rv_type = OrderedRV | ||
| rv_op = OrderedRV.rv_op | ||
|  | ||
| def __new__(cls, name, dist, *, support_shape=None, **kwargs): | ||
| support_shape = get_support_shape_1d( | ||
| support_shape=support_shape, | ||
| shape=None, # shape will be checked in `cls.dist` | ||
| dims=kwargs.get("dims", None), | ||
| observed=kwargs.get("observed", None), | ||
| ) | ||
| return super().__new__(cls, name, dist, support_shape=support_shape, **kwargs) | ||
|  | ||
| @classmethod | ||
| def dist(cls, dist, *, support_shape=None, **kwargs): | ||
| if not isinstance(dist, TensorVariable) or not isinstance( | ||
| dist.owner.op, RandomVariable | SymbolicRandomVariable | ||
| ): | ||
| raise ValueError( | ||
| f"Ordered dist must be a distribution created via the `.dist()` API, got {type(dist)}" | ||
| ) | ||
| if dist.owner.op.ndim_supp > 0: | ||
| raise NotImplementedError("Ordering of multivariate distributions not supported") | ||
| if not all( | ||
| all(param.type.broadcastable) for param in dist.owner.op.dist_params(dist.owner) | ||
| ): | ||
| raise ValueError("Ordered dist must be an IID variable") | ||
|  | ||
| support_shape = get_support_shape_1d( | ||
| support_shape=support_shape, | ||
| shape=kwargs.get("shape", None), | ||
| ) | ||
| if support_shape is not None: | ||
| dist = change_dist_size(dist, support_shape) | ||
|  | ||
| dist = pt.atleast_1d(dist) | ||
|  | ||
| return super().dist([dist], **kwargs) | ||
|  | ||
|  | ||
| @_default_transform.register(OrderedRV) | ||
| def default_transform_ordered(op, rv): | ||
| if rv.type.dtype.startswith("float"): | ||
| return ordered | ||
| else: | ||
| return None | ||
|  | ||
|  | ||
| @_support_point.register(OrderedRV) | ||
| def support_point_ordered(op, rv, dist): | ||
| # FIXME: This does not work with the default ordered transform | ||
| # which maps [0, 0, 0] to [0, -inf, -inf]. | ||
| # return support_point(dist) | ||
| return rv # Draw from the prior | ||
  
    
      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
    
  
  
    
              
      
      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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@aseyboldt @lucianopaz any idea if we could easily modify the OrderedTransform to work nicely with equal values in the constrained space? The current one maps a
[0, 0, 0]vector to[0, -inf, -inf]. My guess is not.But then I don't have a nice way to define a valid support_point. I could try to add an increasing jitter but that will sooner or later fail for bounded distributions.