def test_seed_different(self):
     """
     Test different seed -> different result.
     """
     npoints = 50000
     res,error = mcimport(lambda x: x<1.0,npoints,exponential,seed=[1234,5678])
     res2, error2 = mcimport(lambda x: x<1.0,npoints,exponential,seed=[1235,5678])
     assert res != res2
     assert error != error2
 def test_seed(self):
     """
     Test same seed -> same result.
     """
     npoints = 50000
     res,error = mcimport(lambda x: x<1.0,npoints,exponential,seed=[1234,5678])
     res2, error2 = mcimport(lambda x: x<1.0,npoints,exponential,seed=[1234,5678])
     assert res == res2
     assert error == error2
 def run_parallel(self,f,npoints,distribution,expected_value,expected_variance,**kwargs):
     res,sd = mcimport(f,npoints,distribution,nprocs=2,**kwargs)
     error = np.sqrt(expected_variance/float(npoints))
     assert_within_tol(res,expected_value,3.*max(error,1e-10),
         "Error in <f> in parallel run.")
     assert_within_tol(sd,error,0.1*max(error,1e-10),
         "Error in expected error in parallel run.")
 def test_ret_arr(self):
     """
     Test an integrand that returns an array.
     """
     func = lambda x: np.array((x**2,x**3))
     npoints = 2000
     (res_sq, res_cb), (sd_sq, sd_cb) = mcimport(func,npoints,distribution=exponential,
             nprocs=1, seed=123456)
     res_sq2, sd_sq2 = mcimport(lambda x: x**2,npoints,distribution=exponential,
             nprocs=1,seed=123456)
     res_cb2, sd_cb2 = mcimport(lambda x: x**3,npoints,distribution=exponential,
             nprocs=1,seed=123456)
     assert_almost_equal(res_sq, res_sq2)
     assert_almost_equal(res_cb, res_cb2)
     assert_almost_equal(sd_sq, sd_sq2)
     assert_almost_equal(sd_sq, sd_sq2)
 def test_ret_arr_args(self):
     """
     Test an integrand that returns an array with an argument.
     """
     func = lambda x, a,b : np.array((a*x**2,b*x**3))
     npoints = 2000
     aval, bval = 4.,5.
     (res_sq, res_cb), (sd_sq, sd_cb) = mcimport(func,npoints,distribution=exponential,
             nprocs=1,seed=123456,args=(aval,bval))
     res_sq2, sd_sq2 = mcimport(lambda x,a: a*x**2,npoints,distribution=exponential,
             nprocs=1,seed=123456,args=(aval,))
     res_cb2, sd_cb2 = mcimport(lambda x,b: b*x**3,npoints,distribution=exponential,
             nprocs=1,seed=123456,args=(bval,))
     assert_almost_equal(res_sq, res_sq2)
     assert_almost_equal(res_cb, res_cb2)
     assert_almost_equal(sd_sq, sd_sq2)
     assert_almost_equal(sd_sq, sd_sq2)
    def run_check_seeded_distribution(self,f,ntrials,*args,**kwargs):
        """
        Check that the results returned by integrating f are normally distributed.

        Seeds each trial with the trial number.
        """
        import scipy.stats
        results, errors = [], []
        for itrial in range(ntrials):
            res, err = mcimport(f,*args,seed=itrial,**kwargs)
            results.append(res)
            errors.append(err)
        results = np.array(results).flatten()
        w,p = scipy.stats.shapiro(results)
        self.assertGreater(p,0.1)