-
Notifications
You must be signed in to change notification settings - Fork 15
/
func_expdist.ncl
52 lines (47 loc) · 1.09 KB
/
func_expdist.ncl
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
undef("expdist")
function expdist(x[*],ib)
begin
;; generate exponential distribution by given x and beta
;; beta(b) should be scalar and float/double
;; f(x) = exp(-x/b)
if(typeof(ib).eq."integer")then
b = int2flt(ib)
else
b = ib
end if
y = exp(-x/b)
if(isdimnamed(x,0))then
y!0 = x!0
y&$y!0$ = x&$x!0$
end if
return y
end
undef("fitTyCountsExp")
function fitTyCountsExp(counts[*])
begin
;; fit exponential distribution by given x and beta
;; counts(0) must .eq. 1
;; return best b
vmaxs = counts&$counts!0$
x = int2flt((vmaxs-35))/35
x!0 = "x"
x&x = x
olderr = 100.
oldb = 0.
do i = -99, 99
b = 1. + int2flt(i)/100
ga= expdist(x,b)
ga = ga*b
err = counts-ga
gaerr = sum(abs(err))/dimsizes(counts)
if(olderr.gt.gaerr)then
olderr = gaerr
oldb = b
else
;print("rmse/beta: "+olderr+"/"+oldb)
break
end if
;print("rmse/beta: "+gaerr+"/"+b)
end do
return oldb
end