Skip to content

Commit

Permalink
abstract the pdf and cdf caclulation for the normal dist
Browse files Browse the repository at this point in the history
  • Loading branch information
MAfarrag committed Dec 1, 2023
1 parent 9b84585 commit 94aa5ab
Showing 1 changed file with 30 additions and 16 deletions.
46 changes: 30 additions & 16 deletions statista/distributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2090,6 +2090,18 @@ def __init__(
self.chistatic = None
self.chi_Pvalue = None

@staticmethod
def _pdf_eq(
data: Union[list, np.ndarray], parameters: Dict[str, Union[float, Any]]
) -> np.ndarray:
loc = parameters.get("loc")
scale = parameters.get("scale")
if scale <= 0:
raise ValueError("Scale parameter is negative")

Check warning on line 2100 in statista/distributions.py

View check run for this annotation

Codecov / codecov/patch

statista/distributions.py#L2100

Added line #L2100 was not covered by tests
pdf = norm.pdf(data, loc=loc, scale=scale)

return pdf

def pdf(
self,
parameters: Dict[str, Union[float, Any]],
Expand Down Expand Up @@ -2118,15 +2130,10 @@ def pdf(
pdf : [array]
probability density function pdf.
"""
loc = parameters.get("loc")
scale = parameters.get("scale")

if scale <= 0:
raise ValueError("Scale parameter is negative")

ts = super().pdf(parameters, actual_data=actual_data)

pdf = norm.pdf(ts, loc=loc, scale=scale)
pdf = self._pdf_eq(ts, parameters)

if plot_figure:
Qx = np.linspace(
float(self.data_sorted[0]), 1.5 * float(self.data_sorted[-1]), 10000
Expand All @@ -2146,6 +2153,21 @@ def pdf(
else:
return pdf

@staticmethod
def _cdf_eq(
data: Union[list, np.ndarray], parameters: Dict[str, Union[float, Any]]
) -> np.ndarray:
loc = parameters.get("loc")
scale = parameters.get("scale")

if scale <= 0:
raise ValueError("Scale parameter is negative")

Check warning on line 2164 in statista/distributions.py

View check run for this annotation

Codecov / codecov/patch

statista/distributions.py#L2164

Added line #L2164 was not covered by tests
if loc <= 0:
raise ValueError("Threshold parameter should be greater than zero")

Check warning on line 2166 in statista/distributions.py

View check run for this annotation

Codecov / codecov/patch

statista/distributions.py#L2166

Added line #L2166 was not covered by tests

cdf = norm.cdf(data, loc=loc, scale=scale)
return cdf

def cdf(
self,
parameters: Dict[str, Union[float, Any]],
Expand All @@ -2169,20 +2191,12 @@ def cdf(
- scale: [numeric]
scale parameter of the Normal distribution.
"""
loc = parameters.get("loc")
scale = parameters.get("scale")

if scale <= 0:
raise ValueError("Scale parameter is negative")
if loc <= 0:
raise ValueError("Threshold parameter should be greater than zero")

if isinstance(actual_data, bool):
ts = self.data
else:
ts = actual_data

cdf = norm.cdf(ts, loc=loc, scale=scale)
cdf = self._cdf_eq(ts, parameters)

if plot_figure:
Qx = np.linspace(
Expand Down

0 comments on commit 94aa5ab

Please sign in to comment.