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