Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ev geo mean #2640

Draft
wants to merge 4 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/gluonts/ev/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
MeanWeightedSumQuantileLoss,
OWA,
)
from .aggregations import Aggregation, Sum, Mean
from .aggregations import Aggregation, Sum, Mean, GeometricMean
from .evaluator import Evaluator, DirectEvaluator, DerivedEvaluator

__all__ = [
Expand Down Expand Up @@ -81,6 +81,7 @@
"Aggregation",
"Sum",
"Mean",
"GeometricMean",
"Metric",
"Evaluator",
"DirectEvaluator",
Expand Down
19 changes: 18 additions & 1 deletion src/gluonts/ev/aggregations.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# express or implied. See the License for the specific language governing
# permissions and limitations under the License.

from dataclasses import dataclass
from dataclasses import dataclass, field
from typing import List, Optional, Union

import numpy as np
Expand Down Expand Up @@ -104,3 +104,20 @@ def get(self) -> np.ndarray:
return self.partial_result / self.n

return np.ma.concatenate(self.partial_result)


@dataclass
class GeometricMean(Aggregation):
"""
Map-reduce way of calculating the geometric mean of a stream of values.
"""

partial_result: Optional[Union[List[np.ndarray], np.ndarray]] = None
n: Optional[Union[int, np.ndarray]] = None
mean: Mean = field(default_factory=Mean, init=False)

def step(self, values: np.ndarray) -> None:
self.mean.step(np.log(values))

def get(self):
return np.exp(self.mean.get())