Sei sulla pagina 1di 10

Chapter 1

Discussion Section of 1/9/13


1.1
1

R demo

grad.desc = function(FUN = function(x, y) x^2 + 2 *

2
3

y^2, rg = c(-3, -3, 3, 3), init = c(-3, 3), gamma = 0.05,

4
5

tol = 0.001, gr = NULL, len = 50, interact = FALSE, col.contour = "red",

6
7

col.arrow = "blue", main) {

8
9

nmax = ani.options("nmax")

10
11

x = seq(rg[1], rg[3], length = len)

12
13

y = seq(rg[2], rg[4], length = len)

14
15

nms = names(formals(FUN))

16
17

grad = if (is.null(gr))

18
19

deriv(as.expression(body(FUN)), nms, function.arg = TRUE) else {

20

function(...) {

21
22

res = FUN(...)

23
24

attr(res, gradient) = matrix(gr(...), nrow = 1, ncol = 2)

25
26

res

27
28

29
30
31

32
33

z = outer(x, y, FUN)

CHAPTER 1. DISCUSSION SECTION OF 1/9/13

34
35

if (interact) {

36

contour(x, y, z, col = "red", xlab = nms[1], ylab = nms[2],

37
38

main = "Choose initial values by clicking on the graph")

39
40

xy = unlist(locator(1))

41
42
43

} else {

44

xy = init

45
46
47

48
49

newxy = xy - gamma * attr(grad(xy[1], xy[2]), "gradient")

50
51

gap = abs(FUN(newxy[1], newxy[2]) - FUN(xy[1], xy[2]))

52
53

if (missing(main))

54

main = eval(substitute(expression(z == x), list(x = body(FUN))))

55
56
57

i = 1

58
59

while (gap > tol && i <= nmax) {

60

#dev.hold()

61
62

contour(x, y, z, col = col.contour, xlab = nms[1], ylab = nms[2],

63
64

main = main)

65
66

xy = rbind(xy, newxy[i, ])

67
68

newxy = rbind(newxy, xy[i + 1, ] - gamma * attr(grad(xy[i +

69
70

1, 1], xy[i + 1, 2]), "gradient"))

71
72

arrows(xy[1:i, 1], xy[1:i, 2], newxy[1:i, 1], newxy[1:i,

73
74

2], length = par("din")[1] / 50, col = col.arrow)

75
76

gap = abs(FUN(newxy[i + 1, 1], newxy[i + 1, 2]) - FUN(xy[i +

77
78

1, 1], xy[i + 1, 2]))

79
80

ani.pause()

81
82

i = i + 1

83
84

if (i >= nmax) warning(Maximum number of iterations reached!)

85
86
87

88
89

invisible(list(par = newxy[i - 1, ], value = FUN(newxy[i -

90
91

1, 1], newxy[i - 1, 2]), iter = i - 1, gradient = grad,

1.1.

R DEMO

92

persp = function(...) persp(x, y, z, ...)))

93
94
95

96
97

grad.desc()

98
99
100
101

# source("z.r")

102
103
104
105

#install package mvtnorm

106
107
108
109

sigma <- matrix(c(4,2,2,3), ncol=2)

110
111

x <- rmvnorm(n=500, mean=c(1,2), sigma=sigma)

112
113

colMeans(x)

114
115

var(x)

116
117
118
119

#install.packages("mvtnorm","/a/b/c/")

120
121

#.libPaths("/a/b/c/")

122
123

library(mvtnorm)

124
125
126
127

sigma <- matrix(c(4,2,2,3), ncol=2)

128
129

x <- rmvnorm(n=500, mean=c(1,2), sigma=sigma)

130
131

colMeans(x)

132
133

var(x)

134
135
136
137

#vector

138
139
140
141

a = c(1,2)

142
143
144
145

#list

146
147

Nile

148
149

hn <- hist(Nile)

CHAPTER 1. DISCUSSION SECTION OF 1/9/13

150
151
152
153

hn

154
155
156
157

hn[1]

158
159

hn[[1]]

160
161

hn[[1]][1]

162
163
164
165

# Matrix construction from dataframe

166
167
168
169

PSNR = data.frame(cbind(c(0.72761,0.70622,0.713415,0.707539),c(0.8625723,0.835956,0.924474,0.909785),c(0.9254554,0.901021,0.9521

170
171
172
173

PSNR

174
175
176
177

P = as.matrix(PSNR)

178
179
180
181

182
183
184
185

str(P)

186
187
188
189

class(P)

190
191
192
193

dim(P)

194
195
196
197

#matrix construction from scratch

198
199
200
201

P = matrix(nrow = 5 ,ncol= 5)

202
203
204
205
206
207

str(P) #structure

1.1.

R DEMO

208
209

class(P) #class

210
211
212
213

dim(P)

214
215
216
217

218
219
220
221

P[1,1] = 5

222
223
224
225

226
227
228
229

P = matrix(c(1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5),nrow = 5, ncol =5)

230
231
232
233

234
235
236
237

#Matrix construction using rbind / cbind

238
239
240
241

P = rbind(c(1,2,3,4,5),c(6,7,8,9,0))

242
243
244
245

246
247
248
249

P = cbind(c(1,2,3,4,5),c(6,7,8,9,0))

250
251
252
253

254
255
256
257

#Adding to a matrix

258
259
260
261

P = rbind(c(1,2,3,4,5),c(6,7,8,9,0))

262
263
264
265

P = rbind(P,P)

CHAPTER 1. DISCUSSION SECTION OF 1/9/13

266
267
268
269

270
271
272
273

#matrix indexing

274
275
276
277

P = matrix(c(1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5),nrow = 5, ncol =5)

278
279
280
281

P[1,]

282
283
284
285

P[,1]

286
287
288
289

P[1:4,]

290
291
292
293

P[,1:4]

294
295
296
297

#Submatrices from a matrix

298
299

P = matrix(c(1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5),nrow = 5, ncol =5)

300
301
302
303

P[c(2,4),]

304
305
306
307

P[c(2,4),c(1,3)]

308
309
310
311

#Copying submatrices

312
313
314
315

P[2:3,] = rep(100,10)

316
317
318
319

320
321
322
323

nrow(P)

1.1.

R DEMO

324
325
326
327

#Deleting from a matrix

328
329
330
331

P = matrix(c(1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5),nrow = 5, ncol =5)

332
333
334
335

P = P[,1:4]

336
337

338
339
340
341

#Matrix addition / subtraction

342
343
344
345

P + P

346
347
348
349

#Scalar multiplication/div

350
351
352
353

5 * P

354
355
356
357

#component by component mult/div

358
359
360
361

P * P

362
363
364
365

V = c(1,2,3,4,5)

366
367
368
369

P * V #row i multiplied by component i

370
371
372
373

#multiply column i by component i

374
375
376
377

V = V[-5]

378
379
380
381

t(t(P) *V)

382
383
384
385

#matrix multiplication

386
387
388
389

P %*% t(P)

390
391
392
393

#matrix multiplication with a vector

394
395
396
397

V = c(1,2,3,4) #column vector

398
399
400
401

P %*% V

402
403
404
405

# Outer product

406
407
408
409

V %o% V

410
411
412
413

#inner product

414
415
416
417

crossprod(V,V) #actually returns t(A) %*% B

418
419
420
421

#Inverse of a matrix

422
423

P = cbind(P,c(1,2,0,1,0))

424
425
426
427

P[,3] = c(13,14,17,19,23)

428
429
430
431

P[,4] = c(29,31,37,43,47)

432
433
434
435

solve(P)

436
437
438
439

#Solving Linear systems

CHAPTER 1. DISCUSSION SECTION OF 1/9/13

1.1.

R DEMO

440
441

P = matrix(c(1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5),nrow = 5, ncol =5)

442
443
444
445

V = c(1,2,3,4,5)

446
447
448
449

P[,3] = c(13,14,17,19,23)

450
451
452
453

P[,4] = c(29,31,37,43,47)

454
455
456
457

P[,5] = c(30,31,37,43,47)

458
459
460
461

solve(P,V)

462
463
464
465

#Eigen values and Eigen vector

466
467
468
469

P = matrix(c(1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5),nrow = 5, ncol =5)

470
471
472
473

y = eigen(P)

474
475
476
477

#obtaining stationary distributions using eigenvalues

478
479
480
481

p <- matrix ( rep ( 0 , 100 ) , nrow=10)

482
483

onesixth <- 1/6

484
485

for ( i in 1 : 10 ) {

486
487

for ( j in 1 : 6 ) {

488
489

k <- i + j

490
491

if ( k > 10) k <- k - 10

492
493

p[ i , k ] <- onesixth

494
495

496
497

10

CHAPTER 1. DISCUSSION SECTION OF 1/9/13

498
499
500
501

findpi1 <- function(p) {

502
503

n <- nrow(p)

504
505

imp <- diag(n) - t(p) # I-P

506
507

imp[n,] <- rep(1,n) # insert row of 1s

508
509

rhs <- c(rep(0,n-1),1) # form right-hand side

510
511

pivec <- solve(imp,rhs) # solve the system

512
513

return(pivec)

514
515

516
517
518
519

n <- nrow(p)

520
521
522
523

pivec <- eigen(t(p))$vectors[,1] # find first eigenvector of P transpose

524
525
526
527

if (Re(pivec[1]) < 0) pivec <- -Re(pivec) #could be negative

528
529
530
531
532
533

pivec <- pivec / sum(pivec) # normalize to sum to 1

Potrebbero piacerti anche