def covariance(*stack): items = checkParams(stack, 'plural') X = average(items['Xi']) Y = average(items['Yi']) return items['XiYiFi']/items['Fi'] - X*Y
def variance(*items): items = checkParams(items, 'single') count = 0 count2 = 0 for item in items.keys(): count += item**2 * items[item] count2 += items[item] return count / count2 - average(items)**2
def average(*items): items = checkParams(items, 'single') X = 0 count = 0 for item in items.keys(): X += item * items[item] count += items[item] return X / count
def varianceB(*items): items = checkParams(items, 'single') count = 0 count2 = 0 mean = average(items) for item in items.keys(): count += ((item - mean)**2)*items[item] count2 += items[item] return count / count2
def regressionLine(value, *stack): items = checkParams(stack, 'plural') value = {k.lower(): v for k,v in value.items()} if 'x' in value: diff = covariance(items)/varianceB(items['Xi']) return diff*value['x']-diff*average(items['Xi'])+average(items['Yi']) if 'y' in value: diff = covariance(items)/varianceB(items['Yi']) return diff*value['y']-diff*average(items['Yi'])+average(items['Xi'])
def correlationCoefficient(*stack): items = checkParams(stack, 'plural') return covariance(items)/(sqrt(varianceB(items['Xi']))*sqrt(varianceB(items['Yi'])))
def percentile(P, *items): items = checkParams(items, 'single') items = items.copy() items, Fi = givePositions(items) return getPosition(items, P*Fi/100)
def quartile(Q, *items): items = checkParams(items, 'single') items = items.copy() items, Fi = givePositions(items) return getPosition(items, Q*Fi/4)
def median(*items): items = checkParams(items, 'single') items = items.copy() items, Fi = givePositions(items) return getPosition(items, Fi/2)