netflypsb commited on
Commit
50c7793
·
verified ·
1 Parent(s): e6a9ea8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +97 -0
app.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import random
3
+ import numpy as np
4
+
5
+ def simple_random_sampling(population_size, sample_size, seed=None):
6
+ if seed is not None:
7
+ random.seed(seed)
8
+ population = list(range(1, population_size + 1))
9
+ if sample_size > population_size:
10
+ st.error("Sample size cannot be greater than population size.")
11
+ return None
12
+ return random.sample(population, sample_size)
13
+
14
+ def stratified_sampling(population_size, sample_size, strata_sizes, seed=None):
15
+ if seed is not None:
16
+ random.seed(seed)
17
+ strata_boundaries = np.cumsum(strata_sizes)
18
+ population = list(range(1, population_size + 1))
19
+ sample = []
20
+
21
+ for i, size in enumerate(strata_sizes):
22
+ start = 0 if i == 0 else strata_boundaries[i-1]
23
+ end = strata_boundaries[i]
24
+ stratum_population = population[start:end]
25
+ stratum_sample_size = int(size / population_size * sample_size)
26
+ sample.extend(random.sample(stratum_population, stratum_sample_size))
27
+ return sample
28
+
29
+ def cluster_sampling(clusters, selected_clusters, seed=None):
30
+ if seed is not None:
31
+ random.seed(seed)
32
+ selected = random.sample(clusters, selected_clusters)
33
+ return [item for sublist in selected for item in sublist]
34
+
35
+ def systematic_sampling(population_size, sample_size, seed=None):
36
+ if seed is not None:
37
+ random.seed(seed)
38
+ step = population_size // sample_size
39
+ start = random.randint(0, step - 1)
40
+ return list(range(start, population_size, step))[:sample_size]
41
+
42
+ def snowball_sampling(initial_samples, recruit_per_sample, population):
43
+ sample = set(initial_samples)
44
+ while len(sample) < recruit_per_sample:
45
+ recruits = [random.choice(population) for _ in range(recruit_per_sample)]
46
+ sample.update(recruits)
47
+ return list(sample)[:recruit_per_sample]
48
+
49
+ def main():
50
+ st.title('Advanced Sampling Methods App')
51
+ st.markdown("""
52
+ ## Description
53
+ Select from various sampling methods depending on your research needs.
54
+ Each method has specific inputs tailored to different research scenarios.
55
+ """)
56
+
57
+ sampling_method = st.sidebar.selectbox("Choose the sampling method",
58
+ ("Simple Random Sampling", "Stratified Sampling",
59
+ "Cluster Sampling", "Systematic Sampling",
60
+ "Snowball Sampling"))
61
+
62
+ population_size = st.sidebar.number_input('Enter Population Size', min_value=1, value=1000, step=1)
63
+ sample_size = st.sidebar.number_input('Enter Sample Size', min_value=1, value=50, step=1)
64
+ seed = st.sidebar.number_input('Enter Random Seed (optional)', min_value=0, value=None, step=1, format='%d', key='seed')
65
+
66
+ if sampling_method == "Stratified Sampling":
67
+ strata_sizes = st.sidebar.text_input('Enter Strata Sizes (comma-separated, e.g., 10,20,30)')
68
+ strata_sizes = list(map(int, strata_sizes.split(','))) if strata_sizes else []
69
+
70
+ if sampling_method == "Cluster Sampling":
71
+ num_clusters = st.sidebar.number_input('Enter Number of Clusters', min_value=1, value=5, step=1)
72
+ selected_clusters = st.sidebar.number_input('Enter Number of Clusters to Sample', min_value=1, value=2, step=1)
73
+ clusters = [list(range(i * 100, (i + 1) * 100)) for i in range(num_clusters)]
74
+
75
+ if st.sidebar.button('Generate Sample'):
76
+ sample = []
77
+ if sampling_method == "Simple Random Sampling":
78
+ sample = simple_random_sampling(population_size, sample_size, seed)
79
+ elif sampling_method == "Stratified Sampling":
80
+ sample = stratified_sampling(population_size, sample_size, strata_sizes, seed)
81
+ elif sampling_method == "Cluster Sampling":
82
+ sample = cluster_sampling(clusters, selected_clusters, seed)
83
+ elif sampling_method == "Systematic Sampling":
84
+ sample = systematic_sampling(population_size, sample_size, seed)
85
+ elif sampling_method == "Snowball Sampling":
86
+ initial_samples = [random.randint(1, population_size) for _ in range(5)]
87
+ sample = snowball_sampling(initial_samples, sample_size, range(1, population_size + 1))
88
+
89
+ if sample:
90
+ st.write(f'### {sampling_method} Output')
91
+ st.write(f'Sample: {sample}')
92
+ st.write(f'Sample Size: {len(sample)}')
93
+ if seed is not None:
94
+ st.write(f'Random Seed Used: {seed}')
95
+
96
+ if __name__ == '__main__':
97
+ main()