Changeset 181
- Timestamp:
- 05/15/08 10:05:12 (7 months ago)
- Files:
-
- projects/AsynCluster/trunk/svpmc/model.c (modified) (10 diffs)
- projects/AsynCluster/trunk/svpmc/test/test_model.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
projects/AsynCluster/trunk/svpmc/model.c
r180 r181 100 100 // volatility shock v 101 101 PyArrayObject* ri; // Inverted cross-correlation matrix, innovation shocks 102 PyArrayObject* c;// Scaling constants for multivariate normal pdf, given g102 PyArrayObject* sc; // Scaling constants for multivariate normal pdf, given g 103 103 }; 104 104 … … 153 153 154 154 155 // Matrix multiplication x*y -> +z 156 // 157 // The square matrix 'x' is a PyArrayObject and 'y' and 'z' are 1-D arrays. The 158 // result is added to what's already in 'z'. 155 // Matrix multiplication x*y -> z, where the square matrix 'x' is a 156 // PyArrayObject and 'y' and 'z' are 1-D arrays. 159 157 void matrixMultiply(PyArrayObject* x, double *y, double *z) 160 158 { 161 159 int kr, km; 162 double val,sum;160 register double sum; 163 161 const int rows = (int) x->dimensions[0]; 164 162 // Compute the dot product for each row of x … … 169 167 for(km=0; km<rows; km++) 170 168 sum += PP2(x, kr, km) * PA1(y, km); 171 PA1(z, kr) += sum;169 PA1(z, kr) = sum; 172 170 } 173 171 } … … 180 178 int k; 181 179 const int N = (int) P->d->dimensions[0]; 182 for(k=0; k<N; k++)183 SPA1(S, h, k) = 0;184 180 185 181 // Run the VAR(1) process to get the current multivariate log-volatility … … 188 184 // Start with the matrix product for the VAR(1) term... 189 185 matrixMultiply(P->e, S->h, S->x0); 190 // then compute and addthe multivariate volatility shock given the proposed186 // ...then compute the multivariate volatility shock given the proposed 191 187 // normal variate... 192 matrixMultiply(P->rz, S->z, S->x 0);193 // ... plus the multivariate volatility offset188 matrixMultiply(P->rz, S->z, S->x1); 189 // ...then add them, plus the multivariate volatility offset, to be the new h 194 190 for(k=0; k<N; k++) 195 SPA1(S, x0, k) += SPP1(P, d, k); 196 // Now overwrite the previous log-volatility value 197 for(k=0; k<N; k++) 198 // TODO: Do this more efficiently by swapping pointers? 199 SPA1(S, h, k) = SPA1(S, x0, k); 200 } 201 202 203 // Computes and returns the log-likelihood of a multivariate innovation x given 204 // a 1-D array containing a multivariate log-volatility. 191 SPA1(S, h, k) = SPA1(S, x0, k) + SPA1(S, x1, k) + SPP1(P, d, k); 192 } 193 194 195 // Computes the log-likelihood of a multivariate innovation x given a 1-D array 196 // containing a multivariate log-volatility. 205 197 void logLikelihood(struct sample *S, struct parameters *P, double *x) 206 198 { 207 int k;199 register int k; 208 200 double *y; 209 201 double val; … … 235 227 // exponential being done in log space by subtraction of the log of the 236 228 // scaling coefficient 237 val = SPA1(S, x0, k) / SPP2(P, c, k, 0);238 L -= 0.5 * val*val + SPP2(P, c, k, 1); // Fast squaring instead of pow()229 val = SPA1(S, x0, k) / SPP2(P, sc, k, 0); 230 L -= 0.5 * val*val + SPP2(P, sc, k, 1); // Fast squaring instead of pow() 239 231 // Since the innovation has been inverse-scaled by the square root of the 240 232 // proposed volatility, the probability density needs to be scaled as … … 244 236 245 237 // All done 246 S .L= L;238 S->Lx = L; 247 239 } 248 240 … … 250 242 // Returns with zero only if the proposals are close enough to finish 251 243 // likelihood computation 252 int notCloseEnough(struct sample *sp , int n)244 int notCloseEnough(struct sample *sp[], int n) 253 245 { 254 246 int k; 255 247 double L; 256 double Lmin = + DBL_MAX; // Anything will be less positive257 double Lmax = - DBL_MAX; // Anything will be more positive248 double Lmin = +1E20; // Anything will be less positive 249 double Lmax = -1E20; // Anything will be more positive 258 250 for(k=0; k<n; k++) { 259 L = PA1(sp, k).Lx;251 L = sp[k]->Lx; 260 252 if(L < Lmin) 261 253 Lmin = L; … … 288 280 // rz Concurrent xcorr, innovation-volatility normals, [pp] 289 281 // ri Inverse concurrent xcorr, innovation shocks, [pp] 290 // cConstants for multivariate normal PDF282 // sc Constants for multivariate normal PDF 291 283 292 284 //--- Return values (tuple) --- … … 307 299 // Generate parameter struct 308 300 P.d = d_array; P.e = e_array; P.g = g_array; 309 P.rz = rz_array; P.ri = ri_array; P. c =c_array;301 P.rz = rz_array; P.ri = ri_array; P.sc = sc_array; 310 302 311 303 // Initialize various arrays projects/AsynCluster/trunk/svpmc/test/test_model.py
r179 r181 354 354 355 355 356 class Test_Model_logVol atilities_VAR(BaseCase):356 class Test_Model_logVol(BaseCase): 357 357 N = 1000 358 359 def _draw_h(self, modelObj): 360 modelObj.cs = s.array([1.0, 1.0]) 361 modelObj.cr = [0.0] 362 modelObj.g = s.array([0.0]) 363 z = s.randn(modelObj.p, modelObj.n) 364 v = s.array(self._rv(modelObj.fs, modelObj.fr) * z) 365 return modelObj.logVolatilities( 366 z, v, s.zeros_like(z), modelObj.d.copy())[2] 358 359 code = """ 360 int k; 361 struct sample S; 362 struct parameters P; 363 364 P.d = d_array; P.e = e_array; P.rz = rz_array; 365 S.z = (double *) malloc(sizeof(double) * Nz[0]); 366 S.h = (double *) calloc(sizeof(double), Nz[0]); 367 S.x0 = (double *) malloc(sizeof(double) * Nz[0]); 368 S.x1 = (double *) malloc(sizeof(double) * Nz[0]); 369 for(k=0; k<Nz[0]; k++) 370 PA1(S.z, k) = Z1(k); 371 372 logVol(&S, &P); 373 374 for(k=0; k<Nz[0]; k++) 375 H1(k) = PA1(S.h, k); 376 """ 377 378 def _draw_h(self, z, d, e, fs, fr): 379 h = s.zeros_like(z) 380 rz = s.array(self._rv(fs, fr)) 381 for k, zk in enumerate(z.transpose()): 382 self.inline( 383 self.code, z=zk, 384 d=s.array(d), e=s.array(e), h=h[:,k], rz=rz) 385 return h 367 386 368 387 def test_univariate_LPF(self): 369 388 e = 0.95 370 389 # Univariate model 371 modelObj = self.modelFactory(372 y = s.zeros((1, self.N)),373 cs = [1.0],374 cr = [],375 d = s.array([0.0]),376 e = s.array([[e]]),377 g = s.array([0.0]))378 390 z = s.randn(1, self.N) 379 h = modelObj.logVolatilities( 380 z, z.copy(), s.zeros_like(z), s.array([0.0]))[2] 391 h = self._draw_h(z, d=[1], e=[[e]], fs=[1], fr=[]) 381 392 # IIR filtering 382 393 self._check_psd(h, e)
