How to measure the similarity between two vectors? [duplicate] - matlab

This question already has an answer here:
How to measure the similarity of two data
(1 answer)
Closed 5 years ago.
I have two vectors for eg
Aideal=rand(256,1);
and
A_estimated=rand(256,1);
How can I measure the similarity ? By similarity I mean I want each element of A_estimated to be almost same as that of Aideal.
Can Anyone please help.

mae(A-B) % mean(abs(A-B)) % Average or mean value of array
sae(A-B) % sum(abs(A-B)) % Sum absolute error performance function
norm(A-B,1) % sum(abs(A-B)) % 1-norm of the vector, which is the sum of the element magnitudes.
norm(A-B,inf) % max(abs(A-B)) % maximum absolute row sum of the diff of vectors.
mse(A-B) % mean((A-B).^2) % Mean of Sum of squared error
sse(A-B) % sum((A-B).^2) % Sum of squared error
norm(A-B) % sqrt(sse(A-B))

If you want to compare two vectors with respecto cosine similarity below code is enough for you
function [similarity] = CosineSimilarity(x1,x2)
%--------------------------------------------------------------------------
% Syntax: [similarity] = CosineSimilarity(x1,x2);
%
% Definition: Cosine similarity is a measure of similarity between two
% non-zero vectors of an inner product space that measures
% the cosine of the angle between them. The cosine of 0° is
% 1, and it is less than 1 for any other angle. It is thus a
% judgment of orientation and not magnitude: two vectors
% with the same orientation have a cosine similarity of 1,
% two vectors at 90° have a similarity of 0, and two vectors
% diametrically opposed have a similarity of -1, independent
% of their magnitude. Cosine similarity is particularly used
% in positive space, where the outcome is neatly bounded in
% [0,1]. The name derives from the term "direction cosine":
% in this case, note that unit vectors are maximally
% "similar" if they're parallel and maximally "dissimilar"
% if they're orthogonal (perpendicular). This is analogous
% to the cosine, which is unity (maximum value) when the
% segments subtend a zero angle and zero (uncorrelated)
% when the segments are perpendicular.[1].
%
% Inputs: [x1] is a vector
% [x2] is a vector
%
% Outputs: [similarity] is between 0 and 1
%
% Complexity: No
%
% Dependencies No dependency.
%
% Author: Ugur Ayan, PhD
% ugur.ayan#ugurayan.com.tr
% http://www.ugurayan.com.tr
%
% Date: May 15, 2016
%
% Refrences [1] https://en.wikipedia.org/wiki/Cosine_similarity
%--------------------------------------------------------------------------
if ( length (x1) == length(x2) )
similarity = sum(x1.*x2) / (norm(x1) * norm(x2));
else
disp('Vectors dimensions does not match');
end

Related

How to use trapz to calculate a triple integral on a spherical harmonic?

I have a combination of spherical harmonics.
Because spherical harmonics are an orthogonal basis, we can say:
Now, I have a function that gives me a spherical harmonic, which gives a spherical harmonic matrix. (the famous spharm4)
First, I want to check if the Y_6^2 is normalized (the integral should be equal to zero) using trapz. How can a 3D integral be done with trapz function using spherical coordinates?
After that, I want to find the coefficient b using eq(2), but I still can’t understand how to use the trapz function correctly and multiply the matrices.
Any help would be appreciated.
Function that gives me a spherical harmonic:
function varargout=plm2spec(lmcosi,norma,in,ot)
% [sdl,l,bta,lfit,logy,logpm]=PLM2SPEC(lmcosi,norma,in,ot)
%
% Calculates the power spectrum of real spherical harmonic
% sine and cosine coefficients contained in the matrix 'lmcosi' which is
% of the standard form that can be plotted by PLM2XYZ and PLOTPLM.
%
% INPUT:
%
% lmcosi Spherical harmonic coefficients [l m Ccos Csin]
% norma 1 multiplication by (l+1)
% This gives the mean-square value of the
% gradient of a potential in Schmidt-harmonics
% 2 division by (2*l+1) [default]
% This gives the proper power spectral density
% as we've come to know it
% 3 none, i.e. a scaling factor of 1
% in Index to minimum degree to consider for the spectral fit [defaulted]
% ot Index to maximum degree to consider for the spectral fit [defaulted]
%
% OUTPUT:
%
% sdl Spectral density: energy per degree
% l Degree
% bta Spectral slope of loglog(l,sdl)
% lfit,logy Spectral line plot given by loglog(lfit,logy)
% logpm Error on spectral line plot given by
% loglog(lfit,logpm)
%
% SEE ALSO: MTVAR
%
% EXAMPLE:
%
% [sdl,l,bta,lfit,logy,logpm]=plm2spec(fralmanac('EGM96'));
%
% SEE ALSO: ACTSPEC
%
% The normalization by (2l+1) is what's required when the spherical
% harmonics are normalized to 4pi. See DT p. 858. A "delta"-function then
% retains a flat spectrum. See Dahlen and Simons 2008.
% See papers by Hipkin 2001, Kaula 1967, Lowes 1966, 1974, Nagata 1965
% (Lowes, JGR 71(8), 2179 [1966])
% (Nagata, JGeomagGeoel 17, 153-155 [1965])
%
% Last modified by fjsimons-at-alum.mit.edu, 03/18/2020
defval('norma',2)
lmin=lmcosi(1);
lmax=lmcosi(end,1);
pin=0;
for l=lmin:lmax
clm=shcos(lmcosi,l);
slm=shsin(lmcosi,l);
pin=pin+1;
sdl(pin)=clm(:)'*clm(:)+slm(:)'*slm(:);
end
switch norma
case 1
normfac=(lmin:lmax)+1;
case 2
normfac=1./(2*(lmin:lmax)+1);
case 3
normfac=1;
disp('Not further normalized')
otherwise
error('No valid normalization specified')
end
% disp(sprintf('Normalization %i',norma))
sdl=normfac.*sdl;
sdl=sdl(:);
% Figure out the range over which to make the fit
l=lmin:lmax;
l=l(:);
if lmin==0
defval('in',3);
elseif lmin==1
defval('in',2);
else
defval('in',1);
end
defval('ot',lmax)
lfit=l(in:ot);
if nargout>=3
% Calculate spectral slope
[bt,E]=polyfit(log10(lfit),log10(sdl(in:ot)),1);
bta=bt(1);
[logy,loge]=polyval(bt,log10(lfit),E);
logy=10.^logy;
logpm=[logy./(10.^loge) logy.*(10.^loge)];
else
[bta,lfit,logy,logpm]=deal(NaN);
end

Demeaned Returns for Covariance (Matlab)

I've got this code:
function [sigma,shrinkage]=covMarket(x,shrink)
% function sigma=covmarket(x)
% x (t*n): t iid observations on n random variables
% sigma (n*n): invertible covariance matrix estimator
%
% This estimator is a weighted average of the sample
% covariance matrix and a "prior" or "shrinkage target".
% Here, the prior is given by a one-factor model.
% The factor is equal to the cross-sectional average
% of all the random variables.
% The notation follows Ledoit and Wolf (2003)
% This version: 04/2014
% de-mean returns
t=size(x,1);
n=size(x,2);
meanx=mean(x);
x=x-meanx(ones(t,1),:);
xmkt=mean(x')';
sample=cov([x xmkt])*(t-1)/t;
covmkt=sample(1:n,n+1);
varmkt=sample(n+1,n+1);
sample(:,n+1)=[];
sample(n+1,:)=[];
prior=covmkt*covmkt'./varmkt;
prior(logical(eye(n)))=diag(sample);
if (nargin < 2 | shrink == -1) % compute shrinkage parameters
c=norm(sample-prior,'fro')^2;
y=x.^2;
p=1/t*sum(sum(y'*y))-sum(sum(sample.^2));
% r is divided into diagonal
% and off-diagonal terms, and the off-diagonal term
% is itself divided into smaller terms
rdiag=1/t*sum(sum(y.^2))-sum(diag(sample).^2);
z=x.*xmkt(:,ones(1,n));
v1=1/t*y'*z-covmkt(:,ones(1,n)).*sample;
roff1=sum(sum(v1.*covmkt(:,ones(1,n))'))/varmkt...
-sum(diag(v1).*covmkt)/varmkt;
v3=1/t*z'*z-varmkt*sample;
roff3=sum(sum(v3.*(covmkt*covmkt')))/varmkt^2 ...
-sum(diag(v3).*covmkt.^2)/varmkt^2;
roff=2*roff1-roff3;
r=rdiag+roff;
% compute shrinkage constant
k=(p-r)/c;
shrinkage=max(0,min(1,k/t))
else % use specified number
shrinkage = shrink;
end
% compute the estimator
sigma=shrinkage*prior+(1-shrinkage)*sample;
end
It's a Part of the Matlab code from Ledoit/Wolf (2003). I don't understand why the demeaning the returns before calculating the covariance? Is this Matlab specific? In my opinion, there is no need for demeaning returns before calculating with the cov-function. (The function does it on its own)
Thanks for help in advance!

trying to implement knnclassify in matlab for fisheriris dataset

I am trying to implement knnclassify in matlab for fisheriris data set and try to get the confusion matrix for it. Below is the code for matlab implementation of knnclassify.
I am unable to understand how to incorporate the fisheriris datase in this code.
and how to calculate the confusion matrix.
function classifications = knnclassify(train_points, train_labels, test_points, k);
%-------------------------------------------
% K nearest neighbour (KNN) classification
% code by Jaakko Peltonen 2008
%-------------------------------------------
% Classifies test points based on majority vote of their k nearest
% neighbours in train points. 'Nearest' is determined from squared
% Euclidean distance. In case the majority voting results in a tie,
% gives equal portions to each class in the tie.
%
% Inputs:
%---------
% train_points: matrix, the i:th row has the features of the i:th
% training point.
%
% train_labels: two possible formats.
% Format 1: a column vector where the i:th element is an integer
% value indicating the label of the i:th training
% point. The labels should start from zero.
%
% Format 2: a matrix where the i:th row has the class memberships
% of the i:th training point. Each class membership is
% a value from 0 to 1, where 1 means the point fully
% belongs to that class.
%
% test_points: feature matrix for test points. Same format as
% train_points. You can give an empty matrix: then the method
% computes leave-one-out classification error based only on
% the training points.
%
% Outputs:
%---------
% classifications: matrix, the i:th row has the predicted class
% memberships of the i:th test point. Each class
% membership if a value from 0 to 1, where 1 means
% the test point is predicted to fully belong to
% that class.
%
nDim = size(test_points,2);
nTrainPoints = size(train_points,1);
% if the training labels were provided in format 1,
% convert them to format 2.
if size(train_labels,2)==1,
nClasses = max(train_labels)+1;
train_labels2 = zeros(nTrainPoints,nClasses);
for i=1:nTrainPoints,
train_labels2(i,train_labels(i)+1) = 1;
end;
train_labels = train_labels2;
end;
nClasses = size(train_labels,2);
% if test_points is empty, perform leave-one-out classification
if isempty(test_points),
test_points = train_points;
leave_one_out = 1;
else
leave_one_out = 0;
end;
nTestPoints = size(test_points,1);
%
% Perform the KNN classification. For leave-one-out classification,
% this code assumes that k < nTrainPoints.
%
classifications = zeros(nTestPoints, nClasses);
for i=1:nTestPoints,
% find squared Euclidean distances to all training points
difference = train_points(:,1:nDim) - repmat(test_points(i,:),[nTrainPoints 1]);
distances = sum(difference.^2,2);
% in leave-one-out classification, make sure the point being
% classified is not chosen among the k neighbors.
if leave_one_out == 1,
distances(i) = inf;
end;
% collect the 'votes' of the k closest points
[sorted_distances, indices] = sort(distances);
classamounts = zeros(1, nClasses);
for j=1:k,
classamounts = classamounts + train_labels(indices(j),:);
end;
% choose the class by majority vote
indices = find(classamounts == max(classamounts));
if (length(indices) == 1),
% there is a single winner
classifications(i,indices(1)) = 1;
else
% there was a tie between two or more classes
classifications(i,indices) = 1/length(indices);
end;
end;
Answer for the first question:
You first need to fix small bug in the function:
Change
difference = train_points(:,1:nDim) - repmat...
to
difference = train_points - repmat...
Than the script to run this classifier on fisheriris:
clear
data=load('fisheriris');
[~,~,ci]=unique(data.species);
data.labels=ci-1; % start from 0
portion_to_test=40/100;
n=size(data.species,1);% #samples
test_idx=randperm(n,round(n*portion_to_test));
train_idx=setdiff(1:n,test_idx);
train_meas=data.meas(train_idx,:);
train_labels=data.labels(train_idx);
test_meas=data.meas(test_idx,:);
test_labels=data.labels(test_idx);
result=knnclassify(train_meas,train_labels,test_meas,1);
[~,mi]=max(result,[],2);
result_labels=mi-1; % start from 0;
error=sum(result_labels~=test_labels)/length(test_labels)

SNR plot and rectangular fit (Matlab)

I have attached 3 images and the SNR function to calculate the SNR between each of the images. How can I plot this SNR so its easy to understand the levels from the plot rather than just numbers.
SNR function:
% function to calculate the drop in SNR between Best focussed image to
% slightly out of focus image. The value is presented in dB scale and amplitude
%for comparison.
% Terms:
%%Signal image = best focussed image
%%Noise image = slight off focussed image
%%Dark scan = for future reference with respect to Signal image
%---------------Define function to calcuate SNR---------------%
function SNR = SNR(signal, noise, typ, noisy)
% snr - calculate signal-to-noise ratio in decibel or amplitude
%
% Syntax: SNR = snr(signal, noise)
% SNR = snr(signal, signal_noise, typ, true)
%
% Inputs:
% signal - signal amplitude
% noise - noise amplitude or noisy signal
% typ - type of SNR (default:'db' to decibel or 'amp' to amplitude)
% noisy - eval noise flag (use noise as noisy signal)
%
% Outputs:
% SNR - Signal-to-Noise Ratio
%
% Example:
% dt = 0.01;
% T = 0:dt:10;
% sig = sin(2*pi*T);
% noisy = sig + (0 + .5 * randn(1,length(T))); % error mean 0 and sd .5
% snr_db = snr(sig,noisy,'db',true)
%
% Other m-files required: none
% Subfunctions: rms
%------------------------------- BEGIN CODE -------------------------------
if ~exist('typ', 'var')
typ = 'db';
end
if ~exist('noisy', 'var')
noisy = false;
end
if noisy % eval noise
noise = signal-noise;
end
if strcmp(typ,'db')
SNR = 20*log10(rms(signal)/rms(noise));
elseif strcmp(typ,'amp')% string comparison for type value.
SNR = rms(signal)/rms(noise);
end
end
%-------------------------------- END CODE --------------------------------
RMS function
function RMS= rms(varargin)
%
% Written by Phillip M. Feldman March 31, 2006
%
% rms computes the root-mean-square (RMS) of values supplied as a
% vector, matrix, or list of discrete values (scalars). If the input is
% a matrix, rms returns a row vector containing the RMS of each column.
% David Feldman proposed the following simpler function definition:
%
% RMS = sqrt(mean([varargin{:}].^2))
%
% With this definition, the function accepts ([1,2],[3,4]) as input,
% producing 2.7386 (this is the same result that one would get with
% input of (1,2,3,4). I'm not sure how the function should behave for
% input of ([1,2],[3,4]). Probably it should produce the vector
% [rms(1,3) rms(2,4)]. For the moment, however, my code simply produces
% an error message when the input is a list that contains one or more
% non-scalars.
if (nargin == 0)
error('Missing input.');
end
% Section 1: Restructure input to create x vector.
if (nargin == 1)
x= varargin{1};
else
for i= 1 : size(varargin,2)
if (prod(size(varargin{i})) ~= 1)
error(['When input is provided as a list, ' ...
'list elements must be scalar.']);
end
x(i)= varargin{i};
end
end
% Section 2: Compute RMS value of x.
RMS= sqrt (mean (x .^2) );
Script
% sig= best focussed image
% noisy= off focussed image
% dark = no light image
%-------------------------
% calculate SNR between:
% sig and noise
% signal and dark
% noise and dark
clear
sig = rgb2gray(imread('S1-BestFocus.bmp'));
noisy = rgb2gray(imread('S1-OffFocus.bmp'));
dark=rgb2gray(imread('DarkScan.bmp'));
sig_noise = SNR(sig,noisy,'db',true);
sig_dark = SNR(sig,dark,'db',true);
noise_dark = SNR(noisy,dark,'db',true);
Figures:
figures for calculation
I am imaging a slit of 15-18 microns in width and 1mm in length, the slit is not uniform, hence I have to check how much is the variation along the length of the slit in terms of width. What is the best possible way to get the measurement. ( one method is to use rectangular fit).

plot 3D line, matlab

My question is pretty standard but can't find a solution of that.
I have points=[x,y,z] and want to plot best fit line.
I am using function given below (and Thanx Smith)
% LS3DLINE.M Least-squares line in 3 dimensions.
%
% Version 1.0
% Last amended I M Smith 27 May 2002.
% Created I M Smith 08 Mar 2002
% ---------------------------------------------------------------------
% Input
% X Array [x y z] where x = vector of x-coordinates,
% y = vector of y-coordinates and z = vector of
% z-coordinates.
% Dimension: m x 3.
%
% Output
% x0 Centroid of the data = point on the best-fit line.
% Dimension: 3 x 1.
%
% a Direction cosines of the best-fit line.
% Dimension: 3 x 1.
%
% <Optional...
% d Residuals.
% Dimension: m x 1.
%
% normd Norm of residual errors.
% Dimension: 1 x 1.
% ...>
%
% [x0, a <, d, normd >] = ls3dline(X)
I have a.
So equation may be
points*a+dist=0
where dist is min. distance from origon.
Now my question is how to plot best filt line in 3D.
It helps to actually read the content of the function, which uses Singular Value Decomposition.
% calculate centroid
x0 = mean(X)';
% form matrix A of translated points
A = [(X(:, 1) - x0(1)) (X(:, 2) - x0(2)) (X(:, 3) - x0(3))];
% calculate the SVD of A
[U, S, V] = svd(A, 0);
% find the largest singular value in S and extract from V the
% corresponding right singular vector
[s, i] = max(diag(S));
a = V(:, i);
The best orthogonal fitting line is
P = x0 + a.*t
as the parameter t varies. This is the direction of maximum variation which means that variation in the orthogonal direction is minimum. The sum of the squares of the points' orthogonal distances to this line is minimized.
This is distinct from linear regression which minimizes the y variation from the line of regression. That regression assumes that all errors are in the y coordinates, whereas orthogonal fitting assumes the errors in both the x and y coordinates are of equal expected magnitudes.
[Credit: Roger Stafford , http://www.mathworks.com/matlabcentral/newsreader/view_thread/294030]
Then you only need to create some t and plot it:
for t=0:100,
P(t,:) = x0 + a.*t;
end
scatter3(P(:,1),P(:,2),P(:,3));
You may want to use plot3() instead, in which case you need only a pair of points. Since a line is infinite by definition, it is up to you to determine where it should begin and end (depends on application).