正在加载图片...
14.4 Contingency Table Analysis of Two Distributions 635 #include <math.h> #include "nrutil.h" #define TINY 1.0e-30 A small number. void cntab2(int **nn,int ni,int nj,float *h,float *hx,float *hy, float *hygx,float *hxgy,float *uygx,float tuxgy,float *uxy) Given a two-dimensional contingency table in the form of an integer array nn[i]j],where i labels the x variable and ranges from 1 to ni,j labels the y variable and ranges from 1 to nj, this routine returns the entropy h of the whole table,the entropy hx of the x distribution,the entropy hy of the y distribution,the entropy hygx of y given x,the entropy hxgy of x given y, the dependency uygx of y on z (eq.14.4.15).the dependency uxgy of z on y (eq.14.4.16). and the symmetrical dependency uxy (eq.14.4.17). int i,j; float sum=0.0,p,*sumi,*sumji granted for 18881992 sumi-vector(1,ni); sumj=vector(1,nj); 1600 (including this one) for(1=1;1<=n1;1++)[ Get the row totals. sum1[i]=0.0; 872 for (j=1;j<=nj;j++){ /Cambridge sumi[i]+nn[i][j]; 7422 from NUMERICAL RECIPES IN sum +nn[i][j]; for (j=1;j<=nj;j++){ Get the column totals. sumj[i]=0.0; (North America server computer, to make one paper UnN电.t THE for (i=1;i<-ni;i++) ART sumj[j]+nn[i][j]; *hx=0.0; Entropy of the x distribution, Programs for(1=1;1<n1;1++) copyfor thei if (sumi[i]){ p=sumi[i]/sum; st st whx -p*log(p); to dir Copyright (C) *hy=0.0; and of the y distribution for (j=1;j<=nj;j++) if (sumj[j]){ rectcustsen OF SCIENTIFIC COMPUTING(ISBN p=sumj[j]/sum; *hy -p*log(p); *h=0.0; v@cambri 10-621 for(1=1;1<=n1;1++) Total entropy:loop over both z for (j=1;i<=nj;j++) and y. if (nn[i][j]){ 1988-1992 by Numerical Recipes 43108 p=nn[i][j]/sum; *h -p*log(p); *hygx=(*h)-(*hx); Uses equation(14.4.18). (outside North Amer *hxgy=(*h)-(*hy); as does this. Software. *uygx=(*hy-*hygx)/(*hy+TINY); Equation (14.4.15). *uxgy=(*hx-*hxgy)/(*hx+TINY); Equation (14.4.16). ying of *uxy=2.0*(*hx+*hy-*h)/(*hx+*hy+TINY) Equation (14.4.17) free_vector(sumi,1,nj); free_vector(sumi,1,ni); CITED REFERENCES AND FURTHER READING: Dunn,O.J.,and Clark,V.A.1974,Applied Statistics:Analysis of Variance and Regression(New York:Wiley).14.4 Contingency Table Analysis of Two Distributions 635 Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copyin Copyright (C) 1988-1992 by Cambridge University Press. Programs Copyright (C) 1988-1992 by Numerical Recipes Software. Sample page from NUMERICAL RECIPES IN C: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43108-5) g of machine￾readable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books or CDROMs, visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to directcustserv@cambridge.org (outside North America). #include <math.h> #include "nrutil.h" #define TINY 1.0e-30 A small number. void cntab2(int **nn, int ni, int nj, float *h, float *hx, float *hy, float *hygx, float *hxgy, float *uygx, float *uxgy, float *uxy) Given a two-dimensional contingency table in the form of an integer array nn[i][j], where i labels the x variable and ranges from 1 to ni, j labels the y variable and ranges from 1 to nj, this routine returns the entropy h of the whole table, the entropy hx of the x distribution, the entropy hy of the y distribution, the entropy hygx of y given x, the entropy hxgy of x given y, the dependency uygx of y on x (eq. 14.4.15), the dependency uxgy of x on y (eq. 14.4.16), and the symmetrical dependency uxy (eq. 14.4.17). { int i,j; float sum=0.0,p,*sumi,*sumj; sumi=vector(1,ni); sumj=vector(1,nj); for (i=1;i<=ni;i++) { Get the row totals. sumi[i]=0.0; for (j=1;j<=nj;j++) { sumi[i] += nn[i][j]; sum += nn[i][j]; } } for (j=1;j<=nj;j++) { Get the column totals. sumj[j]=0.0; for (i=1;i<=ni;i++) sumj[j] += nn[i][j]; } *hx=0.0; Entropy of the x distribution, for (i=1;i<=ni;i++) if (sumi[i]) { p=sumi[i]/sum; *hx -= p*log(p); } *hy=0.0; and of the y distribution. for (j=1;j<=nj;j++) if (sumj[j]) { p=sumj[j]/sum; *hy -= p*log(p); } *h=0.0; for (i=1;i<=ni;i++) Total entropy: loop over both x for (j=1;j<=nj;j++) and y. if (nn[i][j]) { p=nn[i][j]/sum; *h -= p*log(p); } *hygx=(*h)-(*hx); Uses equation (14.4.18), *hxgy=(*h)-(*hy); as does this. *uygx=(*hy-*hygx)/(*hy+TINY); Equation (14.4.15). *uxgy=(*hx-*hxgy)/(*hx+TINY); Equation (14.4.16). *uxy=2.0*(*hx+*hy-*h)/(*hx+*hy+TINY); Equation (14.4.17). free_vector(sumj,1,nj); free_vector(sumi,1,ni); } CITED REFERENCES AND FURTHER READING: Dunn, O.J., and Clark, V.A. 1974, Applied Statistics: Analysis of Variance and Regression (New York: Wiley)
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有