Skip to content

Commit

Permalink
Revert "🐍 Use the stdlib for our score statistics (#20)"
Browse files Browse the repository at this point in the history
This reverts commit e67d666.
  • Loading branch information
drewbrew authored May 29, 2024
1 parent e67d666 commit 493fc88
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions grants/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from __future__ import annotations

from statistics import mean, variance, stdev

from django.db import models
from urlman import Urls

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

def variance(self):
data = [s.score for s in self.scores.all() if s.score]
if not data:
n = len(data)
if n == 0:
return 0
c = sum(data) / float(len(data))
if n < 2:
return 0
return variance(data)
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)

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


class Allocation(models.Model):
Expand Down

0 comments on commit 493fc88

Please sign in to comment.