gphys 1.5.0 → 1.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/ChangeLog +7414 -0
- data/LICENSE.txt +1 -1
- data/Rakefile +0 -2
- data/doc/derivative/math-doc/document/images.log +385 -0
- data/doc/ep_flux/math-doc/document/images.log +1375 -0
- data/doc/ganalysis/doc/NumRu.html +203 -0
- data/doc/ganalysis/doc/NumRu/GAnalysis.html +931 -0
- data/doc/ganalysis/doc/NumRu/GAnalysis/BetaPlane.html +574 -0
- data/doc/ganalysis/doc/NumRu/GAnalysis/Fitting.html +576 -0
- data/doc/ganalysis/doc/NumRu/GAnalysis/LogP.html +425 -0
- data/doc/ganalysis/doc/NumRu/GAnalysis/Met.html +2021 -0
- data/doc/ganalysis/doc/NumRu/GAnalysis/MetZ.html +524 -0
- data/doc/ganalysis/doc/NumRu/GAnalysis/Planet.html +1047 -0
- data/doc/ganalysis/doc/NumRu/GAnalysis/QG.html +794 -0
- data/doc/ganalysis/doc/NumRu/GAnalysis/QG/Uninitialized.html +215 -0
- data/doc/ganalysis/doc/NumRu/GAnalysis/QG_common.html +603 -0
- data/doc/ganalysis/doc/NumRu/GAnalysis/QG_sphere.html +760 -0
- data/doc/ganalysis/doc/NumRu/GAnalysis/QG_sphere_common.html +251 -0
- data/doc/ganalysis/doc/NumRu/GAnalysis/QG_sphere_div.html +424 -0
- data/doc/ganalysis/doc/NumRu/GAnalysis/SigmaCoord.html +321 -0
- data/doc/ganalysis/doc/NumRu/GGraph.html +334 -0
- data/doc/ganalysis/doc/NumRu/GPhys.html +579 -0
- data/doc/ganalysis/doc/Object.html +210 -0
- data/doc/ganalysis/doc/__/__/lib/numru/ganalysis/beta_plane_rb.html +60 -0
- data/doc/ganalysis/doc/__/__/lib/numru/ganalysis/covariance_rb.html +56 -0
- data/doc/ganalysis/doc/__/__/lib/numru/ganalysis/eof_rb.html +64 -0
- data/doc/ganalysis/doc/__/__/lib/numru/ganalysis/fitting_rb.html +54 -0
- data/doc/ganalysis/doc/__/__/lib/numru/ganalysis/histogram_rb.html +58 -0
- data/doc/ganalysis/doc/__/__/lib/numru/ganalysis/log_p_rb.html +60 -0
- data/doc/ganalysis/doc/__/__/lib/numru/ganalysis/met_rb.html +60 -0
- data/doc/ganalysis/doc/__/__/lib/numru/ganalysis/met_z_rb.html +58 -0
- data/doc/ganalysis/doc/__/__/lib/numru/ganalysis/planet_rb.html +58 -0
- data/doc/ganalysis/doc/__/__/lib/numru/ganalysis/qg_rb.html +64 -0
- data/doc/ganalysis/doc/__/__/lib/numru/ganalysis/sigma_coord_rb.html +56 -0
- data/doc/ganalysis/doc/__/__/lib/numru/ganalysis_rb.html +98 -0
- data/doc/ganalysis/doc/created.rid +13 -0
- data/doc/ganalysis/doc/images/brick.png +0 -0
- data/doc/ganalysis/doc/images/brick_link.png +0 -0
- data/doc/ganalysis/doc/images/bug.png +0 -0
- data/doc/ganalysis/doc/images/bullet_black.png +0 -0
- data/doc/ganalysis/doc/images/bullet_toggle_minus.png +0 -0
- data/doc/ganalysis/doc/images/bullet_toggle_plus.png +0 -0
- data/doc/ganalysis/doc/images/date.png +0 -0
- data/doc/ganalysis/doc/images/find.png +0 -0
- data/doc/ganalysis/doc/images/loadingAnimation.gif +0 -0
- data/doc/ganalysis/doc/images/macFFBgHack.png +0 -0
- data/doc/ganalysis/doc/images/package.png +0 -0
- data/doc/ganalysis/doc/images/page_green.png +0 -0
- data/doc/ganalysis/doc/images/page_white_text.png +0 -0
- data/doc/ganalysis/doc/images/page_white_width.png +0 -0
- data/doc/ganalysis/doc/images/plugin.png +0 -0
- data/doc/ganalysis/doc/images/ruby.png +0 -0
- data/doc/ganalysis/doc/images/tag_green.png +0 -0
- data/doc/ganalysis/doc/images/wrench.png +0 -0
- data/doc/ganalysis/doc/images/wrench_orange.png +0 -0
- data/doc/ganalysis/doc/images/zoom.png +0 -0
- data/doc/ganalysis/doc/index.html +383 -0
- data/doc/ganalysis/doc/js/darkfish.js +118 -0
- data/doc/ganalysis/doc/js/jquery.js +32 -0
- data/doc/ganalysis/doc/js/quicksearch.js +114 -0
- data/doc/ganalysis/doc/js/thickbox-compressed.js +10 -0
- data/doc/ganalysis/doc/rdoc.css +763 -0
- data/ext/numru/gphys/ext_init.c +1 -0
- data/ext/numru/gphys/quad_mesh_sample.c +478 -0
- data/gphys.gemspec +2 -2
- data/lib/numru/dclext.rb +394 -14
- data/lib/numru/derivative.rb +6 -0
- data/lib/numru/ganalysis/qg.rb +6 -4
- data/lib/numru/ggraph.rb +41 -8
- data/lib/numru/gphys/gphys.rb +62 -14
- data/lib/numru/gphys/gphys_io.rb +4 -4
- data/lib/numru/gphys/version.rb +2 -2
- metadata +84 -79
- data/.gitignore +0 -14
- data/TODO_ep_flux +0 -6
- data/gphys-bigmem.gemspec +0 -44
- data/install.rb +0 -130
- data/sample/cira86_to_nc.rb +0 -122
- data/sample/druby_cli1.rb +0 -23
- data/sample/druby_cli2.rb +0 -28
- data/sample/druby_serv1.rb +0 -30
- data/sample/druby_serv2.rb +0 -51
- data/sample/ep_flux/demo_NCEP_1.rb +0 -48
- data/sample/ep_flux/demo_NCEP_2.rb +0 -57
- data/sample/ep_flux/demo_NCEP_3.rb +0 -81
- data/sample/ggraph_latlon_labelling_dr002690.rb +0 -159
- data/sample/ggraph_mapfit-axes_dr002687.rb +0 -131
- data/sample/map_projection.rb +0 -121
- data/sample/ncep_theta_coord.rb +0 -79
- data/test_old/eof_slp.rb +0 -28
- data/test_old/mltbit.dat +0 -0
- data/test_old/test_ep_flux.rb +0 -533
- data/test_old/test_multibitIO.rb +0 -19
|
@@ -0,0 +1,576 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
|
|
3
|
+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
|
4
|
+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
|
5
|
+
<head>
|
|
6
|
+
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
|
|
7
|
+
|
|
8
|
+
<title>Module: NumRu::GAnalysis::Fitting</title>
|
|
9
|
+
|
|
10
|
+
<link rel="stylesheet" href="../../rdoc.css" type="text/css" media="screen" />
|
|
11
|
+
|
|
12
|
+
<script src="../../js/jquery.js" type="text/javascript" charset="utf-8"></script>
|
|
13
|
+
<script src="../../js/thickbox-compressed.js" type="text/javascript" charset="utf-8"></script>
|
|
14
|
+
<script src="../../js/quicksearch.js" type="text/javascript" charset="utf-8"></script>
|
|
15
|
+
<script src="../../js/darkfish.js" type="text/javascript" charset="utf-8"></script>
|
|
16
|
+
|
|
17
|
+
</head>
|
|
18
|
+
<body id="top" class="module">
|
|
19
|
+
|
|
20
|
+
<div id="metadata">
|
|
21
|
+
<div id="home-metadata">
|
|
22
|
+
<div id="home-section" class="section">
|
|
23
|
+
<h3 class="section-header">
|
|
24
|
+
<a href="../../index.html">Home</a>
|
|
25
|
+
<a href="../../index.html#classes">Classes</a>
|
|
26
|
+
<a href="../../index.html#methods">Methods</a>
|
|
27
|
+
</h3>
|
|
28
|
+
</div>
|
|
29
|
+
</div>
|
|
30
|
+
|
|
31
|
+
<div id="file-metadata">
|
|
32
|
+
<div id="file-list-section" class="section">
|
|
33
|
+
<h3 class="section-header">In Files</h3>
|
|
34
|
+
<div class="section-body">
|
|
35
|
+
<ul>
|
|
36
|
+
|
|
37
|
+
<li><a href="../../__/__/lib/numru/ganalysis/fitting_rb.html?TB_iframe=true&height=550&width=785"
|
|
38
|
+
class="thickbox" title="../../lib/numru/ganalysis/fitting.rb">../../lib/numru/ganalysis/fitting.rb</a></li>
|
|
39
|
+
|
|
40
|
+
</ul>
|
|
41
|
+
</div>
|
|
42
|
+
</div>
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
</div>
|
|
46
|
+
|
|
47
|
+
<div id="class-metadata">
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
<!-- Method Quickref -->
|
|
56
|
+
<div id="method-list-section" class="section">
|
|
57
|
+
<h3 class="section-header">Methods</h3>
|
|
58
|
+
<ul class="link-list">
|
|
59
|
+
|
|
60
|
+
<li><a href="#method-i-least_square_fit">#least_square_fit</a></li>
|
|
61
|
+
|
|
62
|
+
</ul>
|
|
63
|
+
</div>
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
</div>
|
|
68
|
+
|
|
69
|
+
<div id="project-metadata">
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
<div id="classindex-section" class="section project-section">
|
|
74
|
+
<h3 class="section-header">Class/Module Index
|
|
75
|
+
<span class="search-toggle"><img src="../../images/find.png"
|
|
76
|
+
height="16" width="16" alt="[+]"
|
|
77
|
+
title="show/hide quicksearch" /></span></h3>
|
|
78
|
+
<form action="#" method="get" accept-charset="utf-8" class="initially-hidden">
|
|
79
|
+
<fieldset>
|
|
80
|
+
<legend>Quicksearch</legend>
|
|
81
|
+
<input type="text" name="quicksearch" value=""
|
|
82
|
+
class="quicksearch-field" />
|
|
83
|
+
</fieldset>
|
|
84
|
+
</form>
|
|
85
|
+
|
|
86
|
+
<ul class="link-list">
|
|
87
|
+
|
|
88
|
+
<li><a href="../../NumRu.html">NumRu</a></li>
|
|
89
|
+
|
|
90
|
+
<li><a href="../../NumRu/GAnalysis.html">NumRu::GAnalysis</a></li>
|
|
91
|
+
|
|
92
|
+
<li><a href="../../NumRu/GAnalysis/BetaPlane.html">NumRu::GAnalysis::BetaPlane</a></li>
|
|
93
|
+
|
|
94
|
+
<li><a href="../../NumRu/GAnalysis/Fitting.html">NumRu::GAnalysis::Fitting</a></li>
|
|
95
|
+
|
|
96
|
+
<li><a href="../../NumRu/GAnalysis/LogP.html">NumRu::GAnalysis::LogP</a></li>
|
|
97
|
+
|
|
98
|
+
<li><a href="../../NumRu/GAnalysis/Met.html">NumRu::GAnalysis::Met</a></li>
|
|
99
|
+
|
|
100
|
+
<li><a href="../../NumRu/GAnalysis/MetZ.html">NumRu::GAnalysis::MetZ</a></li>
|
|
101
|
+
|
|
102
|
+
<li><a href="../../NumRu/GAnalysis/Planet.html">NumRu::GAnalysis::Planet</a></li>
|
|
103
|
+
|
|
104
|
+
<li><a href="../../NumRu/GAnalysis/QG.html">NumRu::GAnalysis::QG</a></li>
|
|
105
|
+
|
|
106
|
+
<li><a href="../../NumRu/GAnalysis/QG/Uninitialized.html">NumRu::GAnalysis::QG::Uninitialized</a></li>
|
|
107
|
+
|
|
108
|
+
<li><a href="../../NumRu/GAnalysis/QG_common.html">NumRu::GAnalysis::QG_common</a></li>
|
|
109
|
+
|
|
110
|
+
<li><a href="../../NumRu/GAnalysis/QG_sphere.html">NumRu::GAnalysis::QG_sphere</a></li>
|
|
111
|
+
|
|
112
|
+
<li><a href="../../NumRu/GAnalysis/QG_sphere_common.html">NumRu::GAnalysis::QG_sphere_common</a></li>
|
|
113
|
+
|
|
114
|
+
<li><a href="../../NumRu/GAnalysis/QG_sphere_div.html">NumRu::GAnalysis::QG_sphere_div</a></li>
|
|
115
|
+
|
|
116
|
+
<li><a href="../../NumRu/GAnalysis/SigmaCoord.html">NumRu::GAnalysis::SigmaCoord</a></li>
|
|
117
|
+
|
|
118
|
+
<li><a href="../../NumRu/GGraph.html">NumRu::GGraph</a></li>
|
|
119
|
+
|
|
120
|
+
<li><a href="../../NumRu/GPhys.html">NumRu::GPhys</a></li>
|
|
121
|
+
|
|
122
|
+
<li><a href="../../Object.html">Object</a></li>
|
|
123
|
+
|
|
124
|
+
</ul>
|
|
125
|
+
<div id="no-class-search-results" style="display: none;">No matching classes.</div>
|
|
126
|
+
</div>
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
</div>
|
|
130
|
+
</div>
|
|
131
|
+
|
|
132
|
+
<div id="documentation">
|
|
133
|
+
<h1 class="module">NumRu::GAnalysis::Fitting</h1>
|
|
134
|
+
|
|
135
|
+
<div id="description" class="description">
|
|
136
|
+
|
|
137
|
+
<p>Library for function fitting</p>
|
|
138
|
+
|
|
139
|
+
</div><!-- description -->
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
<div id="5Buntitled-5D" class="documentation-section">
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
<!-- Constants -->
|
|
151
|
+
<div id="constants-list" class="section">
|
|
152
|
+
<h3 class="section-header">Constants</h3>
|
|
153
|
+
<dl>
|
|
154
|
+
|
|
155
|
+
<dt><a name="X">X</a></dt>
|
|
156
|
+
|
|
157
|
+
<dd class="description"><p>predefined Proc for fitting: Polynomial x (function of the 1st dim)</p></dd>
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
<dt><a name="XX">XX</a></dt>
|
|
161
|
+
|
|
162
|
+
<dd class="description"><p>predefined Proc for fitting: Polynomial x**2 (function of the 1st dim)</p></dd>
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
<dt><a name="XY">XY</a></dt>
|
|
166
|
+
|
|
167
|
+
<dd class="description"><p>predefined Proc for fitting: Polynomial x*y (function of the 1st&2nd
|
|
168
|
+
dims)</p></dd>
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
<dt><a name="Y">Y</a></dt>
|
|
172
|
+
|
|
173
|
+
<dd class="description"><p>predefined Proc for fitting: Polynomial y (function of the 2nd dim)</p></dd>
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
<dt><a name="YY">YY</a></dt>
|
|
177
|
+
|
|
178
|
+
<dd class="description"><p>predefined Proc for fitting: Polynomial y**2 (function of the 2nd dim)</p></dd>
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
</dl>
|
|
182
|
+
</div>
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
<!-- Methods -->
|
|
188
|
+
|
|
189
|
+
<div id="public-instance-method-details" class="method-section section">
|
|
190
|
+
<h3 class="section-header">Public Instance Methods</h3>
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
<div id="least_square_fit-method" class="method-detail ">
|
|
194
|
+
<a name="method-i-least_square_fit"></a>
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
<div class="method-heading">
|
|
198
|
+
<span class="method-name">least_square_fit</span><span
|
|
199
|
+
class="method-args">(data, grid_locs, functions, ensemble_dims=nil, indep_dims=nil)</span>
|
|
200
|
+
<span class="method-click-advice">click to toggle source</span>
|
|
201
|
+
</div>
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
<div class="method-description">
|
|
205
|
+
|
|
206
|
+
<p>Least square fit of a linear combination of any functions (basic NArray
|
|
207
|
+
version).</p>
|
|
208
|
+
|
|
209
|
+
<h3>ARGUMENTS</h3>
|
|
210
|
+
<ul><li>
|
|
211
|
+
<p><tt>data</tt> [NArray or NArrayMiss] multi-D data to fit</p>
|
|
212
|
+
</li><li>
|
|
213
|
+
<p><tt>grid_locs</tt> [Array of 1D NArrays] Grid points of independent
|
|
214
|
+
variables (so grid_locs.length == the # of independent variables).</p>
|
|
215
|
+
</li><li>
|
|
216
|
+
<p><tt>functions</tt> [Array of Procs] Proc objects to represent the
|
|
217
|
+
functions, which accept the elements of <tt>grid_locs</tt> as the arguments
|
|
218
|
+
(so the number of arguments fed is equal to the length of
|
|
219
|
+
<tt>grid_locs</tt>).</p>
|
|
220
|
+
</li><li>
|
|
221
|
+
<p><tt>ensemble_dims</tt> (optional) [nil (defualt) or Array of Integers] When
|
|
222
|
+
<tt>grid_locs.length < data.rank</tt>, this argument can be used to
|
|
223
|
+
specify the dimensions that are not included in grid_locs and are used for
|
|
224
|
+
ensemble averaging</p>
|
|
225
|
+
</li><li>
|
|
226
|
+
<p><tt>indep_dims</tt> (optional) [nil (defualt) or Array of Integers] When
|
|
227
|
+
<tt>grid_locs.length < data.rank</tt>, this argument can be used to
|
|
228
|
+
specify the dimensions that are not included in <tt>grid_locs</tt> and are
|
|
229
|
+
treated as independent, so the fitting is made for each of their component.</p>
|
|
230
|
+
</li></ul>
|
|
231
|
+
|
|
232
|
+
<p>Note that the sum of the lengths of <tt>grid_locs</tt>,
|
|
233
|
+
<tt>ensemble_dims</tt> and <tt>indep_dims</tt> must be equal to the rank (#
|
|
234
|
+
of dims) of <tt>data</tt>.</p>
|
|
235
|
+
|
|
236
|
+
<h3>RETURN VALUES</h3>
|
|
237
|
+
|
|
238
|
+
<pre>[ c, bf, diff ]</pre>
|
|
239
|
+
|
|
240
|
+
<p>where</p>
|
|
241
|
+
<ul><li>
|
|
242
|
+
<p><tt>c</tt> is a NArray containing the coefficients of the functions and the
|
|
243
|
+
constant offset; its length is one greater than the number of
|
|
244
|
+
<tt>functions</tt> because of the offset. It is 1D unless the
|
|
245
|
+
<tt>indep_dims</tt> argument is used (see the examples below).</p>
|
|
246
|
+
</li><li>
|
|
247
|
+
<p><tt>bf</tt> is a NArray having the best fit grid point values. Its rank is
|
|
248
|
+
equal to data.rank, but the lengths along <tt>ensemble_dims</tt> are
|
|
249
|
+
simply 1.</p>
|
|
250
|
+
</li><li>
|
|
251
|
+
<p>rms of the difference between the data and best fit</p>
|
|
252
|
+
</li></ul>
|
|
253
|
+
|
|
254
|
+
<h3>EXAMPLES</h3>
|
|
255
|
+
<ul><li>
|
|
256
|
+
<p>Simple 1D case</p>
|
|
257
|
+
|
|
258
|
+
<p>Line fitting:</p>
|
|
259
|
+
|
|
260
|
+
<pre>nx = 5
|
|
261
|
+
x = NArray.float(nx).indgen! - nx/2
|
|
262
|
+
data = x + x*x*0.1
|
|
263
|
+
c, bf = GAnalysis::Fitting.least_square_fit(data, [x],
|
|
264
|
+
[GAnalysis::Fitting::X])
|
|
265
|
+
p "data:", data, "c:", c, "bf:", bf</pre>
|
|
266
|
+
|
|
267
|
+
<p>Here, +GAnalysis::Fitting::X+ is a predefined Proc to represent the first
|
|
268
|
+
order polynomial x. The data values given as above follow f(x) = x +
|
|
269
|
+
x**2/10. Then the result printed by the last line is</p>
|
|
270
|
+
|
|
271
|
+
<pre>"data:"
|
|
272
|
+
NArray.float(5):
|
|
273
|
+
[ -1.6, -0.9, 0.0, 1.1, 2.4 ]
|
|
274
|
+
"c:"
|
|
275
|
+
NArray.float(2):
|
|
276
|
+
[ 1.0, 0.2 ]
|
|
277
|
+
"bf:"
|
|
278
|
+
NArray.float(5):
|
|
279
|
+
[ -1.8, -0.8, 0.2, 1.2, 2.2 ]</pre>
|
|
280
|
+
|
|
281
|
+
<p>The <tt>c</tt> values indicate that the fitting result is f(x) = 1.0*x +
|
|
282
|
+
0.2, and the <tt>bf</tt> values are its grid point values.</p>
|
|
283
|
+
|
|
284
|
+
<p>Parabolic fitting:</p>
|
|
285
|
+
|
|
286
|
+
<p>You can also fit the data by 2nd order polynomial as</p>
|
|
287
|
+
|
|
288
|
+
<pre>c, bf = GAnalysis::Fitting.least_square_fit(data, [x],
|
|
289
|
+
[GAnalysis::Fitting::XX,GAnalysis::Fitting::X])</pre>
|
|
290
|
+
|
|
291
|
+
<p>Then the result will be</p>
|
|
292
|
+
|
|
293
|
+
<pre>p c #--> [0.1, 1.0, 0.0]</pre>
|
|
294
|
+
|
|
295
|
+
<p>which indicates the original 2nd order polynomial 0.1 x**2 + x, so it
|
|
296
|
+
follows <tt>data == bf</tt> (except for round-off error if any).</p>
|
|
297
|
+
</li><li>
|
|
298
|
+
<p>1D fitting of multi-D data (ensemble case)</p>
|
|
299
|
+
|
|
300
|
+
<p>Suppose you have a 2D NArray (or NArrayMiss) data, in which the 1st dim
|
|
301
|
+
represents x and the 2nd dim represents something else (such as time
|
|
302
|
+
sequence, or just a simple ensemble). If you want to use the entire data to
|
|
303
|
+
get a single fit, use the <tt>ensemble_dims</tt> argument to specify the
|
|
304
|
+
non-x dimension(s). You can fit the data, for example, by p*sin(x) +
|
|
305
|
+
q*cos(x) + r as follows:</p>
|
|
306
|
+
|
|
307
|
+
<pre>sin = proc{|x| NMath.sin(x)}
|
|
308
|
+
cos = proc{|x| NMath.cos(x)}
|
|
309
|
+
c, bf = GAnalysis::Fitting.least_square_fit(data, [x],
|
|
310
|
+
[sin, cos], [1])</pre>
|
|
311
|
+
|
|
312
|
+
<p>Here, the last parameter [1] is given as the arguemnt
|
|
313
|
+
<tt>ensemble_dims</tt> to express that the dimension 1 (2nd dimension) of
|
|
314
|
+
<tt>data</tt> is the ensemble dimension, so the x coordinate is the
|
|
315
|
+
remaining dimension 0 (1st dimension). The coefficients of the functions
|
|
316
|
+
are returned by the 1st return value as a NArray, so</p>
|
|
317
|
+
|
|
318
|
+
<pre>p = c[0]
|
|
319
|
+
q = c[1]
|
|
320
|
+
r = c[2]</pre>
|
|
321
|
+
</li><li>
|
|
322
|
+
<p>1D fitting of multi-D data (individual fitting)</p>
|
|
323
|
+
|
|
324
|
+
<p>Suppose you have the same data as above, but you want to fit it for each of
|
|
325
|
+
the 2nd dim elements. You can do it as follows:</p>
|
|
326
|
+
|
|
327
|
+
<pre>c, bf = GAnalysis::Fitting.least_square_fit(data, [x],
|
|
328
|
+
[sin, cos], nil, [1])</pre>
|
|
329
|
+
|
|
330
|
+
<p>Here, <tt>nil</tt> is given as the 4th argument (<tt>ensemble_dims</tt>)
|
|
331
|
+
and [1] is given as the fifth (<tt>indep_dims</tt>). In this case, the
|
|
332
|
+
return value <tt>c</tt> is 2-dimensional; the first being the coefficients
|
|
333
|
+
as above and the second representing the non-x (i.e., the second) dim of
|
|
334
|
+
<tt>data</tt>.</p>
|
|
335
|
+
</li><li>
|
|
336
|
+
<p>2D fitting</p>
|
|
337
|
+
|
|
338
|
+
<p>It can be done like</p>
|
|
339
|
+
|
|
340
|
+
<pre>cosx = proc {|x,y| NMath.cos(x).newdim!(-1)}
|
|
341
|
+
sinx = proc {|x,y| NMath.sin(x).newdim!(-1)}
|
|
342
|
+
cosy = proc {|x,y| NMath.cos(y).newdim!(0)}
|
|
343
|
+
siny = proc {|x,y| NMath.sin(y).newdim!(0)}
|
|
344
|
+
c, bf = GAnalysis::Fitting.least_square_fit(data4D, [x,y],
|
|
345
|
+
[cosx, sinx, cosy, siny], [2,3])</pre>
|
|
346
|
+
|
|
347
|
+
<p>where <tt>data4D</tt> is a 4D NArray, whose first and second dimensions
|
|
348
|
+
(dimensions 0 and 1) represent x and y axis, respectively, and the 1D
|
|
349
|
+
NArrays <tt>x</tt> and <tt>y</tt> are the grid points. Note that the
|
|
350
|
+
functions (<tt>cosx</tt> etc) accept 2 arguments (x and y), and they use
|
|
351
|
+
NArray’s <tt>newdim</tt> method to return 2D NArray (newdim!(-1) inserts a
|
|
352
|
+
1-element dim to the end, and newdim(0) inserts a 1-element dim to the
|
|
353
|
+
beginning).</p>
|
|
354
|
+
</li></ul>
|
|
355
|
+
|
|
356
|
+
<p>TYPICAL ERRORS</p>
|
|
357
|
+
<ul><li>
|
|
358
|
+
<p>Error is raised (from the LU decomposition), if the problem cannot be
|
|
359
|
+
solved. That happens if you specify a same function twice (redundantly) in
|
|
360
|
+
the <tt>functions</tt> argument, as a matter of course.</p>
|
|
361
|
+
</li><li>
|
|
362
|
+
<p>Error is raised if the number of data is insuffcient for the number of
|
|
363
|
+
functions (also unsolvable).</p>
|
|
364
|
+
</li></ul>
|
|
365
|
+
|
|
366
|
+
|
|
367
|
+
|
|
368
|
+
<div class="method-source-code" id="least_square_fit-source">
|
|
369
|
+
<pre>
|
|
370
|
+
<span class="ruby-comment"># File ../../lib/numru/ganalysis/fitting.rb, line 217</span>
|
|
371
|
+
<span class="ruby-keyword">def</span> <span class="ruby-identifier">least_square_fit</span>(<span class="ruby-identifier">data</span>, <span class="ruby-identifier">grid_locs</span>, <span class="ruby-identifier">functions</span>, <span class="ruby-identifier">ensemble_dims</span>=<span class="ruby-keyword">nil</span>,
|
|
372
|
+
<span class="ruby-identifier">indep_dims</span>=<span class="ruby-keyword">nil</span>)
|
|
373
|
+
|
|
374
|
+
<span class="ruby-comment">#< argument check ></span>
|
|
375
|
+
|
|
376
|
+
<span class="ruby-identifier">grid_locs</span>.<span class="ruby-identifier">each_with_index</span>{<span class="ruby-operator">|</span><span class="ruby-identifier">x</span>,<span class="ruby-identifier">i</span><span class="ruby-operator">|</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier">ensure_1D_NArray</span>(<span class="ruby-identifier">x</span>, <span class="ruby-identifier">i</span>)}
|
|
377
|
+
<span class="ruby-identifier">functions</span>.<span class="ruby-identifier">each</span>{<span class="ruby-operator">|</span><span class="ruby-identifier">f</span><span class="ruby-operator">|</span> <span class="ruby-identifier">raise</span>(<span class="ruby-string">"Found non-Proc arg"</span>) <span class="ruby-keyword">if</span> <span class="ruby-operator">!</span><span class="ruby-identifier">f</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Proc</span>)}
|
|
378
|
+
|
|
379
|
+
<span class="ruby-identifier">functions</span> = <span class="ruby-identifier">functions</span> <span class="ruby-operator">+</span> [<span class="ruby-identifier">@@unity</span>] <span class="ruby-comment"># constanf offset</span>
|
|
380
|
+
|
|
381
|
+
<span class="ruby-identifier">ng</span> = <span class="ruby-identifier">grid_locs</span>.<span class="ruby-identifier">length</span>
|
|
382
|
+
<span class="ruby-identifier">rank</span> = <span class="ruby-identifier">data</span>.<span class="ruby-identifier">rank</span>
|
|
383
|
+
<span class="ruby-identifier">ensemble_dims</span> = [ <span class="ruby-identifier">ensemble_dims</span> ] <span class="ruby-keyword">if</span> <span class="ruby-identifier">ensemble_dims</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Integer</span>)
|
|
384
|
+
<span class="ruby-identifier">indep_dims</span> = [ <span class="ruby-identifier">indep_dims</span> ] <span class="ruby-keyword">if</span> <span class="ruby-identifier">indep_dims</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Integer</span>)
|
|
385
|
+
|
|
386
|
+
<span class="ruby-identifier">ensemble_dims</span> = <span class="ruby-constant">Array</span>.<span class="ruby-identifier">new</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">ensemble_dims</span>.<span class="ruby-identifier">nil?</span> <span class="ruby-comment"># --> always an Array</span>
|
|
387
|
+
<span class="ruby-identifier">n_indep</span> = ( <span class="ruby-identifier">indep_dims</span> <span class="ruby-operator">?</span> <span class="ruby-identifier">indep_dims</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">:</span> <span class="ruby-value">0</span> )
|
|
388
|
+
|
|
389
|
+
<span class="ruby-keyword">if</span> <span class="ruby-identifier">ng</span> <span class="ruby-operator"><</span> <span class="ruby-identifier">rank</span>
|
|
390
|
+
<span class="ruby-identifier">ensemble_dims</span> = <span class="ruby-identifier">ensemble_dims</span>.<span class="ruby-identifier">collect</span>{<span class="ruby-operator">|</span><span class="ruby-identifier">d</span><span class="ruby-operator">|</span>
|
|
391
|
+
<span class="ruby-keyword">if</span> <span class="ruby-identifier">d</span><span class="ruby-operator"><</span><span class="ruby-operator">-</span><span class="ruby-identifier">rank</span> <span class="ruby-operator">||</span> <span class="ruby-identifier">d</span><span class="ruby-operator">>=</span><span class="ruby-identifier">rank</span>
|
|
392
|
+
<span class="ruby-identifier">raise</span> <span class="ruby-node">"Invalid ensemble_dims value (#{d}) for rank #{rank} NArray"</span>
|
|
393
|
+
<span class="ruby-keyword">end</span>
|
|
394
|
+
<span class="ruby-identifier">d</span> <span class="ruby-operator">+=</span> <span class="ruby-identifier">rank</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">d</span><span class="ruby-operator"><</span><span class="ruby-value">0</span>
|
|
395
|
+
<span class="ruby-identifier">d</span>
|
|
396
|
+
}
|
|
397
|
+
<span class="ruby-identifier">ensemble_dims</span>.<span class="ruby-identifier">sort!</span>
|
|
398
|
+
<span class="ruby-keyword">if</span> <span class="ruby-identifier">indep_dims</span>
|
|
399
|
+
<span class="ruby-identifier">indep_dims</span> = <span class="ruby-identifier">indep_dims</span>.<span class="ruby-identifier">collect</span>{<span class="ruby-operator">|</span><span class="ruby-identifier">d</span><span class="ruby-operator">|</span>
|
|
400
|
+
<span class="ruby-keyword">if</span> <span class="ruby-identifier">d</span><span class="ruby-operator"><</span><span class="ruby-operator">-</span><span class="ruby-identifier">rank</span> <span class="ruby-operator">||</span> <span class="ruby-identifier">d</span><span class="ruby-operator">>=</span><span class="ruby-identifier">rank</span>
|
|
401
|
+
<span class="ruby-identifier">raise</span> <span class="ruby-node">"Invalid indep_dims value (#{d}) for rank #{rank} NArray"</span>
|
|
402
|
+
<span class="ruby-keyword">end</span>
|
|
403
|
+
<span class="ruby-identifier">d</span> <span class="ruby-operator">+=</span> <span class="ruby-identifier">rank</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">d</span><span class="ruby-operator"><</span><span class="ruby-value">0</span>
|
|
404
|
+
<span class="ruby-identifier">d</span>
|
|
405
|
+
}
|
|
406
|
+
<span class="ruby-identifier">indep_dims</span>.<span class="ruby-identifier">sort!</span>
|
|
407
|
+
<span class="ruby-keyword">end</span>
|
|
408
|
+
<span class="ruby-keyword">elsif</span> <span class="ruby-identifier">ng</span> <span class="ruby-operator">></span> <span class="ruby-identifier">rank</span>
|
|
409
|
+
<span class="ruby-identifier">raise</span> <span class="ruby-node">"# of grid_locs (#{ng}) > data.rank (#{rank})"</span>
|
|
410
|
+
<span class="ruby-keyword">end</span>
|
|
411
|
+
|
|
412
|
+
<span class="ruby-keyword">if</span> <span class="ruby-identifier">data</span>.<span class="ruby-identifier">rank</span> <span class="ruby-operator">!=</span> <span class="ruby-identifier">ng</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">ensemble_dims</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">n_indep</span>
|
|
413
|
+
<span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>,
|
|
414
|
+
<span class="ruby-string">"lengths of grid_locs, ensemble_dims and indep_dims != data.rank"</span>
|
|
415
|
+
<span class="ruby-keyword">end</span>
|
|
416
|
+
|
|
417
|
+
<span class="ruby-identifier">otherdims</span> = <span class="ruby-identifier">ensemble_dims</span>
|
|
418
|
+
<span class="ruby-keyword">if</span> <span class="ruby-identifier">indep_dims</span>
|
|
419
|
+
<span class="ruby-identifier">otherdims</span> <span class="ruby-operator">+=</span> <span class="ruby-identifier">indep_dims</span>
|
|
420
|
+
<span class="ruby-identifier">otherdims</span>.<span class="ruby-identifier">sort!</span>.<span class="ruby-identifier">uniq!</span>
|
|
421
|
+
<span class="ruby-keyword">if</span> <span class="ruby-identifier">otherdims</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">!=</span> <span class="ruby-identifier">ensemble_dims</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">n_indep</span>
|
|
422
|
+
<span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">"Overlap in ensemble_dims and indep_dims"</span>
|
|
423
|
+
<span class="ruby-keyword">end</span>
|
|
424
|
+
<span class="ruby-keyword">end</span>
|
|
425
|
+
|
|
426
|
+
<span class="ruby-comment">#< pre-process data ></span>
|
|
427
|
+
|
|
428
|
+
<span class="ruby-identifier">d0</span> = <span class="ruby-identifier">data</span>.<span class="ruby-identifier">mean</span>
|
|
429
|
+
<span class="ruby-identifier">data</span> = <span class="ruby-identifier">data</span> <span class="ruby-operator">-</span> <span class="ruby-identifier">d0</span> <span class="ruby-comment"># constant offset for numerical stability</span>
|
|
430
|
+
|
|
431
|
+
<span class="ruby-keyword">if</span> <span class="ruby-identifier">data</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">NArrayMiss</span>)
|
|
432
|
+
<span class="ruby-identifier">mask</span> = <span class="ruby-identifier">data</span>.<span class="ruby-identifier">get_mask</span>
|
|
433
|
+
<span class="ruby-keyword">elsif</span> <span class="ruby-identifier">data</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">NArray</span>)
|
|
434
|
+
<span class="ruby-identifier">mask</span> = <span class="ruby-keyword">nil</span> <span class="ruby-comment"># NArray.byte(*data.shape).fill!(1)</span>
|
|
435
|
+
<span class="ruby-keyword">else</span>
|
|
436
|
+
<span class="ruby-identifier">raise</span> <span class="ruby-node">"Data type (#{data.class}) is not NArray or NArrayMiss"</span>
|
|
437
|
+
<span class="ruby-keyword">end</span>
|
|
438
|
+
|
|
439
|
+
<span class="ruby-comment">#< derive the matrix ></span>
|
|
440
|
+
|
|
441
|
+
<span class="ruby-identifier">fv</span> = <span class="ruby-identifier">functions</span>.<span class="ruby-identifier">collect</span>{<span class="ruby-operator">|</span><span class="ruby-identifier">f</span><span class="ruby-operator">|</span>
|
|
442
|
+
<span class="ruby-identifier">f</span> = <span class="ruby-identifier">f</span>[*<span class="ruby-identifier">grid_locs</span>]
|
|
443
|
+
<span class="ruby-identifier">otherdims</span>.<span class="ruby-identifier">each</span>{<span class="ruby-operator">|</span><span class="ruby-identifier">d</span><span class="ruby-operator">|</span> <span class="ruby-identifier">f</span>.<span class="ruby-identifier">newdim!</span>(<span class="ruby-identifier">d</span>)}
|
|
444
|
+
<span class="ruby-identifier">f</span>
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
<span class="ruby-identifier">ms</span> = <span class="ruby-identifier">fv</span>.<span class="ruby-identifier">length</span> <span class="ruby-comment"># matrix size</span>
|
|
448
|
+
|
|
449
|
+
<span class="ruby-keyword">if</span> ( (<span class="ruby-identifier">len</span>=<span class="ruby-identifier">data</span>.<span class="ruby-identifier">length</span>) <span class="ruby-operator"><</span> <span class="ruby-identifier">ms</span> )
|
|
450
|
+
<span class="ruby-identifier">raise</span> <span class="ruby-node">"Insufficient data length (#{len}) for the # of funcs+1 (#{ms})"</span>
|
|
451
|
+
<span class="ruby-keyword">end</span>
|
|
452
|
+
|
|
453
|
+
<span class="ruby-identifier">mat</span> = <span class="ruby-constant">NMatrix</span>.<span class="ruby-identifier">float</span>(<span class="ruby-identifier">ms</span>,<span class="ruby-identifier">ms</span>) <span class="ruby-comment"># wil be symmetric</span>
|
|
454
|
+
|
|
455
|
+
<span class="ruby-keyword">for</span> <span class="ruby-identifier">i</span> <span class="ruby-keyword">in</span> <span class="ruby-value">0</span><span class="ruby-operator">...</span><span class="ruby-identifier">ms</span>
|
|
456
|
+
<span class="ruby-keyword">for</span> <span class="ruby-identifier">j</span> <span class="ruby-keyword">in</span> <span class="ruby-value">0</span><span class="ruby-operator">..</span><span class="ruby-identifier">i</span>
|
|
457
|
+
<span class="ruby-keyword">if</span> <span class="ruby-identifier">mask</span>
|
|
458
|
+
<span class="ruby-identifier">fvij</span> = <span class="ruby-constant">NArrayMiss</span>.<span class="ruby-identifier">to_nam</span>( <span class="ruby-identifier">fv</span>[<span class="ruby-identifier">i</span>] * <span class="ruby-identifier">fv</span>[<span class="ruby-identifier">j</span>] * <span class="ruby-identifier">mask</span>, <span class="ruby-identifier">mask</span> )
|
|
459
|
+
<span class="ruby-identifier">mat</span>[<span class="ruby-identifier">i</span>,<span class="ruby-identifier">j</span>] = (<span class="ruby-identifier">fvij</span>).<span class="ruby-identifier">mean</span>
|
|
460
|
+
<span class="ruby-keyword">else</span>
|
|
461
|
+
<span class="ruby-identifier">mat</span>[<span class="ruby-identifier">i</span>,<span class="ruby-identifier">j</span>] = (<span class="ruby-identifier">fv</span>[<span class="ruby-identifier">i</span>] * <span class="ruby-identifier">fv</span>[<span class="ruby-identifier">j</span>]).<span class="ruby-identifier">mean</span>
|
|
462
|
+
<span class="ruby-keyword">end</span>
|
|
463
|
+
<span class="ruby-keyword">end</span>
|
|
464
|
+
<span class="ruby-keyword">end</span>
|
|
465
|
+
|
|
466
|
+
<span class="ruby-keyword">for</span> <span class="ruby-identifier">i</span> <span class="ruby-keyword">in</span> <span class="ruby-value">0</span><span class="ruby-operator">...</span><span class="ruby-identifier">ms</span>
|
|
467
|
+
<span class="ruby-keyword">for</span> <span class="ruby-identifier">j</span> <span class="ruby-keyword">in</span> <span class="ruby-identifier">i</span><span class="ruby-operator">+</span><span class="ruby-value">1</span><span class="ruby-operator">...</span><span class="ruby-identifier">ms</span>
|
|
468
|
+
<span class="ruby-identifier">mat</span>[<span class="ruby-identifier">i</span>,<span class="ruby-identifier">j</span>] = <span class="ruby-identifier">mat</span>[<span class="ruby-identifier">j</span>,<span class="ruby-identifier">i</span>] <span class="ruby-comment"># symmetric</span>
|
|
469
|
+
<span class="ruby-keyword">end</span>
|
|
470
|
+
<span class="ruby-keyword">end</span>
|
|
471
|
+
<span class="ruby-comment">#p "*** mat ***",mat</span>
|
|
472
|
+
<span class="ruby-identifier">lu</span> = <span class="ruby-identifier">mat</span>.<span class="ruby-identifier">lu</span>
|
|
473
|
+
|
|
474
|
+
<span class="ruby-comment">#< derive the vector, solve, and best fit ></span>
|
|
475
|
+
|
|
476
|
+
<span class="ruby-keyword">unless</span> <span class="ruby-identifier">indep_dims</span> <span class="ruby-comment"># fitting only once</span>
|
|
477
|
+
<span class="ruby-comment"># derive the vector</span>
|
|
478
|
+
<span class="ruby-identifier">b</span> = <span class="ruby-constant">NVector</span>.<span class="ruby-identifier">float</span>(<span class="ruby-identifier">ms</span>)
|
|
479
|
+
<span class="ruby-keyword">for</span> <span class="ruby-identifier">i</span> <span class="ruby-keyword">in</span> <span class="ruby-value">0</span><span class="ruby-operator">...</span><span class="ruby-identifier">ms</span>
|
|
480
|
+
<span class="ruby-identifier">b</span>[<span class="ruby-identifier">i</span>] = (<span class="ruby-identifier">data</span> * <span class="ruby-identifier">fv</span>[<span class="ruby-identifier">i</span>]).<span class="ruby-identifier">mean</span>
|
|
481
|
+
<span class="ruby-keyword">end</span>
|
|
482
|
+
|
|
483
|
+
<span class="ruby-comment"># solve</span>
|
|
484
|
+
<span class="ruby-identifier">c</span> = <span class="ruby-identifier">lu</span>.<span class="ruby-identifier">solve</span>(<span class="ruby-identifier">b</span>)
|
|
485
|
+
<span class="ruby-identifier">c</span>[<span class="ruby-value">-1</span>] <span class="ruby-operator">+=</span> <span class="ruby-identifier">d0</span> <span class="ruby-comment"># add the mean subtracted</span>
|
|
486
|
+
|
|
487
|
+
<span class="ruby-comment"># convert c from NVector to NArray (just for cleanliness)</span>
|
|
488
|
+
<span class="ruby-identifier">na</span> = <span class="ruby-constant">NArray</span>.<span class="ruby-identifier">float</span>(<span class="ruby-identifier">ms</span>)
|
|
489
|
+
<span class="ruby-identifier">na</span>[<span class="ruby-keyword">true</span>] = <span class="ruby-identifier">c</span>[<span class="ruby-keyword">true</span>]
|
|
490
|
+
<span class="ruby-identifier">c</span> = <span class="ruby-identifier">na</span>
|
|
491
|
+
|
|
492
|
+
<span class="ruby-comment"># best fit</span>
|
|
493
|
+
<span class="ruby-identifier">bf</span> = <span class="ruby-identifier">c</span>[<span class="ruby-value">-1</span>] <span class="ruby-comment"># the constant offset</span>
|
|
494
|
+
<span class="ruby-keyword">for</span> <span class="ruby-identifier">i</span> <span class="ruby-keyword">in</span> <span class="ruby-value">0</span><span class="ruby-operator">...</span><span class="ruby-identifier">ms</span><span class="ruby-operator">-</span><span class="ruby-value">1</span>
|
|
495
|
+
<span class="ruby-identifier">bf</span> <span class="ruby-operator">+=</span> <span class="ruby-identifier">c</span>[<span class="ruby-identifier">i</span>]*<span class="ruby-identifier">fv</span>[<span class="ruby-identifier">i</span>]
|
|
496
|
+
<span class="ruby-keyword">end</span>
|
|
497
|
+
|
|
498
|
+
<span class="ruby-keyword">else</span> <span class="ruby-comment"># fitting multiple times</span>
|
|
499
|
+
|
|
500
|
+
<span class="ruby-comment"># derive vectors</span>
|
|
501
|
+
<span class="ruby-identifier">idshp</span> = <span class="ruby-identifier">indep_dims</span>.<span class="ruby-identifier">collect</span>{<span class="ruby-operator">|</span><span class="ruby-identifier">d</span><span class="ruby-operator">|</span> <span class="ruby-identifier">data</span>.<span class="ruby-identifier">shape</span>[<span class="ruby-identifier">d</span>]}
|
|
502
|
+
<span class="ruby-identifier">bs</span> = <span class="ruby-constant">NArray</span>.<span class="ruby-identifier">float</span>(<span class="ruby-identifier">ms</span>,*<span class="ruby-identifier">idshp</span>)
|
|
503
|
+
<span class="ruby-identifier">meandims</span> = (<span class="ruby-value">0</span><span class="ruby-operator">...</span><span class="ruby-identifier">rank</span>).<span class="ruby-identifier">collect</span>{<span class="ruby-operator">|</span><span class="ruby-identifier">d</span><span class="ruby-operator">|</span> <span class="ruby-identifier">d</span>} <span class="ruby-operator">-</span> <span class="ruby-identifier">indep_dims</span>
|
|
504
|
+
<span class="ruby-keyword">for</span> <span class="ruby-identifier">i</span> <span class="ruby-keyword">in</span> <span class="ruby-value">0</span><span class="ruby-operator">...</span><span class="ruby-identifier">ms</span>
|
|
505
|
+
<span class="ruby-identifier">bsi</span> = (<span class="ruby-identifier">data</span> * <span class="ruby-identifier">fv</span>[<span class="ruby-identifier">i</span>]).<span class="ruby-identifier">mean</span>(*<span class="ruby-identifier">meandims</span>)
|
|
506
|
+
<span class="ruby-keyword">if</span> <span class="ruby-identifier">bsi</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">NArrayMiss</span>)
|
|
507
|
+
<span class="ruby-keyword">if</span> <span class="ruby-identifier">bsi</span>.<span class="ruby-identifier">count_invalid</span> <span class="ruby-operator">></span> <span class="ruby-value">0</span>
|
|
508
|
+
<span class="ruby-identifier">raise</span>(<span class="ruby-string">"Found invalid data everywhere along indep_dims. Trim data in advance and try again."</span>)
|
|
509
|
+
<span class="ruby-keyword">end</span>
|
|
510
|
+
<span class="ruby-identifier">bsi</span> = <span class="ruby-identifier">bsi</span>.<span class="ruby-identifier">to_na</span>
|
|
511
|
+
<span class="ruby-keyword">end</span>
|
|
512
|
+
<span class="ruby-identifier">bs</span>[<span class="ruby-identifier">i</span>,<span class="ruby-keyword">false</span>] = <span class="ruby-identifier">bsi</span>
|
|
513
|
+
<span class="ruby-keyword">end</span>
|
|
514
|
+
<span class="ruby-identifier">idlen</span> = <span class="ruby-value">1</span>
|
|
515
|
+
<span class="ruby-identifier">idshp</span>.<span class="ruby-identifier">each</span>{<span class="ruby-operator">|</span><span class="ruby-identifier">l</span><span class="ruby-operator">|</span> <span class="ruby-identifier">idlen</span> <span class="ruby-operator">*=</span> <span class="ruby-identifier">l</span>}
|
|
516
|
+
|
|
517
|
+
<span class="ruby-comment"># solve</span>
|
|
518
|
+
<span class="ruby-identifier">bs</span> = <span class="ruby-identifier">bs</span>.<span class="ruby-identifier">reshape</span>(<span class="ruby-identifier">ms</span>, <span class="ruby-identifier">idlen</span>)
|
|
519
|
+
<span class="ruby-identifier">c</span> = <span class="ruby-constant">NArray</span>.<span class="ruby-identifier">float</span>(<span class="ruby-identifier">ms</span>,<span class="ruby-identifier">idlen</span>)
|
|
520
|
+
<span class="ruby-identifier">b</span> = <span class="ruby-constant">NVector</span>.<span class="ruby-identifier">float</span>(<span class="ruby-identifier">ms</span>)
|
|
521
|
+
<span class="ruby-keyword">for</span> <span class="ruby-identifier">id</span> <span class="ruby-keyword">in</span> <span class="ruby-value">0</span><span class="ruby-operator">...</span><span class="ruby-identifier">idlen</span>
|
|
522
|
+
<span class="ruby-identifier">b</span>[<span class="ruby-keyword">true</span>] = <span class="ruby-identifier">bs</span>[<span class="ruby-keyword">true</span>,<span class="ruby-identifier">id</span>]
|
|
523
|
+
<span class="ruby-identifier">c</span>[<span class="ruby-keyword">true</span>,<span class="ruby-identifier">id</span>] = <span class="ruby-identifier">lu</span>.<span class="ruby-identifier">solve</span>(<span class="ruby-identifier">b</span>)
|
|
524
|
+
<span class="ruby-keyword">end</span>
|
|
525
|
+
<span class="ruby-identifier">c</span>[<span class="ruby-value">-1</span>,<span class="ruby-keyword">true</span>] <span class="ruby-operator">+=</span> <span class="ruby-identifier">d0</span>
|
|
526
|
+
<span class="ruby-identifier">c</span> = <span class="ruby-identifier">c</span>.<span class="ruby-identifier">reshape</span>(<span class="ruby-identifier">ms</span>, *<span class="ruby-identifier">idshp</span>)
|
|
527
|
+
|
|
528
|
+
<span class="ruby-comment"># best fit</span>
|
|
529
|
+
<span class="ruby-identifier">idshp_full</span> = <span class="ruby-constant">Array</span>.<span class="ruby-identifier">new</span>
|
|
530
|
+
<span class="ruby-keyword">for</span> <span class="ruby-identifier">d</span> <span class="ruby-keyword">in</span> <span class="ruby-value">0</span><span class="ruby-operator">...</span><span class="ruby-identifier">rank</span>
|
|
531
|
+
<span class="ruby-keyword">if</span> <span class="ruby-identifier">indep_dims</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">d</span>)
|
|
532
|
+
<span class="ruby-identifier">idshp_full</span>[<span class="ruby-identifier">d</span>] = <span class="ruby-identifier">data</span>.<span class="ruby-identifier">shape</span>[<span class="ruby-identifier">d</span>]
|
|
533
|
+
<span class="ruby-keyword">else</span>
|
|
534
|
+
<span class="ruby-identifier">idshp_full</span>[<span class="ruby-identifier">d</span>] = <span class="ruby-value">1</span>
|
|
535
|
+
<span class="ruby-keyword">end</span>
|
|
536
|
+
<span class="ruby-keyword">end</span>
|
|
537
|
+
<span class="ruby-identifier">cs</span> = <span class="ruby-identifier">c</span>.<span class="ruby-identifier">reshape</span>(<span class="ruby-identifier">ms</span>, *<span class="ruby-identifier">idshp_full</span>)
|
|
538
|
+
<span class="ruby-identifier">bf</span> = <span class="ruby-identifier">cs</span>[<span class="ruby-value">-1</span>,<span class="ruby-keyword">false</span>]
|
|
539
|
+
<span class="ruby-keyword">for</span> <span class="ruby-identifier">i</span> <span class="ruby-keyword">in</span> <span class="ruby-value">0</span><span class="ruby-operator">...</span><span class="ruby-identifier">ms</span><span class="ruby-operator">-</span><span class="ruby-value">1</span>
|
|
540
|
+
<span class="ruby-identifier">bf</span> <span class="ruby-operator">+=</span> <span class="ruby-identifier">cs</span>[<span class="ruby-identifier">i</span>,<span class="ruby-keyword">false</span>]*<span class="ruby-identifier">fv</span>[<span class="ruby-identifier">i</span>]
|
|
541
|
+
<span class="ruby-keyword">end</span>
|
|
542
|
+
|
|
543
|
+
<span class="ruby-keyword">end</span>
|
|
544
|
+
|
|
545
|
+
<span class="ruby-identifier">diff</span> = <span class="ruby-constant">Math</span>.<span class="ruby-identifier">sqrt</span>( ( (<span class="ruby-identifier">data</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">d0</span> <span class="ruby-operator">-</span> <span class="ruby-identifier">bf</span>)<span class="ruby-operator">**</span><span class="ruby-value">2</span> ).<span class="ruby-identifier">mean</span> )
|
|
546
|
+
|
|
547
|
+
<span class="ruby-comment">#< return ></span>
|
|
548
|
+
|
|
549
|
+
[ <span class="ruby-identifier">c</span>, <span class="ruby-identifier">bf</span>, <span class="ruby-identifier">diff</span> ]
|
|
550
|
+
<span class="ruby-keyword">end</span></pre>
|
|
551
|
+
</div><!-- least_square_fit-source -->
|
|
552
|
+
|
|
553
|
+
</div>
|
|
554
|
+
|
|
555
|
+
|
|
556
|
+
|
|
557
|
+
|
|
558
|
+
</div><!-- least_square_fit-method -->
|
|
559
|
+
|
|
560
|
+
|
|
561
|
+
</div><!-- public-instance-method-details -->
|
|
562
|
+
|
|
563
|
+
</div><!-- 5Buntitled-5D -->
|
|
564
|
+
|
|
565
|
+
|
|
566
|
+
</div><!-- documentation -->
|
|
567
|
+
|
|
568
|
+
<div id="validator-badges">
|
|
569
|
+
<p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
|
|
570
|
+
<p><small>Generated with the <a href="http://deveiate.org/projects/Darkfish-Rdoc/">Darkfish
|
|
571
|
+
Rdoc Generator</a> 2</small>.</p>
|
|
572
|
+
</div>
|
|
573
|
+
|
|
574
|
+
</body>
|
|
575
|
+
</html>
|
|
576
|
+
|