function normsinv(pp){
var x,y;
var q = 1 - pp;
bb = [1.570796288,0.03706987906,-0.0008364353589,-0.0002250947176,
0.000006841218299,0.000005824238515,-0.00000104527497,8.360937017e-8,-3.231081277e-09,
3.657763036e-11,6.936233982e-13];
if (q == 0.5) return 0;
var p;
if (q > 0.5) p = 1 - q;
else p = q;
var b;
y =-Math.log(4 * p * (1 - p));
b = y * bb[10];
var i;
for (i = 9; i >= 0; i--)
{
b = y * (bb[i] + b);
}
x = Math.sqrt(b);
if (q > 0.5) x = -x;
return x;
}
function gamma( x)
{
var i;
var y, t, s, u;
var a = [ 0.0000677106,-0.0003442342,
0.0015397681,-0.0024467480,0.0109736958,
-0.0002109075,0.0742379071,0.0815782188,
0.4118402518,0.4227843370,1.0];
if (x <= 0.0)
throw "x不能小于0";
y = x;
if (y <= 1.0)
{ t = 1.0 / (y * (y + 1.0)); y += 2.0; }
else if (y <= 2.0)
{ t = 1.0 / y; y += 1.0; }
else if (y <= 3.0) t = 1.0;
else
{
t = 1.0;
while (y > 3.0)
{ y -= 1.0; t = t * y; }
}
s = a[0]; u = y - 2.0;
for (i = 1; i <= 10; i++)
s = s * u + a[i];
s *= t;
return s;
}
function betacf(a, b, x)
{
var MAXIT = 1000;
var EPS = 1e-10;
var FPMIN = 1e-8;
var m, m2;
var aa, c, d, del, h, qab, qam, qap;
qab = a + b;
qap = a + 1;
qam = a - 1;
c = 1;
d = 1 - qab * x / qap;
if (Math.abs(d) < FPMIN) d = FPMIN;
d = 1 / d;
h = d;
for (m = 1; m <= MAXIT; m++)
{
m2 = 2 * m;
aa = m * (b - m) * x / ((qam + m2) * (a + m2));
d = 1 + aa * d;
if (Math.abs(d) < FPMIN) d = FPMIN;
c = 1 + aa / c;
if (Math.abs(c) < FPMIN) c = FPMIN;
d = 1 / d;
h *= d * c;
aa = -(a + m) * (qab + m) * x / ((a + m2) * (qap + m2));
d = 1 + aa * d;
if (Math.abs(d) < FPMIN) d = FPMIN;
c = 1 + aa / c;
if (Math.abs(c) < FPMIN) c = FPMIN;
d = 1 / d;
del = d * c;
h *= del;
if (Math.abs(del - 1) <= EPS) break;
}
if (m > MAXIT) throw "a or b too big, or MAXIT too small in betacf";
return h;
}
function gammln(xx)
{
var j;
var x, y, tmp, ser;
var cof = [76.18009172947146,-86.50532032941677,24.01409824083091,-1.231739572450155,
0.1208650973866179e-2,-0.5395239384953e-5];
y = x = xx;
tmp = x + 5.5;
tmp -= (x + 0.5) * Math.log(tmp);
ser = 1.000000000190015;
for (j = 0; j < 6; j++) ser += cof[j] / ++y;
return -tmp + Math.log(2.5066282746310005 * ser / x);
}
function betai(a, b, x)
{
var bt;
if (x < 0 || x > 1) throw "Bad x in routine batai";
if (x == 0 || x == 1) bt = 0;
else
bt = Math.exp(gammln(a + b) - gammln(a) - gammln(b) + a * Math.log(x) + b * Math.log(1 - x));
if (x < (a + 1) / (a + b + 2))
return bt * betacf(a, b, x) / a;
else
return 1 - bt * betacf(b, a, 1 - x) / b;
}
function tdist2(t, free)
{
if (free < 1) throw "free must biger than 0";
var f;
f = free;
var p;
p = 1 - betai(f / 2.0, 0.5, f / (f + t * t));
return p;
}
function tdist(t, free)
{
if (t == 0)
{
return 0.5;
}
if (t > 0)
{
return 0.5 + tdist2(t, free) / 2;
}
return (1 - tdist2(-t, free)) / 2;
}
var outpp;
function t_dist(n, t)
{
var sign, ibi, n2, i;
var tt, x, p, u, ga1, ga2,dd;
if (t == 0)
{
ga1 = gamma(n / 2.0);
ga2 = gamma(0.5 + n / 2.0);
outpp = 0.5;
return ga2 / (Math.sqrt(n * Math.PI) * ga1);
}
if (t < 0)
{
sign = -1;
}
else sign = 1;
tt = t * t;
x = tt / (n + tt);
if (n % 2 == 0)
{
p = Math.sqrt(x);
u = p * (1 - x) / 2.0;
ibi = 2;
}
else
{
u = Math.sqrt(x * (1 - x)) / Math.PI;
p = 1 - 2 * Math.atan(Math.sqrt((1 - x) / x)) / Math.PI;
ibi = 1;
}
if (ibi == n)
{
dd = u / Math.abs(t);
outpp = 0.5 + sign * p / 2.0;
return dd;
}
else
{
n2 = n - 2;
}
for (i = ibi; i <= n2; i += 2)
{
p += 2.0 * u / i;
u = u * (1 + i) / i * (1 - x);
}
dd = u / Math.abs(t);
outpp = 0.5 + sign * p / 2.0;
return dd;
}
function tdistinv(pvar, n)
{
var q;
q = 1 - pvar;
var t=0.0;
var pis, dfr2, c, q2, p, yq, e, ga1, ga2, ga3, t0, pp, d;
var k;
pis = Math.sqrt(Math.PI);
dfr2 = n / 2.0;
if (n == 1) return Math.tan(Math.PI * (0.5 - q));
if (n == 2)
{
if (q > 0.5) c = -1;
else c = 1;
q2 = (1 - 2 * q);
q2 *= q2;
return Math.sqrt(2.0 * q2 / (1.0 - q2)) * c;
}
p = 1 - q;
yq = normsinv(p);
e = 1.0 - 1.0 / (4.0 * n);
e = e * e - yq * yq / (2.0 * n);
if (e > 0.5) t0 = yq / Math.sqrt(e);
else
{
ga1 = gammln(dfr2);
ga2 = gammln(dfr2 + 0.5);
ga3 = Math.exp((ga1 - ga2) / n);
t0 = Math.pow(Math.sqrt(n) / (pis * q * n), (1.0 / n) / ga3);
}
for (k = 1; k <= 30; k++)
{
d = t_dist(n, t0);
pp=outpp;
if (d == 0) return t0;
t = t0 - (pp - p) / d;
if (Math.abs(t0 - t) < 0.000001 * Math.abs(t))
{
return t;
}
else t0 = t;
}
return t;
}
function tdist2inv(p, free)
{
var x2;
x2 = 300;
if (p > 1 - 1e-8) return x2;
if (p == 0) return 0;
var x1 = 0;
var xacc = 1e-12;
var JMAX = 400;
var j;
var dx, f, fmid, xmid, rtb;
f = tdist2(x1, free) - p;
fmid = tdist2(x2, free) - p;
if (f * fmid >= 0) throw "faild in tdist2inv";
if (f < 0)
{
dx = x2 - x1;
rtb = x1;
}
else
{
dx = x1 - x2;
rtb = x2;
}
for (j = 0; j < JMAX; j++)
{
dx *= 0.5;
xmid = rtb + dx;
fmid = tdist2(xmid, free) - p;
if (fmid <= 0) rtb = xmid;
if (Math.abs(dx) < xacc || fmid == 0) return rtb;
}
return 0;
}
var outgamser, outgln;
function gser(a, x)
{
var gamser,gln;
var ITMAX = 10000;
var EPS = 1e-10;
var n;
var sum, del, ap;
gamser = 0;
gln = gammln(a);
outgln=gln;
if (x <= 0)
{
if (x < 0) throw "x less than 0";
outgamser = 0;
return;
}
else
{
ap = a;
del = sum = 1.0 / a;
for (n = 0; n < ITMAX; n++)
{
ap++;
del *= x / ap;
sum += del;
if (Math.abs(del) < Math.abs(sum) * EPS)
{
outgamser = sum * Math.exp(-x + a * Math.log(x) - gln);
return;
}
}
}
}
var outgammcf;
function gcf(a, x)
{
var gammcf, gln;
var ITMAX = 10000;
var EPS = 1e-10;
var FPMIN = 1e-10;
var i;
var an, b, c, d, del, h;
gln = gammln(a);
outgln=gln;
gammcf = 0;
outgammcf=gammcf;
b = x + 1 - a;
c = 1 / FPMIN;
d = 1 / b;
h = d;
for (i = 1; i <= ITMAX; i++)
{
an = -i * (i - a);
b += 2;
d = an * d + b;
if (Math.abs(d) < FPMIN) d = FPMIN;
c = b + an / c;
if (Math.abs(c) < FPMIN) c = FPMIN;
d = 1 / d;
del = d * c;
h *= del;
if (Math.abs(del - 1) <= EPS) break;
}
if (i > ITMAX) throw "a too large, ITMAX too small in gcf";
outgammcf = Math.exp(-x + a * Math.log(x) - gln) * h;
}
function gammp(a, x)
{
var gamser, gammcf, gln;
if (x < 0 || a <= 0) throw "invalid arguments in gammp";
if (x < a + 1)
{
gser(a, x);
return outgamser;
}
else
{
gcf(a, x);
return 1.0 - outgammcf;
}
}
function chidist(x2, free)
{
var f;
f = free;
if (free < 1)
{
throw "自由度必须大于0";
}
if (x2 <= 0) return 0;
return gammp(f / 2, x2 / 2);
}
function erf(x){
var t,z,ans;
z=Math.abs(x);
t=1.0/(1.0+0.5*z);
ans = t*Math.exp(-z * z - 1.26551223 + t * (1.00002368 + t * (0.37409196 + t * (0.09678418 +
t * (-0.18628806 + t * (0.27886807 + t * (-1.13520398 + t * (1.48851587 +
t * (-0.82215223 + t * 0.17087277)))))))));
return (x >= 0.0 ? ans : 2.0 - ans);
}
function normsdist(x){
return 0.5 + (1 - erf(x /Math.sqrt(2))) / 2;
}
var outf;
function cx2(n, x2)
{
var f;
var i, n2, iai;
var pis, x, chs, u, pp;
outf = f = 0;
if (x2 == 0) return 0;
pis = Math.sqrt(Math.PI);
x = x2 / 2;
chs = Math.sqrt(x2);
if (n % 2 == 0)
{
u = x * Math.exp(-x);
outf = f = 1 - Math.exp(-x);
iai = 2;
}
else
{
u = Math.sqrt(x) * Math.exp(-x) / pis;
pp = normsdist(chs);
outf=f = 2 * (pp - 0.5);
iai = 1;
}
if (iai == n) return u / x2;
n2 = n - 2;
for (i = iai; i <= n2; i += 2)
{
f = f - 2 * u / i;
u = x2 * u / i;
}
outf=f;
return u / x2;
}
function chiinv(varp, n)
{
var q;
q = 1 - varp;
var i;
var xx;
var x, p, w, x0, pp, d;
if (n == 1)
{
x = normsinv(1 - q / 2);
return x * x;
}
if (n == 2)
{
return -2.0 * Math.log(q);
}
p = 1 - q;
x = normsinv(p);
w = 2.0 / (9.0 * n);
x0 = n * Math.pow(1 - w + x * Math.sqrt(w), 3);
xx = x0;
for (i = 1; i <= 30; i++)
{
d = cx2(n, x0);
pp=outf;
if (d == 0) return x0;
xx = x0 - (pp - p) / d;
if (Math.abs(xx - x0) < 1e-6 * Math.abs(xx)) return xx;
x0 = xx;
}
return xx;
}
function fdist(x, free1, free2)
{
var m, n;
if (x <= 0) return 0;
if (free1 <= 0 || free2 <= 0) throw "freedegree must >0";
m = free1;
n = free2;
var p;
p = 1 - betai(n / 2, m / 2, n / (n + m * x));
return p;
}
function fdistinv(p, free1, free2)
{
if (free1 < 1 || free2 < 1) throw "free must >0 in fdistinv!";
if (p < 0 || p > 1) throw "p must be probability in fdistinv!";
if (p == 0) return 0;
var xup;
var n1, n2;
n1 = free1;
n2 = free2;
var e, dev;
if (n2 <= 4)
{
xup = 500000;
}
else
{
e = n2 / (n2 - 2);
dev = 2 * n2 * n2 * (n1 + n2 - 2) / (n1 * (n2 - 2) * (n2 - 2) * (n2 - 4));
dev = Math.sqrt(dev);
xup = e + 20 * dev;
}
var x2;
x2 = xup;
if (p > 1 - 1e-8) return x2;
var x1 = 0;
var xacc = 1e-12;
var JMAX = 400;
var j;
var dx, f, fmid, xmid, rtb;
f = fdist(x1, free1, free2) - p;
fmid = fdist(x2, free1, free2) - p;
if (f * fmid >= 0) throw "faild in fdist2inv";
if (f < 0)
{
dx = x2 - x1;
rtb = x1;
}
else
{
dx = x1 - x2;
rtb = x2;
}
for (j = 0; j < JMAX; j++)
{
dx *= 0.5;
xmid = rtb + dx;
fmid = fdist(xmid, free1, free2) - p;
if (fmid <= 0) rtb = xmid;
if (Math.abs(dx) < xacc || fmid == 0) return rtb;
}
return 0;
}
function tobias(s)
{
if(s.substr(0,1)=="q")
{
s1=s.substring(1);
b=parseFloat(s1);
if(b<=0){
alert("输入数据无效!");
return -1;
}
else
return Math.sqrt(b);
}
else
{
b=parseFloat(s);
if (b<=0)
{
alert("输入格式不对!");
return -1;
}
return b;
}
}
function dtos(d){
var s,i,j;
s=d.toPrecision(6);
j=s.indexOf(".");
if(j<0) return s;
if(j==s.length-1)return s.substr(0,j); // 如果小数点后面什么也没有,就不要小数点
for(i=s.length-1;i>j;i--){ // 删去末尾的字符0
if(s.substr(i,1)!="0"){
return s.substr(0,i+1);
}
}
return s.substr(0,j);
}
function intervalt1(alpha,n,mean,ovar)
{
if (ovar <= 0) throw "var must larger than 0 in intervalt1";
var minc;
minc = tdist2inv(1 - alpha, n - 1) * Math.sqrt(ovar / n);
var x12=new Array();
x12[0] = mean - minc;
x12[1] = mean + minc;
return x12;
}
function intervalt1low(alpha,n,mean,ovar)
{
if (alpha <= 0 || alpha >= 1) "alpha must be probability in intervalt1low";
if (ovar <= 0) throw "var must larger than 0 in intervalt1low";
var minc;
minc = tdistinv(1 - alpha, n - 1) * Math.sqrt(ovar / n);
var x1;
x1 = mean - minc;
return x1;
}
function intervalt1high(alpha,n,mean,ovar)
{
if (alpha <= 0 || alpha >= 1) throw "alpha must be probability in intervalt1high";
if (ovar <= 0) throw "var must larger than 0 in intervalt1high";
var minc;
minc = tdistinv(1 - alpha, n - 1) * Math.sqrt(ovar / n);
var x2;
x2 = mean + minc;
return x2;
}
var outs1,outs2
function intervalx1(alpha,n,s2)
{
if (alpha <= 0 || alpha >= 1) throw "alpha must be probability in intervalx1!";
if (s2 <= 0) throw "var must larger than 0 in intervalx1";
var s12=new Array();
s12[0] = chiinv(1 - alpha / 2, n - 1);
s12[1] = chiinv(alpha / 2, n - 1);
s12[0] = s2 * (n - 1) / s12[0];
s12[1] = s2 * (n - 1) / s12[1];
return s12;
}
function intervalx1low(alpha,n,s2)
{
if (alpha <= 0 || alpha >= 1) throw "alpha must be probability in intervalx1low!";
if (s2 <= 0) throw "var must larger than 0 in intervalx1low!";
var s1;
s1 = chiinv(1 - alpha, n - 1);
s1 = s2 * (n - 1) / s1;
return s1;
}
function intervalx1high(alpha,n,s2)
{
if (alpha <= 0 || alpha >= 1) throw "alpha must be probability in intervalx1high!";
if (s2 <= 0) throw "var must larger than 0 in intervalx1high!";
var s22;
s22 = chiinv(alpha,n-1);
s22 = s2 * (n - 1) / s22;
return s2;
}
function intervalz1(alpha,n,bias,mean)
{
var x12;
if(bias<0) return;
if (alpha <= 0 || alpha >= 1) throw "alpha must be probability in intervalz1";
var minc;
minc = normsinv(1 - alpha / 2) *bias / Math.sqrt(n);
x12=new Array();
x12[0] = mean - minc;
x12[1] = mean + minc;
return x12;
}
function intervalz1low(alpha,n,bias,mean)
{
if(bias<=0) return;
if (alpha <= 0 || alpha >= 1) throw "alpha must be probability in intervalz1low";
var minc;
minc = normsinv(1 - alpha) *bias / Math.sqrt(n);
var x1;
x1 = mean - minc;
return x1;
}
function intervalz1high(alpha,n,bias,mean)
{
if(bias<=0) return;
if (alpha <= 0 || alpha >= 1) throw "alpha must be probability in intervalz1high";
var minc;
minc = normsinv(1 - alpha) * bias / Math.sqrt(n);
var x2;
x2 = mean + minc;
return x2;
}
function intervalz2(alpha,n1,xm,bs1,n2,ym,bs2)
{
var m,x12;
x12=new Array();
m =xm-ym;
var s1, s2;
s1 = bs1 * bs1;
s2 = bs2 * bs2;
var xinc;
xinc = normsinv(1 - alpha / 2) * Math.sqrt((s1 / n1) + (s2 / n2));
x12[0] = m - xinc;
x12[1] = m + xinc;
return x12;
}
function intervalz2low(alpha,n1,xm,bs1,n2,ym,bs2)
{
var m,x1;
m =xm-ym;
var s1, s2;
s1 = bs1 * bs1;
s2 = bs2 * bs2;
var xinc;
xinc = normsinv(1 - alpha) * Math.sqrt((s1 / n1) + (s2 / n2));
x1 = m - xinc;
return x1;
}
function intervalz2high(alpha,n1,xm,bs1,n2,ym,bs2)
{
var m,x2;
m = xm-ym;
var s1, s2;
s1 = bs1 * bs1;
s2 = bs2 * bs2;
var xinc;
xinc = normsinv(1 - alpha) * Math.sqrt((s1 / n1) + (s2 / n2));
x2 = m + xinc;
return x2;
}
function intervalt2(alpha,n1,mean1,var1,n2,mean2,var2)
{
var sw;
sw = ((n1 - 1) * var1*var1 + (n2 - 1) * var2*var2) / (n1 + n2 - 2);
sw = Math.sqrt(sw);
var xinc;
xinc=tdist2inv(1-alpha,n1+n2-2);
xinc *= sw * Math.sqrt((1.0 / n1) + (1.0 / n2));
var x12=new Array();
x12[0] = mean1 - mean2 - xinc;
x12[1] = mean1 - mean2 + xinc;
return x12;
}
function intervalt2low(alpha,n1,mean1,var1,n2,mean2,var2)
{
var sw,x1;
sw = ((n1 - 1) * var1*var1 + (n2 - 1) * var2*var2) / (n1 + n2 - 2);
sw = Math.sqrt(sw);
var xinc;
xinc = tdistinv(1 - alpha, n1 + n2 - 2);
xinc *= sw * Math.sqrt((1.0 / n1) + (1.0 / n2));
x1 = mean1 - mean2 - xinc;
return x1;
}
function intervalt2high(alpha,n1,mean1,var1,n2,mean2,var2)
{
var sw;
sw = ((n1 - 1) * var1*var1 + (n2 - 1) * var2*var2) / (n1 + n2 - 2);
sw = Math.sqrt(sw);
var xinc;
xinc = tdistinv(1 - alpha, n1 + n2 - 2);
xinc *= sw * Math.sqrt((1.0 / n1) + (1.0 / n2));
x2 = mean1 - mean2 + xinc;
return x2;
}
function intervalf2(alpha,n1,var1,n2,var2)
{
var v,f12;
v = (var1*var1) / (var2*var2);
f12=new Array();
f12[0] = v / fdistinv(1 - alpha / 2, n1 - 1, n2 - 1);
f12[1] = v / fdistinv(alpha / 2, n1 - 1, n2 - 1);
return f12;
}
function intervalf2low(alpha,n1,var1,n2,var2)
{
var v,f1;
v = (var1*var1) / (var2*var2);
f1 = v / fdistinv(1 - alpha, n1 - 1, n2 - 1);
return f1;
}
function intervalf2high(alpha,n1,var1,n2,var2)
{
var v,f2;
v = (var1*var1) / (var2*var2);
f2 = v / fdistinv(alpha, n1 - 1, n2 - 1);
return f2;
}
function geta(){
if(Radio1.checked){
return parseFloat(Radio1.value);
}
else if(Radio2.checked){
return parseFloat(Radio2.value);
}
else if(Radio3.checked){
return parseFloat(Radio3.value);
}
else if(Radio4.checked){
return parseFloat(Radio4.value);
}
return 0.05;
}
function gotoview(n){
view1.style.display="none";
view2.style.display="none";
view3.style.display="none";
view4.style.display="none";
view5.style.display="none";
view6.style.display="none";
switch (n){
case 1:
view1.style.display="block";
Button1_onclick();
break;
case 2:
view2.style.display="block";
Button2_onclick();
break;
case 3:
view3.style.display="block";
Button3_onclick();
break;
case 4:
view4.style.display="block";
Button4_onclick();
break;
case 5:
view5.style.display="block";
Button5_onclick();
break;
case 6:
view6.style.display="block";
Button6_onclick();
break;
}
}
function Radioa_onclick() {
if(Radioa1.checked){
diva.style.display="block";
divb.style.display="none";
if(Radiob1.checked)
gotoview(1);
else if(Radiob2.checked)
gotoview(2);
else if(Radiob3.checked)
gotoview(3);
}
else if(Radioa2.checked){
diva.style.display="none";
divb.style.display="block";
if(Radioc1.checked)
gotoview(4);
else if(Radioc2.checked)
gotoview(5);
else if(Radioc3.checked)
gotoview(6);
}
}
function Button1_onclick() {
var alpha,p,n,mean,bias;
alpha=geta();
p=1-alpha;
n=parseInt(Text1.value);
mean=parseFloat(Text2.value);
bias=tobias(Text3.value);
var x1,x2,x12;
label1.innerHTML="";
if(n>0 && mean<1e20 && bias>0){
x12=intervalz1(alpha,n,bias,mean);
label1.innerHTML+="双侧置信区间:
";
label1.innerHTML+="以置信概率" + dtos(p) + "肯定μ落在区间(" +dtos(x12[0]) + ", " +dtos(x12[1]) + ")中";
x1=intervalz1low(alpha,n,bias,mean);
label1.innerHTML+="单侧置信下限:
";
label1.innerHTML+="以置信概率" +dtos(p) + "肯定μ大于" +dtos(x1);
x2=intervalz1high(alpha,n,bias,mean);
label1.innerHTML+="单侧置信上限:
";
label1.innerHTML+="以置信概率" +dtos(p) + "肯定μ小于" +dtos(x2);
}
}
function Button2_onclick() {
var alpha,p,n,mean,ovar;
alpha=geta();
p=1-alpha;
n=parseInt(Text4.value);
mean=parseFloat(Text5.value);
ovar=tobias(Text6.value);
var x1,x2,x12;
label2.innerHTML="";
if(n>0 && mean<1e20 && ovar>0){
ovar*=ovar;
x12=intervalt1(alpha,n,mean,ovar);
label2.innerHTML+="双侧置信区间:
";
label2.innerHTML+="以置信概率" +dtos(p) + "肯定μ落在区间(" +dtos(x12[0]) + ", " +dtos(x12[1]) + ")中";
x1=intervalt1low(alpha,n,mean,ovar);
label2.innerHTML+="单侧置信下限:
";
label2.innerHTML+="以置信概率" +dtos(p) + "肯定μ大于" +dtos(x1);
x2=intervalt1high(alpha,n,mean,ovar);
label2.innerHTML+="单侧置信上限:
";
label2.innerHTML+="以置信概率" +dtos(p) + "肯定μ小于" +dtos(x2);
}
}
function Button3_onclick() {
var alpha,p,n,ovar;
alpha=geta();
p=1-alpha;
n=parseInt(Text7.value);
ovar=tobias(Text8.value);
var s1,s2,s12;
label3.innerHTML="";
if(n>0 && ovar>0){
ovar*=ovar;
s12=intervalx1(alpha,n,ovar);
label3.innerHTML+="双侧置信区间:
";
label3.innerHTML+="以置信概率" +dtos(p) + "肯定σ
2落在区间(" +dtos(s12[0]) + ", " +dtos(s12[1]) + ")中";
label3.innerHTML+="
或者肯定σ落在区间(" +dtos(Math.sqrt(s12[0])) + ", " +dtos(Math.sqrt(s12[1])) + ")中";
s1=intervalx1low(alpha,n,ovar);
label3.innerHTML+="单侧置信下限:
";
label3.innerHTML+= "以置信概率" +dtos(p) + "肯定σ
2大于" +dtos(s1);
label3.innerHTML+="
或者肯定σ大于" +dtos(Math.sqrt(s1));
s2=intervalx1high(alpha,n,ovar);
label3.innerHTML+="单侧置信上限:
";
label3.innerHTML+="以置信概率" +dtos(p) + "肯定σ
2小于" + dtos(s2);
label3.innerHTML+="
或者肯定σ小于" +dtos(Math.sqrt(s2));
}
}
function Button4_onclick() {
var n,xm,sig1,m,ym,sig2,alpha,p;
alpha=geta();
p=1-alpha;
n=parseInt(Text9.value);
xm=parseFloat(Text10.value);
sig1=tobias(Text11.value);
m=parseInt(Text12.value);
ym=parseFloat(Text13.value);
sig2=tobias(Text14.value);
var x1,x2,x12;
label4.innerHTML="";
if(n>0 && m>0 && xm<1e30 && ym<1e30 && sig1>0 && sig2>0){
x12=intervalz2(alpha,n,xm,sig1,m,ym,sig2);
label4.innerHTML+="双侧置信区间:
";
label4.innerHTML+="以置信概率" +dtos(p) + "肯定μ
1-μ
2落在区间("
+dtos(x12[0]) + ", " +dtos(x12[1]) + ")中";
x1=intervalz2low(alpha,n,xm,sig1,m,ym,sig2);
label4.innerHTML+="单侧置信下限:
";
label4.innerHTML+="以置信概率" +dtos(p) + "肯定μ
1-μ
2大于" +dtos(x1);
x2=intervalz2high(alpha,n,xm,sig1,m,ym,sig2);
label4.innerHTML+="单侧置信上限:
";
label4.innerHTML+="以置信概率" +dtos(p) + "肯定μ
1-μ
2小于" +dtos(x2);
}
}
function Button5_onclick() {
var n,xm,sig1,m,ym,sig2,alpha,p;
alpha=geta();
p=1-alpha;
n=parseInt(Text15.value);
xm=parseFloat(Text16.value);
sig1=tobias(Text17.value);
m=parseInt(Text18.value);
ym=parseFloat(Text19.value);
sig2=tobias(Text20.value);
var x1,x2,x12;
label5.innerHTML="";
if(n>0 && m>0 && xm<1e30 && ym<1e30 && sig1>0 && sig2>0){
x12=intervalt2(alpha,n,xm,sig1,m,ym,sig2);
label5.innerHTML+="双侧置信区间:
";
label5.innerHTML+= "以置信概率" +dtos(p) + "肯定μ
1-μ
2落在区间("
+dtos(x12[0]) + ", " +dtos(x12[1]) + ")中";
x1=intervalt2low(alpha,n,xm,sig1,m,ym,sig2);
label5.innerHTML+="单侧置信下限:
";
label5.innerHTML+="以置信概率" +dtos(p) + "肯定μ
1-μ
2大于" +dtos(x1);
x2=intervalt2high(alpha,n,xm,sig1,m,ym,sig2);
label5.innerHTML+="单侧置信上限:
";
label5.innerHTML+="以置信概率" +dtos(p) + "肯定μ
1-μ
2小于" +dtos(x2);
}
}
function Button6_onclick() {
var n,sig1,m,sig2,alpha,p;
alpha=geta();
p=1-alpha;
n=parseInt(Text21.value);
sig1=tobias(Text22.value);
m=parseInt(Text23.value);
sig2=tobias(Text24.value);
var f1,f2,f12;
label6.innerHTML="";
if(n>0 && m>0 && sig1>0 && sig2>0){
f12=intervalf2(alpha,n,sig1,m,sig2);
label6.innerHTML+= "双侧置信区间:
";
label6.innerHTML+="以置信概率" +dtos(p) + "肯定σ
12/σ
22落在区间("
+dtos(f12[0]) + ", " +dtos(f12[1]) + ")中";
f1=intervalf2low(alpha,n,sig1,m,sig2);
label6.innerHTML+= "单侧置信下限:
";
label6.innerHTML+="以置信概率" +dtos(p) + "肯定σ
12/σ
22大于" +dtos(f1);
f2=intervalf2high(alpha,n,sig1,m,sig2);
label6.innerHTML+= "单侧置信上限:
";
label6.innerHTML+="以置信概率" +dtos(p) + "肯定σ
12/σ
22小于"
+dtos(f2);
}
}// JavaScript Document
// 以下是计算t分布及线性回归等程序
function normsinv(pp){
var x,y;
var q = 1 - pp;
bb = [1.570796288,0.03706987906,-0.0008364353589,-0.0002250947176,
0.000006841218299,0.000005824238515,-0.00000104527497,8.360937017e-8,-3.231081277e-09,
3.657763036e-11,6.936233982e-13];
if (q == 0.5) return 0;
var p;
if (q > 0.5) p = 1 - q;
else p = q;
var b;
y =-Math.log(4 * p * (1 - p));
b = y * bb[10];
var i;
for (i = 9; i >= 0; i--)
{
b = y * (bb[i] + b);
}
x = Math.sqrt(b);
if (q > 0.5) x = -x;
return x;
}
function gamma( x)
{
var i;
var y, t, s, u;
var a = [ 0.0000677106,-0.0003442342,
0.0015397681,-0.0024467480,0.0109736958,
-0.0002109075,0.0742379071,0.0815782188,
0.4118402518,0.4227843370,1.0];
if (x <= 0.0)
throw "x不能小于0";
y = x;
if (y <= 1.0)
{ t = 1.0 / (y * (y + 1.0)); y += 2.0; }
else if (y <= 2.0)
{ t = 1.0 / y; y += 1.0; }
else if (y <= 3.0) t = 1.0;
else
{
t = 1.0;
while (y > 3.0)
{ y -= 1.0; t = t * y; }
}
s = a[0]; u = y - 2.0;
for (i = 1; i <= 10; i++)
s = s * u + a[i];
s *= t;
return s;
}
function betacf(a, b, x)
{
var MAXIT = 1000;
var EPS = 1e-10;
var FPMIN = 1e-8;
var m, m2;
var aa, c, d, del, h, qab, qam, qap;
qab = a + b;
qap = a + 1;
qam = a - 1;
c = 1;
d = 1 - qab * x / qap;
if (Math.abs(d) < FPMIN) d = FPMIN;
d = 1 / d;
h = d;
for (m = 1; m <= MAXIT; m++)
{
m2 = 2 * m;
aa = m * (b - m) * x / ((qam + m2) * (a + m2));
d = 1 + aa * d;
if (Math.abs(d) < FPMIN) d = FPMIN;
c = 1 + aa / c;
if (Math.abs(c) < FPMIN) c = FPMIN;
d = 1 / d;
h *= d * c;
aa = -(a + m) * (qab + m) * x / ((a + m2) * (qap + m2));
d = 1 + aa * d;
if (Math.abs(d) < FPMIN) d = FPMIN;
c = 1 + aa / c;
if (Math.abs(c) < FPMIN) c = FPMIN;
d = 1 / d;
del = d * c;
h *= del;
if (Math.abs(del - 1) <= EPS) break;
}
if (m > MAXIT) throw "a or b too big, or MAXIT too small in betacf";
return h;
}
function gammln(xx)
{
var j;
var x, y, tmp, ser;
var cof = [76.18009172947146,-86.50532032941677,24.01409824083091,-1.231739572450155,
0.1208650973866179e-2,-0.5395239384953e-5];
y = x = xx;
tmp = x + 5.5;
tmp -= (x + 0.5) * Math.log(tmp);
ser = 1.000000000190015;
for (j = 0; j < 6; j++) ser += cof[j] / ++y;
return -tmp + Math.log(2.5066282746310005 * ser / x);
}
function betai(a, b, x)
{
var bt;
if (x < 0 || x > 1) throw "Bad x in routine batai";
if (x == 0 || x == 1) bt = 0;
else
bt = Math.exp(gammln(a + b) - gammln(a) - gammln(b) + a * Math.log(x) + b * Math.log(1 - x));
if (x < (a + 1) / (a + b + 2))
return bt * betacf(a, b, x) / a;
else
return 1 - bt * betacf(b, a, 1 - x) / b;
}
function tdist2(t, free)
{
if (free < 1) throw "free must biger than 0";
var f;
f = free;
var p;
p = 1 - betai(f / 2.0, 0.5, f / (f + t * t));
return p;
}
function tdist(t, free)
{
if (t == 0)
{
return 0.5;
}
if (t > 0)
{
return 0.5 + tdist2(t, free) / 2;
}
return (1 - tdist2(-t, free)) / 2;
}
var outpp;
function t_dist(n, t)
{
var sign, ibi, n2, i;
var tt, x, p, u, ga1, ga2,dd;
if (t == 0)
{
ga1 = gamma(n / 2.0);
ga2 = gamma(0.5 + n / 2.0);
outpp = 0.5;
return ga2 / (Math.sqrt(n * Math.PI) * ga1);
}
if (t < 0)
{
sign = -1;
}
else sign = 1;
tt = t * t;
x = tt / (n + tt);
if (n % 2 == 0)
{
p = Math.sqrt(x);
u = p * (1 - x) / 2.0;
ibi = 2;
}
else
{
u = Math.sqrt(x * (1 - x)) / Math.PI;
p = 1 - 2 * Math.atan(Math.sqrt((1 - x) / x)) / Math.PI;
ibi = 1;
}
if (ibi == n)
{
dd = u / Math.abs(t);
outpp = 0.5 + sign * p / 2.0;
return dd;
}
else
{
n2 = n - 2;
}
for (i = ibi; i <= n2; i += 2)
{
p += 2.0 * u / i;
u = u * (1 + i) / i * (1 - x);
}
dd = u / Math.abs(t);
outpp = 0.5 + sign * p / 2.0;
return dd;
}
function tdistinv(pvar, n)
{
var q;
q = 1 - pvar;
var t=0.0;
var pis, dfr2, c, q2, p, yq, e, ga1, ga2, ga3, t0, pp, d;
var k;
pis = Math.sqrt(Math.PI);
dfr2 = n / 2.0;
if (n == 1) return Math.tan(Math.PI * (0.5 - q));
if (n == 2)
{
if (q > 0.5) c = -1;
else c = 1;
q2 = (1 - 2 * q);
q2 *= q2;
return Math.sqrt(2.0 * q2 / (1.0 - q2)) * c;
}
p = 1 - q;
yq = normsinv(p);
e = 1.0 - 1.0 / (4.0 * n);
e = e * e - yq * yq / (2.0 * n);
if (e > 0.5) t0 = yq / Math.sqrt(e);
else
{
ga1 = gammln(dfr2);
ga2 = gammln(dfr2 + 0.5);
ga3 = Math.exp((ga1 - ga2) / n);
t0 = Math.pow(Math.sqrt(n) / (pis * q * n), (1.0 / n) / ga3);
}
for (k = 1; k <= 30; k++)
{
d = t_dist(n, t0);
pp=outpp;
if (d == 0) return t0;
t = t0 - (pp - p) / d;
if (Math.abs(t0 - t) < 0.000001 * Math.abs(t))
{
return t;
}
else t0 = t;
}
return t;
}
function relhold(freedom, alpha)
{
var t;
t = tdistinv(1 - alpha / 2, freedom);
return t / Math.sqrt(freedom + t * t);
}
// 线性回归的核心程序
function linehuigui(data, alpha)
{
var a,b,sig2,sigx2,r,ra,t,ta,xmean;
var n;
n = data.length / 2;
var i;
var sumx = 0, sumy = 0, sumxx = 0, sumyy = 0, sumxy = 0;
var x, y;
for (i = 0; i < n; i++)
{
x = data[2 * i];
y = data[2 * i + 1];
sumx += x;
sumy += y;
sumxx += x * x;
sumyy += y * y;
sumxy += x * y;
}
xmean = sumx / n;
var sxx, syy, sxy;
sxx = sumxx - sumx * sumx / n;
syy = sumyy - sumy * sumy / n;
sxy = sumxy - sumx * sumy / n;
b = sxy / sxx;
a = (sumy / n) - (sumx / n) * b;
sig2 = (syy - b * sxy) / (n - 2.0);
sigx2 = sxx;
t = b * Math.sqrt(sxx / sig2);
ta = tdistinv(1 - alpha / 2, n - 2);
r = sxy / Math.sqrt(sxx * syy);
ra = relhold(n - 2, alpha);
var out=new Array();
out[0]=a;
out[1]=b;
out[2]=sig2;
out[3]=sigx2;
out[4]=r;
out[5]=ra;
out[6]=t;
out[7]=ta;
out[8]=xmean;
return out;
}
// 以下是绘制曲线程序
function updown(a){// 将给定的数分离成两个数相乘,一个是数的部分,另一个是指数部分,利用函数toExponential来做
var r=new Array(2);
var s,s1,s2;
s=a.toExponential(4);
var i,j,c,b;
i=s.indexOf("e");
j=s.length;
s1=s.substring(0,i);
s2=s.substring(i+1,j);
c=parseFloat(s1);
b=parseFloat(s2);
r[0]=c;
r[1]=b;
return r;
}
function getscals(low, high)// 根据最低值low和最高值high计算刻度的数量和刻度,返回一个实数矩阵,维数就是刻度,内容就是各个刻度的值
{
var l=high-low;
if(low>0)l=high;
if(high<0){
l=-low;
high=0;
}
var llow=low;
var lhigh=high;
var bs=new Array();
var bbb=new Array();
var bbb=updown(l/4);
var a=bbb[0];
var index=bbb[1];
llow/=Math.pow(10,index);
lhigh/=Math.pow(10,index);
var ascale;
if(a<=2)
ascale=2;
else if(a<=5){
ascale=5;
}
else ascale=10;
var abegin=0;
var i,d,dd,s;
if(llow<0){
for(i=1;i<1000;i++){
abegin=-i*ascale;
if(abegin<=llow)
break;
}
}
else abegin=0;
for(i=0;i<50;i++){
d=abegin+i*ascale;
dd=d*Math.pow(10,index);
s=dd.toPrecision(8);
bs[i]=parseFloat(s);
//bs[i]=d*Math.pow(10,index);
if(d>=lhigh || Math.abs(d-lhigh)<1e-5)break;
}
return bs;
}
var ctx;
var width=600,hight=400;//图像的宽度和高度
var xbegin,ybegin,xlength,ylength;//曲线的绘制范围,x和y的起始点和宽高度
var axmin,aymin,axwidth,aywidth;
function drawaxis(xl,xh,yl,yh){//绘制xy坐标轴,xl,xh,yl,yh是x和y取值的最小值和最大值
var yofx,xofy;//,ybegin,xbegin;//x轴的y坐标和y轴的x坐标及曲线的x和y的起始点
var xscalelength,yscalelength;//两个刻度之间的距离
//var xlength,ylength;//曲线的x和y的幅度
var xscals,yscals; // 是数组,存放x和y的刻度
var iyofx,ixofy;//y刻度的第几个数是0,x刻度的第几个数是0
xscals=getscals(xl,xh);
axmin=xscals[0];
axwidth=xscals[xscals.length-1]-xscals[0];
yscals=getscals(yl,yh);
aymin=yscals[0];
aywidth=yscals[yscals.length-1]-yscals[0];
yofx=ybegin=hight-30;
ylength=hight-70-30;
yscalelength=ylength/(yscals.length-1);
var i;
for(i=0;i
j;i--){ // 删去末尾的字符0
if(s.substr(i,1)!="0"){
return s.substr(0,i+1);
}
}
return s.substr(0,j);
}
function strtodouble(s)
{
var index=s.search(/\//);
if(index<0){
return parseFloat(s);
}
var ss="abc";
ss=s.substring(index+1,s.length);
var a,b;
var sss;
sss=s.substring(0,index);
a=parseFloat(ss);
b=parseFloat(sss);
//return (0.0+sss)/(0.0+ss);
return b/a;
}
function stringstodouble(s)
{
var reg_pattern=/[0123456789.\-\/]+/g;
var arr=s.match(reg_pattern);
var a=new Array();
var i;
if(arr!=null)
for(i=0;ixmax)xmax=datax[i];
if(datay[i]ymax)ymax=datay[i];
}
var canvas=document.getElementById('my_canvas');
if(canvas.getContext)
{
ctx=canvas.getContext('2d');
ctx.globalCompositeOperation="source-over";
ctx.clearRect(0,0,600,400);
ctx.lineWidth=1;
// 在下面开始绘制曲线
drawaxis(xmin,xmax,ymin,ymax);
ctx.closePath();
ctx.beginPath();
var ix,iy;
for(i=0;i";
spanresult.innerHTML+="从t检验的角度看:t检验统计量为t=" +dtos(t) + ",而t" +dtos(alpha/2) +
"(" + (n - 2).toString() + ")=" +dtos(ta)+"
";
if (Math.abs(t) > ta) {
spanresult.innerHTML += "因为|t|>tα/2, 所以拒绝b=0的原假设,回归效果显著。";
divpre.style.display="block";
}
else
{
spanresult.innerHTML += "因为|t|≤tα/2, 所以接受b=0的原假设,回归效果不好。";
}
spanresult.innerHTML+= "
从相关系数检验法的角度看:r=" + dtos(r) + ",r" +dtos(alpha) + "=" + dtos(ra)+"
";
if (Math.abs(r) > ra)
{
spanresult.innerHTML += "因为|r|>rα, 所以拒绝b=0的原假设,回归效果显著。";
}
else
{
spanresult.innerHTML += "因为|r|≤rα, 所以接受b=0的原假设,回归效果不好。";
}
ctx.lineWidth=1;
ctx.beginPath();//注意,从此开始的四句毫无意义,但是如果不加,就会有重叠
ctx.moveTo(1,1);
ctx.lineTo(2,1);
ctx.closePath();
ctx.stroke();
}
}
function Button2_onclick() {
var yinc, x0, y0;
if(Text1.value=="" || Text1.value==null){
spanr2.innerHTML="请输入x0的值";
return;
}
x0=parseFloat(Text1.value);
if(isNaN(x0)){
spanr2.innerHTML="输入x0的格式错误!";
return;
}
y0 = a + b * x0;
yinc = ta * Math.sqrt(sig2 * (((n + 1.0) / n) + (x0 - xmean) * (x0 - xmean) / sigx2));
spanr2.innerHTML="y0的预测区间是:"+dtos(y0)+"±"+dtos(yinc)+",
";
spanr2.innerHTML += "也就是:(" + dtos(y0 - yinc) + ", " + dtos(y0 + yinc) + ")";
}
function Button3_onclick() {
var s;
s="6,31,10,58,21,124,40,220,62,299,62,190,90,320,100,406,120,380";
TextArea1.value=s;
spanresult.innerHTML="";
spanr2.innerHTML="";
divpre.style.display="none";
}
function Button4_onclick() {
var s;
s="100,45,110,51,120,54,130,61,140,66,150,70,160,74,170,78,180,85,190,89";
TextArea1.value=s;
spanresult.innerHTML="";
spanr2.innerHTML="";
divpre.style.display="none";
}
function Radio1_onclick() {
spanresult.innerHTML="";
spanr2.innerHTML="";
divpre.style.display="none";
}
// JavaScript Document