Exemplo n.º 1
0
def derivative_quant_err(m, p, dist='norm', pw_opt=2):  
    '''
    Compute the derivative of expected variance of quantization error
    '''
    from scipy.stats import norm, laplace
    if dist == 'norm':
        cdf_func = norm.cdf(p)
        pdf_func = norm.pdf(p)
    elif dist == 'laplace':  
        # https://en.wikipedia.org/wiki/Laplace_distribution
        cdf_func = laplace.cdf(p, 0, np.sqrt(0.5))   
        pdf_func = laplace.pdf(p, 0, np.sqrt(0.5)) # pdf(p, a, b) has variance 2*b^2
    else:
        raise RuntimeError("Not implemented for distribution: %s !!!" % dist) 
    
    ## option 1: overlapping
    if pw_opt == 1: 
        # quant_err = [F(p) - F(-p)] * p^2 + 2*[F(m) - F(p)] * m^2
        df_dp = 2 * pdf_func * (p * p - m * m) + 2 * p * (2 * cdf_func - 1.0)
    ## option 2: non-overlapping
    else:  
        # quant_err = [F(p) - F(-p)] * p^2 + 2*[F(m) - F(p)] * (m - p)^2
        df_dp = p - 2 * m + 2 * m * cdf_func + m * pdf_func * (2 * p - m) 

    return df_dp
Exemplo n.º 2
0
    def laplace(shape, scale):
        """
        Standard Laplace noise multiplied by `scale`
        Parameters
        ----------
        shape : tuple
            Shape of noise.
        scale : float
            Scale of noise.
        """
        rv = laplace(scale=scale, loc=0.)
        density = lambda x: np.product(rv.pdf(x))
        cdf = lambda x: laplace.cdf(x, loc=0., scale=scale)
        pdf = lambda x: laplace.pdf(x, loc=0., scale=scale)
        derivative_log_density = lambda x: -np.sign(x) / scale
        grad_negative_log_density = lambda x: np.sign(x) / scale
        sampler = lambda size: rv.rvs(size=shape + size)

        constant = -np.product(shape) * np.log(2 * scale)
        return randomization(shape,
                             density,
                             cdf,
                             pdf,
                             derivative_log_density,
                             grad_negative_log_density,
                             sampler,
                             lipschitz=1. / scale**2,
                             log_density=lambda x: -np.fabs(np.atleast_2d(x)).
                             sum(1) / scale - np.log(scale) + constant)
Exemplo n.º 3
0
def testLaplace1(seed):
    # Test that laplace distribution does what it should
    ripl = get_ripl(seed=seed)
    # samples
    ripl.assume("a", "(laplace -3 2)", label="pid")
    observed = collectSamples(ripl, "pid")
    # true CDF
    laplace_cdf = lambda x: laplace.cdf(x, -3, 2)
    return reportKnownContinuous(laplace_cdf, observed)
Exemplo n.º 4
0
def laplace_distribution(select_size,
                         scale=1 / m.sqrt(2),
                         loc=0,
                         asked=rvs,
                         x=0):
    if asked == rvs:
        return laplace.rvs(size=select_size, scale=scale, loc=loc)
    elif asked == pdf:
        return laplace.pdf(x, loc=loc, scale=scale)
    elif asked == cdf:
        return laplace.cdf(x, loc=loc, scale=scale)
    return
Exemplo n.º 5
0
def distribution_function(x, mu, sigma, distribution):
    if distribution == Distribution.NORMAL:
        return norm.cdf(x, mu, sigma)
    elif distribution == Distribution.CAUCHY:
        return cauchy.cdf(x, mu, sigma)
    elif distribution == Distribution.LAPLACE:
        return laplace.cdf(x, mu, sigma)
    elif distribution == Distribution.POISSON:
        return poisson.cdf(x, mu, sigma)
    elif distribution == Distribution.UNIFORM:
        return uniform.cdf(x, mu, sigma)
    else:
        return None
Exemplo n.º 6
0
def compute_asst_loglik(prec_df, param_df, i, precdist):
    theta = param_df.loc[i, 'theta']
    loc = param_df.loc[i, 'loc']
    scale = param_df.loc[i, 'scale']

    if precdist == 'uniform':
        cdf_val = uniform.cdf(prec_df['midpoint'], loc, scale)
    elif precdist == 'norm':
        cdf_val = norm.cdf(prec_df['midpoint'], loc, scale)
    elif precdist == 'laplace':
        cdf_val = laplace.cdf(prec_df['midpoint'], loc, scale)
    elif precdist == 'mixnorm':
        cdf_val = 0.5 * norm.cdf(prec_df['midpoint'], loc, scale) + \
            0.5 * norm.cdf(prec_df['midpoint'], -loc, scale)
    else:
        print 'Cannot find cdf given precinct distribution, see ' + \
                'compute_asst_loglik().'
        assert (False)

    return binom.logpmf(prec_df['num_votes_0'], prec_df['tot_votes'],
                        cdf_val) + np.log(theta)
Exemplo n.º 7
0
    def test_initialize(self):
        def sqrtmi(mat):
            """
			Compute matrix inverse square root.

			@type  mat: array_like
			@param mat: matrix for which to compute inverse square root
			"""

            # find eigenvectors
            eigvals, eigvecs = eig(mat)

            # eliminate eigenvectors whose eigenvalues are zero
            eigvecs = eigvecs[:, eigvals > 0.]
            eigvals = eigvals[eigvals > 0.]

            # inverse square root
            return dot(eigvecs, dot(diag(1. / sqrt(eigvals)), eigvecs.T))

        # white data
        data = randn(5, 1000)
        data = dot(sqrtmi(cov(data)), data)

        isa = ISA(5, 10)
        isa.initialize(data)

        # rows of A should be roughly orthogonal
        self.assertLess(sum(square(dot(isa.A, isa.A.T) - eye(5)).flatten()),
                        1e-3)

        p = kstest(
            isa.sample_prior(100).flatten(),
            lambda x: laplace.cdf(x, scale=1. / sqrt(2.)))[1]

        # prior marginals should be roughly Laplace
        self.assertGreater(p, 0.0001)

        # test initialization with larger subspaces
        isa = ISA(5, 10, ssize=2)
        isa.initialize(data)
Exemplo n.º 8
0
	def test_initialize(self):
		def sqrtmi(mat):
			"""
			Compute matrix inverse square root.

			@type  mat: array_like
			@param mat: matrix for which to compute inverse square root
			"""

			# find eigenvectors
			eigvals, eigvecs = eig(mat)

			# eliminate eigenvectors whose eigenvalues are zero
			eigvecs = eigvecs[:, eigvals > 0.]
			eigvals = eigvals[eigvals > 0.]

			# inverse square root
			return dot(eigvecs, dot(diag(1. / sqrt(eigvals)), eigvecs.T))

		# white data
		data = randn(5, 1000)
		data = dot(sqrtmi(cov(data)), data)

		isa = ISA(5, 10)
		isa.initialize(data)

		# rows of A should be roughly orthogonal
		self.assertLess(sum(square(dot(isa.A, isa.A.T) - eye(5)).flatten()), 1e-3)

		p = kstest(
			isa.sample_prior(100).flatten(),
			lambda x: laplace.cdf(x, scale=1. / sqrt(2.)))[1]

		# prior marginals should be roughly Laplace
		self.assertGreater(p, 0.0001)

		# test initialization with larger subspaces
		isa = ISA(5, 10, ssize=2)
		isa.initialize(data)
Exemplo n.º 9
0
import numpy as np
from scipy.stats import laplace
import matplotlib.pyplot as plt
fig, ax = plt.subplots(1, 1)

mean, var, skew, kurt = laplace.stats(moments='mvsk')

x = np.linspace(laplace.ppf(0.01),                  laplace.ppf(0.99), 100)
ax.plot(x, laplace.pdf(x),        'r-', lw=5, alpha=0.6, label='laplace pdf')



rv = laplace()
ax.plot(x, rv.pdf(x), 'k-', lw=2, label='frozen pdf')
vals = laplace.ppf([0.001, 0.5, 0.999])
np.allclose([0.001, 0.5, 0.999], laplace.cdf(vals))

r = laplace.rvs(size=1000)
#ax.hist(r, normed=True, histtype='stepfilled', alpha=0.2)
#ax.legend(loc='best', frameon=False)

plt.show()
Exemplo n.º 10
0
 def func(self, x):
     return laplace.cdf(x, loc=self.mu, scale=self.sigma)
Exemplo n.º 11
0
 def _make_l1_table(self, inc=0.001, grid_type='radius', upper=3):
     return diffmethod_table(self.Phi_l1,
                             f=lambda r: laplace.cdf(np.sqrt(2) * r),
                             inc=inc,
                             grid_type=grid_type,
                             upper=upper)
Exemplo n.º 12
0
def cumulative_laplace(x):
    return laplace.cdf(x, 0, 1 / np.sqrt(2))
Exemplo n.º 13
0
import numpy as np
from scipy.stats import laplace
import matplotlib.pyplot as plt
import sys

t_green = '#009933'

# params
mu = float(sys.argv[1])
b = float(sys.argv[2])

# generate samples
# lots of samples are a lazy-man's smoothing
lap_X = laplace.rvs(loc=mu, scale=b, size=1000000)
x = np.linspace(start=-8*b,stop=8*b,num=1000000)
lap_Q = laplace.cdf(x=x, loc=mu, scale=b)

# plotting pdf!
fig = plt.figure()
h = plt.hist(lap_X, bins=500, alpha=1.0, color=t_green, normed=True, histtype='stepfilled', antialiased=True, linewidth=0)
# to make the plot symmetric, find maximum distance to centre
lower_dx = mu - h[1][0]
upper_dx= h[1][-1] - mu
max_dx = np.max([abs(lower_dx), abs(upper_dx)])
plt.xlim(-max_dx + mu, max_dx +mu)
# labels etc.
plt.title('Laplace PDF with mu = '+str(mu)+' and b = '+str(b), family='monospace', size=16)
plt.xlabel('x', family='monospace', size=16)
plt.ylabel('p(x)', family='monospace', size=16)
plt.tight_layout()      # requires Agg
plt.savefig('laplacePDF_mu'+str(mu)+'_b'+str(b)+'.png', dpi=200)
Exemplo n.º 14
0
def cumulative_laplace(x):
    return laplace.cdf(x, 0, 1 / LAPLACE_COEF)
Exemplo n.º 15
0
import matplotlib as mpl
mpl.use('svg')
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
from scipy.stats import laplace as l
import numpy as np
import math

x = np.linspace(-3, 3, 100)

plt.plot(x, l.pdf(x), linewidth=2.0, label='PDF')
plt.plot(x, l.cdf(x), linewidth=2.0, label='CDF')

plt.legend(bbox_to_anchor=(.35, 1))
plt.savefig('cdf.svg', bbox_inches='tight')
import matplotlib.pyplot as plt
from scipy.stats import laplace
import numpy as np
x = np.linspace(-3, 3, 100)
plt.plot(x, laplace.pdf(x),linewidth=2.0, label="laplace PDF")
plt.plot(x, laplace.cdf(x),linewidth=2.0, label="laplace CDF")
plt.legend(bbox_to_anchor=(.35,1))
plt.show()
uniform_expected = np.zeros(uniform_num_bins-1)
gauss_expected = np.zeros(gauss_num_bins-1)
rayleigh_expected = np.zeros(rayleigh_num_bins-1)
laplace_expected = np.zeros(laplace_num_bins-1)

for k in range(len(uniform_hist[0])):
    uniform_expected[k] = num_tests/float(uniform_num_bins-1)

for k in range(len(gauss_hist[0])):
    gauss_expected[k] = float(norm.cdf(gauss_hist[1][k+1])-norm.cdf(gauss_hist[1][k]))*num_tests

for k in range(len(rayleigh_hist[0])):
    rayleigh_expected[k] = float(rayleigh.cdf(rayleigh_hist[1][k+1])-rayleigh.cdf(rayleigh_hist[1][k]))*num_tests

for k in range(len(laplace_hist[0])):
    laplace_expected[k] = float(laplace.cdf(laplace_hist[1][k+1])-laplace.cdf(laplace_hist[1][k]))*num_tests

#*** PLOT HISTOGRAMS AND EXPECTATIONS TAKEN FROM SCIPY ***#

uniform_bins_center = uniform_bins[0:-1]+(uniform_bins[1]-uniform_bins[0])/2.0
gauss_bins_center = gauss_bins[0:-1]+(gauss_bins[1]-gauss_bins[0])/2.0
rayleigh_bins_center = rayleigh_bins[0:-1]+(rayleigh_bins[1]-rayleigh_bins[0])/2.0
laplace_bins_center = laplace_bins[0:-1]+(laplace_bins[1]-laplace_bins[0])/2.0

plt.figure(1)

plt.subplot(2,1,1)
plt.plot(uniform_bins_center,uniform_hist[0],'s--',uniform_bins_center,uniform_expected,'o:')
plt.xlabel('Bins'), plt.ylabel('Count'), plt.title('Uniform: Distribution')
plt.legend(['histogram gr::random','calculation scipy'],loc=1)
Exemplo n.º 18
0
from scipy.stats import laplace
from scipy.stats import cauchy
from scipy.stats import uniform
from scipy.stats import poisson
from scipy.stats import norm
import matplotlib.pyplot as plt
from math import sqrt, floor, exp, pi
import math
import numpy as np

fig, ax = plt.subplots(1, 3)
r = laplace.rvs(size=20)
x = np.linspace(min(laplace.ppf(0.01), min(r)), max(laplace.ppf(0.99), max(r)),
                100)
ax[0].plot(x, laplace.cdf(x, 0, sqrt(2)))
ax[0].hist(r,
           density=True,
           bins=floor(len(r)),
           histtype='step',
           cumulative=True)
ax[0].set_xlabel('x')
ax[0].set_title('Распределение Лапласа, n=20')

r2 = laplace.rvs(size=60)
x2 = np.linspace(min(laplace.ppf(0.01), min(r2)),
                 max(laplace.ppf(0.99), max(r2)), 100)
ax[1].plot(x2, laplace.cdf(x2, 0, sqrt(2)))
ax[1].hist(r2,
           density=True,
           bins=floor(len(r2)),
           histtype='step',
Exemplo n.º 19
0
for k in range(len(uniform_hist[0])):
    uniform_expected[k] = num_tests / float(uniform_num_bins - 1)

for k in range(len(gauss_hist[0])):
    gauss_expected[k] = float(
        norm.cdf(gauss_hist[1][k + 1]) -
        norm.cdf(gauss_hist[1][k])) * num_tests

for k in range(len(rayleigh_hist[0])):
    rayleigh_expected[k] = float(
        rayleigh.cdf(rayleigh_hist[1][k + 1]) -
        rayleigh.cdf(rayleigh_hist[1][k])) * num_tests

for k in range(len(laplace_hist[0])):
    laplace_expected[k] = float(
        laplace.cdf(laplace_hist[1][k + 1]) -
        laplace.cdf(laplace_hist[1][k])) * num_tests

#*** PLOT HISTOGRAMS AND EXPECTATIONS TAKEN FROM SCIPY ***#

uniform_bins_center = uniform_bins[0:-1] + (uniform_bins[1] -
                                            uniform_bins[0]) / 2.0
gauss_bins_center = gauss_bins[0:-1] + (gauss_bins[1] - gauss_bins[0]) / 2.0
rayleigh_bins_center = rayleigh_bins[0:-1] + (rayleigh_bins[1] -
                                              rayleigh_bins[0]) / 2.0
laplace_bins_center = laplace_bins[0:-1] + (laplace_bins[1] -
                                            laplace_bins[0]) / 2.0

plt.figure(1)

plt.subplot(2, 1, 1)
Exemplo n.º 20
0
from math import sqrt, floor
import math
import numpy as np

fig, ax = plt.subplots(2, 3)
r = laplace.rvs(size=10)
x = np.linspace(min(laplace.ppf(0.01),min(r)), max(laplace.ppf(0.99),max(r)), 100)
ax[0][0].plot(x, laplace.pdf(x,0,sqrt(2)), label='теор.')
ax[0][0].set_title('Распределение Лапласа, n=10')
ax[0][0].hist(r, density=True, bins=floor(sqrt(len(r))),histtype='step',label='практ.')
ax[0][0].legend(loc='best', frameon=False)
ax[0][0].set_xlabel('x')
ax[0][0].set_ylabel('Функция плотности')
ax[0][0].legend()

ax[1][0].plot(x, laplace.cdf(x,0,sqrt(2)), label='теор.')
ax[1][0].hist(r, density=True, bins=floor(sqrt(len(r))),histtype='step',cumulative=True,label='практ.')
ax[1][0].legend(loc='best', frameon=False)
ax[1][0].set_xlabel('x')
ax[1][0].set_ylabel('Функция распределения')
ax[1][0].legend()

r2 = laplace.rvs(size=100)
x2 = np.linspace(min(laplace.ppf(0.01),min(r2)), max(laplace.ppf(0.99),max(r2)), 100)
ax[0][1].plot(x2, laplace.pdf(x2,0,sqrt(2)), label='теор.')
ax[0][1].set_title('Распределение Лапласа, n=100')
ax[0][1].hist(r2, density=True, bins=floor(sqrt(len(r2))),histtype='step',label='практ.')
ax[0][1].legend(loc='best', frameon=False)
ax[0][1].set_xlabel('x')
ax[0][1].set_ylabel('Функция плотности')
ax[0][1].legend()