Skip to content
Open
Changes from all commits
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
41 changes: 41 additions & 0 deletions tensorflow_probability/python/distributions/tweedieloss.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import tensorflow.compat.v2 as tf
from tensorflow_probability.python.distributions import distribution



_tweedie_sample_note = """definition is [here](https://en.wikipedia.org/wiki/Tweedie_distribution),
Reference Document [here](https://arxiv.org/pdf/1912.12356.pdf)"""


class Tweedie(distribution.AutoCompositeTensorDistribution):

def __init__(self, p=1.5,
name='Tweedie'):
"""Input Paramters for the Tweedie Loss are the p value, lies bettern (1,2) for Poisson-gamma EDM
|---------------|---------------------|
| Value of p | Distribution |
|---------------|---------------------|
| 0 | Normal |
| 1 | Poisson |
| (1,2) | Poisson-Gamma |
| 2 | Gamma |
|---------------|---------------------|

"""
self.p =p
self.name = name

def log_likelihood(self,y,y_pred):
"""The Log likelihood"""
self.loglikelihood = - y * (tf.pow(y_pred, 1-self.p)/(1-self.p)) + (tf.pow(y_pred,2-self.p)/(2-self.p))
return tf.reduce_mean(self.loglikelihood)

def deviance(self,y,y_pred):
if self.p ==0:
return tf.pow(y-y_pred,2)
elif self.p ==1:
return 2*(y*log(y_pred/y) + y_pred - y)
elif self.p ==2:
return 2*(log(y_pred/y)+ y/y_pred -1)
else:
return 2*(((max(y,0)**(2-self.p))/((1-self.p)*(2-self.p))) - (y*(tf.pow(y_pred,(1-self.p)))/(1-self.p)) + (tf.pow(y_pred,2-self.p)/(2-self.p)))