Skip to content

Commit

Permalink
Merge pull request #16 from e10v/dev
Browse files Browse the repository at this point in the history
Add ratio variance and covariance
  • Loading branch information
e10v authored Jan 27, 2024
2 parents 9d0fb0d + 18ae8cf commit 45219c1
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ testpaths = ["tests"]
[tool.coverage.run]
source = ["src/tea_tasting"]
[tool.coverage.report]
exclude_lines = ["if TYPE_CHECKING:", "pragma: no cover", " ..."]
exclude_lines = ["if TYPE_CHECKING:", "pragma: no cover", "@overload"]


[tool.pyright]
Expand Down
57 changes: 57 additions & 0 deletions src/tea_tasting/aggr.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,63 @@ def cov(self: Aggregates, left: str | None, right: str | None) -> float | int:
return 0
return self._cov[tea_tasting._utils.sorted_tuple(left, right)]

def ratio_var(
self: Aggregates,
numer: str | None,
denom: str | None,
) -> float | int:
"""Sample variance of the ratio of two variables using the Delta method.
References:
[Delta method](https://en.wikipedia.org/wiki/Delta_method).
[Taylor expansions for the moments of functions of random variables](https://en.wikipedia.org/wiki/Taylor_expansions_for_the_moments_of_functions_of_random_variables).
Args:
numer: Numerator variable name.
denom: Denominator variable name.
Returns:
Sample variance of the ratio of two variables.
"""
denom_mean_sq = self.mean(denom) * self.mean(denom)
return (
self.var(numer)
- 2 * self.cov(numer, denom) * self.mean(numer) / self.mean(denom)
+ self.var(denom) * self.mean(numer) * self.mean(numer) / denom_mean_sq
) / denom_mean_sq

def ratio_cov(
self: Aggregates,
left_numer: str | None,
left_denom: str | None,
right_numer: str | None,
right_denom: str | None,
) -> float | int:
"""Sample covariance of the ratios of variables using the Delta method.
References:
[Delta method](https://en.wikipedia.org/wiki/Delta_method).
[Taylor expansions for the moments of functions of random variables](https://en.wikipedia.org/wiki/Taylor_expansions_for_the_moments_of_functions_of_random_variables).
Args:
left_numer: First numerator variable name.
left_denom: First denominator variable name.
right_numer: Second numerator variable name.
right_denom: Second denominator variable name.
Returns:
Sample covariance of the ratios of variables.
"""
left_ratio_of_means = self.mean(left_numer) / self.mean(left_denom)
right_ratio_of_means = self.mean(right_numer) / self.mean(right_denom)
return (
self.cov(left_numer, right_numer)
- self.cov(left_numer, right_denom) * right_ratio_of_means
- self.cov(left_denom, right_numer) * left_ratio_of_means
+ self.cov(left_denom, right_denom)
* left_ratio_of_means * right_ratio_of_means
) / self.mean(left_denom) / self.mean(right_denom)

def filter(
self: Aggregates,
has_count: bool,
Expand Down
18 changes: 15 additions & 3 deletions tests/test_aggr.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@


COUNT = 100
MEAN = {"x": 1.0, "y": 2}
VAR = {"x": 3.0, "y": 4}
COV = {("x", "y"): 5.0}
MEAN = {"x": 5.0, "y": 4}
VAR = {"x": 3.0, "y": 2}
COV = {("x", "y"): 1.0}

@pytest.fixture
def aggr() -> tea_tasting.aggr.Aggregates:
Expand Down Expand Up @@ -67,6 +67,18 @@ def test_aggregates_none(aggr: tea_tasting.aggr.Aggregates):
assert aggr.cov(None, "y") == 0
assert aggr.cov("x", None) == 0

def test_aggregates_ratio_var(aggr: tea_tasting.aggr.Aggregates):
assert aggr.ratio_var("x", "y") == pytest.approx(0.2265625)

def test_aggregates_ratio_cov():
aggr = tea_tasting.aggr.Aggregates(
count=None,
mean={"a": 8, "b": 7, "c": 6, "d": 5},
var={},
cov={("a", "c"): 4, ("a", "d"): 3, ("b", "c"): 2, ("b", "d"): 1},
)
assert aggr.ratio_cov("a", "b", "c", "d") == pytest.approx(-0.0146938775510204)

def test_aggregates_filter(aggr: tea_tasting.aggr.Aggregates):
filtered_aggr = aggr.filter(
has_count=False,
Expand Down

0 comments on commit 45219c1

Please sign in to comment.