-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: statistical functions (#1420)
* refactor: statistical bound functions * test: add and update test for statistical bound functions * refactor: exponential distribution functions * test: add and update test for exponential distribution functions * refactor: abstract s-distribution functions * enhancement: add check for empty data array in do_fit() * test: update and add tests for refactored functions * fix: fix mypy error * fix: fix pylint errors
- Loading branch information
1 parent
c609bcf
commit 1862211
Showing
14 changed files
with
727 additions
and
275 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -431,6 +431,7 @@ ignore = [ | |
'D204', | ||
'D213', | ||
] | ||
match='.*(?<!_test)\.py' | ||
|
||
[tool.ruff] | ||
exclude = ["*.pyi"] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
# type: ignore | ||
# -*- coding: utf-8 -*- | ||
# | ||
# ramstk.analyses.statistics.distributions.py is part of the RAMSTK Project | ||
# | ||
# All rights reserved. | ||
# Copyright since 2007 Doyle "weibullguy" Rowland doyle.rowland <AT> reliaqual <DOT> com | ||
"""Exponential Module.""" | ||
|
||
# Standard Library Imports | ||
from typing import Optional | ||
|
||
# Third Party Imports | ||
from scipy.stats import expon, lognorm, norm, weibull_min | ||
|
||
|
||
def calculate_hazard_rate( | ||
time: float, | ||
location: float = 0.0, | ||
scale: Optional[float] = None, | ||
shape: Optional[float] = None, | ||
dist_type: str = "exponential", | ||
) -> float: | ||
"""Calculate the hazard rate for a given distribution.""" | ||
if time <= 0.0: | ||
return 0.0 | ||
|
||
# Exponential distribution doesn't use the `shape` parameter. | ||
if dist_type == "exponential": | ||
return 1.0 / expon.mean(loc=location, scale=scale) | ||
|
||
# Lognormal distribution uses `shape`, `location`, and `scale`. | ||
elif dist_type == "lognormal": | ||
return lognorm.pdf(time, shape, loc=location, scale=scale) / lognorm.cdf( | ||
time, shape, loc=location, scale=scale | ||
) | ||
|
||
# Normal distribution uses 'scale' and 'location'. | ||
elif dist_type == "normal": | ||
return norm.pdf(time, loc=location, scale=scale) / norm.sf( | ||
time, loc=location, scale=scale | ||
) | ||
|
||
# Weibull distribution uses `shape`, `location`, and `scale`. | ||
elif dist_type == "weibull": | ||
return ( | ||
0.0 | ||
if time <= 0.0 | ||
else weibull_min.pdf(time, shape, loc=location, scale=scale) | ||
/ weibull_min.cdf(time, shape, loc=location, scale=scale) | ||
) | ||
|
||
else: | ||
raise ValueError(f"Unsupported distribution: {dist_type}") | ||
|
||
|
||
def calculate_mtbf( | ||
shape: float = None, | ||
location: float = 0.0, | ||
scale: float = 1.0, | ||
dist_type: str = "exponential", | ||
) -> float: | ||
"""Calculate the MTBF for a given distribution. | ||
:param shape: the shape parameter. | ||
:param location: the location parameter. | ||
:param scale: the scale (MTBF) parameter. | ||
:param dist_type: the type of distribution. | ||
:return: the MTBF value. | ||
:rtype: float | ||
""" | ||
if dist_type == "exponential": | ||
return expon.mean(loc=location, scale=scale) | ||
elif dist_type == "lognormal": | ||
return lognorm.mean(shape, loc=location, scale=scale) | ||
elif dist_type == "normal": | ||
return norm.mean(loc=location, scale=scale) | ||
elif dist_type == "weibull": | ||
return weibull_min.mean(shape, loc=location, scale=scale) | ||
else: | ||
raise ValueError(f"Unsupported distribution type: {dist_type}") | ||
|
||
|
||
def calculate_survival( | ||
shape: float = None, | ||
time: float = 0.0, | ||
location: float = 0.0, | ||
scale: float = 1.0, | ||
dist_type: str = "exponential", | ||
) -> float: | ||
"""Calculate the survival function at time T for a given distribution. | ||
:param shape: the shape parameter. | ||
:param time: the time at which to calculate the survival function. | ||
:param location: the location parameter. | ||
:param scale: the scale parameter. | ||
:param dist_type: the type of distribution. | ||
:return: the survival function value at time T. | ||
:rtype: float | ||
""" | ||
if dist_type == "exponential": | ||
return expon.sf(time, loc=location, scale=scale) | ||
elif dist_type == "lognormal": | ||
return lognorm.sf(time, shape, loc=location, scale=scale) | ||
elif dist_type == "normal": | ||
return norm.sf(time, location, scale) | ||
elif dist_type == "weibull": | ||
return weibull_min.sf(time, shape, loc=location, scale=scale) | ||
else: | ||
raise ValueError(f"Unsupported distribution type: {dist_type}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.