Skip to content

Commit

Permalink
[BUG] 5918 and 5919, fix missing loc/scale in TDistribution methods (
Browse files Browse the repository at this point in the history
…#5942)

#### Reference Issues/PRs
Fixes #5918 and fixes #5919 

#### What does this implement/fix? Explain your changes.
Uses scale/location to compute `var`, `pdf`, `log_pdf`, `cdf` and `ppf`
from TDistribution
  • Loading branch information
ivarzap committed Feb 20, 2024
1 parent 20bc5a0 commit e535a1a
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions sktime/proba/t.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,18 @@ def var(self):
df_arr = df_arr.astype(np.float32)
df_arr[df_arr <= 2] = np.inf
mask = (df_arr > 2) & (df_arr != np.inf)
df_arr[mask] = df_arr[mask] / (df_arr[mask] - 2)
df_arr[mask] = self._sigma[mask] ** 2 * df_arr[mask] / (df_arr[mask] - 2)
return pd.DataFrame(df_arr, index=self.index, columns=self.columns)

def pdf(self, x):
"""Probability density function."""
d = self.loc[x.index, x.columns]
pdf_arr = gamma((d._df + 1) / 2)
pdf_arr = pdf_arr / (np.sqrt(np.pi * d._df) * gamma(d._df / 2))
pdf_arr = pdf_arr * (1 + x**2 / d._df) ** (-(d._df + 1) / 2)
pdf_arr = pdf_arr * (1 + ((x - d._mu) / d._sigma) ** 2 / d._df) ** (
-(d._df + 1) / 2
)
pdf_arr = pdf_arr / d._sigma
return pd.DataFrame(pdf_arr, index=x.index, columns=x.columns)

def log_pdf(self, x):
Expand All @@ -117,14 +120,18 @@ def log_pdf(self, x):
lpdf_arr = loggamma((d._df + 1) / 2)
lpdf_arr = lpdf_arr - 0.5 * np.log(d._df * np.pi)
lpdf_arr = lpdf_arr - loggamma(d._df / 2)
lpdf_arr = lpdf_arr - ((d._df + 1) / 2) * np.log(1 + x**2 / d._df)
lpdf_arr = lpdf_arr - ((d._df + 1) / 2) * np.log(
1 + ((x - d._mu) / d._sigma) ** 2 / d._df
)
lpdf_arr = lpdf_arr - np.log(d._sigma)
return pd.DataFrame(lpdf_arr, index=x.index, columns=x.columns)

def cdf(self, x):
"""Cumulative distribution function."""
d = self.loc[x.index, x.columns]
cdf_arr = x * gamma((d._df + 1) / 2)
cdf_arr = cdf_arr * hyp2f1(0.5, (d._df + 1) / 2, 3 / 2, -(x**2) / d._df)
x_ = (x - d._mu) / d._sigma
cdf_arr = x_ * gamma((d._df + 1) / 2)
cdf_arr = cdf_arr * hyp2f1(0.5, (d._df + 1) / 2, 3 / 2, -(x_**2) / d._df)
cdf_arr = 0.5 + cdf_arr / (np.sqrt(np.pi * d._df) * gamma(d._df / 2))
return pd.DataFrame(cdf_arr, index=x.index, columns=x.columns)

Expand All @@ -145,6 +152,7 @@ def ppf(self, p):
ppf_arr[mask1 | mask2] = np.sqrt(ppf_arr[mask1 | mask2] - 1)
ppf_arr[mask1 | mask2] = np.sqrt(d._df[mask1 | mask2]) * ppf_arr[mask1 | mask2]
ppf_arr[mask1] = -ppf_arr[mask1]
ppf_arr = d._sigma * ppf_arr + d._mu
return pd.DataFrame(ppf_arr, index=p.index, columns=p.columns)

@classmethod
Expand Down

0 comments on commit e535a1a

Please sign in to comment.