Skip to content
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

Added MVN_cdf.py #60

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions pymc_experimental/distributions/mvncdf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import aesara
from aesara.graph.basic import Apply
from aesara.graph.op import Op
from aesara.tensor.type import TensorType

# Op calculates mvncdf


def conditional_covariance(Sigma, mu, conditioned, conditioned_values):

"""
Sigma: d x d covariance matrix
mu: mean, array length d
conditioned: array of dimensions to condition on
conditioned_values: array of conditional values equal in length to `conditioned`
"""

target = (1 - conditioned).astype(bool)

Sigma_22 = Sigma[conditioned][:, conditioned]
Sigma_21 = Sigma[conditioned][:, target]
Sigma_12 = Sigma[target][:, conditioned]
Sigma_11 = Sigma[target][:, target]

Sigma_cond = Sigma_11 - np.matmul(np.matmul(Sigma_12, np.linalg.inv(Sigma_22)), Sigma_21)
JessSpearing marked this conversation as resolved.
Show resolved Hide resolved
mean_cond = np.delete(mu, conditioned) + np.matmul(Sigma_12, np.linalg.inv(Sigma_22)).dot(
conditioned_values - mu[conditioned]
)

return Sigma_cond, mean_cond


class Mvncdf(Op):
__props__ = ()

def make_node(self, upper, mu, cov):
JessSpearing marked this conversation as resolved.
Show resolved Hide resolved
upper = at.as_tensor_variable(upper)
mu = at.as_tensor_variable(mu)
cov = at.as_tensor_variable(cov)

return Apply(self, [x], [x.type()])
JessSpearing marked this conversation as resolved.
Show resolved Hide resolved

def perform(self, node, inputs, output_storage):
# here do the integration
return scipy.stats.multivariate_normal.logcdf(upper, mu, cov)
JessSpearing marked this conversation as resolved.
Show resolved Hide resolved

def infer_shape(self, fgraph, node, i0_shapes):
return
JessSpearing marked this conversation as resolved.
Show resolved Hide resolved

def grad(self, inputs, output_grads):

grad_ = []
for i in range(len(mu)):
grad_.append(conditional_covariance(cov, mu, i, upper[i]))
JessSpearing marked this conversation as resolved.
Show resolved Hide resolved

return np.array(grad_)
JessSpearing marked this conversation as resolved.
Show resolved Hide resolved

def R_op(self, inputs, eval_points):
# R_op can receive None as eval_points.
# That mean there is no diferientiable path through that input
# If this imply that you cannot compute some outputs,
# return None for those.
if eval_points[0] is None:
return eval_points
return self.grad(inputs, eval_points)