-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsim_functional_data.html
More file actions
554 lines (459 loc) · 22.1 KB
/
sim_functional_data.html
File metadata and controls
554 lines (459 loc) · 22.1 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="pandoc" />
<meta name="author" content="suswei" />
<title>Functional manifold data examples</title>
<script src="site_libs/jquery-1.11.3/jquery.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="site_libs/bootstrap-3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<script src="site_libs/bootstrap-3.3.5/js/bootstrap.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/html5shiv.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/respond.min.js"></script>
<script src="site_libs/navigation-1.1/tabsets.js"></script>
<link href="site_libs/highlightjs-9.12.0/default.css" rel="stylesheet" />
<script src="site_libs/highlightjs-9.12.0/highlight.js"></script>
<style type="text/css">code{white-space: pre;}</style>
<style type="text/css">
pre:not([class]) {
background-color: white;
}
</style>
<script type="text/javascript">
if (window.hljs) {
hljs.configure({languages: []});
hljs.initHighlightingOnLoad();
if (document.readyState && document.readyState === "complete") {
window.setTimeout(function() { hljs.initHighlighting(); }, 0);
}
}
</script>
<style type="text/css">
h1 {
font-size: 34px;
}
h1.title {
font-size: 38px;
}
h2 {
font-size: 30px;
}
h3 {
font-size: 24px;
}
h4 {
font-size: 18px;
}
h5 {
font-size: 16px;
}
h6 {
font-size: 12px;
}
.table th:not([align]) {
text-align: left;
}
</style>
</head>
<body>
<style type = "text/css">
.main-container {
max-width: 940px;
margin-left: auto;
margin-right: auto;
}
code {
color: inherit;
background-color: rgba(0, 0, 0, 0.04);
}
img {
max-width:100%;
height: auto;
}
.tabbed-pane {
padding-top: 12px;
}
.html-widget {
margin-bottom: 20px;
}
button.code-folding-btn:focus {
outline: none;
}
</style>
<style type="text/css">
/* padding for bootstrap navbar */
body {
padding-top: 51px;
padding-bottom: 40px;
}
/* offset scroll position for anchor links (for fixed navbar) */
.section h1 {
padding-top: 56px;
margin-top: -56px;
}
.section h2 {
padding-top: 56px;
margin-top: -56px;
}
.section h3 {
padding-top: 56px;
margin-top: -56px;
}
.section h4 {
padding-top: 56px;
margin-top: -56px;
}
.section h5 {
padding-top: 56px;
margin-top: -56px;
}
.section h6 {
padding-top: 56px;
margin-top: -56px;
}
</style>
<script>
// manage active state of menu based on current page
$(document).ready(function () {
// active menu anchor
href = window.location.pathname
href = href.substr(href.lastIndexOf('/') + 1)
if (href === "")
href = "index.html";
var menuAnchor = $('a[href="' + href + '"]');
// mark it active
menuAnchor.parent().addClass('active');
// if it's got a parent navbar menu mark it active as well
menuAnchor.closest('li.dropdown').addClass('active');
});
</script>
<div class="container-fluid main-container">
<!-- tabsets -->
<script>
$(document).ready(function () {
window.buildTabsets("TOC");
});
</script>
<!-- code folding -->
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.html">The hidden manifold distance for functional manifold data</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>
<a href="index.html">Paper</a>
</li>
<li>
<a href="sim_functional_data.html">Functional manifold simulation scenarios</a>
</li>
<li>
<a href="test_analytic_geodesic_distance.html">Verifying analytic geodesic distance derivation</a>
</li>
<li>
<a href="README.html">About</a>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
</ul>
</div><!--/.nav-collapse -->
</div><!--/.container -->
</div><!--/.navbar -->
<div class="fluid-row" id="header">
<h1 class="title toc-ignore">Functional manifold data examples</h1>
<h4 class="author"><em>suswei</em></h4>
<h4 class="date"><em>Tue Jun 25 16:30:09 2019</em></h4>
</div>
<div id="scenario-1" class="section level2">
<h2>Scenario 1:</h2>
<p>Consider the manifold <span class="math display">\[ {\mathcal{M}}= \{X_\alpha: -1 \le \alpha \le 1\} \]</span> with the <span class="math inline">\(L_2\)</span> inner product as the metric tensor, where <span class="math inline">\(X_\alpha: [a,b] \to {\mathbb{R}}\)</span> is given by <span class="math inline">\(X_\alpha(t) = t-\alpha\)</span>. Below, we set <span class="math inline">\(a=-4\)</span> and <span class="math inline">\(b=4\)</span>. The shortest path <span class="math inline">\(\gamma: [0,1] \to {\mathcal{M}}\)</span> between two functions <span class="math inline">\(X_{\alpha_1} = (t-\alpha_1)\)</span> and <span class="math inline">\(X_{\alpha_2} = (t-\alpha_2)\)</span> in <span class="math inline">\({\mathcal{M}}\)</span> is clearly given by <span class="math inline">\(\gamma(t) = t X_{\alpha_1} + (1-t) X_{\alpha_2}\)</span>. The geodesic distance between <span class="math inline">\(X_{\alpha_1}\)</span> and <span class="math inline">\(X_{\alpha_2}\)</span> is <span class="math display">\[L(\gamma) = \int_0^1 || \dot \gamma(t) ||_{L^2} \,dt = ||X_{\alpha_1} - X_{\alpha_2}||_{L_2} = (\alpha_1-\alpha_2)\sqrt{b-a}.\]</span></p>
</div>
<div id="scenario-2" class="section level2">
<h2>Scenario 2:</h2>
This scenario is modified from what is referred to as Manifold 2 in Chen and Muller 2012 by fixing the variance of the normal density to be <span class="math inline">\(1\)</span>. We have <span class="math display">\[{\mathcal{M}}= \left \{X_\beta: \beta \in [-1,1], t \in [a,b]\right \}\]</span> with the <span class="math inline">\(L_2\)</span> inner product as the metric tensor of <span class="math inline">\({\mathcal{M}}\)</span>, where <span class="math inline">\(X_\beta: [a,b] \to {\mathbb{R}}\)</span> is given by <span class="math inline">\(X_\beta(t) = \frac{1}{\sqrt{2\pi}} \exp{[-\frac{1}{2}(t-\beta)^2]}\)</span>. Below, we set <span class="math inline">\(a=-4\)</span> and <span class="math inline">\(b=4\)</span>. The geodesic distance between the curves <span class="math inline">\(X_{\beta_1}\)</span> and <span class="math inline">\(X_{\beta_2}\)</span> is given by
<span class="math display">\[\begin{eqnarray*}
d(X_{\beta_1},X_{\beta_2}) &=& \int_{\beta_1}^{\beta_2} \left \| \frac{d X_\beta (t)}{d\beta} \right\|_{L^2} d\beta \\
&=& \int_{\beta_1}^{\beta_2} \sqrt{ \frac{1}{2\sqrt{\pi}} \int_{-4}^4 \frac{1}{\sqrt{\pi}} \exp\{-(t-\beta)^2\}(t-\beta)^2 dt } \ d\beta \\
&=& \int_{\beta_1}^{\beta_2} \sqrt{ \frac{1}{2\sqrt{\pi}} \int_{-4}^4(t-\beta)^2 f(t) dt } \ d\beta, \textrm{ where $f$ is the density of a N$(\beta,1/2)$ } \\
&\approx& \int_{\beta_1}^{\beta_2} \sqrt{ \frac{1}{2\sqrt{\pi}} \frac{1}{2}} \ d\beta \\
&=& (\beta_2-\beta_1) \frac{1}{2\pi^{1/4}},
\end{eqnarray*}\]</span>
<p>where the approximation comes from the fact that we are integrating on <span class="math inline">\([a,b]=[-4,4]\)</span> and not on <span class="math inline">\({\mathbb{R}}\)</span>. We can see this manifold is isometric, since the geodesic distance between <span class="math inline">\(X_{\beta_1}\)</span> and <span class="math inline">\(X_{\beta_2}\)</span> in <span class="math inline">\({\mathcal{M}}\)</span> is the Euclidan distance between the <span class="math inline">\(\beta\)</span>’s, up to some scaling factor. Note that the “straight” line connecting <span class="math inline">\(X_{\beta_1}\)</span> and <span class="math inline">\(X_{\beta_2}\)</span> in <span class="math inline">\({\mathcal{M}}\)</span> does not always stay inside of <span class="math inline">\({\mathcal{M}}\)</span>, so we cannot employ the calculation technique of Scenario 1.</p>
</div>
<div id="scenario-3" class="section level2">
<h2>Scenario 3:</h2>
<p>Fix <span class="math inline">\(\mu_1,\sigma_1^2, \mu_2, \sigma_2^2\)</span>. Let <span class="math inline">\(f_1\)</span> be the normal density with mean <span class="math inline">\(\mu_1\)</span> and variance <span class="math inline">\(\sigma_1^2\)</span>. Define <span class="math inline">\(f_2\)</span> analogously. Consider the manifold <span class="math display">\[ {\mathcal{M}}= \{ X_c: 0 \le c \le 1 \} \]</span> with the <span class="math inline">\(L_2\)</span> inner product as the metric tensor where <span class="math inline">\(X_c: [a,b] \to {\mathbb{R}}\)</span> is given by <span class="math inline">\(X_c(t) = c f_1(t) + (1-c) f_2(t)\)</span>. Again, since <span class="math display">\[ \gamma(t) := t X_{c_1} + (1-t) X_{c_2}\]</span> belongs to <span class="math inline">\(M\)</span> for all <span class="math inline">\(t\)</span>, it must be the shortest path between <span class="math inline">\(X_1\)</span> and <span class="math inline">\(X_2\)</span>. The geodesic distance between <span class="math inline">\(X_{c_1}\)</span> and <span class="math inline">\(X_{c_1}\)</span> is <span class="math display">\[\int_0^1 || \dot \gamma(t) ||_{L^2} \,dt = ||X_1 - X_2||_{L_2} = (c_1-c_2) ||f_1 - f_2||_{L_2}\]</span> which has no easy analytic solution but which we estimate using numerical integration. One may also verify the equation <span class="math display">\[ \int_{c_1}^{c_2} \left \| \frac{d X_c (t)}{dc} \right\|_{L^2} dc \]</span> gives the same result.</p>
</div>
<div id="scenario-4" class="section level2">
<h2>Scenario 4:</h2>
<p>It was shown in <a href="https://www-sop.inria.fr/ariana/Projets/Shapes/ThirdYearReport/JoshietalCVPR07b.pdf">Srivastava2007</a> that the square root representation of probability density functions has a nice closed form geodesic. They consider the manifold <span class="math display">\[ {\mathcal{M}}= \{ \psi:[0,1] \to {\mathbb{R}}: \psi \ge 0, \int_0^1 \psi^2(s) \,ds = 1 \}\]</span> with the metric tensor given by the Fisher-Rao metric tensor <span class="math display">\[ <v_1,v_2> = \int_0^1 v_1(s) v_2(s) \,ds \]</span> for two tangent vectors <span class="math inline">\(v_1,v_2 \in T_\psi({\mathcal{M}})\)</span>. Note that this concides with the <span class="math inline">\(L_2[0,1]\)</span> inner product. [Srivastava2007] showed that the geodesic distance between any two <span class="math inline">\(\psi_1\)</span> and <span class="math inline">\(\psi_2\)</span> in <span class="math inline">\({\mathcal{M}}\)</span> is simply <span class="math display">\[d(\psi_1,\psi_2) = \cos^{-1}<\psi_1,\psi_2>\]</span>. We will specifically examine the square root of <span class="math inline">\(Beta(\alpha,\beta)\)</span> distributions which is supported on <span class="math inline">\([0,1]\)</span>. That is, <span class="math display">\[ M = \{ \psi_{\alpha,\beta}: 1 \le \alpha \le 5, 2 \le \beta \le 5\} \]</span> where <span class="math inline">\(\psi_{\alpha,\beta}: [0,1] \to {\mathbb{R}}\)</span> is the pdf of <span class="math inline">\(Beta(\alpha,\beta)\)</span>.</p>
</div>
<div id="scenario-5" class="section level2">
<h2>Scenario 5:</h2>
<p>This is based on Equations (17) and (18) of <a href="https://statistics.uni-bonn.de/fileadmin/Fachbereich_Wirtschaft/Einrichtungen/Statistik/WS0910/Topics_in_Econometrics_and_Statistics/Register2PCA.pdf" class="uri">https://statistics.uni-bonn.de/fileadmin/Fachbereich_Wirtschaft/Einrichtungen/Statistik/WS0910/Topics_in_Econometrics_and_Statistics/Register2PCA.pdf</a> but with <span class="math inline">\(z_{i1}, z_{i2}\)</span> set to <span class="math inline">\(1\)</span>. Let <span class="math inline">\(X_\alpha(t) = \mu(h_\alpha(t))\)</span> defined on <span class="math inline">\([-3,3]\)</span> where <span class="math display">\[ \mu(t) = \exp\{(t-1.5)^2/2\} + \exp\{(t+1.5)^2/2\}\]</span> and <span class="math display">\[ h_\alpha(t) = 6 \frac{ \exp\{\alpha(t+3)/6\} - 1}{\exp\{\alpha\}-1} \]</span> Consider the manifold <span class="math display">\[ M = \{X_\alpha: -1 \le \alpha \le 1\} \]</span>. The geodesic distance is then <span class="math display">\[ d(X_{\alpha_1},X_{\alpha_2}) = \int_{\alpha_1}^{\alpha_2} \left \| \frac{d X_\alpha (t)}{d\alpha} \right\|_{L^2} d\alpha\]</span></p>
<pre class="r"><code>## Input
# sce: scenario number
# samplesize: number of functional data
# K: number of time grid points
# SNR (signal to noise ratio)
# reg_sampling : 0 if manifold parameters are drawn uniformly and 1 if manifold parameters are drawn from a concentrated measure. SHOULD ALWAYS SET THIS TO 1
# com_grid : if 1 each curve is observed on the same grid and if 0 the grid for each curve is randomly genarated from unif[a,b]
# plot_true : if 1 plot the true data, the observed data and the true geodesic matrix
## Output
# noiseless_data : samplesize x K matrix containing the original data (no noise)
# noisy_data : samplesize x K matrix containing the observed data
# analytic_geo : samplesize x samplesize matrix containing analytic pairwise geo disctance
# grid : samplesize x K matrix containing the grid on which each data is observed
# reg_grid : vector of dim K containing a common grid to use for smoothing
#Example of call
# data<- sim_functional_data(1,100,15,0.5,1,1,1)
library(fields)</code></pre>
<pre><code>## Loading required package: spam</code></pre>
<pre><code>## Loading required package: dotCall64</code></pre>
<pre><code>## Loading required package: grid</code></pre>
<pre><code>## Spam version 2.2-0 (2018-06-19) is loaded.
## Type 'help( Spam)' or 'demo( spam)' for a short introduction
## and overview of this package.
## Help for individual functions is also obtained by adding the
## suffix '.spam' to the function name, e.g. 'help( chol.spam)'.</code></pre>
<pre><code>##
## Attaching package: 'spam'</code></pre>
<pre><code>## The following objects are masked from 'package:base':
##
## backsolve, forwardsolve</code></pre>
<pre><code>## Loading required package: maps</code></pre>
<pre><code>## See www.image.ucar.edu/~nychka/Fields for
## a vignette and other supplements.</code></pre>
<pre class="r"><code>source('full_geo_from_adj_geo.R')
sim_functional_data<-function(sce,samplesize=100,K=30,SNR=1,reg_sampling=1,com_grid=1,plot_true=1){
if(sce == 1){
a<- -4
b<- 4
if(reg_sampling==0){
alpha<- runif(samplesize,-1,1)
alpha=sort(alpha)
} else if(reg_sampling==1){
# alpha <- seq(-1,1,length.out=samplesize)
alpha<- rnorm(samplesize,0,0.2)
alpha=sort(alpha)
}
mu_t <- function(t,al){
h_t <- t-al
}
# TODO: Add mathematical derviation for adja_geo
adja_geo <- (alpha[-1]- alpha[-samplesize])*sqrt(b-a)
### Calculate the analytic geodesic matrix
analytic_geo <- full_geo(adja_geo,samplesize)
} else if(sce ==2){
a<- -4
b<- 4
if(reg_sampling==0){
alpha<- runif(samplesize,-1,1)
alpha=sort(alpha)
} else if(reg_sampling==1){
# alpha <- seq(-1,1,length.out=samplesize)
alpha<- rnorm(samplesize,0,0.1)
alpha=sort(alpha)
}
mu_t <- function(t,al){
fct <- dnorm(t,al,1)
}
adja_geo <- (alpha[-1]- alpha[-samplesize])/(2*pi^(1/4))
### Calculate the analytic geodesic matrix
analytic_geo <- full_geo(adja_geo,samplesize)
}else if(sce==3){
a <- -4
b <- 4
# we make samplesize different combinations of the parameters alpha and sigma
nb_alpha<- 10
nb_beta<- samplesize/10
if(reg_sampling==0){
alpha<- runif(nb_alpha,-1,1)
alpha=sort(alpha)
sig<- runif(nb_beta,0.5,1.5)
sig<-sort(sig)
} else if(reg_sampling==1){
# alpha <- seq(-1,1,length.out=nb_alpha)
# sig<- seq(0.5,1.5,length.out=nb_beta)
alpha <- rnorm(nb_alpha,0,0.2)
alpha = sort(alpha)
beta <- rnorm(nb_beta,1,0.2)
beta <- sort(beta)
}
alpha_beta<- expand.grid(alpha,beta)
mu_t <- function(t,alpha_beta){
fct <- dnorm(t,alpha_beta[,1],alpha_beta[,2])
}
analytic_geo <- matrix(0,samplesize,samplesize)
for(comb1 in 1:(samplesize-1)){
for(comb2 in comb1:(samplesize)){
f <- function(x){
(dnorm(x,alpha_beta[comb1,1],alpha_beta[comb1,2]) - dnorm(x,alpha_beta[comb2,1],alpha_beta[comb2,2]))^2
}
analytic_geo[comb1,comb2]<- sqrt(integrand)
}
}
analytic_geo <- analytic_geo + t(analytic_geo)
}else if(sce==4){
a = 0
b = 1
nb_alpha<- 10
nb_beta<- samplesize/10
if(reg_sampling==0){
alpha<- runif(nb_alpha,1,5)
alpha=sort(alpha)
beta<- runif(nb_beta,2,5)
beta<-sort(beta)
} else if(reg_sampling==1){
# alpha <- seq(1,5,length.out=nb_alpha)
# beta<- seq(2,5,length.out=nb_beta)
alpha<- rnorm(nb_alpha,3,0.2)
alpha=sort(alpha)
beta<- rnorm(nb_beta,2,0.2)
beta<-sort(beta)
}
alpha_beta<- expand.grid(alpha,beta)
mu_t <- function(t,alpha_beta){
fct <- sqrt(dbeta(t,alpha_beta[,1],alpha_beta[,2]))
}
analytic_geo <- matrix(0,samplesize,samplesize)
for(comb1 in 1:(samplesize-1)){
for(comb2 in (comb1+1):samplesize){
f <- function(x){
sqrt(dbeta(x,alpha_beta[comb1,1],alpha_beta[comb1,2])) * sqrt(dbeta(x,alpha_beta[comb2,1],alpha_beta[comb2,2]))
}
inprod = integrate(f,lower=a,upper=b)$value
analytic_geo[comb1,comb2]<-
acos(pmin(pmax(inprod,-1.0),1.0))
}
}
analytic_geo <- analytic_geo + t(analytic_geo)
}else if(sce==5){
a = -3
b = 3
if(reg_sampling==0){
alpha<- runif(samplesize,0.5,1)
alpha=sort(alpha)
} else if(reg_sampling==1){
# alpha <- seq(-1,1,length.out=samplesize)
alpha<- rnorm(samplesize,0.75,0.2)
alpha=sort(alpha)
}
mu_t <- function(t){
exp((t-1.5)^2/2) + exp((t+1.5)^2/2)
}
dmu_dt <- function(t){
(t-1.5)*exp((t-1.5)^2/2) + (t+1.5)*exp((t+1.5)^2/2)
}
h_alpha_t <- function(t,alph){
6*( exp(alph*(t+3)/6) - 1)/(exp(alph) - 1) -3
}
dh_dalpha <- function(t,alph){
numerator = (t-3)/6 * exp(alph*(t+9)/6) - (t+3)/6 * exp(alph*(t+3)/6) + exp(alph)
denom = (exp(alph)-1)^2
return(6*numerator/denom)
}
X_alpha_t <- function(t,alph){
return(mu_t(h_alpha_t(t,alph)))
}
# returns the L2 norm of X(t). X is passed in as a function
L2norm <- function(X,lower,upper){
integrand <- function(s){
X(s)^2
}
return(sqrt(integrate(integrand,lower=lower,upper=upper)$value))
}
# returns dX_alpha/dalpha as a function
dX_dalpha <- function(t,alph){
dmu_dt(h_alpha_t(t,alph))*dh_dalpha(t,alph)
}
# L2 norm of dX_\alpha/d\alpha
analytic_geo_integrand <- function(alph){
dX_dalpha_fix_alpha <- function(t){
dX_dalpha(t,alph)
}
L2norm(dX_dalpha_fix_alpha,lower=a,upper=b)
}
analytic_geo <- matrix(0,samplesize,samplesize)
for(comb1 in 1:(samplesize-1)){
for(comb2 in (comb1+1):samplesize){
analytic_geo[comb1,comb2]<- integrate(analytic_geo_integrand,lower=alpha[comb1],upper=alpha[comb2])
}
}
analytic_geo <- analytic_geo + t(analytic_geo)
}
noiseless_data <- matrix(ncol=K,nrow=samplesize)
grid<-matrix(ncol=K,nrow=samplesize)
reg_grid=seq(a,b,length.out=K)
if(com_grid==0){
reg_noiseless_data = matrix(ncol=K,nrow=samplesize)
for(i in 1:samplesize){
tmp_grid=sort(runif(K,a,b))
grid[i,]=tmp_grid
if(sce==1 || sce==2){
noiseless_data[i,] <- mu_t(tmp_grid,alpha[i])
reg_noiseless_data[i,]<-mu_t(reg_grid,alpha[i])
} else if(sce==3 || sce==4){
noiseless_data[i,] <- mu_t(tmp_grid,alpha_beta[i,])
reg_noiseless_data[i,]<-mu_t(reg_grid,alpha_beta[i,])
} else if(sce == 5) {
noiseless_data[i,] <- X_alpha_t(tmp_grid,alpha[i])
reg_noiseless_data[i,]<-X_alpha_t(reg_grid,alpha[i])
}
}
}else if (com_grid==1){
for(i in 1:samplesize){
grid[i,]=reg_grid
if(sce==1 || sce==2){
noiseless_data[i,] <- mu_t(reg_grid,alpha[i])
}else if(sce==3 || sce==4){
noiseless_data[i,] <- mu_t(reg_grid,alpha_beta[i,])
} else if(sce == 5) {
noiseless_data[i,]<-X_alpha_t(reg_grid,alpha[i])
}
}
reg_noiseless_data=noiseless_data
}
mean_signal= apply(reg_noiseless_data,2,mean)
var_signal= (1/(samplesize*K))*sum((reg_noiseless_data-matrix(mean_signal,ncol=K,nrow=samplesize,byrow=TRUE))^2)
sd_noise= sqrt(var_signal/(10^(SNR/10)))
epsilon<- matrix(rnorm(samplesize*K,0,sd_noise),nrow=samplesize)
noisy_data <- (noiseless_data + epsilon)
if (plot_true==1){
par(mfrow=c(1,3))
matplot(t(grid),t(noiseless_data),main="True data",type='l', col=rainbow(samplesize))
matplot(t(grid),t(noisy_data),main="Observed data",type='l', col=rainbow(samplesize))
image.plot(analytic_geo,main='analytic geodesic')
}
return(list('noiseless_data'=noiseless_data,'noisy_data'=noisy_data,'analytic_geo'=analytic_geo,'grid'=grid,'reg_grid'=reg_grid))
}</code></pre>
</div>
</div>
<script>
// add bootstrap table styles to pandoc tables
function bootstrapStylePandocTables() {
$('tr.header').parent('thead').parent('table').addClass('table table-condensed');
}
$(document).ready(function () {
bootstrapStylePandocTables();
});
</script>
<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
document.getElementsByTagName("head")[0].appendChild(script);
})();
</script>
</body>
</html>