Edward Brown added file figures/sample-datasets/measurements.py  about 9 years ago

Commit id: 561f4b7b06fd269b99ad50ba2c604b08238d1168

deletions | additions      

         

################################################################################  # Edward Brown  # Michigan State University  #  # a simple class to make a measurement with gaussian uncertainties.  #  ################################################################################  import numpy as np  import numpy.random as nr  class measurementWithUncertainty:  """  Upon initialization, generates a normal (gaussian) distribution with a mean   in the range [10.0,20.0] and a standard deviation in the range [1.0,2.0].   The mean and standard deviation are themselves chosen at random.    Example  -------    >>> from measurements import measurementWithUncertainty  >>> d = measurementWithUncertainty()  made data 12.492 +/- 1.12  >>> x = d.make_measurements(10)  >>> print x  [ 13.85981811 11.70694115 10.28276517 12.41290813 11.7629273  11.5970737 10.06126323 13.05767109 13.45734073 12.21781776]  >>> print d.mean()  12.4922289309  >>> print d.stddev()  1.12196280663    Here the call to make_measurements(N) generates N measurements chosen   randomly from this distribution.  """    _sample_low = 10.0  _sample_high = 20.0  _stddev_bias = 1.0    def __init__(self):  """  Sets the mean and standard deviation of our sample.  """  a = self._sample_low  b = self._sample_high  self._mean = (b-a)*nr.random() + a  self._stddev = nr.random() + self._stddev_bias  print 'made data {0:7.3f} +/- {1:6.3}'.format(self._mean,self._stddev)  def make_measurements(self,n):  """  Returns a set of measurements drawn from distribution. Called with   argument n, the number of desired measurements.  """  return nr.normal(loc=self._mean,scale=self._stddev,size=n)  def mean(self):  """  Returns the mean of the distribution. Note that this returns the   parameter in the distribution. and *not* the average of a set of   numbers drawn from the distribution.  """  return self._mean    def stddev(self):  """  Returns the standard deviation of the distribution. Note that this   returns the parameter in the distribution, and *not* the variance of a   set of numbers drawn from the distribution.  """  return self._stddev