CALCUALTORSRSDSDSDS
This commit is contained in:
parent
120692ccf7
commit
b920579da3
6 changed files with 94 additions and 7 deletions
15
python/calculators/binomial distribution.py
Normal file
15
python/calculators/binomial distribution.py
Normal file
|
@ -0,0 +1,15 @@
|
|||
import operator as op
|
||||
from functools import reduce
|
||||
|
||||
def nCr(n, r):
|
||||
r = min(r, n-r)
|
||||
num = reduce(op.mul, range(n, n - r, -1), 1)
|
||||
den = reduce(op.mul, range(1, r + 1), 1)
|
||||
return num / den
|
||||
|
||||
def binomial(x, n, p):
|
||||
q = 1 - p
|
||||
|
||||
return nCr(n, x) * (p**x) * (q**(n-x))
|
||||
|
||||
print(binomial(5, 100, 0.1))
|
19
python/calculators/pmcc.py
Normal file
19
python/calculators/pmcc.py
Normal file
|
@ -0,0 +1,19 @@
|
|||
from operator import mul
|
||||
import math
|
||||
|
||||
def pmcc(x, y):
|
||||
if len(x) != len(y):
|
||||
raise Exception('List x must be of the same length as list y. List x is {0} values long, whereas list y is {1} values long.'.format(len(x), len(y)))
|
||||
|
||||
n = len(x)
|
||||
xy = list(map(mul, x, y))
|
||||
xsq = [z**2 for z in x]
|
||||
ysq = [z**2 for z in y]
|
||||
|
||||
sxy = sum(xy) - ((sum(x) * sum(y)) / n)
|
||||
sxx = sum(xsq) - ((sum(x)**2) / n)
|
||||
syy = sum(ysq) - ((sum(y)**2) / n)
|
||||
|
||||
return sxy / math.sqrt(sxx * syy)
|
||||
|
||||
print(pmcc([1,2,3,4,5], [5,3,8,7,12]))
|
|
@ -3,18 +3,24 @@ import operator
|
|||
def diff(a):
|
||||
return list(map(operator.sub, a[1:], a[:-1]))
|
||||
|
||||
def formatNumber(x):
|
||||
if x % 1 == 0:
|
||||
return int(x)
|
||||
else:
|
||||
return x
|
||||
|
||||
sequence = [-0.5,1,4.5,10,17.5]
|
||||
row1 = diff(sequence)
|
||||
row2 = diff(row1)
|
||||
|
||||
a = row2[0] / 2
|
||||
b = row1[0] - (3 * a)
|
||||
c = sequence[0] - a - b
|
||||
a = formatNumber(row2[0] / 2)
|
||||
b = formatNumber(row1[0] - (3 * a))
|
||||
c = formatNumber(sequence[0] - a - b)
|
||||
|
||||
print('''
|
||||
a = {0}
|
||||
b = {1}
|
||||
c = {2}
|
||||
b = {2}
|
||||
c = {4}
|
||||
|
||||
Equation: {0}n^2 + {1}n + {2}
|
||||
'''.format(a, b, c))
|
||||
Equation: {0}n²{1}{2}n{3}{4}
|
||||
'''.format(a, '+' if b >= 0 else '', b, '+' if c >= 0 else '', c))
|
||||
|
|
33
python/calculators/srcc.py
Normal file
33
python/calculators/srcc.py
Normal file
|
@ -0,0 +1,33 @@
|
|||
import numpy
|
||||
|
||||
def rank(x):
|
||||
y = x.sort()
|
||||
print(y)
|
||||
res = []
|
||||
for i in x:
|
||||
res.append(y.index(x[i]) + 1)
|
||||
return res
|
||||
|
||||
def spearman(a, b):
|
||||
if len(a) != len(b):
|
||||
print('Both datasets must be the same length!')
|
||||
return undefined
|
||||
|
||||
n = len(a)
|
||||
|
||||
# rank set a
|
||||
aranks = sorted(range(n), reverse=True, key=a.__getitem__)
|
||||
print(aranks)
|
||||
|
||||
# rank set b
|
||||
branks = rank(b)
|
||||
|
||||
# work out the difference between ranks
|
||||
d = aranks - branks
|
||||
d = d**2
|
||||
d = numpy.sum(d)
|
||||
|
||||
# plug the values into the formula
|
||||
return 1 - ((6*d) / (n**3 - n))
|
||||
|
||||
print(spearman([1,2,3,5,4], [5,4,3,2,1]))
|
10
python/calculators/stdev.py
Normal file
10
python/calculators/stdev.py
Normal file
|
@ -0,0 +1,10 @@
|
|||
import math
|
||||
|
||||
def sd(x):
|
||||
n = len(x)
|
||||
mean = sum(x) / n
|
||||
squared = [y ** 2 for y in x]
|
||||
|
||||
return math.sqrt(abs((sum(squared) / n) - (mean**2)))
|
||||
|
||||
print(sd([135,230,132,323]))
|
|
@ -38,3 +38,7 @@ Here you can find a bunch of random work from my GCSE Computer Science class dum
|
|||
- [14 - Italy](python/pythonchallenge.com/14%20-%20Italy.py)
|
||||
- [Calculators](python/calculators)
|
||||
- [Quadratic nth Term](python/calculators/quadratic%20nth%20term.py)
|
||||
- [Binomial Distribution](python/calculators/binomial%20distribution.py)
|
||||
- [Pearson's Product-Moment Correlation Coefficient](python/calculators/pmcc.py)
|
||||
- [Spearman's Rank Correlation Coefficient](python/calculators/srcc.py)
|
||||
- [Standard Deviation](python/calculators/stdev.py)
|
||||
|
|
Loading…
Reference in a new issue