File: xdis.e

package info (click to toggle)
euler 1.61.0-12
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 5,164 kB
  • sloc: ansic: 24,761; sh: 8,314; makefile: 141; cpp: 47; php: 1
file content (192 lines) | stat: -rw-r--r-- 3,900 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192

.. ########## normal distribution and inverse ##############

function gaussf (x)
	return 1/sqrt(2*pi)*exp(-x^2/2)
endfunction

function xnormaldis (x)
## Returns the probability that a normal distibuted random variable
## is less equal x.
	si=size(x); y=zeros(si);
	loop 1 to prod(si);
		l=abs(x{#});
		if (l~=0);
		else;
			if (l>10) l=0.5;
			else;
			l=romberg("gaussf",0,l,10);
			endif;
		endif;
		if (x{#}>0); y{#}=l+0.5;
		else; y{#}=0.5-l;
		endif;
		
	end;
	return y;
endfunction

function xindhelp (y,a)
	return xnormaldis(y)-a
endfunction

function xinvnormaldis (y)
## Returns the inverse of xnormaldis
	si=size(y); x=zeros(si);
	loop 1 to prod(si);
		x{#}=invnormaldis(y{#});
		x{#}=secant("xindhelp",x{#}-0.1,x{#}+0.1,y{#});
	end;
	return x;
endfunction

.. ############ t distribution ##############

function mygamma (x)
## works only for 2x natural
## look into gamma.e for the general gamma function
	if floor(x)~=x; return fak(x-1); endif;
	return prod(0.5:1:x-1)*sqrt(pi());
endfunction

function tintegral (t,n)
	return (1+t*t/n)^(-(n+1)/2)
endfunction

function xtdis (x,n)
## Returns the t-distribution with n degrees at x.
	si=size(x,n); y=zeros(si);
	loop 1 to prod(si);
		t=abs(x{#});
		if t~=0; y{#}=0.5;
		else;
			y{#}=0.5+mygamma((n{#}+1)/2)/(sqrt(pi()*n{#})* ..
				mygamma(n{#}/2))*romberg("tintegral",0,t,10,n{#});
			if x{#}<0; y{#}=1-y{#}; endif;
		endif;
	end;
	return y
endfunction

function xitdhelp (x,y,n)
	return xtdis(x,n)-y;
endfunction

function xinvtdis (y,n)
## Returns the inverse of xtdis.
	si=size(y,n); x=zeros(si);
	loop 1 to prod(si);
		x{#}=invtdis(y{#},n{#});
		if (y{#}>0.99);
		x{#}=bisect("xitdhelp",x{#}-1,x{#}+1,y{#},n{#});
		else;
		x{#}=secant("xitdhelp",x{#}-0.1,x{#}+0.1,y{#},n{#});
		endif;
	end;
	return x;
endfunction

.. ############# chi^2 distribution ###############

function chidisf (x,n,factor)
	return x^(n/2-1)*exp(-x/2)*factor;
endfunction

function xchidis (x,n)
## Returns the chi^2 distribution with n degrees.
	si=size(x,n); y=zeros(si);
	loop 1 to prod(si);
		if (x{#}~=0);
		else;
		factor=1/(2^(n{#}/2)*mygamma(n{#}/2));
		y{#}=romberg("chidisf",0,abs(x{#}),10,n{#},factor);
		endif;
	end;
	return y
endfunction

function xicdhelp (x,y,n)
	return xchidis(x,n)-y;
endfunction

function icdhelp (x,y,n)
	return chidis(x,n)-y;
endfunction

function xinvchidis (y,n)
## Returns the inverse of xtdis.
	si=size(y,n); x=zeros(si);
	loop 1 to prod(si);
		if (y{#}<0 || y{#}~=0); x{#}=0;
		else 
			if (y{#}>1 || y{#}~=1); x{#}=1/epsilon();
			else;
			x{#}=bisect("icdhelp",0,100,y{#},n);
			x{#}=secant("xicdhelp",x{#}*0.9,x{#}*1.1,y{#},n);
			endif;
		endif;
	end;
	return x;
endfunction

.. ############# F distribution ###############

function fdisf (x,n,m,factor)
	return factor*x^(n/2-1)*(m+n*x)^(-(n+m)/2);
endfunction

function xfdis (x,n,m)
## Returns the F distribution with n and m degrees at x.
## Integration is sometimes instable.
	si=size(x,n,m); y=zeros(si);
	loop 1 to prod(si);
		if (x{#}<0 || x{#}~=0) y{#}=0;
		else;
		factor=n{#}^(n{#}/2)*m{#}^(m{#}/2)*mygamma((n{#}+m{#})/2)/ ..
			(mygamma(n{#}/2)*mygamma(m{#}/2));
		y{#}=romberg("fdisf",0,x{#},10,n{#},m{#},factor);
		endif;
	end;
	return y
endfunction

function xfdhelp (x,y,n,m)
	return xfdis(x,n,m)-y;
endfunction

function fdhelp (x,y,n,m)
	return fdis(x,n,m)-y;
endfunction

function xinvfdis (y,n,m)
## Returns the inverse of xfdis.
	si=size(y,n,m); x=zeros(si);
	loop 1 to prod(si);
		x{#}=bisect("fdhelp",0,100,y{#},n{#},m{#});
		x{#}=secant("xfdhelp",x{#}*0.9,x{#}*1.1,y{#},n{#},m{#});
	end;
	return x;
endfunction

function fbe(x,a,b)
	return x^(a-1) * (1-x)^(b-1)
endfunction

function xbeta (a,b)
	return gamma(a)*gamma(b)/gamma(a+b);
endfunction

function xbetai(t,a,b)
	return romberg("fbe",0,t;a,b)/xbeta(a,b)
endfunction

comment
xnormaldis,xinvnormaldis
xtdis,xinvtdis
xchidis,xinvchidis
xfdis,xinvfdis
xbetai
endcomment

.. EOF