Changeset 139
- Timestamp:
- 04/05/08 17:11:44 (9 months ago)
- Files:
-
- projects/AsynCluster/trunk/svpmc/sample.py (modified) (2 diffs)
- projects/AsynCluster/trunk/svpmc/sample_NormalWalk.c (modified) (1 diff)
- projects/AsynCluster/trunk/svpmc/test/test_sample.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
projects/AsynCluster/trunk/svpmc/sample.py
r138 r139 81 81 updated array returned. 82 82 """ 83 m = 30 84 83 85 def __init__(self, rows, cols): 84 86 self.x = s.randn(rows, cols) 85 self. lp_x = 087 self.p = s.zeros_like(self.x) 86 88 87 89 def __call__(self, w): … … 89 91 raise ValueError( 90 92 "Wiggle array must have same dimensions as walker array") 91 return self.c('x', ' w', w=w)93 return self.c('x', 'p', 'm', 'w', w=w) 92 94 93 95 projects/AsynCluster/trunk/svpmc/sample_NormalWalk.c
r138 r139 22 22 23 23 // Supplied variables 24 // 'x', ' w'24 // 'x', 'p', 'm', 'w' 25 25 26 26 27 int i, j; 28 double xp, dlp, lp_xp; 29 double lp_x = 0; 27 int i, j, k; 28 double xp, dp, p_xp; 30 29 31 30 for(i=0; i<Nx[0]; i++) { 32 31 for(j=0; j<Nx[1]; j++) { 33 xp = X2(i,j) + W2(i,j)*normal(); 34 lp_xp = -0.5 * pow(xp, 2); 35 dlp = lp_xp - lp_x; 36 if(dlp > 0 || log(drand48()) < dlp) { 37 X2(i,j) = xp; 38 lp_x = lp_xp; 32 for(k=0; k<m; k++) { 33 xp = X2(i,j) + W2(i,j) * (drand48() - 0.5); 34 p_xp = -0.5 * pow(xp, 2); 35 dp = p_xp - P2(i,j); 36 if(dp > 0 || log(drand48()) < dp) { 37 X2(i,j) = xp; 38 P2(i,j) = p_xp; 39 } 39 40 } 40 41 } projects/AsynCluster/trunk/svpmc/test/test_sample.py
r138 r139 25 25 import sample 26 26 import util 27 28 29 VERBOSE = False 27 30 28 31 … … 68 71 69 72 class Test_NormalWalk(util.TestCase): 70 def test_univariate(self): 73 def check(self, x): 74 p = stats.normaltest(x)[1] 75 self.failUnless( 76 p > 0.05, "Deviation from normality with significance p=%f" % p) 77 if VERBOSE: 78 fig = self.fig 79 sp = fig.add_subplot(211) 80 sp.plot(x) 81 sp = fig.add_subplot(212) 82 sp.hist(x, bins=20) 83 84 def test_univariate_bigJumps(self): 85 walker = sample.NormalWalk(1, 1) 86 w = s.array([[1.0]]) 87 x = s.array([walker(w)[0,0] for k in xrange(200)]) 88 self.check(x) 89 90 def test_univariate_smallJumps(self): 71 91 walker = sample.NormalWalk(1, 1) 72 92 w = s.array([[0.1]]) 73 x = s.array([walker(w)[0,0] for k in xrange(100000)]) 74 fig = self.fig 75 sp = fig.add_subplot(211) 76 sp.plot(x) 77 sp = fig.add_subplot(212) 78 sp.hist(x, bins=20) 79 93 x = s.array([walker(w)[0,0] for k in xrange(30000)]) 94 self.check(x[::30]) 80 95 81 96
