Showing posts with label explanation. Show all posts
Showing posts with label explanation. Show all posts

Sunday, October 23, 2016

Generalized coordinates

In this post I’ll try to explain what Generalized coordinates are, how they came to be and finally where they are used with a simple.

To start with imagine a system of point masses present in a dimensional space. The point has a position vector .

Note that if the system was unconstrained, then as any of the bodies can occupy any point in the space, we will need number of scalars to completely define any particular configuration of the system.

For visualization purposes consider , so we use scalars (the coordinates) to define the position of each point mass. As we have a total of such masses we need scalars.

But often the systems we need to analyse are under some sort of constraints. In case of a robotic arm, each hinge point always maintains a fixed distance between them due to the rigid rod connecting them.

In such situations we don’t need all scalars, rather we can use less number of scalars and still manage to uniquely represent all possible states that this constrained system can take. This is because the constraints decrease the degrees of freedom available to that system.

Holonomic constraints

We classify constraints as either being holonomic or nonholonomic.

If a constraint can be expressed purely as a relationship between the position variables () and time .

Then using each such constraint equation we can reduce the number of scalars needed by one. If we knew the time and all but one position variable, using the constraint equation that missing coordinate can be deduced.

When the constraint has inequalities or higher order derivative terms in them, they are classified as nonholonomic. Note that we have an equality symbol in the above expression. Also the function has to of the position variables themselves, not their derivatives.

All constraints which are unable to be expressed in the above form are called nonholonomic constraints.

Note that if there were velocity terms () in the constraint equation, then integrating that constraint could in some cases yield an equation of the form shown above, if so then it’s a holonomic constraint. So what Wikipedia means when they say that if the constraint has velocity terms they “are not usually holonomic” is that it’s kinda hard to bring it to this form in that case.

When constraints are purely dependent on the positions at any time its a holonomic constraint, but in a nonholonomic constraint the constraint changes depending on how you reach that position! This seems to be analogous to path and state functions.

To visualise how a holonomic constraint reduces DOF, imagine a point mass is constrained to move along the line . So without this constraint we needed both position variables to describe its position anywhere in the XY plane, but now we just need . As if we have , its position is now known to be due to the constraint.

Constrained to move in a circle of radius

So a point mass is moving around in a plane (say the plane). We can describe its position at any point of time using the vector,

If we are given that this point mass is constrained to move in a circle of radius , i.e. the constraint equation would like this,


Note how the equation can be written as , i.e. in the form . Therefore we conclude that this is a holonomic constraint.

So the “generalized coordinate” I will use is , the angle between the axis and the position vector .

circle

We know that and .

Let the point mass move a little, it can of course only move in a small arc along the circle due to the constraint imposed on it. Such displacement is called Virtual displacement. In terms of it’s , while in terms of it’s

The following formula relates this change in the generalized coordinates () to that of the change in natural coordinates ().

The term is a measure of how much change in happens for a unit change in , keeping all other generalized coordinates constant - that’s what partial differentiation is.

So by multiplying with , you get the contributed of the generalized coordinate - in the displacement .

Summing over all these contributions (from to ) we get the net virtual displacement .

Coming back to our example, we can therefore note that

Credits to Maschen - for this image, CC0, Link
wikiImage

This makes sense as for small displacement, the shown in the figure will be a straight line, thus instead of a sector, we have a right angled triangle with , and as the sides. will be the length of the arc which is .

So from pythagoras theorem we have .

And sure enough this fits in with our “derived” relationship between the generalized and natural coordinates.

You can also try doing this with rather than like how I showed above. Where is the length of the curve traced out by the point mass from some reference point.

I thought of writing it up but it turns out that the length of a curve from to is . Which is kinda complicated,

Well as always let me know if you have any thoughts on my post.

Written with StackEdit.

Tuesday, April 19, 2016

Dimensionality Reduction

Dimensionality reduction is a method of reducing the number of random variables under consideration via obtaining a set “uncorrelated” principle variables.

If we have features (each of them are the random variables we just talked about) then each data sample will be a dimensional vector in the feature space. when is a large number, patterns in data can be hard to find as graphical representation is not possible.

What if we are able to identify a set of “principal variables” which are transformations of the existing variables such that we don’t lose much information.

PCA is a famous example when such data transformation are linear, there are other non linear methods too.

Here I’ll be now talking about PCA (Principal Component Analysis) and K-LDA (kernelized version of linear discriminant analysis (LDA)] ) and using these techniques in order to reduce the 2 dimensional data in this dataset into a single dimensions.

Principal Component Analysis

PCA is the orthogonal projection of the data onto a lower dimensional linear space. such that the variance of the projected data is maximized.

Variance is spread of the data, if the data is more spread, the better it is for us as we can easily separate them into clusters etc. We lack information if all the data vectors are localized around the mean.

So to perform PCA, we calculate the directions (in the dimensional feature space) in which variance is maximum (say such directions). Using these directions as basis vectors we can now project all the data vectors onto this subspace.

Here , and we choose the directions by calculating the eigenvectors of the covariance matrix. The eigenvectors corresponding to the larger eigenvalues will be the directions where the variance varies more.

Now I will be doing the above method in MATLAB, as part of assignment as I am in ICE department I am assigned the 6th dataset - TRAIN{1,6} so as you can see my - has 4 clusters -

  • So we can load the data set and initialize the clusters. After which we can plot them as a scatter graph.
load TRAINTEST2D

cluster1 = TRAIN{1,6}{1,1}; % Green
cluster2 = TRAIN{1,6}{1,2}; % Blue
cluster3 = TRAIN{1,6}{1,3}; % Red
cluster4 = TRAIN{1,6}{1,4}; % Cyan


scatter(cluster1(1,:), cluster1(2,:), 'g'); 
hold on;
scatter(cluster2(1,:), cluster2(2,:), 'b');
hold on;
scatter(cluster3(1,:), cluster3(2,:), 'r');
hold on;
scatter(cluster4(1,:), cluster4(2,:), 'c');
hold on;

DataSet initial

  • So we have data comprising a set of observations of variables. This data can be arranged as a set of data vectors with each representing a single grouped observation of the p variables.
    Here 4 clusters with 13 data vectors each, contributed to form . Write as row vectors, each of which has p columns. Place these row vectors into a single matrix X of dimensions .
  • Now we center our data at the origin. This is supposed to help in reducing the Mean Square Error when we approximate the data. This answer also provides some insight into how it’s necessary to compute the covariance matrix. Note that that after centering at origin, if you execute sum(B) you will get a very small number close to 0. So we can conclude the data is successfully centered.
    But the images before and after centreing don’t look that different for this dataset as the empirical mean is the entire data set just shifts as a whole fractionally.
% number of data vectors - 13*4
n = 52;
% number of dimensions (before reduction)
p = 2; 
% arrange data vectors as rows
X = [cluster1';cluster2'; cluster3'; cluster4'];
% find both x and y's mean - empirical mean vector
u = (1/n)*sum(X);
% subtract from the mean - Thus achieving centering
B = X - ones(52,1)*u;


% Plot the data after centering it around the Origin
figure(2);
scatter(B(1:13,1), B(1:13,2), 'g'); 
hold on;
scatter(B(14:26,1), B(14:26,2), 'b');
hold on;
scatter(B(27:39,1), B(27:39,2), 'r');
hold on;
scatter(B(40:52,1), B(40:52,2), 'c');
hold on;
legend('cluster 1','cluster 2','cluster 3','cluster 4');

dataset after centreing

  • Next I will find the empirical covariance matrix from the outer product of matrix B with itself.
C = (B'*B)/(n-1);
  • Then we can find the eigenvectors of the covariance matrix, choose the one which has a larger eigenvalue (In this data set ). Now we can project all the 2D points on the line (in the direction of that particular eigenvector).
% we get a column vector of eigen values - the first will be the one with
% the largest magnitude
E = svds(C);
% V will have the eigen vectors
[V,D] = eig(C);

% Thus the axis along which variance is Maximum is
maxVarianceAxis = V(:,2);

% No need to divide by (maxVarianceAxis'*maxVarianceAxis) as
% eig generates orthonormal eigenvectors
projectionMatrix = (maxVarianceAxis*maxVarianceAxis');

projCluster1 = projectionMatrix*cluster1;
projCluster2 = projectionMatrix*cluster2;
projCluster3 = projectionMatrix*cluster3;
projCluster4 = projectionMatrix*cluster4;

% Plot the data after projecting onto the principle component
figure(3);
scatter(projCluster1(1,:), projCluster1(2,:), 'g'); 
hold on;
scatter(projCluster2(1,:), projCluster2(2,:), 'b');
hold on;
scatter(projCluster3(1,:), projCluster3(2,:), 'r');
hold on;
scatter(projCluster4(1,:), projCluster4(2,:), 'c');
hold on;
legend('cluster 1','cluster 2','cluster 3','cluster 4');

After projecting onto principle component axis

As we can see the separation of blue and green clusters is good, but the light blue cluster is getting mixed up with the red cluster.

Before I wrap up, I’d like to show how the projection these data vectors onto the other eigenvector looks like,

enter image description here

See how the red and light blue is well separated here (But not as well separated green and the dark blue in the projection onto the eigenvector corresponding to the largest eigenvalue. The variance shown here is only slightly larger compared to the answer. (because the eigenvalues are close - about 0.2 difference) .

So here we had 2 features which we reduced to a single feature (the position of the data vector on the line - a single value if expressed as the distance from the origin). Thus we successfully implemented PCA and extracted a abstract feature where the data has maximum variance - globally thereby reducing dimensionality.

This leads us to LDA, where we no longer look at this same dataset as data vectors rather we take into consideration that that they are vectors from different classes.

Linear Discriminant Analysis

This is a method to find a linear combination of features that characterizes or separates two or more classes of objects.

This method is also known as Fisher’s linear discriminant, the idea is that we don’t wish to project such that the overall variance is maximized, rather we want the the clusters to be spread out (maximize the between-class covariance matrix after projection) while the data vectors of a particular class should be closer to the mean of that class (within-class covariance matrix for all the classes should be minimized).

The idea is that this enables us to easily discriminate new data vectors into classes. The more clearly separated the classes are, the less ambiguity we face when classifying.

I’ll explain using 2 classes and then extend it to multiple classes (in this dataset we are using 4 classes). let there be 2 classes - , having number of vectors respectively. is the vector in class . So the mean of the classes will be,

the projection from to dimensions is done using ,

where the sizes are,

The simplest measure of the separation of the classes, when projected along the line defined by , is the square of the separation between the projected class means. If the mean after projection of class 1 and 2 are - and then,

Here we define the between-class covariance matrix as,

Thus is to be maximized.

While we minimize the sum of the within-class variance’s for each of the classes - .

The within-class variance of the transformed data for the class is given by,


So after taking terms out of the sigma and rewriting the summation in terms of the total within-class covariance matrix - ,

For 2 classes the measure of how close the data vectors are to thier respective classes mean is

Thus is to be minimized.

So with this we can construct the utility function which we need to maximize,

Differentiating ) with respect to , setting it equal to zero, and rearranging gives,

Multi Class LDA

Thus we have found the direction of for which our conditions are optimally achieved.

But extending this idea to multiple classes requires us to make some assumptions - so as to make the mathematics a little more easier.

Suppose that now there are classes. The within-class covariance matrix which is calculated is,

While the between-class covariance matrix is given by,

In the above 2 equations is the mean of all the data vectors, is the mean of the data vectors in the class.

Using the revised covariance matrices and plugging them into the utility function. We will find that the optimal solution for is,

Optimal is an eigenvector of .

Kernel Trick + LDA = K-LDA

K-LDA the kernelized version of linear discriminant analysis (LDA) (also known as kernel Fisher discriminant analysis or kernel discriminant analysis) is when LDA is implicitly performed in a new feature space, which allows non-linear mappings to be learned.

For most real-world data a linear discriminant (offered by LDA) is not complex enough, because the data vectors might not be linearly separable at all.

We first map the data non-linearly into some feature space (a higher dimensional space) and compute LDA there (For different kernel functions these mappings differ). The linear separation which LDA provides in the feature space will yield a non-linear discriminant in the original input space. You can think of a 2 clusters (in the 2d plane) with one of the clusters embedded inside the other, now if we map these vectors to 3 dimensions we can easily introduce a dummy variable (the third axis) such that the 2 clusters are easily separated linearly (by a plane). As you can see below the left side image is the Input space and the right side image is the feature space.

why use feature space

So as we are operating in the feature space, the only change in the LDA explained above is that we use instead of everywhere. Explicitly computing the mappings and then performing LDA can be computationally expensive, and in many cases intractable. For example, may be infinitely dimensional (as in the case of the gaussian kernel).

So instead of designing a nonlinear transformation and calculating the transformed data vectors in the feature space - , we can rewrite the method in terms of dot products and using the kernel trick in which the dot product in the new feature space is replaced by a kernel function.

I used this paper and also refered from wikipedia - we can rewrite the equation for in terms of only dot products,

In the following I am assuming these sizes apply,

The remaining can be understood implicitly, here there are features, and the projection is onto 1 dimensions. These data vectors are separated into classes with the class having number of data vectors.

First of all should lie on the span of all training samples (read on theory of reproducing kernels to understand why)

where , is the total number of vectors.

Now the dot product of the mean of the class (in the feature space) with will give,

Here is the set of all piled together to form a vector. the mean of each class in the feature space is defined just like how we had discussed in the LDA section.

Using the equation for and we can say,


where is a vector whose element is defined as,

Here is the data vector among ALL the vectors while is the vector in the class.

If we define a matrix M as defined below,

Where the element in the vector is defined as,

Then the numerator of the utility function can be written as,

Here is just the between-class covariance matrix defined for the transformed data vectors (in the feature space).

Moving on to the denominator, if we define a matrix as

with the component of defined as , Also here is the matrix with all entries .

Here is known as the kernel matrix for class .

Then the denominator of the utility function can be written as,

Thus using both of the highlighted equations the utility function in the feature space is,

This problem can be solved (just like how LDA was solved in the input space) and the optimal value for is the leading eigenvector of .

MATLAB Implementation

The above equations can be implemented in MATLAB as follows. Note there is 2 features and we wish to reduce it a single dimension. There are 4 classes () each class having 13 vectors () and so the total number of vectors ().

load TRAINTEST2D

cluster1 = TRAIN{1,6}{1,1}; % Green
cluster2 = TRAIN{1,6}{1,2}; % Blue
cluster3 = TRAIN{1,6}{1,3}; % Red
cluster4 = TRAIN{1,6}{1,4}; % Cyan

nT = 52; % Total number of data Vectors
n1 = 13; % # vectors in cluster 1
n2 = 13; % # vectors in cluster 2
n3 = 13; % # vectors in cluster 3
n4 = 13; % # vectors in cluster 4

% Plot the data before dimensionality reduction
figure(1);
scatter(cluster1(1,:), cluster1(2,:), 'g'); 
hold on;
scatter(cluster2(1,:), cluster2(2,:), 'b');
hold on;
scatter(cluster3(1,:), cluster3(2,:), 'r');
hold on;
scatter(cluster4(1,:), cluster4(2,:), 'c');
hold on;
legend('cluster 1','cluster 2','cluster 3','cluster 4');
  • Now I’ll construct the Gram Matrix for all the data vectors,
% CONSTRUCT KERNEL MATRIX
gamma = 10;

% Arrange all the clusters together
X = [cluster1';cluster2'; cluster3'; cluster4'];
K = ones(nT,nT);

% Construct the Gram Matrix 
% K(i,:) - gives the ith vector out of ALL vectors
for i = 1:1:nT
    for j = 1:1:nT
        K(i,j) = kernelGauss(X(i,:),X(j,:),gamma);
    end
end
  • Following the equations given above we can construct M,
% Okay K(i,j) = K(x_i,x_j) Now as X(1:13,:), X(14:26,:), X(27:39,:), 
% X(40:52,:) are the 4 clusters respectively. We can calculate M_i as
% the numbers are like that due to how X was constructed.

M_1 = ones(nT,1);
M_2 = ones(nT,1);
M_3 = ones(nT,1);
M_4 = ones(nT,1);
M_star = ones(nT,1);

% sum(K(1:13,j)) is the sum of k(x,x_j) over all x belonging to cluster 1
% sum(K(14:26,j)) is the sum of k(x,x_j) over all x belonging to cluster 2
% sum(K(27:39,j)) is the sum of k(x,x_j) over all x belonging to cluster 3
% sum(K(40:52,j)) is the sum of k(x,x_j) over all x belonging to cluster 4
% sum(K(:,j)) is the sum of k(x,x_j) over ALL x (all clusters)

for j = 1:1:nT
    M_1(j) = (1/nT)*sum(K(1:13,j));
end
for j = 1:1:nT
    M_2(j) = (1/nT)*sum(K(14:26,j));
end
for j = 1:1:nT
    M_3(j) = (1/nT)*sum(K(27:39,j));
end
for j = 1:1:nT
    M_4(j) = (1/nT)*sum(K(40:52,j));
end

for j = 1:1:nT
    M_star(j) = (1/nT)*sum(K(:,j));
end

% Thus we can construct M
M = n1*(M_1-M_star)*(M_1-M_star)' + n2*(M_2-M_star)*(M_2-M_star)' + n3*(M_3-M_star)*(M_3-M_star)' + n4*(M_4-M_star)*(M_4-M_star)';
  • For constructing N, we will first separate the gram matrix we had made into the kernel matrices for each of the classes.
% Now we shall construct the kernel matrices for each of the clusters
K_1 = K(:,1:13);
K_2 = K(:,14:26);
K_3 = K(:,27:39);
K_4 = K(:,40:52);

% Thus we can construct N
N = K_1*(eye(n1,n1)-(1/n1)*ones(n1,n1))*K_1' + K_2*(eye(n2,n2)-(1/n2)*ones(n2,n2))*K_2' + K_3*(eye(n3,n3)-(1/n3)*ones(n3,n3))*K_3' + K_4*(eye(n4,n4)-(1/n4)*ones(n4,n4))*K_4';
  • Now compute and its eigenvalues - eigenvectors. Note that as N is usually singular we add a multiple of for inv(N) to become computable.
% Note that in practice, \mathbf{N} is usually singular and 
% so a multiple of the identity is added to it
N = N + 2*eye(nT,nT);
cond(N) % is N ill conditioned? 

% Now that we have both N and M, we can find all the eigenvectors
% and choose most prominent eigenvectors
P = inv(N)*M;
[V,D] = eig(P);
  • Now we can project the data onto a line using the leading eigenvector which has a eigenvalue of 3.07.
% the first eigenvector has the largest corresponding eigenvalue - 3.07
% after which 0.4, 0.28, 0.26 Then it drops down...
alpha = V(:,1);

% the projected points
y = ones(nT,1);

for i = 1:1:nT
    y(i) = alpha'*K(:,i);
end

projCluster1 = y(1:13);
projCluster2 = y(14:26);
projCluster3 = y(27:39);
projCluster4 = y(40:52);

% Plot the projected data
figure(3);
scatter(projCluster1, zeros(13,1), 'g'); 
hold on;
scatter(projCluster2, zeros(13,1), 'b');
hold on;
scatter(projCluster3, zeros(13,1), 'r');
hold on;
scatter(projCluster4, zeros(13,1), 'c');
hold on;
legend('cluster 1','cluster 2','cluster 3','cluster 4');

The plot we get for gamma (of the kernel function) being 10 is,

After projection

If we are able to try different values for the kernel function we can surely get great separation of classes.

So in conclusion both methods can be viewed as techniques for linear
dimensionality reduction. However, PCA is unsupervised and depends only on the data vectors (maximize its variance using less features) whereas Fisher linear discriminant (KLDA) also uses class-label information to bring the data vectors closer to other data vectors in the same class while maximizing the variance between classes.

Written with StackEdit.

Thursday, April 14, 2016

Equivalent Kernel

The prediction of the output for a input is,

here the function is called smoother matrix or the equivalent kernel.

Note that we are substituting the solution for which is from when we maximize the posterior p.d.f

This effective kernel defines the weights by which the training
set target values are linearly combined in order to make a prediction for a new value of x.

Here scalar can be thought of as a measure of how important the th input-output vector pair ( the mapping in the Training DataSet) is; for the prediction of the output corresponding to input .

For example in the gaussian kernel, the closer is to , the larger the magnitude of the kernel function. Thus those corresponding will be weighted higher.

This is visually shown below. when we work with an actual dataset.

You can read more about this in the wiki page for Kernel Smoother.

Kernel Matrix

A Kernel Matrix - operates on the training set and produces predictions, is the number of input vectors (, and there are data samples {} in the training dataset.

Here the predictions for the input vectors will be a vector - ,

If we wish to form a “model” of sorts (which gives us predictions) instead of laboriously computing the kernels every time we wish to make a prediction. We can form the kernel and thus for any new input we can find the closest . In order to save memory space and even ensure accuracy we can set a resolution and operate on normalized data.

So if we consider a resolution of 0.01 and the normalization range to be [0,1]. Then the input vectors ( will be if we consider the input to be 2 dimensional.

So even though it will take a long time to compute the entire matrix and even experiment with various kernel parameter values to finally obtain the kernel matrix. But once we have it we can quickly finish predicting for new inputs.

Now I’ll use the above concepts to form such a model using this dataset. Find the whole code here, please ensure you have kernel Functions defined in separate files and added to MATLAB’s path.

% The exponential kernel is closely related to the Gaussian kernel, with only the 
% square of the norm left out. It is also a radial basis function kernel.
function [res] = kernelExp(X1, X2, c)
    res = exp( -(norm(X1-X2)) * c );
  • First load the data into the workspace,

load('ASSIGNMENT1.mat');

  • Separate DATA into the training dataset and testing dataset.
% Training DataSet - 7,500 data samples
inputTrain = DATA(1:7500,1:2); % each row is a 1*2 input vector
outputTrain = DATA(1:7500,3:4); % each row is a 1*2 target vector

% Testing DataSet
inputTest = DATA(7501:10000,1:2);
outputTest = DATA(7501:10000,3:4);
  • Now depending on how the input is normalized (in our case it’s 0 to 1), get the parameters initialized. The larger the resolution the longer the forming of the model will take, but the model will serve to predict a larger number of inputs with great accuracy (we won’t have to round off to the closest). I am preallocating the modelInputsmatrix (as when an array is growing by assignment or concatenation it affects the code performance).
r = 0.1; % Resolution
% The number of X_o's (as input is 1*2) and normalized to [0,1]
M = int8( ((1/r)+1)*((1/r)+1) ); 
N = 7500; % 75% of the total 10,000 samples are used as the training set.
modelInputs = zeros(M,2);
NormalisedInputs = 0:r:1;
  • Construct the set of input vectors using NormalisedInputs, by taking all pairwise combinations of it (as we need a 1*2 vector).
for i = 1:1:length(NormalisedInputs)
    for j = 1:1:length(NormalisedInputs)
        %modelInputs = [modelInputs; [NormalisedInputs(i),NormalisedInputs(j)]];
        modelInputs(j+length(NormalisedInputs)*(i-1),1:2) = [NormalisedInputs(i),NormalisedInputs(j)];
    end
end
  • For a particular value of gamma now we can construct the matrix,
K = zeros(M,N);
gamma = 0.1;

for i = 1:1:M
    for j = 1:1:N
        K(i,j) = kernelGauss(modelInputs(i,1:2),X_i(j,1:2),gamma);
    end
end

They are the following,

K_gauss = zeros(M,N);
K_sigmoid = zeros(M,N);
gamma = 10;
alpha = 1;
c = 1;

for i = 1:1:M
    for j = 1:1:N
        K_gauss(i,j) = kernelGauss(modelInputs(i,1:2),X_i(j,1:2),gamma);
        K_sigmoid(i,j) = kernelHyperTangent(modelInputs(i,1:2),X_i(j,1:2),alpha,c);
    end
end
  • At this point I would like to show visually how the weighing of the training set works, as you can see each row of the matrix corresponds to a single input while the columns are for each of the training samples. So if you run the following code,

Note that I tried for about 2 days to find the MATLAB syntax to represent the X axis as the first input parameter, Y axis as the second input parameter. So when we take say resolution of 0.1. then that would be the scale of the X and Y axis. Now using Z axis we can represent the weight we attribute to that input. So it would be a scatter plot with (x,y,z) where (x,y) is the input and z is its corresponding weight. In this way it would be clearly seen as a hill how the inputs similar to the training input under consideration gets high weightage.

The following code displays the weights corresponding to all 121 input vectors from (0,0) to (1,1) (when we took resolution = 0.1) for the Pth training sample. Here M is 121 cause that many input vectors exist from 0 to 1 with a resolution of 0.1

plot(K_gauss(1:M,P));

When P = 98 (the index of the training sample is 98) with training input - (0.396, 0.495), we get the following plot.

weight distribution

See how we have 5 significant peaks in the weights? if we had plotted it in 3d we could have seen all these peaks were actually close together.
The largest peak is at index 50 which happens to be the input vector (0.4,0.5). Note how similar it is to the training input we are comparing with.
The next 2 peaks are at 39 and 61, which are (0.3,0.5) and (0.5,0.5). So we see a unit resolution change also counts as close.
The final 2 are 28 and 72 which are (0.2,0.5) and (0.6,0.5).

So this shows us how is larger the closer is to .

Now the question arises how much larger should the weights be, if they are close, rather how should the weights vary as the distance between them - changes. This is controlled using the parameter, I used a value of 50 and obtained the above graph. Note that a larger gamma, means that more emphasis on the closeness. So for , you will get a single peak at the the 50th index.

impulse lol

This much must have got you a great idea on how this gaussian kernel works as an instance based learner.

Ok so for a particular training sample (i.e. ) we plotted the weight values (corresponding to each of the input vectors - 121 of them) and showed how the input vectors which are “closer” to that particular training sample’s input is given a larger weight.

Cool so to predict the output for a particular input vector - we use all the weights and weigh ALL the training sample’s outputs.

But first we need to normalize the weights to 1 as we use these weights to only contribute the relative importance of the various training set’s outputs.

% This vector's ith element is the sum of weights used to predict the 
% output for the ith input vector.
sumOfWeights = zeros([M,1]);
for i = 1:1:M
    sumOfWeights(i) = sum(K_gauss(i,1:N));
end

predictions = K_gauss * outputTrain;
% Normalize it
predictions(1:M,1) = predictions(1:M,1)./ sumOfWeights;
predictions(1:M,2) = predictions(1:M,2)./ sumOfWeights;

Moving on to testing this model, we can calculate the MSE when we compare the results of the prediction to the actual outputs.

% Now we are going to test this model - calculate MSE error
L = 10000 - N; % remmaining samples in the data set are used to test
mse = 0;

for i = 1:1:L
    % in this iteration we wish to find the output for inputTest(i,1:2)
    modelInput = inputTest(i,1:2);

    % First we need to find which index this input is closest to
    % in modelInputs
    X1 = round(modelInput(1), numberOfDecimals);
    X2 = round(modelInput(2), numberOfDecimals);

    index1 = find(modelInputs(1:M,1) == X1);
    index2 = find(modelInputs(1:M,2) == X2);
    index = intersect(index1,index2);

    % so the corresponding prediction will be
    modelOutput = predictions(index,1:2);
    actualOuput = outputTest(i,1:2);

    squaredError = (modelOutput - actualOuput)*(modelOutput - actualOuput)';
    mse = mse + squaredError;
end

% as its the Mean we gotta divide with number of samples.
mse = mse/L;

With using the gaussian kernel we get a mean square error of which is reasonable.

Written with StackEdit.

Saturday, March 12, 2016

Bayesian Technique

Here we will explore the relationship between maximizing likelihood p.d.f - , maximizing posterior p.d.f - , minimization of the sum-of-squares error function - and the regularization technique.

When we maximise the posterior probability density function w.r.t the parameter vector using the “bayesian technique” - we need both the likelihood function and the prior. (The denominator in the bayes theorem is just a normalization constant so that doesn’t really matter.)

The model

We have a set of inputs, with corresponding target values .

We assume that there exists some deterministic function such that we can model the relationship between these two as the sum of with additive gaussian noise,

is the precision (inverse variance) of the additive univariate Gaussian noise.

We define as the linear combination of basis functions,

We define the parameter vector as and basis vector as .

This parameter vector is very important as the posterior p.d.f is the updated probability of given some training data. which is found from the prior data of . While the likelihood p.d.f of getting that training data given .

We usually choose because we need a bias term in the model. (to control the extent of the shift in itself - check this answer out)

For the data set as a whole we can write the set of model outputs as a vector ,

Here the basis matrix is a function of and is defined with its th-row being = for such rows.

Likelihood function

We assume that these data points are drawn independently from the distribution we would have to multiply the individual data point’s p.d.f - which are gaussian.

Note that the th data points p.d.f is centered around as the mean.

Does the product of univariate gaussians forms a multivariate distribution in {}?? I say this because we choose a gaussian prior, thus the likelihood should also be gaussian right?

Prior

We choose the corresponding conjugate prior, as we have a likelihood function which is the exponential of a quadratic function of .

No clue why but for now for this to make sense let’s say that the likelihood function is also gaussian - product of all those gaussians.

Thus the prior p.d.f is a normal distribution -

Posterior

The posterior p.d.f is a (as we choose a conjugate prior)

After solving for and we get,

(The complete derivation is available in Bishop - (2.116)) - coming soon


The sizes are,
The mean vectors, and are both and they can be thought of as the optimal parameter vector and pseudo observations respectively.
The covariance matrices, and are both

We shall consider a particular form of Gaussian prior in order to simplify the treatment. Specifically, we assume a zero-mean isotropic Gaussian governed by a single precision parameter ,

So we basically take and

Thus if we use this prior we can simplify the mean vector and the covariance matrix of posterior p.d.f to,


Now if we take log of the posterior pdf - , in order to maximize it with respect to w, we find that what we obtain is equivalent to the minimization of the sum-of-squares error function with the addition of a quadratic regularization term, corresponding to .

Thus we conclude that while maximising likelihood function is equivalent to the minimization of the sum-of-squares error function, maximising posterior p.d.f is equivalent to the regularization technique.

The regularization technique is used to control the over-fitting phenomenon by adding a penalty term to the error function in order to discourage the coefficients from reaching large values.

This penalty term arises naturally when we maximize posterior p.d.f w.r.t

Here the minimization of the sum-of-squares error function - is also same as Maximization of the likelihood p.d.f. Taking log of we get,

thus maximizing likelihood is equal to maximizing (rest are all constants w.r.t )

Thursday, January 21, 2016

Parameter Estimation

Objective

Use System Identification Toolbox of MATLAB to estimate the transfer function of a system - given sampled data of the system’s input and output.
Then use the minimum mean square error (MMSE) estimator method to alternatively estimate the model of the system.

Theory

Mathematical modelings importance in various fields cannot be understated. It is used to describe a system using mathematical concepts and language. When we have an interest in performing tests on a complicated system which is difficult to physically obtain, expensive, and sensitive to failure. Then it is safer and cheaper to perform the same tests on the model using computer simulations rather than carry out repetitive experimentations and observations on the real system.

In making these “Models” we have various types,

Theoretical Models

These models are obtained from fundamental principles, such as the laws of conservation of mass, energy, and momentum along with other chemical principles such as chemical reaction kinetics and thermodynamic equilibrium, etc.

Empirical models

These models are based on experimental plant data. These models are developed using data fitting techniques such as linear and nonlinear regression.

We will be dealing with empirical models in the remainder of this experiment, models obtained exclusively from experimental plant data are also known as black-box models . Such models do not provide detailed description of the underlying physics of the process. However, they do provide a description of the dynamic relationship between inputs and outputs. Thus they are sometimes more adequate for control design and implementation.

Thus we come to the issue of Parameter Estimation, given a set of input output data pairs we need to estimate the values for the coefficients used in the model such that the model best fits the experimental evidence.

Let the relationship between input and output of the system be modeled as the difference equation given below,

where is the output sample and is the input sample.

So for samples of input-output data pairs, we can write the set of simultaneous equations in matrix form in order to form a concise notation.

Consider a vector having elements from to ,
and a matrix having elements from to as the first column and to as the second column,
and finally a vector which holds the two parameters and .

MMSE Derivation

So the equations formed from entering the n data pairs into the difference equation given above can be written in matrix form as follows,

even though we write a equals sign, it is usually not possible to find a which perfectly models the system. we just aim for a such that the model most faithfully fits the given data ( samples). Note that as , (infinite number of data points) until then R.H.S and L.H.S continues to approach each other.

From the minimum mean square error (MMSE) estimator method, we get the following formula to calculate this ,

I will now detail the proof for the above equation which is used to estimate (i.e. the values of and ).

As I said before we need to find a such that the model most faithfully fits the given data, how can we make this statement into a relationship involving the parameters of interest?

So “most faithfully” can be thought of to mean, we want to minimize the error. Let us then define the error of the model as follows,

Now we don’t want to minimize this error - for all data samples. Because we care about the deviation of from , we do not care about the direction of this deviation. This is because positive and negative error of subsequent samples can cancel each other out. Thus we should take the - so that we have the absolute value of Error. We would rather define the error as the value (also known as Mean Square Error) rather than just .

The reason why we prefer MSE (squaring) to taking modulus when we want to quantify this error, is due to the fact that while both eliminate the primary problem of “positive and negative error”; squaring makes the algebra much easier to work with and offers properties that the absolute method does not. Additionally the squared difference has specific mathematical properties; such that it’s continuously differentiable (which nice when you want to minimize it - like now). There are additional reasons which can be found here.

Okay getting back to our hunt for the best fit for the given data, we need to minimize with respect to , for that we differentiate with respect to and equate that to zero.

Lets expand ,

The transpose respects addition,

Opening the brackets, note that

Vector Calculus Side Note


Here both and are vectors while is a matrix.


You can skip the following proof

Now the formulas given above can be understood by the fact that vectors are just used to hold multiple elements together - for the sole reason of applying operators onto them in bulk.

So in the first formula both and are vectors. What both and will be is a scalar value which is the dot product of both vectors.



Differentiating a scalar w.r.t a vector is defined as a new vector with its th element being the differentiation of that scalar with the th element of the vector.

Here I have to add that this topic divides the academia.
Two competing notational conventions split the field of matrix calculus into two separate groups. The two groups can be distinguished by whether they write the derivative of a scalar with respect to a vector as a column vector or a row vector.

For the second formula, the proof is straightforward given the above result. Here is a matrix. Apply product rule, note that the terms in the bracket are considered as constants when differentiating.

From the first formula - when comparing, can be thought of as while as . So,


When we partially differentiate P w.r.t , only is a variable while everything else is considered to be a constant.

Partially differentiating w.r.t and equating to 0 we get the value of for which the Mean Square Error is minimum,

Here is a symmetric matrix thus,

Moving all terms without to one side,

So post-multiplying both sides with ,

Taking transpose on both sides, note that because and .

Thus we get our ,

Simulation

I will first generate “data” using the simulink library in MATLAB. There we can construct a model of a system and run simulations on it.

Basic block diagram

Here as you can see I am applying a step input to a first order system, and writing both the input (Step) and output to the file try.mat, now looking at the settings of the step function and the ‘write to file’ block you can see I have chosen a Sampling Rate of 0.1 secs and initial time is 0 secs.

The settings for the Source - Step Input

The settings for the WriteToFile block

So we store time, input, and output into a variable - data and this file is called try.mat. Note that as we have 0.1 sec sample time we sample every 0.1 secs for 30 secs. Therefore we will have 301 samples ( and an initial 0 data sample).

Here the transfer function of the system used to generate the data is ,

You might get a error - Error encountered in block ‘exp1/To File’ - Error opening or closing file ‘try.mat’ . The error will open the Diagnostic Viewer as shown below,

Cannot Write to file Diagnostic Viewer error

We can fix this error by opening MATLAB as administrator, this error is caused due to MATLAB not having the permission to write to disk. Open MATLAB as shown in the image below,

Open MATLAB as administrator

On opening the Scope block we will see the input and output superimposed on a graph with amplitude as a function of time. The yellow line is the unit step input and the purple line is the systems output.

Scope block shows the input output relationship graphically

In order to do that we need to load this generated data into the MATLAB’s workspace. We use the load try.mat command to achieve this, once we do that the data variable holding the time, input, and output as rows will be added to the MATLAB workspace. As you can see below the first row is the time sampled every 0.1 secs, the second row is the step input which starts at 1 sec and finally the system’s output is the third row in the data variable. Note that I printed data' for better readability that is why the rows I refer to are shown as columns in the image below.

The steps to load the generated data into MATLAB workspace

This data variable can now be separated into its 3 components using the commands as shown below,

Commands to seperate time, input and output from out of data

We will be now using the t, u, and y variables we have made in order to reverse engineer the above system transfer function.

The Minimum Mean Square Error (MMSE) estimator method

The first method is using the equation we just derived which models the system using a first order difference equation, and then minimizes the Mean Square Error between the actual system’s output and the model’s predicted output.

Now that we have the time, input, and output variables in the workspace, we can construct and using the commands as shown below,

The steps to construct Y and Phi

Note that in MATLAB element’s indexes range from 1 to n rather than 0 to n like more programming languages.

As I had explained in the proof, I am taking the all but the first data element when I construct , and all but the last element when i construct .

Then I apply the formula to find the for which the MSE is minimum. This comes out to be for values of = 0.9618 and = 0.1950.

Let us now verify how well these parameters fit the actual system, for this I am using MATLAB’s c2d function to take my continuous time i/o system and find its equivalent discrete time i/o system with a sampling time of 0.1 secs (Just like the data we generated).

The code to find the Actual or more precisely the values of a and b which MATLAB finds for the same system

Note that the shown in the image is a approximation of , and with smaller sampling time this approximation eventually tends to become the continuous-time model itself!

For better understanding I will elaborate on how the tells us what and are. MATLAB tells us,

Cross multiplying, we get

By the Time shifting property of Z transforms i.e. ,

Doesn’t the above equation look familiar? yes it is of the same structure as the difference equation we used to model this system. Just as first order systems are modeled using , we can use the difference equation of this form to model the equivalent discrete time model system.

Thus the best fitting which MATLAB performs using its “inbuilt algorithms” (which are based on the same mathematics we used, but has multiple small modifications to improve its speed and accuracy) is = 0.9672 and = 0.1639.

So an error of = 0.0054 and = -0.0311 seems acceptable.

System Identification Toolbox

We can also use the System Identification Toolbox that comes with MATLAB to estimate the same parameters. This method has the additional benefits in the fact that they have added options for all algorithms to estimate the parameters, runs extremely fast, graphs the model formed etc.

We can use ident to open the System Identification App, we will see a window as shown below open up,

The system identification toolbox window

We can now import the data using which MATLAB will estimate the parameters,

The first dialog box used to import data into ident

Note that we need to keep the sampling time as 0.1 sec and starting time as 0 secs just as we defined it in the simulink model. Now click import to get the data into ident. The time domain data will look like the below image,

Time domain data after importing


Now using Estimate -> Transfer Function Models, we are prompted for number of poles and zeros which we need to enter.

Number of poles and zeros required for estimating

After which they estimate using Instrument Variable approach (which is the default) as shown below,

Estimation of transfer function

Now as you can see the Transfer function has been estimated with a 100% fit.

The transfer function estimated

This is absolutely correct as is 1.666 and is 0.333.

We can even use the “Show in LTI” option as shown in image above to see the out of a LTI system modeled on a .

Show in LTI option


There is also a different MATLAB option to do the estimation, that is using
Estimate -> Process Models,

Just untick the ‘Delay’ checkbox, uncheck ‘Zero’ checkbox - as there are no zero’s, choose the correct number of poles - 1 from the dropdown menu. Then click on estimate - We get the results quite fast,

Estimate using Process Models

As we can see the transfer function has been recovered from the time domain data here, using the model. Where is 5 and is 3.

Conclusion

In conclusion we have used the experimental plant data (Which we generated using simulink) to estimate black-box models . We understood the mathematics behind this generation and also employed more suitable methods available in MATLAB in the process of generating said models.

Addendum

We can show very clearly the benefits of empirical models when we model the FET amplifier circuit - voltage divider bias configuration.

JFET voltage divider bias

Now before we begin, let’s remember that a DC gate-to-source voltage controls the level of DC drain current through a relationship known as Shockley’s equation.

So we can express the change in drain current that will result from a change in gate-to-source voltage by using the transconductance factor .

Now substituting the ac equivalent model for the JFET, we get the following circuit,

This is the AC equivalent model applied to JFET in voltage divider bias configuration

From the above circuit we find the input impedance and output impedance as,

Setting = 0 Volts sets and to zero, also

Redrawn network

So the Gain of the amplifier is,

The analysis thus far has been limited to a particular frequency. For the amplifier, it was a frequency that normally permitted ignoring the effects of the capacitive elements, reducing the analysis to one that included only resistive elements and sources of the independent and controlled variety.

The junction capacitance (input capacitance) will be virtually increased due to Miller’s effect.

where is the gain of the amplifier and is the feedback capacitance.

So now the Transfer function is given by,

Now just like how we obtained the above transfer function by first principle we can use the parameter estimation methods detailed in the sections above to obtain the same transfer function.

Thus knowing that FET amplifier has a first order transfer function characteristics lets us model it using .

Note that this is not a complete black box model, it is infact a grey box model as we know the structure (First order).

Written with StackEdit.