From 7b9d4ea1712fceaaa93ec6af200a0505671a2578 Mon Sep 17 00:00:00 2001 From: newt Date: Wed, 23 Jun 2021 20:02:32 +0100 Subject: [PATCH] CALCUALTORSRSDSDSDS --- python/calculators/binomial distribution.py | 15 ++++++++++ python/calculators/pmcc.py | 19 ++++++++++++ python/calculators/quadratic nth term.py | 20 ++++++++----- python/calculators/srcc.py | 33 +++++++++++++++++++++ python/calculators/stdev.py | 10 +++++++ readme.md | 4 +++ 6 files changed, 94 insertions(+), 7 deletions(-) create mode 100644 python/calculators/binomial distribution.py create mode 100644 python/calculators/pmcc.py create mode 100644 python/calculators/srcc.py create mode 100644 python/calculators/stdev.py diff --git a/python/calculators/binomial distribution.py b/python/calculators/binomial distribution.py new file mode 100644 index 0000000..19b91bf --- /dev/null +++ b/python/calculators/binomial distribution.py @@ -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)) \ No newline at end of file diff --git a/python/calculators/pmcc.py b/python/calculators/pmcc.py new file mode 100644 index 0000000..b42de72 --- /dev/null +++ b/python/calculators/pmcc.py @@ -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])) \ No newline at end of file diff --git a/python/calculators/quadratic nth term.py b/python/calculators/quadratic nth term.py index 5cf5fda..0ec75ac 100644 --- a/python/calculators/quadratic nth term.py +++ b/python/calculators/quadratic nth term.py @@ -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)) diff --git a/python/calculators/srcc.py b/python/calculators/srcc.py new file mode 100644 index 0000000..4d440e7 --- /dev/null +++ b/python/calculators/srcc.py @@ -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])) \ No newline at end of file diff --git a/python/calculators/stdev.py b/python/calculators/stdev.py new file mode 100644 index 0000000..86b8fb5 --- /dev/null +++ b/python/calculators/stdev.py @@ -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])) \ No newline at end of file diff --git a/readme.md b/readme.md index 50e18bc..82ab70a 100644 --- a/readme.md +++ b/readme.md @@ -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)