-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fd25d48
commit 7639c7f
Showing
4 changed files
with
338 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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,68 @@ | ||
# Copyright 2020 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 numpy as np | ||
|
||
from aesara.tensor import clip | ||
|
||
from pymc.distributions.distribution import DerivedDistribution | ||
|
||
|
||
class Censored(DerivedDistribution): | ||
@classmethod | ||
def dist(cls, distribution, lower, upper, **kwargs): | ||
# TODO: Assert distribution is a RandomVariable | ||
if distribution.owner.op.ndim_supp > 0: | ||
raise NotImplemented( | ||
"Censoring of multivariate distributions has not been implemented yet" | ||
) | ||
return super().dist([distribution, lower, upper], **kwargs) | ||
|
||
@classmethod | ||
def rv_op(cls, dist, lower=None, upper=None, size=None, rngs=None): | ||
if lower is None: | ||
lower = -np.inf | ||
if upper is None: | ||
upper = np.inf | ||
|
||
rv_out = clip(dist, lower, upper) | ||
if size is not None: | ||
rv_out = cls.change_size(rv_out, size) | ||
if rngs is not None: | ||
rv_out = cls.change_rngs(rv_out, rngs) | ||
return rv_out | ||
|
||
@classmethod | ||
def ndim_supp(cls, *dist_params): | ||
return 0 | ||
|
||
@classmethod | ||
def change_size(cls, rv, new_size): | ||
dist, lower, upper = rv.owner.inputs | ||
dist_node = dist.owner | ||
rng, old_size, dtype, *dist_params = dist_node.inputs | ||
new_dist = dist_node.op.make_node(rng, new_size, dtype, *dist_params).default_output() | ||
return cls.rv_op(new_dist, lower, upper) | ||
|
||
@classmethod | ||
def change_rngs(cls, rv, new_rngs): | ||
(new_rng,) = new_rngs | ||
dist, lower, upper = rv.owner.inputs | ||
dist_node = dist.owner | ||
olg_rng, size, dtype, *dist_params = dist_node.inputs | ||
new_dist = dist_node.op.make_node(new_rng, size, dtype, *dist_params).default_output() | ||
return cls.rv_op(new_dist, lower, upper) | ||
|
||
@classmethod | ||
def graph_rvs(cls, dist, *bounds): | ||
return (dist,) |
This file contains 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 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