pca.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 "common.h"
  25. #include "pca.h"
  26. PCA *ff_pca_init(int n){
  27. PCA *pca;
  28. if(n<=0)
  29. return NULL;
  30. pca= av_mallocz(sizeof(PCA));
  31. pca->n= n;
  32. pca->count=0;
  33. pca->covariance= av_mallocz(sizeof(double)*n*n);
  34. pca->mean= av_mallocz(sizeof(double)*n);
  35. return pca;
  36. }
  37. void ff_pca_free(PCA *pca){
  38. av_freep(&pca->covariance);
  39. av_freep(&pca->mean);
  40. av_free(pca);
  41. }
  42. void ff_pca_add(PCA *pca, double *v){
  43. int i, j;
  44. const int n= pca->n;
  45. for(i=0; i<n; i++){
  46. pca->mean[i] += v[i];
  47. for(j=i; j<n; j++)
  48. pca->covariance[j + i*n] += v[i]*v[j];
  49. }
  50. pca->count++;
  51. }
  52. int ff_pca(PCA *pca, double *eigenvector, double *eigenvalue){
  53. int i, j, k, pass;
  54. const int n= pca->n;
  55. double z[n];
  56. memset(eigenvector, 0, sizeof(double)*n*n);
  57. for(j=0; j<n; j++){
  58. pca->mean[j] /= pca->count;
  59. eigenvector[j + j*n] = 1.0;
  60. for(i=0; i<=j; i++){
  61. pca->covariance[j + i*n] /= pca->count;
  62. pca->covariance[j + i*n] -= pca->mean[i] * pca->mean[j];
  63. pca->covariance[i + j*n] = pca->covariance[j + i*n];
  64. }
  65. eigenvalue[j]= pca->covariance[j + j*n];
  66. z[j]= 0;
  67. }
  68. for(pass=0; pass < 50; pass++){
  69. double sum=0;
  70. for(i=0; i<n; i++)
  71. for(j=i+1; j<n; j++)
  72. sum += fabs(pca->covariance[j + i*n]);
  73. if(sum == 0){
  74. for(i=0; i<n; i++){
  75. double maxvalue= -1;
  76. for(j=i; j<n; j++){
  77. if(eigenvalue[j] > maxvalue){
  78. maxvalue= eigenvalue[j];
  79. k= j;
  80. }
  81. }
  82. eigenvalue[k]= eigenvalue[i];
  83. eigenvalue[i]= maxvalue;
  84. for(j=0; j<n; j++){
  85. double tmp= eigenvector[k + j*n];
  86. eigenvector[k + j*n]= eigenvector[i + j*n];
  87. eigenvector[i + j*n]= tmp;
  88. }
  89. }
  90. return pass;
  91. }
  92. for(i=0; i<n; i++){
  93. for(j=i+1; j<n; j++){
  94. double covar= pca->covariance[j + i*n];
  95. double t,c,s,tau,theta, h;
  96. if(pass < 3 && fabs(covar) < sum / (5*n*n)) //FIXME why pass < 3
  97. continue;
  98. if(fabs(covar) == 0.0) //FIXME shouldnt be needed
  99. continue;
  100. if(pass >=3 && fabs((eigenvalue[j]+z[j])/covar) > (1LL<<32) && fabs((eigenvalue[i]+z[i])/covar) > (1LL<<32)){
  101. pca->covariance[j + i*n]=0.0;
  102. continue;
  103. }
  104. h= (eigenvalue[j]+z[j]) - (eigenvalue[i]+z[i]);
  105. theta=0.5*h/covar;
  106. t=1.0/(fabs(theta)+sqrt(1.0+theta*theta));
  107. if(theta < 0.0) t = -t;
  108. c=1.0/sqrt(1+t*t);
  109. s=t*c;
  110. tau=s/(1.0+c);
  111. z[i] -= t*covar;
  112. z[j] += t*covar;
  113. #define ROTATE(a,i,j,k,l) {\
  114. double g=a[j + i*n];\
  115. double h=a[l + k*n];\
  116. a[j + i*n]=g-s*(h+g*tau);\
  117. a[l + k*n]=h+s*(g-h*tau); }
  118. for(k=0; k<n; k++) {
  119. if(k!=i && k!=j){
  120. ROTATE(pca->covariance,FFMIN(k,i),FFMAX(k,i),FFMIN(k,j),FFMAX(k,j))
  121. }
  122. ROTATE(eigenvector,k,i,k,j)
  123. }
  124. pca->covariance[j + i*n]=0.0;
  125. }
  126. }
  127. for (i=0; i<n; i++) {
  128. eigenvalue[i] += z[i];
  129. z[i]=0.0;
  130. }
  131. }
  132. return -1;
  133. }
  134. #ifdef TEST
  135. #undef printf
  136. #undef random
  137. #include <stdio.h>
  138. #include <stdlib.h>
  139. int main(){
  140. PCA *pca;
  141. int i, j, k;
  142. #define LEN 8
  143. double eigenvector[LEN*LEN];
  144. double eigenvalue[LEN];
  145. pca= ff_pca_init(LEN);
  146. for(i=0; i<9000000; i++){
  147. double v[2*LEN+100];
  148. double sum=0;
  149. int pos= random()%LEN;
  150. int v2= (random()%101) - 50;
  151. v[0]= (random()%101) - 50;
  152. for(j=1; j<8; j++){
  153. if(j<=pos) v[j]= v[0];
  154. else v[j]= v2;
  155. sum += v[j];
  156. }
  157. /* for(j=0; j<LEN; j++){
  158. v[j] -= v[pos];
  159. }*/
  160. // sum += random()%10;
  161. /* for(j=0; j<LEN; j++){
  162. v[j] -= sum/LEN;
  163. }*/
  164. // lbt1(v+100,v+100,LEN);
  165. ff_pca_add(pca, v);
  166. }
  167. ff_pca(pca, eigenvector, eigenvalue);
  168. for(i=0; i<LEN; i++){
  169. pca->count= 1;
  170. pca->mean[i]= 0;
  171. // (0.5^|x|)^2 = 0.5^2|x| = 0.25^|x|
  172. // pca.covariance[i + i*LEN]= pow(0.5, fabs
  173. for(j=i; j<LEN; j++){
  174. printf("%f ", pca->covariance[i + j*LEN]);
  175. }
  176. printf("\n");
  177. }
  178. #if 1
  179. for(i=0; i<LEN; i++){
  180. double v[LEN];
  181. double error=0;
  182. memset(v, 0, sizeof(v));
  183. for(j=0; j<LEN; j++){
  184. for(k=0; k<LEN; k++){
  185. v[j] += pca->covariance[FFMIN(k,j) + FFMAX(k,j)*LEN] * eigenvector[i + k*LEN];
  186. }
  187. v[j] /= eigenvalue[i];
  188. error += fabs(v[j] - eigenvector[i + j*LEN]);
  189. }
  190. printf("%f ", error);
  191. }
  192. printf("\n");
  193. #endif
  194. for(i=0; i<LEN; i++){
  195. for(j=0; j<LEN; j++){
  196. printf("%9.6f ", eigenvector[i + j*LEN]);
  197. }
  198. printf(" %9.1f %f\n", eigenvalue[i], eigenvalue[i]/eigenvalue[0]);
  199. }
  200. return 0;
  201. }
  202. #endif