Python scipy.stats.laplace() Examples

The following are 26 code examples of scipy.stats.laplace(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module scipy.stats , or try the search function .
Example #1
Source File: probability.py    From CSBDeep with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __init__(self, loc, scale):
        loc.shape == scale.shape or _raise(ValueError())
        #
        self._loc     = loc
        self._scale   = scale
        # expose methods from laplace object
        _laplace      = laplace(loc=self._loc,scale=self._scale)
        self.rvs      = _laplace.rvs
        self.pdf      = _laplace.pdf
        self.logpdf   = _laplace.logpdf
        self.cdf      = _laplace.cdf
        self.logcdf   = _laplace.logcdf
        self.sf       = _laplace.sf
        self.logsf    = _laplace.logsf
        self.ppf      = _laplace.ppf
        self.isf      = _laplace.isf
        self.moment   = _laplace.moment
        self.stats    = _laplace.stats
        self.entropy  = _laplace.entropy
        self.expect   = _laplace.expect
        self.median   = _laplace.median
        self.mean     = _laplace.mean
        self.var      = _laplace.var
        self.std      = _laplace.std
        self.interval = _laplace.interval 
Example #2
Source File: laplace_test.py    From deep_image_model with Apache License 2.0 6 votes vote down vote up
def testLaplacePdfOfSampleMultiDims(self):
    with tf.Session() as sess:
      laplace = tf.contrib.distributions.Laplace(
          loc=[7., 11.], scale=[[5.], [6.]])
      num = 50000
      samples = laplace.sample(num, seed=137)
      pdfs = laplace.pdf(samples)
      sample_vals, pdf_vals = sess.run([samples, pdfs])
      self.assertEqual(samples.get_shape(), (num, 2, 2))
      self.assertEqual(pdfs.get_shape(), (num, 2, 2))
      self.assertAllClose(
          stats.laplace.mean([[7., 11.], [7., 11.]],
                             scale=np.array([[5., 5.], [6., 6.]])),
          sample_vals.mean(axis=0),
          rtol=0.05, atol=0.)
      self.assertAllClose(
          stats.laplace.var([[7., 11.], [7., 11.]],
                            scale=np.array([[5., 5.], [6., 6.]])),
          sample_vals.var(axis=0),
          rtol=0.05, atol=0.)
      self._assertIntegral(sample_vals[:, 0, 0], pdf_vals[:, 0, 0], err=0.02)
      self._assertIntegral(sample_vals[:, 0, 1], pdf_vals[:, 0, 1], err=0.02)
      self._assertIntegral(sample_vals[:, 1, 0], pdf_vals[:, 1, 0], err=0.02)
      self._assertIntegral(sample_vals[:, 1, 1], pdf_vals[:, 1, 1], err=0.02) 
Example #3
Source File: laplace_test.py    From deep_image_model with Apache License 2.0 6 votes vote down vote up
def testLaplaceLogPDF(self):
    with self.test_session():
      batch_size = 6
      loc = tf.constant([2.0] * batch_size)
      scale = tf.constant([3.0] * batch_size)
      loc_v = 2.0
      scale_v = 3.0
      x = np.array([2.5, 2.5, 4.0, 0.1, 1.0, 2.0], dtype=np.float32)
      laplace = tf.contrib.distributions.Laplace(loc=loc, scale=scale)
      expected_log_pdf = stats.laplace.logpdf(x, loc_v, scale=scale_v)
      log_pdf = laplace.log_pdf(x)
      self.assertEqual(log_pdf.get_shape(), (6,))
      self.assertAllClose(log_pdf.eval(), expected_log_pdf)

      pdf = laplace.pdf(x)
      self.assertEqual(pdf.get_shape(), (6,))
      self.assertAllClose(pdf.eval(), np.exp(expected_log_pdf)) 
Example #4
Source File: laplace_test.py    From deep_image_model with Apache License 2.0 6 votes vote down vote up
def testLaplaceLogPDFMultidimensional(self):
    with self.test_session():
      batch_size = 6
      loc = tf.constant([[2.0, 4.0]] * batch_size)
      scale = tf.constant([[3.0, 4.0]] * batch_size)
      loc_v = np.array([2.0, 4.0])
      scale_v = np.array([3.0, 4.0])
      x = np.array([[2.5, 2.5, 4.0, 0.1, 1.0, 2.0]], dtype=np.float32).T
      laplace = tf.contrib.distributions.Laplace(loc=loc, scale=scale)
      expected_log_pdf = stats.laplace.logpdf(x, loc_v, scale=scale_v)
      log_pdf = laplace.log_pdf(x)
      log_pdf_values = log_pdf.eval()
      self.assertEqual(log_pdf.get_shape(), (6, 2))
      self.assertAllClose(log_pdf_values, expected_log_pdf)

      pdf = laplace.pdf(x)
      pdf_values = pdf.eval()
      self.assertEqual(pdf.get_shape(), (6, 2))
      self.assertAllClose(pdf_values, np.exp(expected_log_pdf)) 
Example #5
Source File: laplace_test.py    From deep_image_model with Apache License 2.0 6 votes vote down vote up
def testLaplaceLogPDFMultidimensionalBroadcasting(self):
    with self.test_session():
      batch_size = 6
      loc = tf.constant([[2.0, 4.0]] * batch_size)
      scale = tf.constant(3.0)
      loc_v = np.array([2.0, 4.0])
      scale_v = 3.0
      x = np.array([[2.5, 2.5, 4.0, 0.1, 1.0, 2.0]], dtype=np.float32).T
      laplace = tf.contrib.distributions.Laplace(loc=loc, scale=scale)
      expected_log_pdf = stats.laplace.logpdf(x, loc_v, scale=scale_v)
      log_pdf = laplace.log_pdf(x)
      log_pdf_values = log_pdf.eval()
      self.assertEqual(log_pdf.get_shape(), (6, 2))
      self.assertAllClose(log_pdf_values, expected_log_pdf)

      pdf = laplace.pdf(x)
      pdf_values = pdf.eval()
      self.assertEqual(pdf.get_shape(), (6, 2))
      self.assertAllClose(pdf_values, np.exp(expected_log_pdf)) 
Example #6
Source File: laplace_test.py    From deep_image_model with Apache License 2.0 6 votes vote down vote up
def testLaplaceSample(self):
    with tf.Session():
      loc_v = 4.0
      scale_v = 3.0
      loc = tf.constant(loc_v)
      scale = tf.constant(scale_v)
      n = 100000
      laplace = tf.contrib.distributions.Laplace(loc=loc, scale=scale)
      samples = laplace.sample(n, seed=137)
      sample_values = samples.eval()
      self.assertEqual(samples.get_shape(), (n,))
      self.assertEqual(sample_values.shape, (n,))
      self.assertAllClose(sample_values.mean(),
                          stats.laplace.mean(loc_v, scale=scale_v),
                          rtol=0.05, atol=0.)
      self.assertAllClose(sample_values.var(),
                          stats.laplace.var(loc_v, scale=scale_v),
                          rtol=0.05, atol=0.)
      self.assertTrue(self._kstest(loc_v, scale_v, sample_values)) 
Example #7
Source File: laplace_test.py    From deep_image_model with Apache License 2.0 5 votes vote down vote up
def testLaplaceVariance(self):
    with self.test_session():
      loc_v = np.array([1.0, 3.0, 2.5])
      scale_v = np.array([1.0, 4.0, 5.0])
      laplace = tf.contrib.distributions.Laplace(loc=loc_v, scale=scale_v)
      expected_variances = stats.laplace.var(loc_v, scale=scale_v)
      self.assertEqual(laplace.variance().get_shape(), (3,))
      self.assertAllClose(laplace.variance().eval(), expected_variances) 
Example #8
Source File: ex_transf2.py    From Splunking-Crime with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_loglaplace():
    #if x is laplace then y = exp(x) is loglaplace
    #parameters are tricky
    #the stats.loglaplace parameter is the inverse scale of x
    loglaplaceexpg = ExpTransf_gen(stats.laplace)

    cdfst = stats.loglaplace.cdf(3,3)
    #0.98148148148148151
    #the parameters are shape, loc and scale of underlying laplace
    cdftr = loglaplaceexpg._cdf(3,0,1./3)
    assert_almost_equal(cdfst, cdftr, 14) 
Example #9
Source File: utils.py    From rlsp with MIT License 5 votes vote down vote up
def __init__(self, mu, b=1):
        self.mu = mu
        self.b = b
        self.distribution = laplace(loc=mu, scale=b) 
Example #10
Source File: laplace_test.py    From deep_image_model with Apache License 2.0 5 votes vote down vote up
def testLaplaceWithSoftplusScale(self):
    with self.test_session():
      loc_v = tf.constant([0.0, 1.0], name="loc")
      scale_v = tf.constant([-1.0, 2.0], name="scale")
      laplace = tf.contrib.distributions.LaplaceWithSoftplusScale(
          loc=loc_v, scale=scale_v)
      self.assertAllClose(tf.nn.softplus(scale_v).eval(), laplace.scale.eval())
      self.assertAllClose(loc_v.eval(), laplace.loc.eval()) 
Example #11
Source File: laplace_test.py    From deep_image_model with Apache License 2.0 5 votes vote down vote up
def testLaplaceNonPositiveInitializationParamsRaises(self):
    with self.test_session():
      loc_v = tf.constant(0.0, name="loc")
      scale_v = tf.constant(-1.0, name="scale")
      laplace = tf.contrib.distributions.Laplace(
          loc=loc_v, scale=scale_v, validate_args=True)
      with self.assertRaisesOpError("scale"):
        laplace.mean().eval()
      loc_v = tf.constant(1.0, name="loc")
      scale_v = tf.constant(0.0, name="scale")
      laplace = tf.contrib.distributions.Laplace(
          loc=loc_v, scale=scale_v, validate_args=True)
      with self.assertRaisesOpError("scale"):
        laplace.mean().eval() 
Example #12
Source File: laplace_test.py    From deep_image_model with Apache License 2.0 5 votes vote down vote up
def _kstest(self, loc, scale, samples):
    # Uses the Kolmogorov-Smirnov test for goodness of fit.
    ks, _ = stats.kstest(samples, stats.laplace(loc, scale=scale).cdf)
    # Return True when the test passes.
    return ks < 0.02 
Example #13
Source File: laplace_test.py    From deep_image_model with Apache License 2.0 5 votes vote down vote up
def testLaplaceEntropy(self):
    with self.test_session():
      loc_v = np.array([1.0, 3.0, 2.5])
      scale_v = np.array([1.0, 4.0, 5.0])
      expected_entropy = stats.laplace.entropy(loc_v, scale=scale_v)
      laplace = tf.contrib.distributions.Laplace(loc=loc_v, scale=scale_v)
      self.assertEqual(laplace.entropy().get_shape(), (3,))
      self.assertAllClose(laplace.entropy().eval(), expected_entropy) 
Example #14
Source File: laplace_test.py    From deep_image_model with Apache License 2.0 5 votes vote down vote up
def testLaplaceStd(self):
    with self.test_session():
      loc_v = np.array([1.0, 3.0, 2.5])
      scale_v = np.array([1.0, 4.0, 5.0])
      laplace = tf.contrib.distributions.Laplace(loc=loc_v, scale=scale_v)
      expected_std = stats.laplace.std(loc_v, scale=scale_v)
      self.assertEqual(laplace.std().get_shape(), (3,))
      self.assertAllClose(laplace.std().eval(), expected_std) 
Example #15
Source File: laplace_test.py    From deep_image_model with Apache License 2.0 5 votes vote down vote up
def testLaplaceMode(self):
    with self.test_session():
      loc_v = np.array([0.5, 3.0, 2.5])
      scale_v = np.array([1.0, 4.0, 5.0])
      laplace = tf.contrib.distributions.Laplace(loc=loc_v, scale=scale_v)
      self.assertEqual(laplace.mode().get_shape(), (3,))
      self.assertAllClose(laplace.mode().eval(), loc_v) 
Example #16
Source File: laplace_test.py    From deep_image_model with Apache License 2.0 5 votes vote down vote up
def testLaplaceCDF(self):
    with self.test_session():
      batch_size = 6
      loc = tf.constant([2.0] * batch_size)
      scale = tf.constant([3.0] * batch_size)
      loc_v = 2.0
      scale_v = 3.0
      x = np.array([2.5, 2.5, 4.0, 0.1, 1.0, 2.0], dtype=np.float32)

      laplace = tf.contrib.distributions.Laplace(loc=loc, scale=scale)
      expected_cdf = stats.laplace.cdf(x, loc_v, scale=scale_v)

      cdf = laplace.cdf(x)
      self.assertEqual(cdf.get_shape(), (6,))
      self.assertAllClose(cdf.eval(), expected_cdf) 
Example #17
Source File: laplace_test.py    From deep_image_model with Apache License 2.0 5 votes vote down vote up
def testLaplaceShape(self):
    with self.test_session():
      loc = tf.constant([3.0] * 5)
      scale = tf.constant(11.0)
      laplace = tf.contrib.distributions.Laplace(loc=loc, scale=scale)

      self.assertEqual(laplace.batch_shape().eval(), (5,))
      self.assertEqual(laplace.get_batch_shape(), tf.TensorShape([5]))
      self.assertAllEqual(laplace.event_shape().eval(), [])
      self.assertEqual(laplace.get_event_shape(), tf.TensorShape([])) 
Example #18
Source File: test_laplace.py    From chainer with MIT License 5 votes vote down vote up
def check_backward(self, x_data, y_grad):
        gradient_check.check_backward(
            distributions.laplace._laplace_icdf,
            x_data, y_grad, **self.backward_options) 
Example #19
Source File: test_laplace.py    From chainer with MIT License 5 votes vote down vote up
def check_forward(self, x_data):
        y = distributions.laplace._laplace_icdf(x_data)
        cdf = distributions.laplace._laplace_cdf(y)
        testing.assert_allclose(cdf.array, x_data) 
Example #20
Source File: test_laplace.py    From chainer with MIT License 5 votes vote down vote up
def check_backward(self, x_data, y_grad):
        gradient_check.check_backward(
            distributions.laplace._laplace_cdf,
            x_data, y_grad, **self.backward_options) 
Example #21
Source File: test_laplace.py    From chainer with MIT License 5 votes vote down vote up
def setUp_configure(self):
        from scipy import stats
        self.dist = distributions.Laplace
        self.scipy_dist = stats.laplace

        self.test_targets = set([
            'batch_shape', 'cdf', 'entropy', 'event_shape', 'icdf', 'log_prob',
            'mean', 'prob', 'sample', 'stddev', 'support', 'variance'])

        loc = utils.force_array(
            numpy.random.uniform(-1, 1, self.shape).astype(numpy.float32))
        scale = utils.force_array(numpy.exp(
            numpy.random.uniform(-1, 1, self.shape)).astype(numpy.float32))
        self.params = {'loc': loc, 'scale': scale}
        self.scipy_params = {'loc': loc, 'scale': scale} 
Example #22
Source File: ex_transf2.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_loglaplace():
    #if x is laplace then y = exp(x) is loglaplace
    #parameters are tricky
    #the stats.loglaplace parameter is the inverse scale of x
    loglaplaceexpg = ExpTransf_gen(stats.laplace)

    cdfst = stats.loglaplace.cdf(3,3)
    #0.98148148148148151
    #the parameters are shape, loc and scale of underlying laplace
    cdftr = loglaplaceexpg._cdf(3,0,1./3)
    assert_almost_equal(cdfst, cdftr, 14) 
Example #23
Source File: transformed.py    From vnpy_crypto with MIT License 4 votes vote down vote up
def examples_transf():
    ##lognormal = ExpTransf(a=0.0, xa=-10.0, name = 'Log transformed normal')
    ##print(lognormal.cdf(1)
    ##print(stats.lognorm.cdf(1,1)
    ##print(lognormal.stats()
    ##print(stats.lognorm.stats(1)
    ##print(lognormal.rvs(size=10)

    print('Results for lognormal')
    lognormalg = ExpTransf_gen(stats.norm, a=0, name = 'Log transformed normal general')
    print(lognormalg.cdf(1))
    print(stats.lognorm.cdf(1,1))
    print(lognormalg.stats())
    print(stats.lognorm.stats(1))
    print(lognormalg.rvs(size=5))

    ##print('Results for loggamma'
    ##loggammag = ExpTransf_gen(stats.gamma)
    ##print(loggammag._cdf(1,10)
    ##print(stats.loggamma.cdf(1,10)

    print('Results for expgamma')
    loggammaexpg = LogTransf_gen(stats.gamma)
    print(loggammaexpg._cdf(1,10))
    print(stats.loggamma.cdf(1,10))
    print(loggammaexpg._cdf(2,15))
    print(stats.loggamma.cdf(2,15))


    # this requires change in scipy.stats.distribution
    #print(loggammaexpg.cdf(1,10)

    print('Results for loglaplace')
    loglaplaceg = LogTransf_gen(stats.laplace)
    print(loglaplaceg._cdf(2,10))
    print(stats.loglaplace.cdf(2,10))
    loglaplaceexpg = ExpTransf_gen(stats.laplace)
    print(loglaplaceexpg._cdf(2,10))




## copied from transformtwo.py 
Example #24
Source File: transformed.py    From Splunking-Crime with GNU Affero General Public License v3.0 4 votes vote down vote up
def examples_transf():
    ##lognormal = ExpTransf(a=0.0, xa=-10.0, name = 'Log transformed normal')
    ##print(lognormal.cdf(1)
    ##print(stats.lognorm.cdf(1,1)
    ##print(lognormal.stats()
    ##print(stats.lognorm.stats(1)
    ##print(lognormal.rvs(size=10)

    print('Results for lognormal')
    lognormalg = ExpTransf_gen(stats.norm, a=0, name = 'Log transformed normal general')
    print(lognormalg.cdf(1))
    print(stats.lognorm.cdf(1,1))
    print(lognormalg.stats())
    print(stats.lognorm.stats(1))
    print(lognormalg.rvs(size=5))

    ##print('Results for loggamma'
    ##loggammag = ExpTransf_gen(stats.gamma)
    ##print(loggammag._cdf(1,10)
    ##print(stats.loggamma.cdf(1,10)

    print('Results for expgamma')
    loggammaexpg = LogTransf_gen(stats.gamma)
    print(loggammaexpg._cdf(1,10))
    print(stats.loggamma.cdf(1,10))
    print(loggammaexpg._cdf(2,15))
    print(stats.loggamma.cdf(2,15))


    # this requires change in scipy.stats.distribution
    #print(loggammaexpg.cdf(1,10)

    print('Results for loglaplace')
    loglaplaceg = LogTransf_gen(stats.laplace)
    print(loglaplaceg._cdf(2,10))
    print(stats.loglaplace.cdf(2,10))
    loglaplaceexpg = ExpTransf_gen(stats.laplace)
    print(loglaplaceexpg._cdf(2,10))




## copied from transformtwo.py 
Example #25
Source File: ex_extras.py    From Splunking-Crime with GNU Affero General Public License v3.0 4 votes vote down vote up
def examples_transf():
    ##lognormal = ExpTransf(a=0.0, xa=-10.0, name = 'Log transformed normal')
    ##print(lognormal.cdf(1))
    ##print(stats.lognorm.cdf(1,1))
    ##print(lognormal.stats())
    ##print(stats.lognorm.stats(1))
    ##print(lognormal.rvs(size=10))

    print('Results for lognormal')
    lognormalg = ExpTransf_gen(stats.norm, a=0, name = 'Log transformed normal general')
    print(lognormalg.cdf(1))
    print(stats.lognorm.cdf(1,1))
    print(lognormalg.stats())
    print(stats.lognorm.stats(1))
    print(lognormalg.rvs(size=5))

    ##print('Results for loggamma')
    ##loggammag = ExpTransf_gen(stats.gamma)
    ##print(loggammag._cdf(1,10))
    ##print(stats.loggamma.cdf(1,10))

    print('Results for expgamma')
    loggammaexpg = LogTransf_gen(stats.gamma)
    print(loggammaexpg._cdf(1,10))
    print(stats.loggamma.cdf(1,10))
    print(loggammaexpg._cdf(2,15))
    print(stats.loggamma.cdf(2,15))


    # this requires change in scipy.stats.distribution
    #print(loggammaexpg.cdf(1,10))

    print('Results for loglaplace')
    loglaplaceg = LogTransf_gen(stats.laplace)
    print(loglaplaceg._cdf(2))
    print(stats.loglaplace.cdf(2,1))
    loglaplaceexpg = ExpTransf_gen(stats.laplace)
    print(loglaplaceexpg._cdf(2))
    stats.loglaplace.cdf(3,3)
    #0.98148148148148151
    loglaplaceexpg._cdf(3,0,1./3)
    #0.98148148148148151 
Example #26
Source File: ex_extras.py    From vnpy_crypto with MIT License 4 votes vote down vote up
def examples_transf():
    ##lognormal = ExpTransf(a=0.0, xa=-10.0, name = 'Log transformed normal')
    ##print(lognormal.cdf(1))
    ##print(stats.lognorm.cdf(1,1))
    ##print(lognormal.stats())
    ##print(stats.lognorm.stats(1))
    ##print(lognormal.rvs(size=10))

    print('Results for lognormal')
    lognormalg = ExpTransf_gen(stats.norm, a=0, name = 'Log transformed normal general')
    print(lognormalg.cdf(1))
    print(stats.lognorm.cdf(1,1))
    print(lognormalg.stats())
    print(stats.lognorm.stats(1))
    print(lognormalg.rvs(size=5))

    ##print('Results for loggamma')
    ##loggammag = ExpTransf_gen(stats.gamma)
    ##print(loggammag._cdf(1,10))
    ##print(stats.loggamma.cdf(1,10))

    print('Results for expgamma')
    loggammaexpg = LogTransf_gen(stats.gamma)
    print(loggammaexpg._cdf(1,10))
    print(stats.loggamma.cdf(1,10))
    print(loggammaexpg._cdf(2,15))
    print(stats.loggamma.cdf(2,15))


    # this requires change in scipy.stats.distribution
    #print(loggammaexpg.cdf(1,10))

    print('Results for loglaplace')
    loglaplaceg = LogTransf_gen(stats.laplace)
    print(loglaplaceg._cdf(2))
    print(stats.loglaplace.cdf(2,1))
    loglaplaceexpg = ExpTransf_gen(stats.laplace)
    print(loglaplaceexpg._cdf(2))
    stats.loglaplace.cdf(3,3)
    #0.98148148148148151
    loglaplaceexpg._cdf(3,0,1./3)
    #0.98148148148148151