39
|
1 |
#======================================================================
|
|
2 |
# . L M F I T . N O R M A L
|
|
3 |
# doc: Wed Feb 24 09:40:06 1999
|
|
4 |
# dlm: Fri Jul 28 13:35:24 2006
|
|
5 |
# (c) 1999 A.M. Thurnherr
|
|
6 |
# uE-Info: 34 51 NIL 0 0 72 2 2 4 NIL ofnI
|
|
7 |
#======================================================================
|
|
8 |
|
|
9 |
# What you need to provide if you wanna fit a different
|
|
10 |
# model function to your data:
|
|
11 |
# - a number of global variables to be set during loading
|
|
12 |
# - a number of subs to perform admin tasks (usage, init, ...)
|
|
13 |
# - a sub to evaluate the model function which is to be fitted using
|
|
14 |
# a number of pararams which are all stored in @A (beginning at
|
|
15 |
# A[1]!!!). You also need to return the partial derivatives of
|
|
16 |
# the model function wrt all params.
|
|
17 |
# - the interface is documented between +++++++ lines
|
|
18 |
|
|
19 |
# check if a given distribution is normal
|
|
20 |
# NB:
|
|
21 |
# - fitting is based on gauss curve fitting [.lmfit.gauss]
|
|
22 |
# - heuristics are taken from there and scaled for the normal
|
|
23 |
# parameter choices
|
|
24 |
# - simplified, e.g. y-shift is removed (does not make sense for
|
|
25 |
# distribution)
|
|
26 |
# - added chi^2 significance testing to &modelCleanup() on -x
|
|
27 |
|
|
28 |
# HISTORY:
|
|
29 |
# Oct 04, 1999: - created from [.lmfit.gauss]
|
|
30 |
# Oct 05, 1999: - added chi^2 significance test
|
|
31 |
# - removed -y
|
|
32 |
# - improved heuristics
|
|
33 |
# Mar 17, 2001: - param->arg
|
|
34 |
# Jul 28, 2006: - Version 3.3 [HISTORY]; &isnan()
|
|
35 |
|
|
36 |
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
37 |
#
|
|
38 |
# THE FOLLOWING VARIABLES MUST BE SET GLOBALLY (i.e. during loading)
|
|
39 |
#
|
|
40 |
# $modelOpts string of allowed options
|
|
41 |
# $modelOptsUsage usage information string for options
|
|
42 |
# $modelMinArgs min # of arguments of model
|
|
43 |
# $modelArgsUsage usage information string for arguments
|
|
44 |
#
|
|
45 |
# The following variables may be set later but not after &modelInit()
|
|
46 |
#
|
|
47 |
# $modelNFit number of params to fit in model
|
|
48 |
# @nameA symbolic names of model parameters
|
|
49 |
#
|
|
50 |
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
51 |
|
|
52 |
$modelOpts = "x";
|
|
53 |
$modelOptsUsage = "[-x chi^2 test]";
|
|
54 |
$modelMinArgs = 0;
|
|
55 |
$modelArgsUsage = "[area guess [mean guess [sigma guess]]]";
|
|
56 |
$modelNFit = 3;
|
|
57 |
$nameA[1] = "area";
|
|
58 |
$nameA[2] = "mean";
|
|
59 |
$nameA[3] = "sigma";
|
|
60 |
|
|
61 |
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
62 |
#
|
|
63 |
# &modelUsage() mangle parameters; NB: there may be `infinite' # of
|
|
64 |
# filenames after model arguments; this usually sets
|
|
65 |
# @A (the model parameters) but these can later be
|
|
66 |
# calculated heuristically during &modelInit()
|
|
67 |
#
|
|
68 |
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
69 |
|
|
70 |
sub modelUsage()
|
|
71 |
{
|
|
72 |
$A[1] = nan; $A[2] = nan; $A[3] = nan; # usage
|
|
73 |
$A[1] = &antsFloatArg() if ($#ARGV >= 0 && ! -r $ARGV[0]);
|
|
74 |
$A[2] = &antsFloatArg() if ($#ARGV >= 0 && ! -r $ARGV[0]);
|
|
75 |
$A[3] = &antsFloatArg() if ($#ARGV >= 0 && ! -r $ARGV[0]);
|
|
76 |
&antsUsageError() unless ($#ARGV < 0 || -r $ARGV[0]);
|
|
77 |
}
|
|
78 |
|
|
79 |
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
80 |
#
|
|
81 |
# &modelInit() initializes model after reading of data
|
|
82 |
#
|
|
83 |
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
84 |
|
|
85 |
sub modelInit()
|
|
86 |
{
|
|
87 |
my($i,$j,$ymin,$ymax,$xatymax);
|
|
88 |
|
|
89 |
# --------------------------------------------------
|
|
90 |
# heuristics for initial model param values
|
|
91 |
# --------------------------------------------------
|
|
92 |
|
|
93 |
$ymin = 1e33, $ymax = -1e33, $xatymax = 0;
|
|
94 |
for ($i=0; $i<=$#ants_; $i++) {
|
|
95 |
next if ($antsFlagged[$i]);
|
|
96 |
$ymin = $ants_[$i][$yfnr]
|
|
97 |
if ($ants_[$i][$yfnr] < $ymin);
|
|
98 |
$ymax = $ants_[$i][$yfnr], $xatymax = $ants_[$i][$xfnr]
|
|
99 |
if ($ants_[$i][$yfnr] > $ymax);
|
|
100 |
}
|
|
101 |
$A[1] = $ymax - $ymin if isnan($A[1]); # peak guess
|
|
102 |
$A[2] = $xatymax if isnan($A[2]); # mean guess
|
|
103 |
if (isnan($A[3])) { # e-scale guess
|
|
104 |
for ($i=1;
|
|
105 |
$i<=$#ants_ && !$antsFlagged[$i]
|
|
106 |
&& $ants_[$i][$yfnr]-$ymin<0.36*$A[1];
|
|
107 |
$i++) {}
|
|
108 |
for ($j=$#ants_;
|
|
109 |
$j>=1 && !$antsFlagged[$i]
|
|
110 |
&& $ants_[$j][$yfnr]-$ymin < 0.36*$A[1];
|
|
111 |
$j--) {}
|
|
112 |
$A[3] = abs($ants_[$i][$xfnr]-$ants_[$j][$xfnr]) / 2.0;
|
|
113 |
if ($A[3] == 0.0) {
|
|
114 |
&antsInfo("$model: sigma heuristic failed (set to 1)!");
|
|
115 |
$A[3] = 1.0;
|
|
116 |
}
|
|
117 |
}
|
|
118 |
|
|
119 |
$A[1] *= 1.77 * $A[3] # gauss -> normal
|
|
120 |
unless (isnan($A[1]) || isnan($A[3]));
|
|
121 |
$A[3] *= 0.71 unless isnan($A[3]);
|
|
122 |
}
|
|
123 |
|
|
124 |
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
125 |
#
|
|
126 |
# &modelEvaluate(x,A,dyda) evaluate Normal distribution curve at x
|
|
127 |
# x x value (NOT xfnr)
|
|
128 |
# A reference to @A
|
|
129 |
# dyda reference to array for partial derivatives
|
|
130 |
# (wrt individual params in @A)
|
|
131 |
# <ret val> y value
|
|
132 |
#
|
|
133 |
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
134 |
|
|
135 |
sub modelEvaluate($$$)
|
|
136 |
{
|
|
137 |
my($x,$AR,$dydaR) = @_;
|
|
138 |
|
|
139 |
my($peak) = $AR->[1] / (2.506628274631 * $AR->[3]);
|
|
140 |
my($dx ) = $x - $AR->[2];
|
|
141 |
my($sig2) = $AR->[3] * $AR->[3];
|
|
142 |
my($expo) = exp(-$dx*$dx/(2*$sig2));
|
|
143 |
my($norm) = $peak * $expo;
|
|
144 |
|
|
145 |
if (defined($dydaR)) {
|
|
146 |
$dydaR->[1] = $norm / $AR->[1];
|
|
147 |
$dydaR->[2] = $norm * $dx / $sig2;
|
|
148 |
$dydaR->[3] = $norm/$AR->[3] * ($dx*$dx/$sig2 - 1);
|
|
149 |
}
|
|
150 |
|
|
151 |
return $norm;
|
|
152 |
}
|
|
153 |
|
|
154 |
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
155 |
# &modelCleanup() cleans up after fitting but before output
|
|
156 |
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
157 |
|
|
158 |
sub modelCleanup()
|
|
159 |
{
|
|
160 |
return unless ($opt_x);
|
|
161 |
|
|
162 |
require "$ANTS/libfuns.pl";
|
|
163 |
my($chisq) = 0;
|
|
164 |
my($nval,$prob,$sign);
|
|
165 |
|
|
166 |
for ($i=0; $i<=$#ants_; $i++) {
|
|
167 |
next if ($antsFlagged[$i]);
|
|
168 |
# next if ($ants_[$i][$yfnr] <= 1); # IGNORE TAIL HEURISTICS
|
|
169 |
$nval = &modelEvaluate($ants_[$i][$xfnr],\@A);
|
|
170 |
$chisq += ($ants_[$i][$yfnr] - $nval)**2 / $nval;
|
|
171 |
}
|
|
172 |
$prob = &gammq(($ndata-3)/2,$chisq/2);
|
|
173 |
$sign = int($prob*100);
|
|
174 |
&antsInfo("$model: normal-distr. hypothesis disproved at $sign%% sign. level");
|
|
175 |
}
|