import scipy.special as sp
def tweedie_logp(value, mu, p, phi):
# Implement the log probability function of the Tweedie distribution
# based on the provided parameters: mu, p, and phi
# Return the logarithm of the probability density/mass function of the distribution
# given the observed value
# Your implementation of the Tweedie log probability function goes here
est_lambda = mu ** (2 - p) / (2 - p)
est_alpha = (2 - p) / (p - 1)
est_beta = mu ** (1 - p) / (p - 1)
gam = phi * (p - 1 ) * (mu ** (p - 1 ))
#Calculate the log probability using the provided parameters and observed value
logp_value = (
(1 - p) * np.log(value.eval() / mu) - (value.eval() / (mu ** p * phi)) - np.log(phi)
+ np.log((1 - p) * mu ** (1 - p)))
return logp_value
with pm.Model() as model:
# Prior distributions for the unknown parameters
alpha = pm.Normal('alpha', mu=0, sigma=10)
# Priors for the binary variables
offline_campaign_prior = pm.Bernoulli('MISX_campaign_prior', p=0.5)
discounts_prior = pm.Bernoulli('PA_prior', p=0.5)
promotion_events_prior = pm.Bernoulli('promotion_events_prior', p=0.5)
# Compute the expected sales based on the variables
expected_sales = pm.math.exp(alpha+storedata['day_of_week'].values + storedata['month'].values + storedata['year'].values + storedata['MISX'].values * offline_campaign_prior +
storedata['Status'].values * discounts_prior +
storedata['event'].values * promotion_events_prior)
# Define the likelihood using the custom Tweedie distribution
sales = pm.DensityDist('sales_observed', tweedie_logp, observed={'value': storedata['Rslr_Sales_Amount'].values, 'mu': expected_sales, 'p': 1.5, 'phi': 1})
# Define the model
trace = pm.sample(1000, tune=1000)
TypeError Traceback (most recent call last) Cell In[100], line 39 34 expected_sales = pm.math.exp(alpha+storedata[‘day_of_week’].values + storedata[‘month’].values + storedata[‘year’].values + storedata[‘MISX’].values * offline_campaign_prior + 35 storedata[‘Status’].values * discounts_prior + 36 storedata[‘event’].values * promotion_events_prior) 38 # Define the likelihood using the custom Tweedie distribution —> 39 sales = pm.DensityDist(‘sales_observed’, tweedie_logp, observed={‘value’: storedata[‘Rslr_Sales_Amount’].values, ‘mu’: expected_sales, ‘p’: 1.5, ‘phi’: 1}) 41 # Define the model 42 trace = pm.sample(1000, tune=1000) File ~\AppData\Roaming\Python\Python38\site-packages\pymc\distributions\distribution.py:944, in CustomDist.new(cls, name, dist, random, logp, logcdf, moment, ndim_supp, ndims_params, dtype, *dist_params, **kwargs) 929 def new( 930 cls, 931 name, (…) 941 **kwargs, 942 ): 943 if isinstance(kwargs.get(“observed”, None), dict): → 944 raise TypeError( 945 “Since v4.0.0 the observed parameter should be of type” 946 " pd.Series, np.array, or pm.Data." 947 " Previous versions allowed passing distribution parameters as" 948 " a dictionary in observed, in the current version these " 949 “parameters are positional arguments.” 950 ) 951 dist_params = cls.parse_dist_params(dist_params) 952 cls.check_valid_dist_random(dist, random, dist_params) TypeError: Since v4.0.0 the observed parameter should be of type pd.Series, np.array, or pm.Data. Previous versions allowed passing distribution parameters as a dictionary in observed, in the current version these parameters are positional arguments.