Debian Codegraph @

awklib/eg/network/statist.awk

raw
1 function SetUpServer() {
2 TopHeader = "<HTML><title>Statistics with GAWK</title>"
3 TopDoc = "<BODY>\
4 <h2>Please choose one of the following actions:</h2>\
5 <UL>\
6 <LI><A HREF=" MyPrefix "/AboutServer>About this server</A></LI>\
7 <LI><A HREF=" MyPrefix "/EnterParameters>Enter Parameters</A></LI>\
8 </UL>"
9 TopFooter = "</BODY></HTML>"
10 GnuPlot = "gnuplot 2>&1"
11 m1=m2=0; v1=v2=1; n1=n2=10
12 }
13 function HandleGET() {
14 if (MENU[2] == "AboutServer") {
15 Document = "This is a GUI for a statistical computation.\
16 It compares means and variances of two distributions.\
17 It is implemented as one GAWK script and uses GNUPLOT."
18 } else if (MENU[2] == "EnterParameters") {
19 Document = ""
20 if ("m1" in GETARG) { # are there parameters to compare?
21 Document = Document "<SCRIPT LANGUAGE=\"JavaScript\">\
22 setTimeout(\"window.open(\\\"" MyPrefix "/Image" systime()\
23 "\\\",\\\"dist\\\", \\\"status=no\\\");\", 1000); </SCRIPT>"
24 m1 = GETARG["m1"]; v1 = GETARG["v1"]; n1 = GETARG["n1"]
25 m2 = GETARG["m2"]; v2 = GETARG["v2"]; n2 = GETARG["n2"]
26 t = (m1-m2)/sqrt(v1/n1+v2/n2)
27 df = (v1/n1+v2/n2)*(v1/n1+v2/n2)/((v1/n1)*(v1/n1)/(n1-1) \
28 + (v2/n2)*(v2/n2) /(n2-1))
29 if (v1>v2) {
30 f = v1/v2
31 df1 = n1 - 1
32 df2 = n2 - 1
33 } else {
34 f = v2/v1
35 df1 = n2 - 1
36 df2 = n1 - 1
37 }
38 print "pt=ibeta(" df/2 ",0.5," df/(df+t*t) ")" |& GnuPlot
39 print "pF=2.0*ibeta(" df2/2 "," df1/2 "," \
40 df2/(df2+df1*f) ")" |& GnuPlot
41 print "print pt, pF" |& GnuPlot
42 RS="\n"; GnuPlot |& getline; RS="\r\n" # $1 is pt, $2 is pF
43 print "invsqrt2pi=1.0/sqrt(2.0*pi)" |& GnuPlot
44 print "nd(x)=invsqrt2pi/sd*exp(-0.5*((x-mu)/sd)**2)" |& GnuPlot
45 print "set term png small color" |& GnuPlot
46 #print "set term postscript color" |& GnuPlot
47 #print "set term gif medium size 320,240" |& GnuPlot
48 print "set yrange[-0.3:]" |& GnuPlot
49 print "set label 'p(m1=m2) =" $1 "' at 0,-0.1 left" |& GnuPlot
50 print "set label 'p(v1=v2) =" $2 "' at 0,-0.2 left" |& GnuPlot
51 print "plot mu=" m1 ",sd=" sqrt(v1) ", nd(x) title 'sample 1',\
52 mu=" m2 ",sd=" sqrt(v2) ", nd(x) title 'sample 2'" |& GnuPlot
53 print "quit" |& GnuPlot
54 GnuPlot |& getline Image
55 while ((GnuPlot |& getline) > 0)
56 Image = Image RS $0
57 close(GnuPlot)
58 }
59 Document = Document "\
60 <h3>Do these samples have the same Gaussian distribution?</h3>\
61 <FORM METHOD=GET> <TABLE BORDER CELLPADDING=5>\
62 <TR>\
63 <TD>1. Mean </TD>
64 <TD><input type=text name=m1 value=" m1 " size=8></TD>\
65 <TD>1. Variance</TD>
66 <TD><input type=text name=v1 value=" v1 " size=8></TD>\
67 <TD>1. Count </TD>
68 <TD><input type=text name=n1 value=" n1 " size=8></TD>\
69 </TR><TR>\
70 <TD>2. Mean </TD>
71 <TD><input type=text name=m2 value=" m2 " size=8></TD>\
72 <TD>2. Variance</TD>
73 <TD><input type=text name=v2 value=" v2 " size=8></TD>\
74 <TD>2. Count </TD>
75 <TD><input type=text name=n2 value=" n2 " size=8></TD>\
76 </TR> <input type=submit value=\"Compute\">\
77 </TABLE></FORM><BR>"
78 } else if (MENU[2] ~ "Image") {
79 Reason = "OK" ORS "Content-type: image/png"
80 #Reason = "OK" ORS "Content-type: application/x-postscript"
81 #Reason = "OK" ORS "Content-type: image/gif"
82 Header = Footer = ""
83 Document = Image
84 }
85 }