Skip to content

Commit

Permalink
t mean var
Browse files Browse the repository at this point in the history
  • Loading branch information
abstractqqq committed Feb 19, 2024
1 parent c9703ad commit 48c59bb
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
6 changes: 3 additions & 3 deletions python/polars_ds/num.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,17 @@ def rms(self) -> pl.Expr:
"""
return (self._expr.dot(self._expr) / self._expr.count()).sqrt()

def harmonic_mean(self) -> pl.Expr:
def hmean(self) -> pl.Expr:
"""
Returns the harmonic mean of the expression
"""
return self._expr.count() / (1.0 / self._expr).sum()

def geometric_mean(self) -> pl.Expr:
def gmean(self) -> pl.Expr:
"""
Returns the geometric mean of the expression
"""
return self._expr.product().pow(1.0 / self._expr.count())
return self._expr.ln().mean().exp()

def cv(self, ddof: int = 1) -> pl.Expr:
"""
Expand Down
46 changes: 44 additions & 2 deletions python/polars_ds/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,32 @@ def rand_str(
is_elementwise=True,
)

def t_mean(self, lower: float, upper: float) -> pl.Expr:
"""
Computes the trimmed mean of the variable.
Parameters
----------
lower
The lower end, smaller values will be trimmed
upper
The upper end, larger values will be trimmed
"""
return self._expr.filter(self._expr.is_between(lower, upper)).mean()

def t_var(self, lower: float, upper: float, ddof: int = 1) -> pl.Expr:
"""
Computes the trimmed var of the variable.
Parameters
----------
lower
The lower end, smaller values will be trimmed
upper
The upper end, larger values will be trimmed
"""
return self._expr.filter(self._expr.is_between(lower, upper)).var(ddof)

def w_mean(self, weights: pl.Expr, is_normalized: bool = False) -> pl.Expr:
"""
Computes the weighted mean of self, where weights is an expr represeting
Expand All @@ -564,7 +590,7 @@ def w_mean(self, weights: pl.Expr, is_normalized: bool = False) -> pl.Expr:
Parameters
----------
weights
An expr representing weights
An expr representing weights. Must be of same length as self.
is_normalized
If true, the weights are assumed to sum to 1. If false, will divide by sum of the weights
"""
Expand All @@ -583,7 +609,7 @@ def w_var(self, weights: pl.Expr, is_normalized: bool = False) -> pl.Expr:
Parameters
----------
weights
An expr representing weights
An expr representing weights. Must be of same length as self.
is_normalized
If true, the weights are assumed to sum to 1. If false, will divide by sum of the weights
"""
Expand All @@ -595,3 +621,19 @@ def w_var(self, weights: pl.Expr, is_normalized: bool = False) -> pl.Expr:
denom = weights.sum() * (self._expr.count() - 1) / self._expr.count()

return out / denom

def w_gmean(self, weights: pl.Expr, is_normalized: bool = False) -> pl.Expr:
"""
Computes the weighted geometric mean.
Parameters
----------
weights
An expr representing weights. Must be of same length as self.
is_normalized
If true, the weights are assumed to sum to 1. If false, will divide by sum of the weights
"""
if is_normalized:
return (self._expr.ln().dot(weights)).exp()
else:
return (self._expr.ln().dot(weights) / (weights.sum())).exp()

0 comments on commit 48c59bb

Please sign in to comment.