I built a model using a costum likelihood, including both contiunous and discrete RVs, and ran sampling from posterior successfully, with the following codes:
A reproducible example would be useful – I don’t know an easy way of getting a MultiObservedRV off the top of my head! I guess you are feeding observations into more than one spot?
import pandas as pd #To work with dataset
import numpy as np #Math library
import seaborn as sns #Graph library that use matplot in background
import matplotlib.pyplot as plt #to plot some parameters
import pymc3 as pm
import theano.tensor as tt
#load data
X = pd.read_csv('/my/data/file/path')
X_values = X.to_numpy()
def my_density(theta,W):
def logp(X):
#X is the data containing the first 23 features
def log_expo(lam,x):
return( tt.sum(tt.log(lam) - lam * x) )
def log_bernoulli(p,x):
return( tt.sum(1.* ( tt.switch( x, tt.log(p), tt.log(1 - p) ))) )
LL = np.array([
log_expo(tetha[0],X[:,0]),
log_bernoulli(tetha[1],X[:,1]),
])
return( np.dot(W,LL) )
return(logp)
#hyperparameters for exponential distribution
hyper_expo_lower = 0.000001
hyper_expo_upper = 10
#hyperparameters for prior of bernoulli distribution : X3 - X23
hyper_bern_lower = 0
hyper_bern_upper = 1
with pm.Model() as model:
##set prior for parameters
#for X0
r_0 = pm.Uniform('r_0',lower = hyper_expo_lower, upper = hyper_expo_upper)
#for X1
p_1 = pm.Uniform('p_1',lower = hyper_bern_lower, upper = hyper_bern_upper)
tetha = np.array([
r_0, #for X0
p_1, #for X1
])
##set likelihood
joint_obs = pm.DensityDist('joint_obs',
my_density(tetha,#paramters
W,#weight
),
observed={'X' : X, #samples for X0 - X1
}
)
In the above model, This model contains 2 random variables (X has 2 dimensions). X is the data matrix: each column is a random variable, and each row is a sample.
my_density() is the costum likelihood. It accepts 2 arguments. The first argument tetha contains parameters for the two dimensions.
The second argument W is a weight vector used in likelihood definition, whose values are known.
Is there any way to solve the problem?
Can you tell me how can I get these samples (dataframe each column is a parameter and each row is a sample from the joint distribution of parameters)? If I can get these samples, I can draw them using other python visualization tools.