Changeset 988
- Timestamp:
- 10/26/08 20:43:42 (2 months ago)
- Files:
-
- branches/lebrun/devel/lib/src/Base/Type/Lapack.hxx (modified) (1 diff)
- branches/lebrun/devel/lib/src/Base/Type/MatrixImplementation.cxx (modified) (2 diffs)
- branches/lebrun/devel/lib/src/Base/Type/MatrixImplementation.hxx (modified) (1 diff)
- branches/lebrun/devel/lib/src/Base/Type/SquareMatrix.cxx (modified) (1 diff)
- branches/lebrun/devel/lib/src/Base/Type/SquareMatrix.hxx (modified) (1 diff)
- branches/lebrun/devel/lib/src/Base/Type/SymmetricMatrix.cxx (modified) (3 diffs)
- branches/lebrun/devel/lib/src/Base/Type/SymmetricMatrix.hxx (modified) (1 diff)
- branches/lebrun/devel/lib/src/Uncertainty/Distribution/ExtraFunc/DistFunc.cxx (modified) (4 diffs)
- branches/lebrun/devel/lib/src/Uncertainty/Distribution/ExtraFunc/DistFunc.hxx (modified) (1 diff)
- branches/lebrun/devel/lib/test/t_FittingTest_std.at (modified) (1 diff)
- branches/lebrun/devel/python/test/t_FittingTest_std.atpy (modified) (1 diff)
- branches/lebrun/devel/utils/rotRPackage_1.4.3.tar.gz (deleted)
- branches/lebrun/devel/utils/rotRPackage_1.4.4.tar.gz (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/lebrun/devel/lib/src/Base/Type/Lapack.hxx
r867 r988 50 50 y := alpha*A(trans)*x + beta*y */ 51 51 #define DSYMV_F77 F77_FUNC(dsymv,DSYMV) 52 void DSYMV_F77(char *uplo, int *n, double *alpha, double *a, double *x, int *incx, double *beta, double *y, int *incy, int *luplo);52 void DSYMV_F77(char *uplo, int *n, double *alpha, double *a, int *lda, double *x, int *incx, double *beta, double *y, int *incy, int *luplo); 53 53 54 54 /** Function dspmv is to be used to compute the product of a matrix with a vector (numerical point); optimization for positive definite matrices branches/lebrun/devel/lib/src/Base/Type/MatrixImplementation.cxx
r975 r988 251 251 double beta(0.0); 252 252 int luplo(1); 253 DSYMV_F77(&uplo, &n, &alpha, const_cast<double*>(&((*this)[0])), const_cast<double*>(&(pt[0])), &one, &beta, &prod[0], &one, &luplo);253 DSYMV_F77(&uplo, &n, &alpha, const_cast<double*>(&((*this)[0])), &n, const_cast<double*>(&(pt[0])), &one, &beta, &prod[0], &one, &luplo); 254 254 255 255 return prod; … … 276 276 } 277 277 278 /* Integer power, general matrix */ 279 MatrixImplementation MatrixImplementation::genPower(const UnsignedLong n) 280 { 281 Bool first(true); 282 UnsignedLong exponent(n); 283 MatrixImplementation y; 284 MatrixImplementation z(*this); 285 while (exponent > 0) 286 { 287 // t is the right bit of exponent 288 const UnsignedLong t(exponent % 2); 289 // remove last bit from exponent 290 exponent /= 2; 291 // if right bit is 1 292 if (t != 0) 293 { 294 // if it is the rightmost bit equals to 1 295 if (first) 296 { 297 first = false; 298 y = z; 299 } 300 else y = y.genProd(z); 301 // if no more bit to consider 302 if (exponent == 0) return y; 303 } 304 // Square the contribution 305 z = z.genProd(z); 306 } 307 return y; 308 } 309 310 /* Integer power, symmetric matrix */ 311 MatrixImplementation MatrixImplementation::symPower(const UnsignedLong n) 312 { 313 Bool first(true); 314 UnsignedLong exponent(n); 315 MatrixImplementation y; 316 MatrixImplementation z(*this); 317 while (exponent > 0) 318 { 319 // t is the right bit of exponent 320 const UnsignedLong t(exponent % 2); 321 // remove last bit from exponent 322 exponent /= 2; 323 // if right bit is 1 324 if (t != 0) 325 { 326 // if it is the rightmost bit equals to 1 327 if (first) 328 { 329 first = false; 330 y = z; 331 } 332 else y = y.symProd(z, 'L'); 333 // if no more bit to consider 334 if (exponent == 0) return y; 335 } 336 // Square the contribution 337 z = z.symProd(z, 'L'); 338 } 339 return y; 340 } 341 278 342 /* Empty returns true if there is no element in the MatrixImplementation */ 279 343 const Bool MatrixImplementation::isEmpty() const branches/lebrun/devel/lib/src/Base/Type/MatrixImplementation.hxx
r975 r988 132 132 const char symSide) const throw(InvalidDimensionException); 133 133 134 /** MatrixImplementation integer power */ 135 MatrixImplementation genPower(const UnsignedLong n); 136 MatrixImplementation symPower(const UnsignedLong n); 137 134 138 /** Multiplications with a NumericalPoint (must have consistent dimensions) */ 135 139 NumericalPoint genVectProd (const NumericalPoint & pt) const throw(InvalidDimensionException); branches/lebrun/devel/lib/src/Base/Type/SquareMatrix.cxx
r975 r988 155 155 } 156 156 157 /* SquareMatrix integer power */ 158 SquareMatrix SquareMatrix::power(const UnsignedLong n) 159 { 160 return SquareMatrix(new MatrixImplementation(getImplementation()->genPower(n))); 161 } 157 162 158 163 /* Resolution of a linear system */ branches/lebrun/devel/lib/src/Base/Type/SquareMatrix.hxx
r975 r988 103 103 SquareMatrix operator * (const IdentityMatrix & m) const throw(InvalidDimensionException); 104 104 105 /** SquareMatrix integer power */ 106 SquareMatrix power(const UnsignedLong n); 107 105 108 /** Multiplication with a NumericalPoint (must have consistent dimensions) */ 106 109 NumericalPoint operator * (const NumericalPoint & p) const throw(InvalidDimensionException); branches/lebrun/devel/lib/src/Base/Type/SymmetricMatrix.cxx
r975 r988 170 170 SymmetricMatrix SymmetricMatrix::operator * (const SymmetricMatrix & m) const throw(InvalidDimensionException) 171 171 { 172 checkSymmetry();172 m.checkSymmetry(); 173 173 return SymmetricMatrix(new MatrixImplementation(getImplementation()->symProd(*(m.getImplementation()), 'L') )); 174 174 } … … 183 183 NumericalPoint SymmetricMatrix::operator * (const NumericalPoint & pt) const throw(InvalidDimensionException) 184 184 { 185 checkSymmetry(); 186 return getImplementation()->genVectProd(pt) ; 185 return getImplementation()->symVectProd(pt) ; 187 186 } 188 187 … … 198 197 { 199 198 return SymmetricMatrix(new MatrixImplementation(*getImplementation() / s )); 199 } 200 201 /* SquareMatrix integer power */ 202 SymmetricMatrix SymmetricMatrix::power(const UnsignedLong n) 203 { 204 return SymmetricMatrix(new MatrixImplementation(getImplementation()->symPower(n))); 200 205 } 201 206 branches/lebrun/devel/lib/src/Base/Type/SymmetricMatrix.hxx
r975 r988 116 116 SymmetricMatrix operator * (const IdentityMatrix & m) const throw(InvalidDimensionException); 117 117 118 /** SymmetricMatrix integer power */ 119 SymmetricMatrix power(const UnsignedLong n); 120 118 121 /** Multiplication with a NumericalPoint (must have consistent dimensions) */ 119 122 NumericalPoint operator * (const NumericalPoint & p) const throw(InvalidDimensionException); branches/lebrun/devel/lib/src/Uncertainty/Distribution/ExtraFunc/DistFunc.cxx
r983 r988 32 32 #include "Log.hxx" 33 33 #include "Nct.hxx" 34 #include "SquareMatrix.hxx" 34 35 35 36 #define USE_DCDFLIB … … 47 48 48 49 typedef Base::Stat::RandomGenerator RandomGenerator; 50 typedef Base::Type::SquareMatrix SquareMatrix; 49 51 typedef Base::Common::Log Log; 50 52 … … 269 271 } // End of rGamma 270 272 271 /*********************************************/ 272 /* Kolmogorov distribution */ 273 /*********************************************/ 274 static double K(int n, double d); 275 static void m_multiply(double *A, double *B, double *C, int m); 276 static void m_power(double *A, int eA, double *V, int *eV, int m, int n); 277 static double 278 K(int n, double d) 279 { 280 /* Compute Kolmogorov's distribution. 281 Code published in 273 /****************************/ 274 /* Kolmogorov distribution. */ 275 /****************************/ 276 /* CDF 277 Adapted from: 282 278 George Marsaglia and Wai Wan Tsang and Jingbo Wang (2003), 283 279 "Evaluating Kolmogorov's distribution". 284 280 Journal of Statistical Software, Volume 8, 2003, Issue 18. 285 281 URL: http://www.jstatsoft.org/v08/i18/. 286 */ 287 288 int k, m, i, j, g, eH, eQ; 289 double h, s, *H, *Q; 290 291 k = (int) (n * d) + 1; 292 m = 2 * k - 1; 293 h = k - n * d; 294 H = (double*) calloc(m * m, sizeof(double)); 295 Q = (double*) calloc(m * m, sizeof(double)); 296 for(i = 0; i < m; i++) 297 for(j = 0; j < m; j++) 298 if(i - j + 1 < 0) 299 H[i * m + j] = 0; 300 else 301 H[i * m + j] = 1; 302 for(i = 0; i < m; i++) { 303 H[i * m] -= pow(h, i + 1); 304 H[(m - 1) * m + i] -= pow(h, (m - i)); 305 } 306 H[(m - 1) * m] += ((2 * h - 1 > 0) ? pow(2 * h - 1, m) : 0); 307 for(i = 0; i < m; i++) 308 for(j=0; j < m; j++) 309 if(i - j + 1 > 0) 310 for(g = 1; g <= i - j + 1; g++) 311 H[i * m + j] /= g; 312 eH = 0; 313 m_power(H, eH, Q, &eQ, m, n); 314 s = Q[(k - 1) * m + k - 1]; 315 for(i = 1; i <= n; i++) { 316 s = s * i / n; 317 if(s < 1e-140) { 318 s *= 1e140; 319 eQ -= 140; 320 } 321 } 322 s *= pow(10., eQ); 323 free(H); 324 free(Q); 325 return(s); 326 } 327 328 static void 329 m_multiply(double *A, double *B, double *C, int m) 330 { 331 /* Auxiliary routine used by K(). 332 Matrix multiplication. 333 */ 334 int i, j, k; 335 double s; 336 for(i = 0; i < m; i++) 337 for(j = 0; j < m; j++) { 338 s = 0.; 339 for(k = 0; k < m; k++) 340 s+= A[i * m + k] * B[k * m + j]; 341 C[i * m + j] = s; 342 } 343 } 344 345 static void 346 m_power(double *A, int eA, double *V, int *eV, int m, int n) 347 { 348 /* Auxiliary routine used by K(). 349 Matrix power. 350 */ 351 double *B; 352 int eB , i; 353 354 if(n == 1) { 355 for(i = 0; i < m * m; i++) 356 V[i] = A[i]; 357 *eV = eA; 358 return; 359 } 360 m_power(A, eA, V, eV, m, n / 2); 361 B = (double*) calloc(m * m, sizeof(double)); 362 m_multiply(V, V, B, m); 363 eB = 2 * (*eV); 364 if((n % 2) == 0) { 365 for(i = 0; i < m * m; i++) 366 V[i] = B[i]; 367 *eV = eB; 368 } 369 else { 370 m_multiply(A, B, V, m); 371 *eV = eA + eB; 372 } 373 if(V[(m / 2) * m + (m / 2)] > 1e140) { 374 for(i = 0; i < m * m; i++) 375 V[i] = V[i] * 1e-140; 376 *eV += 140; 377 } 378 free(B); 379 } 282 */ 380 283 NumericalScalar DistFunc::pKolmogorov(const UnsignedLong n, const NumericalScalar x, const Bool tail) 381 284 { … … 384 287 // If x >= 1, we are at the right of the support 385 288 if (x >= 1.0) return (tail ? 0.0 : 1.0); 386 // // We use the summation formula of R. Von Mises 387 // const UnsignedLong kmax(floor(n * (1.0 - x))); 388 // NumericalScalar sum(pow(1.0 - x, n) / x); 389 // NumericalScalar logBinomial(log(n)); 390 // for (UnsignedLong k = 1; k <= kmax; ++k) 391 // { 392 // const NumericalScalar term(x + k / static_cast<NumericalScalar>(n)); 393 // sum += exp(logBinomial + (k - 1) * log(term) + (n - k) * log(1.0 - term)); 394 // logBinomial += log(n - k) - log (k + 1); 395 // } 396 // NumericalScalar result((tail ? 2.0 * x * sum : 1.0 - 2.0 * x * sum)); 397 NumericalScalar result2((tail ? 1.0 - K(n, x) : K(n, x))); 398 return result2; 399 } 400 401 NumericalScalar DistFunc::pKolmogorovAsymptotic(const UnsignedLong n, const NumericalScalar x, const Bool tail) 289 const NumericalScalar s(x * x * n); 290 // Less accurate (7 digits only) but much faster in the right tail 291 if (s > 7.14 || (s > 3.76 && n > 99)) 292 { 293 // Here, result is the complementary CDF 294 const NumericalScalar result(2 * exp(-(2.000071 + 0.331 / sqrt(n) + 1.409 / n) * s)); 295 return (tail ? result : 1.0 - result); 296 } 297 // Now, the exact computation 298 const UnsignedLong k(n * x + 1); 299 const UnsignedLong m(2 * k - 1); 300 SquareMatrix H(m); 301 // Regular part of H 302 const NumericalScalar factor(exp(SpecFunc::LnGamma(n + 1.0) / n - log(n))); 303 for (UnsignedLong j = 0; j < m; ++j) 304 { 305 UnsignedLong k(1); 306 NumericalScalar hij(factor); 307 for (UnsignedLong i = j; i < m; ++i) 308 { 309 hij /= k; 310 H(i, j) = hij; 311 ++k; 312 } 313 } 314 // Borders and upper diagonal of H 315 const NumericalScalar h(k - n * x); 316 NumericalScalar hi(h); 317 for (UnsignedLong i = 1; i < m; ++i) 318 { 319 // Upper diagonal 320 H(i - 1, i) = factor; 321 // Left border 322 H(i - 1, 0) *= (1.0 - hi); 323 // Bottom border 324 H(m - 1, m - i) *= (1.0 - hi); 325 hi *= h; 326 } 327 // Left bottom corner, now hi = h^m 328 H(m - 1, 0) *= (h > 0.5 ? 1.0 - 2.0 * hi + pow(2.0 * h - 1.0, m) : 1.0 - 2.0 * hi); 329 // Here, result is the CDF 330 const NumericalScalar result(H.power(n).operator()(k - 1, k - 1)); 331 return (tail ? 1.0 - result : result); 332 } 333 334 NumericalScalar DistFunc::pKolmogorovAsymptotic(const NumericalScalar x, const Bool tail) 402 335 { 403 336 // If x <= 0, we are at the left of the support branches/lebrun/devel/lib/src/Uncertainty/Distribution/ExtraFunc/DistFunc.hxx
r983 r988 62 62 // For Kolmogorov distribution 63 63 static NumericalScalar pKolmogorov(const UnsignedLong n, const NumericalScalar x, const Bool tail = false); 64 static NumericalScalar pKolmogorovAsymptotic(const UnsignedLong n, constNumericalScalar x, const Bool tail = false);64 static NumericalScalar pKolmogorovAsymptotic(const NumericalScalar x, const Bool tail = false); 65 65 // For NonCentralStudent distribution 66 66 static NumericalScalar dNonCentralStudent(const NumericalScalar nu, const NumericalScalar delta, const NumericalScalar x); branches/lebrun/devel/lib/test/t_FittingTest_std.at
r983 r988 11 11 best model Kolmogorov=class=Beta name=Beta dimension=1 r=1.14515 t=2.39684 a=-1.39122 b=2.45155 12 12 resultBIC=class=SquareMatrix dimension=14 implementation=class=MatrixImplementation name=Unnamed rows=14 columns=14 values=[-0.506611,inf,inf,inf,inf,inf,inf,inf,inf,inf,inf,inf,inf,inf,inf,0.390135,inf,inf,inf,inf,inf,26.4376,inf,inf,inf,inf,inf,inf,4.94401,3.40566,3.36873,9.04993,7.59532,36.5729,3.45423,15.9133,4.0612,2.44214,2.97694,3.35073,3.97803,2.82863,inf,3.02206,inf,4.87206,inf,inf,inf,7.11503,inf,inf,4.24283,inf,inf,inf,2.84271,5.25018,4.44992,10.9583,3.6054,4.34917,3.02071,17.9129,3.54591,2.94694,4.65243,3.81864,3.97857,3.81633,3.25977,4.78199,4.40705,14.8314,3.72037,4.27123,3.35483,23.6423,3.66558,3.31367,4.49168,3.98667,4.03917,3.82167,1.57993,inf,inf,inf,inf,inf,1.96017,inf,inf,1.79553,inf,inf,inf,3.82754,26.8191,20.1943,22.0736,15.3766,25.307,25.7348,25.4331,3.16515,24.2971,25.0307,21.3014,23.5995,23.4314,23.1683,2.98858,inf,inf,inf,inf,inf,2.52017,inf,2.95261,2.20348,inf,inf,inf,2.84408,inf,inf,inf,inf,inf,inf,inf,inf,inf,0,inf,inf,inf,inf,inf,2.88821,inf,8.7561,inf,inf,inf,15.912,inf,inf,2.01493,inf,inf,inf,inf,inf,inf,inf,inf,inf,inf,inf,inf,inf,inf,2.81418,inf,3.14689,inf,inf,inf,inf,inf,inf,inf,inf,inf,inf,inf,3.28126,3.43457,3.06783,inf,inf,inf,inf,inf,inf,inf,inf,inf,inf,inf,inf,inf,1.95746] 13 resultKolmogorov=class=SquareMatrix dimension=11 implementation=class=MatrixImplementation name=Unnamed rows=11 columns=11 values=[0.380754,0,0,0,0,0,0,0,0,0,0,0,0.101178,0,0,0,0,0,0,0,0,0,0,0,0.935178,0,0,0,0,0,6.0 3204e-05,0,0,0,0,0,0.261505,0,0,0,0,0,0,0,0,0,0,0,0.102296,0.488213,0,0,0,0,0,0,0,0,0,0.0115789,0.979805,0,0,0,0,0,0,0,0,0,0.00855122,0,0.256542,0,0,0,0,0,0,0,0,0,0,0,0.515105,0,0,0,0,0,0,0,0.000639599,0,7.75439e-06,0,0.268376,0,0,0,0,0,0,0,0,0,0,0,0.556345,0,0,0,0,0,0,0,0,0,0,0,0.640406]13 resultKolmogorov=class=SquareMatrix dimension=11 implementation=class=MatrixImplementation name=Unnamed rows=11 columns=11 values=[0.380754,0,0,0,0,0,0,0,0,0,0,0,0.101178,0,0,0,0,0,0,0,0,0,0,0,0.935178,0,0,0,0,0,6.07492e-05,0,0,0,0,0,0.261505,0,0,0,0,0,0,0,0,0,0,0,0.102296,0.488213,0,0,0,0,0,0,0,0,0,0.0115789,0.979805,0,0,0,0,0,0,0,0,0,0.00855122,0,0.256542,0,0,0,0,0,0,0,0,0,0,0,0.515105,0,0,0,0,0,0,0,0.000639709,0,7.92517e-06,0,0.268376,0,0,0,0,0,0,0,0,0,0,0,0.556345,0,0,0,0,0,0,0,0,0,0,0,0.640406] 14 14 resultChiSquared=class=SquareMatrix dimension=2 implementation=class=MatrixImplementation name=Unnamed rows=2 columns=2 values=[0,0,0,0.892467] 15 15 ]], branches/lebrun/devel/python/test/t_FittingTest_std.atpy
r983 r988 11 11 best model Kolmogorov= class=Beta name=Beta dimension=1 r=1.14515 t=2.39684 a=-1.39122 b=2.45155 12 12 resultBIC= class=SquareMatrix dimension=14 implementation=class=MatrixImplementation name=Unnamed rows=14 columns=14 values=[-0.506611,inf,inf,inf,inf,inf,inf,inf,inf,inf,inf,inf,inf,inf,inf,0.390135,inf,inf,inf,inf,inf,26.4376,inf,inf,inf,inf,inf,inf,4.94401,3.40566,3.36873,9.04993,7.59532,36.5729,3.45423,15.9133,4.0612,2.44214,2.97694,3.35073,3.97803,2.82863,inf,3.02206,inf,4.87206,inf,inf,inf,7.11503,inf,inf,4.24283,inf,inf,inf,2.84271,5.25018,4.44992,10.9583,3.6054,4.34917,3.02071,17.9129,3.54591,2.94694,4.65243,3.81864,3.97857,3.81633,3.25977,4.78199,4.40705,14.8314,3.72037,4.27123,3.35483,23.6423,3.66558,3.31367,4.49168,3.98667,4.03917,3.82167,1.57993,inf,inf,inf,inf,inf,1.96017,inf,inf,1.79553,inf,inf,inf,3.82754,26.8191,20.1943,22.0736,15.3766,25.307,25.7348,25.4331,3.16515,24.2971,25.0307,21.3014,23.5995,23.4314,23.1683,2.98858,inf,inf,inf,inf,inf,2.52017,inf,2.95261,2.20348,inf,inf,inf,2.84408,inf,inf,inf,inf,inf,inf,inf,inf,inf,0,inf,inf,inf,inf,inf,2.88821,inf,8.7561,inf,inf,inf,15.912,inf,inf,2.01493,inf,inf,inf,inf,inf,inf,inf,inf,inf,inf,inf,inf,inf,inf,2.81418,inf,3.14689,inf,inf,inf,inf,inf,inf,inf,inf,inf,inf,inf,3.28126,3.43457,3.06783,inf,inf,inf,inf,inf,inf,inf,inf,inf,inf,inf,inf,inf,1.95746] 13 resultKolmogorov= class=SquareMatrix dimension=11 implementation=class=MatrixImplementation name=Unnamed rows=11 columns=11 values=[0.380754,0,0,0,0,0,0,0,0,0,0,0,0.101178,0,0,0,0,0,0,0,0,0,0,0,0.935178,0,0,0,0,0,6.0 3204e-05,0,0,0,0,0,0.261505,0,0,0,0,0,0,0,0,0,0,0,0.102296,0.488213,0,0,0,0,0,0,0,0,0,0.0115789,0.979805,0,0,0,0,0,0,0,0,0,0.00855122,0,0.256542,0,0,0,0,0,0,0,0,0,0,0,0.515105,0,0,0,0,0,0,0,0.000639599,0,7.75439e-06,0,0.268376,0,0,0,0,0,0,0,0,0,0,0,0.556345,0,0,0,0,0,0,0,0,0,0,0,0.640406]13 resultKolmogorov= class=SquareMatrix dimension=11 implementation=class=MatrixImplementation name=Unnamed rows=11 columns=11 values=[0.380754,0,0,0,0,0,0,0,0,0,0,0,0.101178,0,0,0,0,0,0,0,0,0,0,0,0.935178,0,0,0,0,0,6.07492e-05,0,0,0,0,0,0.261505,0,0,0,0,0,0,0,0,0,0,0,0.102296,0.488213,0,0,0,0,0,0,0,0,0,0.0115789,0.979805,0,0,0,0,0,0,0,0,0,0.00855122,0,0.256542,0,0,0,0,0,0,0,0,0,0,0,0.515105,0,0,0,0,0,0,0,0.000639709,0,7.92517e-06,0,0.268376,0,0,0,0,0,0,0,0,0,0,0,0.556345,0,0,0,0,0,0,0,0,0,0,0,0.640406] 14 14 resultChiSquared= class=SquareMatrix dimension=2 implementation=class=MatrixImplementation name=Unnamed rows=2 columns=2 values=[0,0,0,0.892467] 15 15 ]],
