def standard_deviation(cls, month): stats = cls.objects.filter(month_id=month) total = stats.values_list("rating", flat=True) N = stats.count() mean = cls.mean(month) sum_diff = sum([(mean-float(each))**2 for each in total]) sd = cmath.sqrt(sum_diff / N-1) return sd.imag * 100
def calcrating(cls, comp, month): stats = CompanyStats.objects.filter( company_id=comp, month_id=month).select_related("param") rating = float(sum([ each.param.associated_value * each.value for each in stats]) / sum( stats.values_list("param__associated_value", flat=True))) return cls.objects.get_or_create(month_id=month, company_id=comp, defaults={ "month_id": month, "company_id": comp, "rating": rating})
def mean(cls, month): stats = cls.objects.filter(month_id=month) total = sum(stats.values_list("rating", flat=True)) N = stats.count() return total / N