Created
September 7, 2013 19:14
-
-
Save justinzane/6478386 to your computer and use it in GitHub Desktop.
Sample Pareto Generator
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
# -*- coding: utf-8 -*- | |
""" | |
Created on Sat Sep 7 07:20:11 2013 | |
@author: justin | |
""" | |
import numpy as np | |
from matplotlib import pyplot as pp | |
import random | |
import math | |
class Pareto(object): | |
def __init__(self, alpha, low, high): | |
self.alpha = alpha | |
self.low = low | |
self.high = high | |
self.rng = random.Random() | |
def get_val(self): | |
r = self.rng.random() | |
numer = -1.0 * (r * math.pow(self.high, self.alpha) + | |
-1.0 * r * math.pow(self.low, self.alpha) + | |
-1.0 * math.pow(self.high, self.alpha)) | |
denom = math.pow(self.high, self.alpha) * math.pow(self.low, self.alpha) | |
frac = numer / denom | |
return(math.pow(frac, -1.0/self.alpha)) | |
if __name__ == "__main__": | |
length = 10000 | |
dams = 16 | |
dist = np.zeros([dams-2,length], dtype=np.int) | |
ax = pp.axes() | |
for dam in range(2,dams): | |
p = Pareto(1.0, dams/2, dam*2) | |
for d in range(length): | |
dist[dam-2][d] = int(p.get_val()+0.5) | |
print(np.mean(dist), np.median(dist)) | |
ax.plot(np.sort(dist[dam-2])) | |
pp.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment