Skip to content

Commit

Permalink
added tweedie
Browse files Browse the repository at this point in the history
  • Loading branch information
svshivapuja committed Aug 30, 2021
1 parent df96f3d commit af7ccb2
Showing 1 changed file with 41 additions and 0 deletions.
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)))

0 comments on commit af7ccb2

Please sign in to comment.