CoCalc Public Filesassignments / assignment14 / my_statistics.py
Author: guy miller
1def mean(ls):
2    avg=0
3    sum0=0
4    counter=0
5    for i in ls:
6        sum0+=i
7        counter+=1
8    avg=sum0/counter
9    return avg
10
11def median(ls):
12    ls.sort()
13    if (len(ls)%2==0):
14        Median=(ls[int(len(ls)/2)-1]+ls[int((len(ls)/2))])/2
15    else:
16        Median=ls[int((len(ls))/2)]
17    return Median
18
19def geometric_mean(ls):
20    multi=1
21    counter=0
22    for i in ls:
23        if(i<0):
24            return "the list contains negative values"
25        i=abs(i)
26        multi*=i
27        counter+=1
28    Geometric_mean=(multi)**(1/counter)
29    return Geometric_mean
30
31def percentile(ls,ls1): #האחוזונים יחולקו לפי 25,50,75,100
32    N=0
33    n=0
34    ls.sort()
35    for i in ls:
36        N+=1
37        if(i!=ls1):
38            n+=1
39        else:
40            c=n
41    if(n==N):
42        return "the value entered is not in the list"
43    if(0.25*N-int(0.25*N)>0):
44        n25th=int(0.25*N+1)
45    else:
46        n25th=0.25*N
47    if(0.5*N-int(0.5*N)>0):
48        n50th=int(0.5*N+1)
49    else:
50        n50th=int(0.5*N)
51    if(0.75*N-int(0.75*N)>0):
52        n75th=int(0.75*N+1)
53    else:
54        n75th=0.75*N
55    n100th=N
56    if(c<=n25th):
57        return "in the 25th percentile"
58    else:
59        if(c<=n50th):
60            return "in the 50th percentile"
61        else:
62            if(c<=n75th):
63                return "in the 75th percentile"
64            else:
65                return "in the 100th percentile"
66
67def data_range(ls):
68    ls.sort()
69    R=max(ls)-min(ls)
70    return R
71
72def variance(ls):
73    m=len(ls)
74    sum1=0
75    for i in ls:
76        sum1+=(i-mean(ls))**2
77    var=sum1/m
78    std=(var)**(0.5)
79    return var
80
81def std(ls):
82    m=len(ls)
83    sum1=0
84    for i in ls:
85        sum1+=(i-mean(ls))**2
86    var=sum1/m
87    std=(var)**(0.5)
88    return std
89
90def Frequency_Tables(ls):
91    count = 0
92    FreqTable = {}
93    for x in ls:
94        for i in ls:
95            if i == x:
96                count+=1
97        if x!=ls[0]:
98            count=count-1
99        dic = {x:count}
100        FreqTable.update(dic)
101        count=1
102    return FreqTable