Changeset 144
- Timestamp:
- 04/09/08 16:46:00 (9 months ago)
- Files:
-
- projects/AsynCluster/trunk/svpmc/model.py (modified) (3 diffs)
- projects/AsynCluster/trunk/svpmc/sample.c (modified) (1 diff)
- projects/AsynCluster/trunk/svpmc/sample.py (modified) (2 diffs)
- projects/AsynCluster/trunk/svpmc/test/test_sample.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
projects/AsynCluster/trunk/svpmc/model.py
r143 r144 299 299 # (p x p) 300 300 301 'f', # Volatility shock concurrent c ross-correlations,302 # upper triangular of (p x p)301 'f', # Volatility shock concurrent correlations (vector with 302 # r01, r02,..., r0p, r12,..., r1p,...) 303 303 ] 304 304 … … 322 322 return self.cache['y'] 323 323 y = property(_get_y) 324 324 325 325 def _get_n(self): 326 326 return self.y.shape[1] … … 342 342 #--- Methods -------------------------------------------------------------- 343 343 344 def simulateVolatilities(self, v): 345 """ 346 """ 347 pass 344 345 348 346 349 347 projects/AsynCluster/trunk/svpmc/sample.c
r140 r144 77 77 } 78 78 79 80 81 // NormalWalk.correlate 82 // 83 // Supplied variables 84 // ---------------------------------------------------------------------------- 85 // y 2-D array of correlated random values (initially empty) 86 // x 2-D array of independent random values 87 // p 2-D array of cross-correlations between values in each column of x 88 89 int i, j, k; 90 double sum; 91 92 // For each column... 93 for(i=1; i<Nv[1]; i++) { 94 // For each time series... 95 for(i=0; i<Nv[0]; i++) { 96 // The offset plus the current shock... 97 sum = D1(i) + V2(i,j); 98 // ...plus the dot product for the VAR(1) term... 99 for(k=0; k<Nv[0]; k++) 100 sum += E2(i,k) * H2(k,j-1); 101 // ...is the modeled output 102 H2(i,j) = sum; 103 } 104 } projects/AsynCluster/trunk/svpmc/sample.py
r140 r144 21 21 22 22 import scipy as s 23 from scipy import random 23 from scipy import random, linalg 24 24 25 25 from weave import Weaver … … 91 91 "Wiggle array must have same dimensions as walker array") 92 92 return self.c('x', 'p', 'm', 'w', w=w) 93 94 def _covarMatrix(self, correlations): 95 """ 96 Returns a covariance matrix from the supplied vector of 97 I{correlations}, in the form used by L{correlate}. 98 """ 99 i = 0 100 p = self.x.shape[0] 101 c = s.ones((p, p)) 102 for j in xrange(p-1): 103 for k in xrange(j+1, p): 104 c[j, k] = c[k, j] = correlations[i] 105 i += 1 106 return c 107 108 def correlate(self, correlations=None): 109 """ 110 Returns a version of my current values correlated along the first axis, given 111 the supplied vector of concurrent I{correlations}, the elements of 112 which are in the following form:: 113 114 [r01, r02,..., r0p, r12,..., r1p,..] 115 116 The values in each column of the result are correlated. The values from 117 one column to the next remain independent. 118 119 The correlations vector can be omitted after the first call to this 120 method of a given instance of me. In such case, the last correlation 121 vector supplied will be re-used. 122 """ 123 if correlations: 124 self.c = linalg.cholesky( 125 self._covarMatrix(correlations), lower=True) 93 126 94 95 127 projects/AsynCluster/trunk/svpmc/test/test_sample.py
r140 r144 108 108 self.fig.add_subplot(111).plot(x1, x2) 109 109 110 def test_covarMatrix_2x2(self): 111 walker = sample.NormalWalk(2, 2) 112 x = walker._covarMatrix([0.5]) 113 y = s.array([[1, 0.5], [0.5, 1.0]]) 114 self.failUnless(s.equal(x, y).all()) 115 116 def test_covarMatrix_3x3(self): 117 walker = sample.NormalWalk(3, 67) 118 x = walker._covarMatrix([0.12, 0.13, 0.23, 0.24]) 119 y = s.array([ 120 [1.00, 0.12, 0.13], 121 [0.12, 1.00, 0.23], 122 [0.13, 0.23, 1.00]]) 123 self.failUnless(s.equal(x, y).all()) 124 125 def test_covarMatrix_4x4(self): 126 walker = sample.NormalWalk(4, 100) 127 x = walker._covarMatrix([0.12, 0.13, 0.14, 0.23, 0.24, 0.34]) 128 y = s.array([ 129 [1.00, 0.12, 0.13, 0.14], 130 [0.12, 1.00, 0.23, 0.24], 131 [0.13, 0.23, 1.00, 0.34], 132 [0.14, 0.24, 0.34, 1.00]]) 133 self.failUnless(s.equal(x, y).all())
