Docsity

Homework 1 Function and Graphic Output - Statistics Computing | STAT 534, Assignments of Statistics

Toggle sidebar
bg1
Hil Lyons
STAT 534
HW 1
Note: Questions 1 and 2 appear in reverse order, i.e. results appear before code
2) Function and Graphic output
a. TENSION = 0; Vertex positions and objective function values
Vertex X1 X2
10.09975761 0.2340478
2 0.14788753 0.5769015
30.34838908 0.8238033
4 0.78656286 0.8352504
50.98091211 0.5833201
6 0.91430777 0.1841121
70.48326779 0.3077599
Index Objective Fcn Distance Energy Spring Energy
10.2365577 0.2365577 0
20.1588782 0.1588782 0
30.1485253 0.1485253 0
40.1449913 0.1449913 0
50.1435934 0.1435934 0
60.1428943 0.1428943 0
70.1424474 0.1424474 0
80.1421129 0.1421129 0
90.1417646 0.1417646 0
10 0.1414875 0.1414875 0
11 0.1412610 0.1412610 0
Note: The Objective Function is the sum of Distance Energy and Spring Energy
b. TENSION = 0; Plot of observations, the true curve, and the fitted polyline
(See next page)
Hil Lyons Page 1 4/10/2006
pf3
pf4
pf5

Partial preview of the text

Download Homework 1 Function and Graphic Output - Statistics Computing | STAT 534 and more Assignments Statistics in PDF only on Docsity!

Hil Lyons

STAT 534

HW 1

Note: Questions 1 and 2 appear in reverse order, i.e. results appear before code

2) Function and Graphic output

a. TENSION = 0 ; Vertex positions and objective function values

Vertex X1 X

Index Objective Fcn Distance Energy Spring Energy

Note: The Objective Function is the sum of Distance Energy and Spring Energy

b. TENSION = 0 ; Plot of observations, the true curve, and the fitted polyline

(See next page)

0.2 0.4 0.6 0.8 1.

True curve (black) and final polyline (red), tension = 0

c. TENSION = 0 ; Plot of objective function vs. iteration number (index)

2 4 6 8 10

Index Objective fcn Objective fcn, tension = 0 (distance energy only)

0.2 0.4 0.6 0.8 1.

True curve (black) and final polyline (red), tension = 1

f. TENSION = 1 ; Plot of objective function vs. iteration number (index)

2 4 6 8 10

Index Objective fcn Objective fcn, tension = 1

1) Code for R functions

Homework 1

Three functions are created to optimize initialized

vertices such as to minimize the distance energy

resulting from the line segments and observation set.

##########################################################

Function 1 -- find.barcords

This function calculates the barycentric coordinates

given observations and vertices. The barycentric

coordinates give the linear combination of vertices

that mark the "projection" of the observations onto the

the line segments determined by the vertices.

Barycentric coordinates are calculated to minimize the

distance between observations and line segments. As

such, not all projections are orthogonal. A similar

condition: lambda is restricted to [0,1].

Example: (0, 0, 0, lambda, 1-lambda, 0); six vertices.

find.barcords <- function(obs, vertices){ m <- ncol(obs) k <- nrow(vertices) n <- nrow(obs) lambda <- NULL dist <- NULL for (i in 1:(k-1)){ vert1 <- matrix(vertices[i,], nrow = n, ncol = m, byrow = T) vert2 <- matrix(vertices[i+1,], nrow = n, ncol = m, byrow = T) lambda <- cbind(lambda, pmax(0, pmin(1, rowSums((vert2-obs)(vert2-vert1)) / rowSums((vert2-vert1)(vert2-vert1))))) dist <- cbind(dist, rowSums((obs - (lambda[,i]vert1 + (1-lambda[,i])vert2))^2)) } barcords <- NULL for (j in 1:n){ seg = which.min(dist[j,]) barcords = rbind(barcords, c(rep(0,seg-1), lambda[j,seg], 1-lambda[j, seg], rep(0, k-seg-1))) } barcords }

Function 2 -- adjust.vertices

This function calculates revised vertices based on

given barycentric coordinates and tension. The

barycentric coordinates coupled with observations

are included to account for the "distance energy", i.e

square distance from observations to vertices fit. The

tension value is included to account for the "spring

energy", i.e. the square length of the segments times

a constant. lsfit() is used, as this is a least squares

problem.

adjust.vertices <- function(obs, barcords, tension = 0.0){ m <- ncol(obs) k <- ncol(barcords) t.mtx <- cbind(diag(rep(sqrt(tension), k-1)), rep(0, k-1)) + cbind(rep(0, k-1), diag(rep(-sqrt(tension), k-1))) new.vertices <- NULL for (i in 1:m){ y <- c(obs[,i], rep(0, k-1)) x <- rbind(barcords, t.mtx) new.vertices <- cbind(new.vertices, lsfit(x, y, intercept = FALSE)$coef) } new.vertices