See the course website for important information about collaboration and late policies, as well as where and when to turn in assignments.

Similar documents
Lecture 8. Divided Differences,Least-Squares Approximations. Ceng375 Numerical Computations at December 9, 2010

Homework #6 Brief Solutions 2012

lecture 10: B-Splines

Computational Physics PHYS 420

Curve fitting using linear models

Algebra 1. Standard 11 Operations of Expressions. Categories Combining Expressions Multiply Expressions Multiple Operations Function Knowledge

Remark. Jacobs University Visualization and Computer Graphics Lab : ESM4A - Numerical Methods 331

Concept of Curve Fitting Difference with Interpolation

9.1: GRAPHING QUADRATICS ALGEBRA 1

February 2017 (1/20) 2 Piecewise Polynomial Interpolation 2.2 (Natural) Cubic Splines. MA378/531 Numerical Analysis II ( NA2 )

Natural Quartic Spline

Knowledge Discovery and Data Mining

Interpolation & Polynomial Approximation. Cubic Spline Interpolation II

Handout 4 - Interpolation Examples

Polynomial and Rational Functions

Homework #6 Brief Solutions 2011

Parametric Curves. University of Texas at Austin CS384G - Computer Graphics Fall 2010 Don Fussell

A Practical Review of Uniform B-Splines

Four equations are necessary to evaluate these coefficients. Eqn

Unit 1 Day 4 Notes Piecewise Functions

Finite Math - J-term Homework. Section Inverse of a Square Matrix

Parametric Curves. University of Texas at Austin CS384G - Computer Graphics

Section 18-1: Graphical Representation of Linear Equations and Functions

Consider functions such that then satisfies these properties: So is represented by the cubic polynomials on on and on.

Linear Interpolating Splines

Sung-Eui Yoon ( 윤성의 )

1.1 Pearson Modeling and Equation Solving

Nonparametric Approaches to Regression

CS 450 Numerical Analysis. Chapter 7: Interpolation

EECS 556 Image Processing W 09. Interpolation. Interpolation techniques B splines

5.1 Introduction to the Graphs of Polynomials

Runge Example Revisited for Splines

2D Spline Curves. CS 4620 Lecture 13

8 Piecewise Polynomial Interpolation

(Spline, Bezier, B-Spline)

Lecture 16: High-dimensional regression, non-linear regression

Engineering Analysis ENG 3420 Fall Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00

Course Number 432/433 Title Algebra II (A & B) H Grade # of Days 120

3.3 Optimizing Functions of Several Variables 3.4 Lagrange Multipliers

Splines. Parameterization of a Curve. Curve Representations. Roller coaster. What Do We Need From Curves in Computer Graphics? Modeling Complex Shapes

Lecture 27: Review. Reading: All chapters in ISLR. STATS 202: Data mining and analysis. December 6, 2017

Set 5, Total points: 100 Issued: week of

Need for Parametric Equations

Polynomial and Rational Functions. Copyright Cengage Learning. All rights reserved.

Cubic Splines and Matlab

CSE 167: Introduction to Computer Graphics Lecture 12: Bézier Curves. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2013

Tentti/Exam Midterm 2/Välikoe 2, December

Algebra 2 Semester 1 (#2221)

Representing Curves Part II. Foley & Van Dam, Chapter 11

Design considerations

Cubic Splines By Dave Slomer

Number of Regions An Introduction to the TI-Nspire CAS Student Worksheet Created by Melissa Sutherland, State University of New York at Geneseo

2D Spline Curves. CS 4620 Lecture 18

Curves and Surfaces Computer Graphics I Lecture 9

Math 370 Exam 1 Review Name. Use the vertical line test to determine whether or not the graph is a graph in which y is a function of x.

Spline Methods Draft. Tom Lyche and Knut Mørken

4.3 Quadratic functions and their properties

A MATRIX FORMULATION OF THE CUBIC BÉZIER CURVE

Today is the last day to register for CU Succeed account AND claim your account. Tuesday is the last day to register for my class

QUADRATIC FUNCTIONS: MINIMUM/MAXIMUM POINTS, USE OF SYMMETRY. 7.1 Minimum/Maximum, Recall: Completing the square

Fitting to a set of data. Lecture on fitting

08 - Designing Approximating Curves

Spline Methods Draft. Tom Lyche and Knut Mørken

Linear Regression: One-Dimensional Case

2.2 Graphs Of Functions. Copyright Cengage Learning. All rights reserved.

For each question, indicate whether the statement is true or false by circling T or F, respectively.

Data Table from an Equation

Spline Methods Draft. Tom Lyche and Knut Mørken. Department of Informatics Centre of Mathematics for Applications University of Oslo

GRAPHING POLYNOMIALS DAY 2 U N I T 1 1

Bézier Splines. B-Splines. B-Splines. CS 475 / CS 675 Computer Graphics. Lecture 14 : Modelling Curves 3 B-Splines. n i t i 1 t n i. J n,i.

Last time... Bias-Variance decomposition. This week

Intro to Modeling Modeling in 3D

An introduction to interpolation and splines

Piecewise Polynomial Interpolation, cont d

Matrix Inverse 2 ( 2) 1 = 2 1 2

PSY 9556B (Feb 5) Latent Growth Modeling

QUESTIONS 1 10 MAY BE DONE WITH A CALCULATOR QUESTIONS ARE TO BE DONE WITHOUT A CALCULATOR. Name

Geometric Modeling of Curves

Georeferencing & Spatial Adjustment

Assignment 2. with (a) (10 pts) naive Gauss elimination, (b) (10 pts) Gauss with partial pivoting

FMA901F: Machine Learning Lecture 3: Linear Models for Regression. Cristian Sminchisescu

Sketching graphs of polynomials

Assignment No: 2. Assessment as per Schedule. Specifications Readability Assignments

Section 3.7 Notes. Rational Functions. is a rational function. The graph of every rational function is smooth (no sharp corners)

CS 475 / CS Computer Graphics. Modelling Curves 3 - B-Splines

Math 111: Midterm 1 Review

Bezier Curves, B-Splines, NURBS

Nonparametric regression using kernel and spline methods

Solve the matrix equation AX B for X by using A.(1-3) Use the Inverse Matrix Calculator Link to check your work

Edge and local feature detection - 2. Importance of edge detection in computer vision

2.1 Quadraticsnts.notebook. September 10, 2018

Algorithms and Data Structures

Spline Models. Introduction to CS and NCS. Regression splines. Smoothing splines

Assessing the Quality of the Natural Cubic Spline Approximation

Learning from Data Linear Parameter Models

CS770/870 Spring 2017 Curve Generation

Cubic spline interpolation

HW2 due on Thursday. Face Recognition: Dimensionality Reduction. Biometrics CSE 190 Lecture 11. Perceptron Revisited: Linear Separators

Tutorial Four: Linear Regression

Minima, Maxima, Saddle points

Transcription:

COS Homework # Due Tuesday, February rd See the course website for important information about collaboration and late policies, as well as where and when to turn in assignments. Data files The questions make use of two data files available from http://www.cs.princeton.edu/courses/archive/spring/cos/w/hw. hw_sample_train.txt hw_sample_train.txt Each data file contains n = lines. Each line contains exactly numbers representing the X and Y coordinates of points generated by adding noise to a secret function. The files hw sample train.txt and hw sample train.txt are only useful for the last question. For verification, here is a graphical representation of both datasets. Sample Sample 7 6 Question Implement linear least square curve fitting. Provide plots showing the points and the fitted curve for both datasets using the following bases. Linear regression: Φ(x) = (, x) Cubic regression: Φ(x) = (, x, x, x ) Cubic splines: Φ(x) = (, x, x, x, [x + ] +, [x] +, [x ] +) The notation [z] + is a shorthand for max(, z). We need only three main subroutines: phi(x) computes returns the vector Φ(x) for the chosen basis.

train(data) computes the weights w for the given training points. Argument data is a matrix n containing the training set. The following pseudo-code for the function train relies on a linear equation solver solve. Adding 6 to the diagonal of xx avoids numerical problems when the matrix is singular or near-singular. function train(data) n := data.nrows() d := phi().nrows() xx := new matrix(d,d) xy := new vector(d) for i := to n phix := phi(data[i,]) xx := xx + phix * transpose(phix) xy := xy + phix * data[i,] for j := to d xx[j,j] := xx[j,j] + e-6 return solve(xx, xy) endfunction test(x, w) returns transpose(w)*phi(x), that is, the value in x of the curve with weights w. Plots for SAMPLE Polynomial d= Polynomial d= Piecewise cubic with knots 6 6 6 6 6 6 Plots for SAMPLE 8 6 Polynomial d= 6 Polynomial d= 6 Piecewise cubic with knots 6 6 6 6 6 6 6 Question Like polynomials, cubic splines often display strong variations outside of the domain of the input examples. Given a set of knots r < r < < r k, we would like to construct a spline basis that always produces a piecewise function whose pieces are affine (polynomials of degree ) when x r or x r k and cubic (polynomials of degree ) when r < x < r k.

Cubic splines can be written as f(x) = a + a x + a x + a x + k b i [x r i ] + () a) Write conditions on the parameters a i and b i expressing that the spline function has no cubic term when x < r or x > r k. When x < r, f(x) = a x + a x + a x + a. When x > r k, f(x) = (a + k b i) x +... x +... x +... The conditions are therefore a = and k = b i =. b) Derive a spline basis that embodies these conditions. Hint: write c i = i j= b j and rewrite expression () with the c i instead of the b i. The resulting splines should have the form k f(x) = a + a x + a x + c i φ i (x) () Since b i = c i c i and a =, we can rewrite () as: f(x) = a + a x + a x + k (c i c i )[x r i ] + Reorganizing the sum and using the conditions c k = k b i = : k f(x) = a + a x + a x + c i ([x r i ] + [x r i+ ] +) () Therefore the basis is Φ(x) = (, x, x,... [x r i ] + [x r i+ ] +... ) with i =... k. c) Write further conditions on the coefficient a i and c i expressing that the spline function has no quadratic term when x < r or x > r k. No quadratic terms when x < r a = Observe (x a) (x b) = (b a)x (b a )x + (b a ). Then, when x > r k, k c i([x r i ] + [x r i+ ] +) = x k c i(r i+ r i ) + x... +.... The additional conditions are therefore a = and k c i(r i+ r i ) =. d) Derive a spline basis that embodies all these conditions, namely that the pieces of the resulting functions are affine when x < r or x > r k. Such splines are called natural cubic splines.

Same story as question (b). Define d i = i j= c i(r i+ r i ); recall a = ; and write () as k d i d i f(x) = a + a x + ([x r i ] + [x r i+ ] r i+ r +) i Reorganizing the sum and using the condition d k =, k ( [x ri ] + [x r i+ ] + f(x) = a + a x + d i [x r i+] + [x r i+ ] ) + r i+ r i r i+ r i+ The basis is then Φ(x) = natural spline basis (knots=,,,) ( ), x,... [x ri] + [x ri+] + r i+ r i [x ri+] + [x ri+] + r i+ r i+,... with i =... k. This basis is plotted on the left with knots The non trivial basis functions are composed of four segments (flat, cubic, cubic, linear) with boundaries at the knots. There are other possible basis for natural splines such as: or (, x,... [x ri] + [x ri+] + r i+ r i [x r k ] + [x r k] + (, x,... [x ri] + [x r k] + r k r i [x r k ] + [x r k] + r k r k... ) r k r k... ). 6 6 e) Count the number of dimensions and the number of constraints (continuity, continuous first derivatives, continuous second derivatives) to confirm that the basis dimension should indeed be k. Each segment of the fitted curve is a cubic polynomial. That would give (k + ) parameters. We have three constraints per knot stating that the function and its first two derivatives are continuous. Finally we have four additional constraints stating that the cubic and quadratic coefficients in the end pieces are zero. Therefore the final dimension is (k + ) k = k. Question We now consider natural splines with k evenly spaced knots such that r = and r k =. Use -fold cross-validation to determine the best k for each of the two samples. Provide one plot for each dataset showing the average of the training and validation MSE obtained during k-fold cross-validation as a function of k =... 8. First there is a problem with the question: there are no natural splines with k <. In fact the case k = corresponds to a simple linear regression. A typical code for computing the k-fold validation error contains A function split sample(data,nfolds,i) to compute the i-th split of the data. This function returns two data matrices containing the training part and the testing part of the split. My

splitting function takes the data in the order of the data file and divides it in five segments of examples. The testing part for the i-th fold is the i-th segment ( examples). The training part is obtained by removing the i-th segment from the whole dataset ( examples are left.) A function mse sample(data,w) to compute the MSE of the curve w on sample data. function mse_sample(data,w) err = for to data.nrows() err = err + square(data[i,] - test(data[i,], w)) return err / data.nrows() endfunction A function kfold error(data,nfolds) to perform the k-fold cross-validation. function kfold_error(data,nfolds) err = for to nfolds trdata,tedata = split_sample(data,nfolds,i) err = err + mse_sample(tedata, train(trdata)) return err / nfolds endfunction The k-fold errors as a function of k look like the following plots. splitting function, there can be some differences... fold mse.7.6.. Sample fold mse 6 Depending on the details of the Sample.. number of knots number of knots Note that the best error for sample is k = which is greater than the maximal k specified in the question. Extra points for whoever pointed this out. The following plots show curve fits for selected solutions. Sample: Natural cubic spline with knots Sample : Natural cubic spline with 8 knots Sample: Natural cubic spline with knots 6 6 6 6 6 6

Question Download the two testing sets provided on the homework page and report the testing set MSE achieved by the models you have selected. The code for this is trivial when you have programmed the k-fold validation. The following tables give my results. The -fold MSE and the optimal k can vary depending on the details of your splitting function. But the test MSE should be the same because it is obtained by retraining on the whole dataset with the selected value of k. Sample k -fold MSE test MSE.6.7.6.7.... 6.6. 7.7.6 8.6.7 9.6.7..8.6..8.9...... Sample k -fold mse test mse..9 7. 7.7.6 6.6.7 7.9 6 8. 8.8 7.9.8 8 6.7.6 9..6.9 6.87 8.76.8 6.9.68 8.7. 8.7... Note how k-fold cross-validation underestimates the best k on sample. This probably happens because each fold is trained on examples instead of. 6