Skip to content

Commit

Permalink
Merge pull request #2 from Aoishi28/AddingNum
Browse files Browse the repository at this point in the history
Adding NUM class
  • Loading branch information
ashishjoshi2605 authored Jan 12, 2023
2 parents d46b239 + e7424e0 commit cfc8020
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/NUM.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import math

class NUM:
'''
Summarizes a stream of numbers
'''

def __init__(self):
self.n=0
self.mu=0
self.m2=0
self.lo=math.inf
self.hi=-math.inf


def add(self,n):
'''
Add 'n', update lo,hi and stuff needed for standard deviation
'''
if(n!='?'):
self.n=self.n+1
d=n-self.mu
self.mu=self.mu+d/self.n
self.m2=self.m2+d*(n-self.mu)
self.lo=math.min(n,self.lo)
self.hi=math.max(n,self.hi)


def mid(self):
'''
Return mean
'''
return self.mu

def div(self):
'''
Return standard deviation using Welford's algorithm http://t.ly/nn_W
'''
if(self.m2<0 or self.n<2):
return 0
else:
return math.pow((self.m2/(self.n-1)),0.5)

0 comments on commit cfc8020

Please sign in to comment.