def test_correlated_scatter(self): n = 50 r = (sp.arange(n, dtype=float) + 10.0 * n) / 10.0 * n data = sp.sin(sp.arange(n)) * r amp = 25.0 theory = data / amp # Generate correlated matrix. C = random.rand(n, n) # [0, 1) # Raise to high power to make values near 1 rare. C = (C**10) * 0.2 C = (C + C.T) / 2.0 C += sp.identity(n) C *= r[:, None] / 2.0 C *= r[None, :] / 2.0 # Generate random numbers in diagonal frame. h, R = linalg.eigh(C) self.assertTrue(sp.alltrue(h > 0)) rand_vals = random.normal(size=n) * sp.sqrt(h) # Rotate back. data += sp.dot(R.T, rand_vals) out = utils.ampfit(data, C, theory) a, s = out['amp'], out['error'] self.assertTrue(sp.allclose(a, amp, atol=5.0 * s, rtol=0)) # Expect the next line to fail 1/100 trials. self.assertFalse(sp.allclose(a, amp, atol=0.01 * s, rtol=0))
def test_uncorrelated_noscatter(self): data = sp.arange(10, dtype=float) theory = data / 2.0 C = sp.identity(10) out = utils.ampfit(data, C, theory) a, s = out['amp'], out['error'] self.assertAlmostEqual(a, 2)
def test_correlated_scatter(self) : n = 50 r = (sp.arange(n, dtype=float) + 10.0*n)/10.0*n data = sp.sin(sp.arange(n)) * r amp = 25.0 theory = data/amp # Generate correlated matrix. C = random.rand(n, n) # [0, 1) # Raise to high power to make values near 1 rare. C = (C**10) * 0.2 C = (C + C.T)/2.0 C += sp.identity(n) C *= r[:, None]/2.0 C *= r[None, :]/2.0 # Generate random numbers in diagonal frame. h, R = linalg.eigh(C) self.assertTrue(sp.alltrue(h>0)) rand_vals = random.normal(size=n)*sp.sqrt(h) # Rotate back. data += sp.dot(R.T, rand_vals) out = utils.ampfit(data, C, theory) a, s = out['amp'], out['error'] self.assertTrue(sp.allclose(a, amp, atol=5.0*s, rtol=0)) # Expect the next line to fail 1/100 trials. self.assertFalse(sp.allclose(a, amp, atol=0.01*s, rtol=0))
def test_uncorrelated_noscatter(self): data = sp.arange(10, dtype=float) theory = data/2.0 C = sp.identity(10) out = utils.ampfit(data, C, theory) a, s = out['amp'], out['error'] self.assertAlmostEqual(a, 2)
def test_uncorrelated_noscatter_error(self): data = sp.arange(10, dtype=float) + 1.0 amp = 5.0 theory = data/amp C = sp.diag((sp.arange(10, dtype=float) + 1.0)**2) a, s = utils.ampfit(data, C, theory) self.assertAlmostEqual(a, amp) self.assertAlmostEqual(s/a, 1.0/sp.sqrt(10))
def test_uncorrelated_noscatter_error(self): data = sp.arange(10, dtype=float) + 1.0 amp = 5.0 theory = data / amp C = sp.diag((sp.arange(10, dtype=float) + 1.0)**2) out = utils.ampfit(data, C, theory) a, s = out['amp'], out['error'] self.assertAlmostEqual(a, amp) self.assertAlmostEqual(s / a, 1.0 / sp.sqrt(10))
def test_uncorrelated_scatter_error(self): n = 1000 data = sp.arange(n, dtype=float) + 1.0 amp = 5.0 theory = data/amp C = sp.diag((sp.arange(n, dtype=float) + 1.0)**2) data += random.normal(size=n)*data a, s = utils.ampfit(data, C, theory) self.assertTrue(sp.allclose(a, amp, rtol=5.0/sp.sqrt(n), atol=0)) # Expect the next line to fail 1/100 trials. self.assertFalse(sp.allclose(a, amp, rtol=0.01/sp.sqrt(n), atol=0)) self.assertAlmostEqual(s, amp/sp.sqrt(1000))
def test_uncorrelated_scatter_error(self): n = 1000 data = sp.arange(n, dtype=float) + 1.0 amp = 5.0 theory = data / amp C = sp.diag((sp.arange(n, dtype=float) + 1.0)**2) data += random.normal(size=n) * data out = utils.ampfit(data, C, theory) a, s = out['amp'], out['error'] self.assertTrue(sp.allclose(a, amp, rtol=5.0 / sp.sqrt(n), atol=0)) # Expect the next line to fail 1/100 trials. self.assertFalse(sp.allclose(a, amp, rtol=0.01 / sp.sqrt(n), atol=0)) self.assertAlmostEqual(s, amp / sp.sqrt(1000))