Skip to content

Commit 9dc7764

Browse files
committed
Codes 4 W06
1 parent 89e54b9 commit 9dc7764

File tree

6 files changed

+156
-0
lines changed

6 files changed

+156
-0
lines changed

Week06/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Bora Canbula
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Week06/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# boracanbula
2+
This is a test package
3+
for the PyPi tutorial.

Week06/pyproject.toml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
[build-system]
2+
requires = ["setuptools", "wheel"]
3+
build-backend = "setuptools.build_meta"
4+
5+
[project]
6+
name = "boracanbula"
7+
version = "0.0.1"
8+
authors = [
9+
{ name = "Bora Canbula", email = "bora.canbula@cbu.edu.tr" },
10+
]
11+
description = "A simple package for calculating statistics."
12+
readme = "README.md"
13+
keywords = ["canbula", "statistics"]
14+
requires-python = ">=3.6.0"
15+
classifiers = [
16+
"Development Status :: 3 - Alpha",
17+
"Intended Audience :: Developers",
18+
"License :: OSI Approved :: MIT License",
19+
"Programming Language :: Python :: 3",
20+
"Operating System :: OS Independent",
21+
]
22+
23+
[project.urls]
24+
Homepage = "https://github.com/canbula/boracanbula"

Week06/src/boracanbula/__init__.py

Whitespace-only changes.
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import random
2+
3+
4+
def simple_random_sampling(data, n, replacement=False):
5+
"""Simple Random Sampling with or without replacement."""
6+
if replacement:
7+
return [random.choice(data) for _ in range(n)]
8+
return random.sample(data, min(n, len(data)))
9+
10+
11+
def stratified_sampling(data, n, strata):
12+
"""Strafied Sampling with proportional representation of strata."""
13+
sample = []
14+
for key in strata:
15+
sample += random.sample(data[key], min(n // len(strata), len(data[key])))
16+
return sample
17+
18+
19+
def cluster_sampling(data, n, clusters):
20+
"""Cluster Sampling with random selection of clusters."""
21+
picked_clusters = random.sample(clusters, min(n, len(clusters)))
22+
sample = []
23+
for cluster in picked_clusters:
24+
sample += data[cluster]
25+
sample = list(set(sample))
26+
return sample
27+
28+
29+
def systematic_sampling(data, n):
30+
"""Systematic Sampling."""
31+
sample = []
32+
for i in range(0, len(data), n):
33+
sample.append(data[i])
34+
return sample
35+
36+
37+
def mean(x: list) -> float:
38+
"""Calculate the mean of a list of numbers."""
39+
return sum(x) / len(x)
40+
41+
42+
def median(data):
43+
"""Find the median of a list of numbers."""
44+
data = sorted(data)
45+
n = len(data)
46+
if n % 2 == 1:
47+
return data[int((n + 1) / 2) - 1]
48+
return (data[(n // 2) - 1] + data[((n // 2) + 1) - 1]) / 2
49+
50+
51+
def mode(data):
52+
"""Find the mode(s) of a list of numbers."""
53+
counts = {}
54+
for value in data:
55+
if value in counts:
56+
counts[value] += 1
57+
else:
58+
counts[value] = 1
59+
max_count = max(counts.values())
60+
mode = [value for value, count in counts.items() if count == max_count]
61+
return mode
62+
63+
64+
def range(x):
65+
"""Calculate the range of x."""
66+
return max(x) - min(x)
67+
68+
69+
def variance(x):
70+
"""Calculate the variance of x."""
71+
mean_ = mean(x)
72+
return sum((xi - mean_) ** 2 for xi in x) / (len(x) - 1)
73+
74+
75+
def standard_deviation(x):
76+
"""Calculate the standard deviation of x."""
77+
return variance(x) ** 0.5
78+
79+
80+
def coefficient_of_variation(x):
81+
"""Calculate the coefficient of variation of x."""
82+
return standard_deviation(x) / mean(x)

Week06/tests/test_statistics.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import sys
2+
3+
sys.path.append(".")
4+
5+
import pytest
6+
from src.boracanbula import statistics
7+
8+
9+
@pytest.mark.parametrize(
10+
"input, expected",
11+
[
12+
([1, 2, 3, 4, 5], 3),
13+
([1, 2, 3, 4, 5, 6], 3.5),
14+
([1, 2, 3, 4], 2.5),
15+
([1, 2, 3], 2),
16+
([1, 2], 1.5),
17+
([1], 1),
18+
([1, 2, 3, 4, 5, 6, 7], 4),
19+
([1, 2, 3, 4, 5, 6, 7, 8], 4.5),
20+
([1, 2, 3, 4, 5, 6, 7, 8, 9], 5),
21+
],
22+
)
23+
def test_mean(input, expected):
24+
assert (
25+
statistics.mean(input) == expected
26+
), f"failed on test_mean with input {input} and expected {expected}"

0 commit comments

Comments
 (0)