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

🐍 Use the stdlib for our score statistics #20

Merged
merged 1 commit into from
May 29, 2024
Merged
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
21 changes: 9 additions & 12 deletions grants/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

from statistics import mean, variance, stdev

from django.db import models
from urlman import Urls

Expand Down Expand Up @@ -141,24 +143,19 @@ def average_score(self):
scores = [s.score for s in self.scores.all() if s.score]
if not scores:
return None
else:
return sum(scores) / float(len(scores))
return mean(scores)

def variance(self):
data = [s.score for s in self.scores.all() if s.score]
n = len(data)
if n == 0:
return 0
c = sum(data) / float(len(data))
if n < 2:
if not data:
return 0
ss = sum((x - c) ** 2 for x in data)
ss -= sum((x - c) for x in data) ** 2 / len(data)
assert not ss < 0, "negative sum of square deviations: %f" % ss
return ss / (n - 1)
return variance(data)

def stdev(self):
return self.variance() ** 0.5
data = [s.score for s in self.scores.all() if s.score]
if not data:
return 0
return stdev(data)


class Allocation(models.Model):
Expand Down
Loading