-
-
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
c1584a7
commit ed7e2b0
Showing
5 changed files
with
396 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,99 @@ | ||
# 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 aesara.tensor as at | ||
import numpy as np | ||
|
||
from aesara.scalar import Clip | ||
from aesara.tensor.elemwise import DimShuffle | ||
|
||
from pymc.distributions.distribution import DerivedDistribution, _get_moment | ||
|
||
|
||
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 = at.constant(-np.inf) | ||
if upper is None: | ||
upper = at.constant(np.inf) | ||
|
||
rv_out = at.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 | ||
# lower / upper may have broadcasted the distribution, in which case we get a Dimshuffle Op | ||
if isinstance(dist.owner.op, DimShuffle): | ||
dist_node = dist_node.inputs[0].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 | ||
# lower / upper may have broadcasted the distribution, in which case we get a Dimshuffle Op | ||
if isinstance(dist.owner.op, DimShuffle): | ||
dist_node = dist_node.inputs[0].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,) | ||
|
||
|
||
@_get_moment.register(Clip) | ||
def get_moment_censored(op, rv, dist, lower, upper): | ||
moment = at.switch( | ||
at.eq(lower, -np.inf), | ||
at.switch( | ||
at.isinf(upper), | ||
# lower = -inf, upper = inf | ||
0, | ||
# lower = -inf, upper = x | ||
upper - 1, | ||
), | ||
at.switch( | ||
at.eq(upper, np.inf), | ||
# lower = x, upper = inf | ||
lower + 1, | ||
# lower = x, upper = x | ||
(lower + upper) / 2, | ||
), | ||
) | ||
moment = at.full_like(dist, moment) | ||
return moment |
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
Oops, something went wrong.