MCMC Estimation of Copula CAN'T WORK
还没有人认领这个 Issue。
评估
- 难度
- 5/5
- 预计耗时
- 一周以上
- 新手友好度
- 18/100
- Issue 类型
- 缺陷
- 描述清晰度
- 需要澄清
- 活跃度
- 停滞
- 技术栈
- jupyter-notebook, python
- 领域
- data, machine-learning
调研方向
未确定任何仓库文件或失败的测试。首先缩减围绕 joint_log_prob、construct_hmc 和 run_chain 的粘贴示例,并使用显示的初始值确定确切错误。完成要求包括:可复现的失败、记录在案的预期结果,以及经过验证且可正常工作的 MCMC copula 估计路径。
由索引模型根据 Issue 内容生成。
描述
The samples is the initial values.
import numpy as np
import os
import re
os.environ["CUDA_VISIBLE_DEVICES"] ="-1"
from scipy.special import gamma
import scipy.stats
import tensorflow as tf
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Dense
from tensorflow.keras.optimizers import Adam
from math import pi
import tensorflow_probability as tfp
tfd = tfp.distributions
tfb = tfp.bijectors
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import seaborn as sns
from scipy.integrate import quad
print("TFP Version", tfp.version)
print("TF Version", tf.version)
np.random.seed(1704)
import scipy
import seaborn
import matplotlib.pyplot as plt
import numpy as np
from scipy.integrate import nquad
data = np.array(value_lists)[:, :, 0]
NUM_POSTERIOR_SAMPLES = 10000
NUM_BURNIN_ITERATIONS = int(0.25 * NUM_BURNIN_ITERATIONS)
NUM_ADAPTATION = int(0.5 * NUM_BURNIN_ITERATIONS)
class GaussianCopulaTriL(tfd.TransformedDistribution):
"""Takes a location, and lower triangular matrix for the Cholesky factor."""
def __init__(self, loc, scale_tril):
super(GaussianCopulaTriL, self).__init__(
distribution=tfd.MultivariateNormalTriL(
loc=loc,
scale_tril=scale_tril),
bijector=tfb.NormalCDF(),
validate_args=False,
name="GaussianCopulaTriLUniform")
def _parameter_properties(self, dtype, num_classes=None):
return dict(
# Annotations may optionally specify properties, such as `event_ndims`,
# `default_constraining_bijector_fn`, `specifies_shape`, etc.; see
# the `ParameterProperties` documentation for details.
loc=tfp.util.ParameterProperties(),
scale_tril=tfp.util.ParameterProperties())
class WarpedGaussianCopula(tfd.TransformedDistribution):
"""Application of a Gaussian Copula on a list of target marginals.
This implements an application of a Gaussian Copula. Given [x_0, ... x_n]
which are distributed marginally (with CDF) [F_0, ... F_n],
`GaussianCopula` represents an application of the Copula, such that the
resulting multivariate distribution has the above specified marginals.
The marginals are specified by `marginal_bijectors`: These are
bijectors whose `inverse` encodes the CDF and `forward` the inverse CDF.
block_sizes is a 1-D Tensor to determine splits for `marginal_bijectors`
length should be same as length of `marginal_bijectors`.
See tfb.Blockwise for details
"""
def __init__(self, loc, scale_tril, marginal_bijectors, block_sizes=None):
super(WarpedGaussianCopula, self).__init__(
distribution=GaussianCopulaTriL(loc=loc, scale_tril=scale_tril),
bijector=tfb.Blockwise(bijectors=marginal_bijectors,
block_sizes=block_sizes),
validate_args=False,
name="GaussianCopula")
def _parameter_properties(self, dtype, num_classes=None):
return dict(
# Annotations may optionally specify properties, such as `event_ndims`,
# `default_constraining_bijector_fn`, `specifies_shape`, etc.; see
# the `ParameterProperties` documentation for details.
loc=tfp.util.ParameterProperties(),
scale_tril=tfp.util.ParameterProperties(),
marginal_bijectors = tfp.util.ParameterProperties(),
block_sizes = tfp.util.ParameterProperties())
def joint_log_prob(data,
correlation,
ijv_loc,
ijv_scale,
cca_loc,
cca_scale):
"""
Estimate the mean and variance of a Normal distribution.
Parameters
----------
@data: the observed data, binary (0, 1)
@mu: the mean parameter. This is "learnable"
@sigma: the variance parameter. This is "learnable"
"""
correlation_prior = tfd.Normal(loc=0., scale=1.)
ijv_loc_prior = tfd.Normal(loc=400., scale=200.)
ijv_scale_prior = tfd.Normal(loc=100., scale=50.)
cca_loc_prior = tfd.Normal(loc=50., scale=50.)
cca_scale_prior = tfd.Normal(loc=20., scale=10.)
zero_prior = tfd.Normal(loc=0., scale=1.)
if True:
rv_data = WarpedGaussianCopula(
loc=[0., 0.],
scale_tril=[[1., 0.], [correlation, tf.sqrt(1. - correlation ** 2)]],
#These encode the marginals we want. In this case we want X_0 has
#Kumaraswamy marginal, and X_1 has Gumbel marginal.
marginal_bijectors=[
tfb.Invert(tfb.KumaraswamyCDF(ijv_loc, ijv_scale)),
tfb.Invert(tfb.GumbelCDF(loc=cca_loc, scale=cca_scale))])
return (
correlation_prior.log_prob(correlation) +
ijv_loc_prior.log_prob(ijv_loc) +
ijv_scale_prior.log_prob(ijv_scale) +
cca_loc_prior.log_prob(cca_loc) +
cca_scale_prior.log_prob(cca_scale) +
tf.reduce_sum(rv_data.log_prob(data))
)
def prediction(correlation_,
ijv_loc_,
ijv_scal_,
cca_loc_,
cca_scale_):
sampler = lambda z: np.random.choice(z, 1, replace=True)
correlation = np.array(sampler(correlation_))[0]
ijv_loc = np.array(sampler(ijv_loc_))[0]
ijv_scale = np.array(sampler(ijv_scal_))[0]
cca_loc = np.array(sampler(cca_loc_))[0]
cca_scale = np.array(sampler(cca_scale_))[0]
print("correlation, ijv_loc, ijv_scale, cca_loc, cca_scale",
correlation, ijv_loc, ijv_scale, cca_loc, cca_scale)
y_hat = WarpedGaussianCopula(
loc=[0., 0.],
scale_tril=[[1., 0.], [correlation, tf.sqrt(1. - correlation ** 2)]],
marginal_bijectors=[
tfb.Invert(tfb.KumaraswamyCDF(ijv_loc, ijv_scale)),
tfb.Invert(tfb.GumbelCDF(loc=cca_loc, scale=cca_scale))])
return y_hat
data = np.array(value_lists)[:, 1:, 0]
unnormalized_posterior = lambda correlation, ijv_loc, ijv_scale, cca_loc, cca_scale: \
joint_log_prob(data, correlation, ijv_loc, ijv_scale, cca_loc, cca_scale)
[correlation_, ijv_loc_, ijv_scale_, cca_loc_, cca_scale_] = run_chain(
construct_hmc(unnormalized_posterior, adaptation_steps=NUM_ADAPTATION),
inits=[0., 300., 200., 50., 20.],
iters=[NUM_POSTERIOR_SAMPLES, NUM_BURNIN_ITERATIONS]
)
- 主要语言
- Jupyter Notebook
- 星标
- 4.4k
- 派生
- 1.1k
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
tensorflow/probability 的其他 Issue
-
KL divergence between two `TransformedDistribution`s with a shared non-injective bijector (`AbsoluteValue`) returns the base KL可能已有人在做 @Andreas-Hum 于 8 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 72/100
tensorflow/probability#2036 ·
-
难度 3/5 1-2 天 新手友好度 68/100
tensorflow/probability#2034 ·
-
难度 5/5 一周以上 新手友好度 35/100
tensorflow/probability#2021 ·
-
难度 4/5 3-5 天 新手友好度 30/100
tensorflow/probability#2015 ·
-
难度 3/5 1-2 天 新手友好度 48/100
tensorflow/probability#2013 ·
查看 tensorflow/probability 的全部 Issue
相似的 Issue
-
Successor registrant to review: Navient Holdings, Inc. (CIK 2152623), 8-K12B 0001193125-26-436899未关闭successor-review
难度 2/5 1-3 小时 新手友好度 78/100
dgunning/edgartools#1889 ·
维护者通常 1 天内回复
-
discovery
难度 2/5 1-3 小时 新手友好度 64/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 72/100
Open-Systems-Pharmacology/OSPSuite.ParameterIdentification#377 ·
维护者通常 1 天内回复
-
A-io-parquet documentation
难度 2/5 1-3 小时 新手友好度 74/100
维护者通常 1 天内回复
-
good first issue scientific testing
难度 2/5 1-3 小时 新手友好度 68/100
Santhosh-zeta/ColdGuard#108 ·
维护者通常 1 天内回复