Created
July 22, 2014 16:53
-
-
Save cybertk/7efc97fcfd5c2489c466 to your computer and use it in GitHub Desktop.
Generate normal distrubution samples with given mu and sigma
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
#!/usr/bin/env python | |
import sys | |
import optparse | |
import numpy as np | |
from progress.counter import Counter | |
def GenerateSamples(mu, sigma, size, delta): | |
bar = Counter('Calculating: ') | |
while True: | |
s = np.random.normal(mu, sigma, size) | |
mu_delta = abs(mu - np.mean(s)) / mu | |
sigma_delta = abs(sigma - np.std(s)) / sigma | |
if mu_delta > delta or sigma_delta > delta: | |
bar.next() | |
continue | |
bar.finish() | |
return s | |
def Main(): | |
parser = optparse.OptionParser(usage=' %prog --mu <mean> --sigma <std> ') | |
parser.add_option('--mu', | |
type='float', | |
action='store', | |
dest='mu', | |
default=1, | |
help='Mean value') | |
parser.add_option('--sigma', | |
type='float', | |
action='store', | |
dest='sigma', | |
default=0.1, | |
help='Standard deviation') | |
parser.add_option('--size', | |
type='float', | |
action='store', | |
dest='size', | |
default=50, | |
help='Number of samples') | |
parser.add_option('--delta', | |
type='float', | |
action='store', | |
dest='delta', | |
default=0.001, | |
help='Delta') | |
opts, args = parser.parse_args() | |
s = GenerateSamples(opts.mu, opts.sigma, opts.size, opts.delta) | |
text = """ | |
%d Samples:\n | |
%s | |
Mean: %s | |
Std: %s | |
""" % (len(s), s, np.mean(s), np.std(s)) | |
print text | |
Notify("cmn@nau.edu.cn", text) | |
if '__main__' == __name__: | |
sys.exit(Main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment