def test_discrete_uniform_moments(self): """ Distributions: Checks that the discrete uniform dist. has the right moments. """ dist = DiscreteUniformDistribution(5) samples = dist.sample(200000).astype(float) assert_sigfigs_equal((2**10 - 1) / 12, np.var(samples), 1) assert_sigfigs_equal(16, np.mean(samples), 1)
def test_mvuniform_moments(self): """ Distributions: Checks that ``MVUniformDistribution`` has the right moments. """ dist = MVUniformDistribution(dim=6) samples = dist.sample(100000) assert_sigfigs_equal(5/(36*7), samples[:,3].var(), 2) assert_sigfigs_equal(np.array([1/6]*6), np.mean(samples, axis=0), 2)
def test_discrete_uniform_moments(self): """ Distributions: Checks that the discrete uniform dist. has the right moments. """ dist = DiscreteUniformDistribution(5) samples = dist.sample(200000).astype(float) assert_sigfigs_equal((2**10-1)/12, np.var(samples), 1) assert_sigfigs_equal(16, np.mean(samples), 1)
def test_constrained_sum_moments(self): """ Distributions: Tests that the contstraint is met in the constrained sum distribution. """ unif = UniformDistribution([[0,1],[0,1]]) dist = ConstrainedSumDistribution(unif, 1) samples = dist.sample(100000) assert_sigfigs_equal(np.array([1/2]*2), np.mean(samples, axis=0), 2)
def test_lognormal_moments(self): """ Distributions: Checks that the log normal distribution has the right moments. """ mu, sig = 3, 2 dist = LogNormalDistribution(mu=mu, sigma=sig) samples = dist.sample(150000) assert_sigfigs_equal(scipy.stats.lognorm.mean(1, 3, 2), samples.mean(), 1) assert_sigfigs_equal(np.round(scipy.stats.lognorm.var(1, 3, 2)), np.round(samples.var()), 1)
def test_assert_sigfigs_equal(self): """ Tests to make sure assert_sigfigs_equal only passes if the correct number of significant figures match """ # these are the same to 6 sigfigs assert_sigfigs_equal(np.array([3.141592]), np.array([3.141593]), 6) # these are only the same to 5 sigfigs self.assertRaises(AssertionError, assert_sigfigs_equal, np.array([3.14159]), np.array([3.14158]), 6) # these are the same to 3 sigfigs assert_sigfigs_equal(np.array([1729]), np.array([1728]), 3) # these are only the same to 3 sigfigs self.assertRaises(AssertionError, assert_sigfigs_equal, np.array([1729]), np.array([1728]), 4)
def test_lognormal_moments(self): """ Distributions: Checks that the log normal distribution has the right moments. """ mu, sig = 3, 2 dist = LogNormalDistribution(mu=mu, sigma=sig) samples = dist.sample(150000) assert_sigfigs_equal( scipy.stats.lognorm.mean(1,3,2), samples.mean(), 1) assert_sigfigs_equal( np.round(scipy.stats.lognorm.var(1,3,2)), np.round(samples.var()), 1)
def test_moments(self): """ Distributions: Tests the moment function (est_mean, etc) of ParticleDistribution. """ dim = 5 n_particles = 100000 # draw particles from a randomly chosen mutivariate normal mu = np.random.randn(dim) cov = np.random.randn(dim, dim) cov = np.dot(cov, cov.T) particle_locations = np.random.multivariate_normal( mu, cov, n_particles) particle_weights = np.random.rand(n_particles) dist = ParticleDistribution(particle_weights=particle_weights, particle_locations=particle_locations) assert_sigfigs_equal(mu, dist.est_mean(), 1) assert_almost_equal(dist.est_meanfn(lambda x: x**2), np.diag(cov) + mu**2, 0) assert (np.linalg.norm(dist.est_covariance_mtx() - cov) < 0.5)
def test_slantednormal_moments(self): """ Distributions: Checks that the slanted normal distribution has the right moments. """ ranges = [[0, 1], [0, 2], [2, 3]] weight = 2 dist = SlantedNormalDistribution(ranges=ranges, weight=weight) samples = dist.sample(150000) assert_sigfigs_equal(np.mean(np.array(ranges), axis=1), np.mean(samples, axis=0), 1) assert_sigfigs_equal(1 / 12 + 4, samples[:, 0].var(), 1) assert_sigfigs_equal(4 / 12 + 4, samples[:, 1].var(), 1) assert_sigfigs_equal(1 / 12 + 4, samples[:, 2].var(), 1)
def test_assert_sigfigs_equal(self): """ Tests to make sure assert_sigfigs_equal only passes if the correct number of significant figures match """ # these are the same to 6 sigfigs assert_sigfigs_equal( np.array([3.141592]), np.array([3.141593]), 6 ) # these are only the same to 5 sigfigs self.assertRaises( AssertionError, assert_sigfigs_equal, np.array([3.14159]), np.array([3.14158]), 6 ) # these are the same to 3 sigfigs assert_sigfigs_equal( np.array([1729]), np.array([1728]), 3 ) # these are only the same to 3 sigfigs self.assertRaises( AssertionError, assert_sigfigs_equal, np.array([1729]), np.array([1728]), 4 )
def test_slantednormal_moments(self): """ Distributions: Checks that the slanted normal distribution has the right moments. """ ranges = [[0,1],[0,2],[2,3]] weight = 2 dist = SlantedNormalDistribution(ranges=ranges, weight=weight) samples = dist.sample(150000) assert_sigfigs_equal( np.mean(np.array(ranges), axis=1), np.mean(samples, axis=0), 1) assert_sigfigs_equal(1/12+4, samples[:,0].var(), 1) assert_sigfigs_equal(4/12+4, samples[:,1].var(), 1) assert_sigfigs_equal(1/12+4, samples[:,2].var(), 1)