Repository navigation
Expand file tree
/
Copy pathenvironment.py
More file actions
74 lines (54 loc) · 2.54 KB
/
Copy pathenvironment.py
File metadata and controls
74 lines (54 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import math
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import random
class SIMULATE_SAMPLE_FROM_WORLD:
"""
Create a distribution to sample values from given a list of real-world values
"""
def __init__(
self,
real_world_values: list,
bin_minimum: int,
bin_maximum: int,
bin_size: int,
name: str = 'simulate_sample_from_real_world'
):
"""
Argument(s):
real_world_values: list of values from the real world to model the simulated sampling distribution from
bin_minimum: minimum value of variable to simulate a sample of
bin_maximum: maximum value of variable to simulate a sample of
bin_size: bin size of variable to simulate a sample of (values within a bin will be pooled together to generate sampling probabilities)
name: name
"""
print('class SIMULATE_SAMPLE_FROM_WORLD:.__init__')
self.real_world_values = real_world_values
self.bin_minimum = bin_minimum
self.bin_maximum = bin_maximum
self.bin_size = bin_size
self._name = name
def __call__(self, n_values):
#bin limits
bin_intervals = np.arange(self.bin_minimum, self.bin_maximum + self.bin_size, self.bin_size).tolist()
#counts for how many professors fall into each bins
bin_counts = pd.cut(self.real_world_values, bins=bin_intervals).value_counts()
#probabilities belonging to a given bin
bin_probabilities = [float(i)/sum(bin_counts) for i in bin_counts]
#sample to see which bin the sample will fall into with probabilities given
bin_choice = random.choices(range(1, len(bin_intervals)), bin_probabilities)
#uniformly sample within the given bin choice
simulated_sample = np.random.uniform(bin_intervals[bin_choice[0]-1], bin_intervals[bin_choice[0]], n_values)
simulated_sample_integers = [int(x) for x in simulated_sample]
#simulated sample value
return simulated_sample_integers
#H-INDEX (TEST)
#list of UBC psychology professor h-indexes; values taken from google scholar
#CHANGE_ME (unfinished)
h_indexes_ubc_psychology = [61, 27, 41, 27, 25, 45, 95, 17, 49, 55, 15, 71, 72, 76, 12, 10, 38, 29, 67, 96, 35, 11, 33, 32, 56]
bin_minimum = 0
bin_maximum = 100
bin_size = 5
simulated_ubc_psychology_h_index = SIMULATE_SAMPLE_FROM_WORLD(h_indexes_ubc_psychology, bin_minimum, bin_maximum, bin_size)
print(simulated_ubc_psychology_h_index(100))