You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

statistics.py 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. from django.db.models import FloatField, IntegerField
  2. from django.db.models.aggregates import Aggregate
  3. __all__ = [
  4. 'CovarPop', 'Corr', 'RegrAvgX', 'RegrAvgY', 'RegrCount', 'RegrIntercept',
  5. 'RegrR2', 'RegrSlope', 'RegrSXX', 'RegrSXY', 'RegrSYY', 'StatAggregate',
  6. ]
  7. class StatAggregate(Aggregate):
  8. output_field = FloatField()
  9. def __init__(self, y, x, output_field=None, filter=None):
  10. if not x or not y:
  11. raise ValueError('Both y and x must be provided.')
  12. super().__init__(y, x, output_field=output_field, filter=filter)
  13. def resolve_expression(self, query=None, allow_joins=True, reuse=None, summarize=False, for_save=False):
  14. return super().resolve_expression(query, allow_joins, reuse, summarize)
  15. class Corr(StatAggregate):
  16. function = 'CORR'
  17. class CovarPop(StatAggregate):
  18. def __init__(self, y, x, sample=False, filter=None):
  19. self.function = 'COVAR_SAMP' if sample else 'COVAR_POP'
  20. super().__init__(y, x, filter=filter)
  21. class RegrAvgX(StatAggregate):
  22. function = 'REGR_AVGX'
  23. class RegrAvgY(StatAggregate):
  24. function = 'REGR_AVGY'
  25. class RegrCount(StatAggregate):
  26. function = 'REGR_COUNT'
  27. output_field = IntegerField()
  28. def convert_value(self, value, expression, connection):
  29. return 0 if value is None else value
  30. class RegrIntercept(StatAggregate):
  31. function = 'REGR_INTERCEPT'
  32. class RegrR2(StatAggregate):
  33. function = 'REGR_R2'
  34. class RegrSlope(StatAggregate):
  35. function = 'REGR_SLOPE'
  36. class RegrSXX(StatAggregate):
  37. function = 'REGR_SXX'
  38. class RegrSXY(StatAggregate):
  39. function = 'REGR_SXY'
  40. class RegrSYY(StatAggregate):
  41. function = 'REGR_SYY'