Created
April 24, 2017 09:53
-
-
Save willwybrow/a18750b5fb8018ffc5ec434058d960a1 to your computer and use it in GitHub Desktop.
Python file to take a normal random point and select a place within a not-normal weighted set of regions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
def weightedly_distribute_and_scale_noise_point(region_boundaries, region_weights, noise_point): | |
""" | |
region_boundaries are n points within the region, starting with the min and ending at the max, forming n-1 buckets | |
region_weights is a list of weights that should be length n-1, this will determine what % of points fall here | |
noise_point should be between -1.0 and 1.0 | |
""" | |
normalised_noise_point = (noise_point + 1.0)/2.0 | |
weight_totals = float(sum(region_weights)) | |
min_point = region_boundaries[0] | |
max_point = region_boundaries[-1] | |
point_range = (max_point - min_point) | |
weight_boundaries = [0.0] | |
running_weight = 0.0 | |
for weight in region_weights: | |
running_weight += weight/weight_totals | |
weight_boundaries.append(running_weight) | |
region_index = np.digitize([normalised_noise_point], weight_boundaries)[0] # this tells you what region you are in, but not how far along | |
wrmax = weight_boundaries[region_index] | |
wrmin = weight_boundaries[region_index - 1] | |
try: | |
how_far_along_region = (normalised_noise_point - wrmin) / (wrmax - wrmin) | |
except ZeroDivisionError: | |
how_far_along_region = 0.0 | |
bmax = region_boundaries[region_index] | |
bmin = region_boundaries[region_index - 1] | |
output_point = bmin + (how_far_along_region * (bmax - bmin)) | |
return output_point | |
if __name__ == "__main__": | |
boundaries = [-10000, -6250, -6000, -4000, -2000, 0, 1000, 2000, 10000] | |
weights = [1, 2, 35, 20, 7, 30, 2, 3] | |
for x in range(2001): | |
p = (x/1000.0) - 1.0 | |
print("{}\t{}".format(p, weightedly_distribute_and_scale_noise_point(boundaries, weights, p))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment