pca.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Principal component analysis
  3. * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. */
  20. /**
  21. * @file pca.c
  22. * Principal component analysis
  23. */
  24. #include <math.h>
  25. #include "avcodec.h"
  26. #include "pca.h"
  27. int ff_pca_init(PCA *pca, int n){
  28. if(n<=0)
  29. return -1;
  30. pca->n= n;
  31. pca->count=0;
  32. pca->covariance= av_mallocz(sizeof(double)*n*n);
  33. pca->mean= av_mallocz(sizeof(double)*n);
  34. return 0;
  35. }
  36. void ff_pca_free(PCA *pca){
  37. av_freep(&pca->covariance);
  38. av_freep(&pca->mean);
  39. }
  40. void ff_pca_add(PCA *pca, double *v){
  41. int i, j;
  42. const int n= pca->n;
  43. for(i=0; i<n; i++){
  44. pca->mean[i] += v[i];
  45. for(j=i; j<n; j++)
  46. pca->covariance[j + i*n] += v[i]*v[j];
  47. }
  48. pca->count++;
  49. }
  50. int ff_pca(PCA *pca, double *eigenvector, double *eigenvalue){
  51. int i, j, k, pass;
  52. const int n= pca->n;
  53. double z[n];
  54. memset(eigenvector, 0, sizeof(double)*n*n);
  55. for(j=0; j<n; j++){
  56. pca->mean[j] /= pca->count;
  57. eigenvector[j + j*n] = 1.0;
  58. for(i=0; i<=j; i++){
  59. pca->covariance[j + i*n] /= pca->count;
  60. pca->covariance[j + i*n] -= pca->mean[i] * pca->mean[j];
  61. pca->covariance[i + j*n] = pca->covariance[j + i*n];
  62. }
  63. eigenvalue[j]= pca->covariance[j + j*n];
  64. z[j]= 0;
  65. }
  66. for(pass=0; pass < 50; pass++){
  67. double sum=0;
  68. for(i=0; i<n; i++)
  69. for(j=i+1; j<n; j++)
  70. sum += fabs(pca->covariance[j + i*n]);
  71. if(sum == 0){
  72. for(i=0; i<n; i++){
  73. double maxvalue= -1;
  74. for(j=i; j<n; j++){
  75. if(eigenvalue[j] > maxvalue){
  76. maxvalue= eigenvalue[j];
  77. k= j;
  78. }
  79. }
  80. eigenvalue[k]= eigenvalue[i];
  81. eigenvalue[i]= maxvalue;
  82. for(j=0; j<n; j++){
  83. double tmp= eigenvector[k + j*n];
  84. eigenvector[k + j*n]= eigenvector[i + j*n];
  85. eigenvector[i + j*n]= tmp;
  86. }
  87. }
  88. return pass;
  89. }
  90. for(i=0; i<n; i++){
  91. for(j=i+1; j<n; j++){
  92. double covar= pca->covariance[j + i*n];
  93. double t,c,s,tau,theta, h;
  94. if(pass < 3 && fabs(covar) < sum / (5*n*n)) //FIXME why pass < 3
  95. continue;
  96. if(fabs(covar) == 0.0) //FIXME shouldnt be needed
  97. continue;
  98. if(pass >=3 && fabs((eigenvalue[j]+z[j])/covar) > (1LL<<32) && fabs((eigenvalue[i]+z[i])/covar) > (1LL<<32)){
  99. pca->covariance[j + i*n]=0.0;
  100. continue;
  101. }
  102. h= (eigenvalue[j]+z[j]) - (eigenvalue[i]+z[i]);
  103. theta=0.5*h/covar;
  104. t=1.0/(fabs(theta)+sqrt(1.0+theta*theta));
  105. if(theta < 0.0) t = -t;
  106. c=1.0/sqrt(1+t*t);
  107. s=t*c;
  108. tau=s/(1.0+c);
  109. z[i] -= t*covar;
  110. z[j] += t*covar;
  111. #define ROTATE(a,i,j,k,l)\
  112. double g=a[j + i*n];\
  113. double h=a[l + k*n];\
  114. a[j + i*n]=g-s*(h+g*tau);\
  115. a[l + k*n]=h+s*(g-h*tau);
  116. for(k=0; k<n; k++) {
  117. if(k!=i && k!=j){
  118. ROTATE(pca->covariance,FFMIN(k,i),FFMAX(k,i),FFMIN(k,j),FFMAX(k,j))
  119. }
  120. ROTATE(eigenvector,k,i,k,j)
  121. }
  122. pca->covariance[j + i*n]=0.0;
  123. }
  124. }
  125. for (i=0; i<n; i++) {
  126. eigenvalue[i] += z[i];
  127. z[i]=0.0;
  128. }
  129. }
  130. return -1;
  131. }
  132. #if 1
  133. #undef printf
  134. #include <stdio.h>
  135. #include <stdlib.h>
  136. int main(){
  137. PCA pca;
  138. int i, j, k;
  139. #define LEN 8
  140. double eigenvector[LEN*LEN];
  141. double eigenvalue[LEN];
  142. ff_pca_init(&pca, LEN);
  143. for(i=0; i<9000000; i++){
  144. double v[2*LEN+100];
  145. double sum=0;
  146. int pos= random()%LEN;
  147. int v2= (random()%101) - 50;
  148. v[0]= (random()%101) - 50;
  149. for(j=1; j<8; j++){
  150. if(j<=pos) v[j]= v[0];
  151. else v[j]= v2;
  152. sum += v[j];
  153. }
  154. /* for(j=0; j<LEN; j++){
  155. v[j] -= v[pos];
  156. }*/
  157. // sum += random()%10;
  158. /* for(j=0; j<LEN; j++){
  159. v[j] -= sum/LEN;
  160. }*/
  161. // lbt1(v+100,v+100,LEN);
  162. ff_pca_add(&pca, v);
  163. }
  164. ff_pca(&pca, eigenvector, eigenvalue);
  165. for(i=0; i<LEN; i++){
  166. pca.count= 1;
  167. pca.mean[i]= 0;
  168. // (0.5^|x|)^2 = 0.5^2|x| = 0.25^|x|
  169. // pca.covariance[i + i*LEN]= pow(0.5, fabs
  170. for(j=i; j<LEN; j++){
  171. printf("%f ", pca.covariance[i + j*LEN]);
  172. }
  173. printf("\n");
  174. }
  175. #if 1
  176. for(i=0; i<LEN; i++){
  177. double v[LEN];
  178. double error=0;
  179. memset(v, 0, sizeof(v));
  180. for(j=0; j<LEN; j++){
  181. for(k=0; k<LEN; k++){
  182. v[j] += pca.covariance[FFMIN(k,j) + FFMAX(k,j)*LEN] * eigenvector[i + k*LEN];
  183. }
  184. v[j] /= eigenvalue[i];
  185. error += fabs(v[j] - eigenvector[i + j*LEN]);
  186. }
  187. printf("%f ", error);
  188. }
  189. printf("\n");
  190. #endif
  191. for(i=0; i<LEN; i++){
  192. for(j=0; j<LEN; j++){
  193. printf("%9.6f ", eigenvector[i + j*LEN]);
  194. }
  195. printf(" %9.1f %f\n", eigenvalue[i], eigenvalue[i]/eigenvalue[0]);
  196. }
  197. return 0;
  198. }
  199. #endif