You are on page 1of 2

# Exercises

Machine Learning

Institute of Computational Science Dept. of Computer Science, ETH Z¨rich u
Peter Orbanz E-Mail porbanz@inf.ethz.ch Web http://www.inf.ethz.ch/∼porbanz/ml

Series (Introducing Matlab)
Problem 1 (First steps): Please skip this if you are already familiar with matlab.

1. Have a look at the matlab functions rand, randn, eig, load, save. Use matlab’s online help (type help rand etc). 2. Write a function to compute the expression y (x) = sin (x) + cos (3x). The function should be implemented as a matlab m-ﬁle, say ﬁrst.m. 3. What is the eﬀect of the command x=linspace(0,3.5,50)? 4. Generate a matrix containing the values of x, y as its ﬁrst and second column. 5. Take a look at matlab’s plot function. Use it to produce a plot of the function y (deﬁned in 2) for x ∈ [0, 5]. You do not need to submit anything for this problem. Problem 2 (Sampling a d-dimensional Gaussian distribution): We consider the problem of sampling a multivariate normal (Gaussian) distribution. Matlab provides a function called randn, which produces pseudo-random samples for a normal distribution with parameters µ = (0, . . . , 0) and Σ = 1, where 1 is the d-dim. identity matrix. We wish to produce samples from a Gaussian N (µ, Σ) with arbitrary parameters µ and Σ, so we have to transform the sample in a suitable manner. Our approach is based on the eigenvalue structure of symmetric matrices: The eigenvectors of a full-rank symmetric matrix form an orthogonal basis of the underlying vector space. With respect to this basis, the matrix is diagonal, with the eigenvalues as diagonal entries. Denote this diagonal matrix of eigenvalues D and the matrix describing the change of basis V . Then Σ = V DV −1 . V is orthogonal (since it describes a change of basis between two orthonormal bases), so V −1 = V t and Σ = V DV t . This representation of Σ is called the Schur decomposition. We can produce a sample from a normal distribution with parameters µ, Σ by drawing a sample vector g from N (0, 1) using randn, changing basis, and adding the expectation vector: √ g = V Dg + µ . ˜ √ √ As you will recall from linear algebra, D = diag Dii . 1. Implement a function x = GSAMPLE(mu, Sigma, n) to produce n draws from a d-dimensional Gaussian. (The dimension d is implicitly speciﬁed by mu and Sigma.) 2. Test your implementation: Apply the matlab functions mean and cov to a suﬃciently large sample generated by your program. They should approximately reproduce your input parameters. 3. Produce 100 samples each in two  three dimensions, using the parameter values µ = (1, 1) , Σ = and  5 2 0 2 1 t and µ = (1, 1, 1) , Σ =  2 3 1 , respectively. Plot your results using the functions plot 1 2 0 1 1 and plot3. When using the plot function, always supply ’x’ as ﬁnal argument, i.e. use a function call of the form plot(A,B,’x’). This tells matlab to display each point as an x-mark. (If your plot looks somewhat like a random walk, you got it wrong.) Please turn in:
t

1000}. Plot the histograms using the plot function. and see how stable the result is with respect to diﬀerent samples. For n = 100000. We will draw a number n of sample points from a one-dimensional Gaussian. this is about sample size and reliability of estimates. Produce four histograms with n = 100 and Nbins = 10. plot one histogram each for Nbins ∈ {10. using a Gaussian distribution as an example data source. Give a brief discussion of the results. Please turn in your plots and discussion. 5. 100.) 4. Remember. Finally. 2 .• A listing of your numerical test results in (2). Problem 3 (Sample Size): A recurrent theme in statistical data analysis is that large numbers of observations are required in order to obtain reliable estimates. The present problem aims at illustrating this phenomenon. Make sure that plots are suﬃciently labeled. • The plots produced in (3). (Matlab lets you to display multiple plots within a single ﬁgure with the subplot function. Repeat the procedure with n = 100000. Please complete the following steps: 1. • Use the matlab function randn to draw n samples from a one-dimensional Gaussian distribution (µ = 0. The basic procedure is the following: • Choose a number n of sample points and a number Nbins of histogram bins. 3. sort them into a histogram. 2. σ = 1). • Turn the complete data sample into a histogram using the function hist. choose n = 100 and Nbins = 1000.