Gaussian Basis Function - matlab

Can you please tell me how can I model a Gaussian Basis Function in a 2 Dimensional Space in order to obtain a scalar output?
I know how to apply this with a scalar input, but I don't understand how should I apply it to a 2 dimensional vector input. I've seen many variations of this that I am confused.

With each Gaussian basis associate a center of the same dimension as the input, lets call it c. If x is your input, you can compute the output as
y = exp( - 0.5 * (x-c)'*(x-c) )
This will work with any dimension of x and c, provided they are the same. A more general form is
y = sqrt(det(S)) * exp( - 0.5 * (x-c)'* S * (x-c) )
where S is some positive definite matrix, well the inverse covariance matrix. A simple case is to take S to be a diagonal matrix with positive entries on the diagonals.

To sample from a multivariate normal distribution, use the MVNRND function from the Statistics Toolbox. Example:
MU = [2 3]; %# mean
COV = [1 1.5; 1.5 3]; %# covariance (can be isotropic/diagonal/full)
p = mvnrnd(MU, COV, 1000); %# sample 1000 2D points
plot(p(:,1), p(:,2), '.') %# plot them

Related

Why isn't my Matlab code for a randomly generated covariance matrix making a positive definite matrix?

Here is my code. I'm getting an error that when I use chol(V) that V is not positive definite. I would think that by construction it must be positive definite. Any idea what's going wrong?
% I want 10000 draws of a 5x1 multivariate normal distribution
N =5;
T = 10000;
% randomly generate standard deviations
sigma = 1 + .1*rand(N,1);
% randomly generate correlations which are between [-1,1]
rho = -1+2*rand(nchoosek(N,2),1);
% This grabs the indices of the elements in the lower triangle below the main diagonal
% itril comes from https://www.mathworks.com/matlabcentral/fileexchange/23391-triangular-and-diagonal-indexing
I = itril(N,-1);
% Initialize correlation matrix
corr = zeros(N);
% Fill in lower triangle of correlation matrix with generated correlations
corr(I) = rho;
% make correlation matrix symmetric with 1s on diagonal
corr = corr+corr'+eye(N);
% Variance matrix is sigma_i*sigma_j*corr(i,j)
V = (sigma*sigma').*corr;
% means vector
mu = rand(N,1);
% generate multivariate normal draws
e = mu' + randn(T,N)*chol(V);
That's just not the way to create a correlation matrix. Just because your matrix is symmetric, has 1's on the diagonal and values between -1 and 1 off the diagonal doesn't mean it's a correlation matrix. E.g. the way you create your matrix, you could have correlation +1 between random variables X1 and X2, +1 between X2 and X3, and -1 between X1 and X3, which is clearly not possible if the matrix entries come from the correlations between "real" random variables. So, there's also no guarantee that the matrix thus generated is positive (semi-) definite.
You should rather generate some matrix X that contains the (linear) dependencies between your random variables, and then your covariance matrix is simply X' * X (or X * X' depending on how you order X).

MATLAB. How can we get a 2d multinomial by interpolation

We are given a n x n data. Approximate the funxtion.
For example. If we are given 2(1,1), 3(1,2), 4(2,1), 5(2,2)
Then we have to interpolate the 2 D- ultinomial as $0*x*y+y+2*x-1$.
This problem can be formulated as a set of linear equations, which are trivial to solve using mldivide function.
Let me illustrate this using the example you included.
% from the given example
in = [...
1,1;
1,2;
2,1;
2,2];
out = [...
2;
3;
4;
5];
% compute the variable terms in the polynomial
x = in(:,1);
y = in(:,2);
xy = x .* y;
c = ones(size(out)); % constant
% compute the coefficients of the polynomial
p = [xy,y,x,c] \ out;
% result: [0; 1; 2; -1]
The statementp = [xy,y,x,c] \ out computes the optimal coefficients (least-square error) when the problem is overconstrained (i.e. no solution exists to exactly satisfy all equations). But if there are only as many equations as there are variables (like in this example, there are 4 equations due to 4 input-output pairs and there are 4 coefficients that need to be estimated), then the coefficients can be computed simply by p = inv([xy,y,x,c]) * out.

Computing coefficients of FIR filter in Matlab

I have to create the function G(z) = [3*H^2(z)-2H^3(z)]*(z^-2) which takes as an input the impulse response of the filter H(z) , and outputs the impulse response of G(z).
I assume H(z) is a generic FIR filter
b = fir1(10,0.5);
h = impz(b);
t = impzlength(b);
where h is the values of the impulse response.
I think H^2(z) = h(n).*z(-2n) and H^3(z) = h(n).*z^(-3n); H(z) is the transfer function of the filter .
I have to calculate the coefficients of num and den of the equation now, but I am stuck.
I thought at first to use coeffs and a for loop but i need also the zero coefficients, while coeffs only provides the non zero coefficients.
Now I thought that maybe there is a work-around for obtaining the coefficients: basically I have to select only certain values of h.
For example, to obtain the coefficients only for z^-3n:
n = 3;
y = h(n:n:end); % = 3 6 9 12 ...
But now I can't figure out how to sum appropriately the coefficients for z^-3n and z^-2n.
Unless you are using a non-standard notation, H^2(z) is not h(n).*z(-2n) but rather the multiplication of a polynomial with coefficients h with itself. This can be computed with:
H2 = conv(h, h);
Similarly, H^3(z) can be computed using:
H3 = conv(H2, h);
Then, summing the polynomials boils down to summing the coefficients, with the only catch that you have to pad H2 so that the two vectors of coefficients have the same size:
H2 = [H2 zeros(1,length(H3)-length(H2))];
S = 3*H2 -2*H3;
The final multiplication by z^(-2) (which can be represented by the polynomial coefficients [0 0 1]) could be achieve in the same way using conv with:
G = conv(S, [0 0 1 zeros(1,length(Sum)-3)]);
or alternatively, you may realize that multiplying by a single term polynomial is essentially equivalent to shifting the coefficients:
G = [0 0 S];

find polynomial equation from array with matlab

I've some x.mat and y.mat.
And I would like to find the polynomial equation from this.
I've tried
p = polyfit(x,y,3);
with y2 = p(1)*x.^3 + p(2)*x.^2 + p(3)*x
but my y2 is not equal to the original y . What is wrong ?
Thanks you
As radarhead wrote in his comment, you forgot the coefficient of zero degree (p(4) here).
Assuming x and y are vectors of same length n, polyfit(x,y,n-1) will return a vector containing the coefficients of the interpolating polynomial (of degree n-1) in descending order.
Then, the value of the interpolating polynomial at a point z will be given by:
p(1)*z^3 + p(2)*z^2 + p(3)*z + p(4)
Don't forget p(4)! As Bas suggested, you can use the polyval function to easily compute the value of a polynomial at a a given point:
polyval(p,z);
To illustrate this, see the code below, which generates 4 data points, plots those points and the polynomial interpolating them:
n = 4;
x = sort(rand(n,1));
y = rand(n,1);
p = polyfit(x,y,n-1);
figure
hold on
plot(x,y,'bo');
xx=linspace(x(1),x(end),100);
plot(xx,polyval(p,xx),'r');
hold off

Numerical integration over non-uniform grid in matlab. Is there any function?

I've got function values in a vector f and also the vector containing values of the argument x. I need to find the define integral value of f. But the argument vector x is not uniform. Is there any function in Matlab that deals with integration over non-uniform grids?
Taken from help :
Z = trapz(X,Y) computes the integral of Y with respect to X using
the trapezoidal method. X and Y must be vectors of the same
length, or X must be a column vector and Y an array whose first
non-singleton dimension is length(X). trapz operates along this
dimension.
As you can see x does not have to be uniform.
For instance:
x = sort(rand(100,1)); %# Create random values of x in [0,1]
y = x;
trapz( x, y)
Returns:
ans =
0.4990
Another example:
x = sort(rand(100,1)); %# Create random values of x in [0,1]
y = x.^2;
trapz( x, y)
returns:
ans =
0.3030
Depending on your function (and how x is distributed), you might get more accuracy by doing a spline interpolation through your data first:
pp = spline(x,y);
quadgk(#(t) ppval(pp,t), [range])
That's the quick-n-dirty way. Ther is a faster and more direct approach, but that is fugly and much less transparent:
result = sum(sum(...
bsxfun(#times, pp.coefs, 1./(4:-1:1)) .*... % coefficients of primitive
bsxfun(#power, diff(pp.breaks).', 4:-1:1)... % all 4 powers of shifted x-values
));
As an example why all this could be useful, I borrow the example from here. The exact answer should be
>> pi/2/sqrt(2)*(17-40^(3/4))
ans =
1.215778726893561e+00
Defining
>> x = [0 sort(3*rand(1,5)) 3];
>> y = (x.^3.*(3-x)).^(1/4)./(5-x);
we find
>> trapz(x,y)
ans =
1.142392438652055e+00
>> pp = spline(x,y);
>> tic; quadgk(#(t) ppval(pp,t), 0, 3), toc
ans =
1.213866446458034e+00
Elapsed time is 0.017472 seconds.
>> tic; result = sum(sum(...
bsxfun(#times, pp.coefs, 1./(4:-1:1)) .*... % coefficients of primitive
bsxfun(#power, diff(pp.breaks).', 4:-1:1)... % all 4 powers of shifted x-values
)), toc
result =
1.213866467945575e+00
Elapsed time is 0.002887 seconds.
So trapz underestimates the value by more than 0.07. With the latter two methods, the error is an order of magnitude less. Also, the less-readable version of the spline approach is an order of magnitude faster.
So, armed with this knowledge: choose wisely :)
You can do Gaussian quadrature over each piecewise pair of x and sum them up to get the complete integral.