I have posted earlier also regarding KL divergence query but unfortunately did not received a reply may be due to the complexity of my question so I have tried to explain my problem by using simple example.
I have one reference sensor signal and 1 measured value of some sensor.
I want to find out the error or difference between Ref and Measured sensor signal Values.
SO I am using KL divergence.
First I normalized my reference and sensor signal histogram and then applied KL divergence.
My data is too much and complicated means it contains a lot of zeroes and negative values and also 0.001 like these values also.
I was applying KL divergence but unfortunately was not being able to get some good results so I was wondering that may be I did not able to get the good concept of KL divergence or I am doing wrong at some point in The code.
It will nice of people if someone help me out in this. I shall be grateful.
I have also seen a post explaining KL divergence and had a look at it but some concepts remained missing so I am posting my example along with query.
Am i on right way or there is some fault in my concepts .
Thanks a lot in advance.
ref = [5 6 7 5 8 7 8 9 -2 -3 -4];
measured_sensor = [3 3 4 5 7 8 9 9 -1 -2 -3];
%normalized histograms for
C= hist( ref);
C1 = C ./ sum(C);
D = hist(measured_sensor);
D1 = D ./ sum(D);
figure(1)
ax11=subplot(321);
bar(C1)
ax12=subplot(322);
bar(D1)
d = zeros(size(C1));
goodIdx = C1>0 & D1>0;
d1 = sum(C1(goodIdx) .* log(C1(goodIdx) ./ D1(goodIdx)))
d2 = sum(D1(goodIdx) .* log(D1(goodIdx) ./ C1(goodIdx)))
d(goodIdx) = d1 + d2
Mean Based Gaussian Hysterisis (Means Error Finding)
ref = [5 6 7 5 8 7 8 9 -2 -3 -4];
measured_sensor = [3 3 4 5 7 8 9 9 -1 -2 -3];
sig_diff = ref - measured_sensor ;
m = mean(sig_diff)
deviation = std(sig_diff);
pos = sig_diff(sig_diff>0)
neg = sig_diff(sig_diff<0)
m_pos = mean(pos)
m_neg = mean (neg)
hysterisis = abs( m_pos)+ abs(m_neg)
figure(6)
ax11=subplot(321);
histfit(sig_diff)
hold on
plot([m m],[0 5000],'r')
plot([m-deviation m-deviation],[0 5000],'r')
plot([m+deviation m+deviation],[0 5000],'r')
hold off
The error value or Hysterisis value that I am getting with mean based Gaussian Distribution is 3.2500.
So I am expecting the error values from KL divergence near to 3.2500 value or in the range with some tolerance is also accepted.
Actually I am confused about these techniques that which one is giving me more precise version of error and with which technique i will get the best results.
Related
EDIT TO REQUIREMENT 6 AND NEW REQUIREMENT ADDED
6) Exactly 4 columns/rows must have degree 3.
7) No two vertices of degree 3 are adjacent to each other.
My goal:
To generate and save all matrices that meet specific requirements. Then compare each matrix to additional matrices that have been manually entered previously to check for specific similarities. I can add more detail if somebody thinks it would be helpful. I believe I have the comparison aspect of the code sorted out already, so I am waiting on the matrix generation portion. I need to do this for multiple sizes but I will focus this question on the 10x10 case.
Specific requirements:
1) Must be a 10x10 matrix (representing a graph on 10 vertices).
2) Must be symmetric (representing an adjacency matrix).
3) Have a diagonal of 0s (no loops).
4) Only 1s and 0s (simple graph).
5) The entire matrix must have exactly 48 1s (the graph has 24 edges).
6) Each column/row must have either 3 or 6 1s (each node as degree 3 or 6).
Application:
I am investigating a conjecture and believe I have come up with a possible solution which could break down the conjecture into smaller pieces and possibly prove some aspects. I want to use brute force to show if my idea works for a small specific case. Also having a base code in place could allow for future modifications to test other possible cases or ideas.
Ideas and thought process:
I used the edges of a graph to manually input my comparison set. For example:
G9=graph([1 1 1 2 2 3 4 4 4 5 5 6 6 6 6 3 3 9 2 2 2 7 7 8],[2 3 4 3 4 4 5 6 7 6 7 7 3 9 10 9 10 10 7 8 9 8 9 9]);
I think this is the only graph, up to isomorphism, which meets the previously listed requirements.
My original thought was to create the possible matrices that satisfy the given conditions then compare them to my comparison set. I still think this is the best approach.
I foolishly attempted to generate random matrices, completely overlooking the massive number of possibilities. Using a while loop, I first generated a random matrix that satisfied the first four requirements. Then in separate nested for statements I checked for requirement 5 using numedes() and requirement 6 using all(mod(degree())). That was a bad approach for several fairly obvious reasons, but I learned a lot through the process and it led me to the code that should do my final comparisons.
This is the first time I have used Matlab so I am learning as I go. I have been working on this one code for nearly 2 weeks and do not know if what I have come up with is "good", but I am proud of what I have been able to do by myself. I have reached the point where I feel like I need some outside advice. I am open to any suggestions and any level of help. A reference to a source, a function suggestion, another approach, or a complete solution with a "plug and play" code would be appreciated. I do not shy away from putting forth the effort to achieve my goals.
I appreciate any feedback.
If you want to brute force it, you've got 3773655750150 possible configurations to test for 3-or-6-connectedness. I think you'll probably need more powerful math (Polya Enumeration Theorem? or some other combinatoric theorem I probably forgot) to solve this one.
edit: This recursive solution is much more constrained and is likely to finish in the next century.
E = containers.Map('KeyType', 'int32', 'ValueType', 'any');
for k = 0:9
E(k) = [];
end
foo(E, 3, 0);
foo(E, 6, 0);
function E = foo(E, D, n)
% E : graph edges (map)
% D : degree (3 or 6)
% n : current node
if (n == 9)
e_degree = cellfun(#length,E.values);
if all(e_degree) && all(~mod(e_degree,3))
print_E(E)
end
return
end
e = E(n); % existing edges
m = setdiff((n+1:9), e); % candidate new edges
K = D - length(e);
% if too many edges, return early
if (K < 0)
return
end
C = combnk(m, K);
N = size(C, 1);
for k = 1:N
c = C(k,:);
E(n) = unique([e, c]);
for kv = 1:K
v = c(kv);
E(v) = unique([E(v), n]);
end
% down the rabbit hole
E = foo(E, D, n + 1);
for D = 3:3:6
E = foo(E, D, n + 1);
end
% remove edges added in this loop
E(n) = setdiff(E(n), c);
for kv = 1:K
v = c(kv);
E(v) = setdiff(E(v), n);
end
end
end
function print_E(E)
for k = 0:9
fprintf('%i: ',k);
fprintf('%i ', E(k));
fprintf('\n');
end
fprintf('\n');
end
suppose that we are determine peaks in vector as follow:
we have real values one dimensional vector with length m,or
x(1),x(2),.....x(m)
if x(1)>x(2) then clearly for first point peak(1)=x(1);else we are then comparing x(3) to x(2),if x(3)
[ indexes,peaks]=function(x,m);
c=[];
b=[];
if x(1)>x(2)
peaks(1)=x(1);
else
for i=2:m-1
if x(i+1)< x(i) & x(i)>x(i-1)
peak(i)=x(i);
end;
end
end
end
peaks are determined also using following picture:
sorry for the second picture,maybe it is not triangle,just A and C are on straight line,but here peak is B,so i can't continue my code for writing algorithm to find peak values in my vector.please help me to continue it
updated.numercial example given
x=[2 1 3 5 4 7 6 8 9]
here because first point is more then second,so it means that peak(1)=2,then we are comparing 1 to 3,because 3 is more then 1,we now want to compare 5 to 3,it is also more,compare 5 to 4,because 5 is more then 4,then it means that peak(2)=5,,so if we continue next peak is 7,and final peak would be 9
in case of first element is less then second,then we are comparing second element to third one,if second is more then third and first elements at the same time,then peak is second,and so on
You could try something like this:
function [peaks,peak_indices] = find_peaks(row_vector)
A = [min(row_vector)-1 row_vector min(row_vector)-1];
j = 1;
for i=1:length(A)-2
temp=A(i:i+2);
if(max(temp)==temp(2))
peaks(j) = row_vector(i);
peak_indices(j) = i;
j = j+1;
end
end
end
Save it as find_peaks.m
Now, you can use it as:
>> A = [2 1 3 5 4 7 6 8 9];
>> [peaks, peak_indices] = find_peaks(A)
peaks =
2 5 7 9
peak_indices =
1 4 6 9
This would however give you "plateaus" as well (adjacent and equal "peaks").
You can use diff to do the comparison and add two points in the beginning and end to cover the border cases:
B=[1 diff(A) -1];
peak_indices = find(B(1:end-1)>=0 & B(2:end)<=0);
peaks = A(peak_indices);
It returns
peak_indices =
1 4 6 9
peaks =
2 5 7 9
for your example.
findpeaks does it if you have a recent matlab version, but it's also a bit slow.
This proposed solution would be quite slow due to the for loop, and you also have a risk of rounding error due to the fact that you compare the maximal value to the central one instead of comparing the position of the maximum, which is better for your purpose.
You can stack the data so as to have three columns : the first one for the preceeding value, the second is the data and the third one is the next value, do a max, and your local maxima are the points for which the position of the max along columns is 2.
I've coded this as a subroutine of my own peak detection function, that adds a further level of iterative peak detection
http://www.mathworks.com/matlabcentral/fileexchange/42927-find-peaks-using-scale-space-approach
Sorry for the perhaps confusing title...
Basically I have a 3x3 matrix containing elevation angle, azimuth angle and range. I want to generate new matrices each time elevation >5 deg. There are usually about 5 segments that have this data and I want to separate each one into a new matrix.
I know how to index but not sure how to put this condition in...
Thanks
sat_tcs=llh2tcsT(sat_llh,station_llh);
sat_elev=atan2(sat_tcs(3,:),sqrt(sat_tcs(1,:).^2+sat_tcs(2,:).^2));
sat_azim=atan2(-sat_tcs(2,:),sat_tcs(1,:));
range=sqrt(sat_tcs(1,:).^2+sat_tcs(2,:).^2+sat_tcs(3,:).^2);` sat_elev(sat_elev < 5*deg2rad) = NaN; sat_look_tcs=[sat_elev;sat_azim;range];
It would be helpful to have some examples of the input and expected output, but taking a guess at what you mean I'd try this:
elevation_column = 3;
threshold = 5;
m = [1 2 3; 4 5 6; 7 8 9; 1 2 3];
n = m(m(:,elevation_column)>threshold,:);
This produces:
n =
4 5 6
7 8 9
Sorry, I would post an image of my graph but supposedly I need reputation points for that..but it the elevation data looks almost sinusoidal and so it has regions over 5 deg and then falls again. I want to generate a new matrix for every set above this angle
suppose that we are determine peaks in vector as follow:
we have real values one dimensional vector with length m,or
x(1),x(2),.....x(m)
if x(1)>x(2) then clearly for first point peak(1)=x(1);else we are then comparing x(3) to x(2),if x(3)
[ indexes,peaks]=function(x,m);
c=[];
b=[];
if x(1)>x(2)
peaks(1)=x(1);
else
for i=2:m-1
if x(i+1)< x(i) & x(i)>x(i-1)
peak(i)=x(i);
end;
end
end
end
peaks are determined also using following picture:
sorry for the second picture,maybe it is not triangle,just A and C are on straight line,but here peak is B,so i can't continue my code for writing algorithm to find peak values in my vector.please help me to continue it
updated.numercial example given
x=[2 1 3 5 4 7 6 8 9]
here because first point is more then second,so it means that peak(1)=2,then we are comparing 1 to 3,because 3 is more then 1,we now want to compare 5 to 3,it is also more,compare 5 to 4,because 5 is more then 4,then it means that peak(2)=5,,so if we continue next peak is 7,and final peak would be 9
in case of first element is less then second,then we are comparing second element to third one,if second is more then third and first elements at the same time,then peak is second,and so on
You could try something like this:
function [peaks,peak_indices] = find_peaks(row_vector)
A = [min(row_vector)-1 row_vector min(row_vector)-1];
j = 1;
for i=1:length(A)-2
temp=A(i:i+2);
if(max(temp)==temp(2))
peaks(j) = row_vector(i);
peak_indices(j) = i;
j = j+1;
end
end
end
Save it as find_peaks.m
Now, you can use it as:
>> A = [2 1 3 5 4 7 6 8 9];
>> [peaks, peak_indices] = find_peaks(A)
peaks =
2 5 7 9
peak_indices =
1 4 6 9
This would however give you "plateaus" as well (adjacent and equal "peaks").
You can use diff to do the comparison and add two points in the beginning and end to cover the border cases:
B=[1 diff(A) -1];
peak_indices = find(B(1:end-1)>=0 & B(2:end)<=0);
peaks = A(peak_indices);
It returns
peak_indices =
1 4 6 9
peaks =
2 5 7 9
for your example.
findpeaks does it if you have a recent matlab version, but it's also a bit slow.
This proposed solution would be quite slow due to the for loop, and you also have a risk of rounding error due to the fact that you compare the maximal value to the central one instead of comparing the position of the maximum, which is better for your purpose.
You can stack the data so as to have three columns : the first one for the preceeding value, the second is the data and the third one is the next value, do a max, and your local maxima are the points for which the position of the max along columns is 2.
I've coded this as a subroutine of my own peak detection function, that adds a further level of iterative peak detection
http://www.mathworks.com/matlabcentral/fileexchange/42927-find-peaks-using-scale-space-approach
The expression is:
for i=1:n
X(:,i) = [P{i}(:)];
end
where X is a DxN matrix and P is a cell-array.
reshape(cat(3,P{:}),[numel(P{1}) n])
Of course, the above solution is just for fun. I would recommend profiling both solutions and only using this one if it has a significant performance advantage.
Maintenance and readability are also very important factors to consider when writing code.
If you obtained the cell array via mat2cell, you may be wanting to arrange blocks of an image into the columns of an array X. This can be achieved in a single step using the command IM2COL
%# rearrange the large array so that each column of X
%# corresponds to the 4 pixels of each 2-by-2 block
X = im2col(largeArray,[2 2],'distinct');
You might be able to get away with:
P{1} = [ 1 2; 3 4];
P{2} = [ 7 8; 9 10];
P{3} = [ 11 12; 13 14];
X = [P{:}]
X =
1 2 7 8 11 12
3 4 9 10 13 14
Then some sort of reshape() to get to where you want to be.