Rectangular grid using MATLAB
1 次查看(过去 30 天)
显示 更早的评论
I have two cell arrays and each cell has a matrix x = (10X5). Each row in x is an array (between -1 and 1) with a mean "m" and std "s". Now i want to represent this matrix in a rectangular grid using MATLAB such that each box has a mean (orange color) and std deviation (filled with red colour in either side of mean) as shown in the example. So basically there should be 10X2 rectangular grids (corresponding to 10 rows and two cells). Can someone please help me with this? I looked up online but could not find anything.
0 个评论
采纳的回答
John BG
2017-6-25
编辑:John BG
2017-6-25
Hi Rajesh
the attached script example_color_bars.m generates 1 graph for 10 samples
1.
generating and visualising the data
clear all;close all;
a = -1;b = 1;
% for p=1:10
% r(p,:) = (b-a)*rand(5,1)+a;
% end
% block of 10 signals
r=(b-a)*rand(10,5)+a % 5 samples of uniform distribution with range [-1 1]
figure(1)
for k=1:1:10
subplot(5,2,k);ax1=stem(r(k,:)); axis([1 5 -1 1])
end
.
.
2.
calculating and plotting means and variance intervals
% stats
m=mean(r,2) % mean=1/N*sum()
std=(var(r')').^.5 % var=E((X-mean)^2)
mtol=0.05 % how wide mean line is: 2*tol
y=[-1*ones(10,1) m-std m-tol m+tol m+std ones(10,1)]
yd=diff(y,1,2);
% y1=ones(1,5);y10=[.5 1.5 .5 1.5 1]; % test values
% y=[repmat(y1,9,1); y10];
x=[1:1:10];
figure(2);ax2=gca;
bh=barh(ax2,yd,.5,'stacked','EdgeColor','none');
bh(1).FaceColor=[.9 .9 .9]; % outside var: grey
bh(5).FaceColor=[.9 .9 .9];
bh(3).FaceColor=[.9 .4 .1]; % mean: orange
bh(2).FaceColor=[.8 0 0]; % variance interval: red
bh(4).FaceColor=[.8 0 0];
ax2.XTickLabel={'-1' '-0.5' '0' '0.5' '1' '1.5'}
.
.
if you find this answer useful would you please be so kind to consider marking my answer as Accepted Answer?
To any other reader, if you find this answer useful please consider clicking on the thumbs-up vote link
thanks in advance
John BG
1 个评论
John BG
2017-6-29
Following some comments to enhance this answer
COMMENT 1:
ADDING SAMPLE MARKERS
% uniform distribution with range [-1 1] and a large enough amount of samples
% has mean=.5*(a+b) var=1/12*(b-a)^2
clear all;close all;
N=5; % N amount of samples
a = -1;b = 1; % lower and upper limits of uniform probability distribution
% block of 10 signals
r=(b-a)*rand(10,N)+a % 5 samples of uniform distribution with range [-1 1]
% generating random points and stem plots
figure(1)
for k=1:1:10
subplot(5,2,k);ax1=stem(r(k,:)); axis([1 N -1 1])
end
% stats
m=mean(r,2) % mean=1/N*sum(X)
sd=(var(r')').^.5 % var=E((X-mean(X))^2)
% y1=ones(1,5);y10=[.5 1.5 .5 1.5 1]; % test signal
% y=[repmat(y1,9,1); y10];
y=[-1*ones(10,1) m-sd m-.05 m+.05 m+sd ones(10,1)]
yd=diff(y,1,2);
x=[1:1:10];
figure(2);ax2=gca;
bh=barh(ax2,yd,.5,'stacked','EdgeColor','none');
bh(1).FaceColor=[.9 .9 .9];bh(5).FaceColor=[.9 .9 .9]; % outside var
bh(3).FaceColor=[.9 .4 .1];
bh(2).FaceColor=[.8 0 0];bh(4).FaceColor=[.8 0 0]; % variance interval
% add mean reference line
hold all;
figure(2);
plot(ax2,[1 1],[0 11],'k--') % 0 mean marker
for k=1:1:size(r,1) % sample mean markers
plot(ax2,[m(k)+1 m(k)+1],[k-.25 k+.25],'Color',[0 1 0])
end
for k=1:1:size(r,1) % samples
for s=1:1:size(r,2)
plot(ax2,[r(k,s)+1 r(k,s)+1],[k-.25 k+.25],'k')
end
end
dxtick=.25;
ax2.XTick=[0:dxtick:2];
ax2.XTickLabel=ax2.XTick-1
.
COMMENT 2:
USING CONTINUOUS UNIFORM DISTRIBUTION STANDARD DEVIATION
% 1/12*(b-a)^2
m=zeros(10,1)
sd2=1/12^.5*(b-a) % when enough samples then var1=1/12*(b-a)^2
y=[-1*ones(10,1) m-sd2 m-.05 m+.05 m+sd2 ones(10,1)]
yd=diff(y,1,2);
figure(3);ax3=gca;
bh3=barh(ax3,yd,.5,'stacked','EdgeColor','none');
bh3(1).FaceColor=[.9 .9 .9];bh3(5).FaceColor=[.9 .9 .9]; % outside var
bh3(3).FaceColor=[.9 .4 .1];
bh3(2).FaceColor=[.8 0 0];bh3(4).FaceColor=[.8 0 0]; % variance interval
% add mean reference line
hold all;
figure(3);plot(ax3,[1 1],[0 11],'k--') % 0 mean marker
for k=1:1:size(r,1) % sample mean markers
plot(ax3,[m(k)+1 m(k)+1],[k-.25 k+.25],'Color',[0 1 0])
end
for k=1:1:size(r,1) % add sample markers
for s=1:1:size(r,2)
plot(ax3,[r(k,s)+1 r(k,s)+1],[k-.25 k+.25],'k')
end
end
dxtick=.25;
ax3.XTick=[0:dxtick:2];
ax3.XTickLabel=ax3.XTick-1
.
COMMENT 3:
CONFIDENCE INTERVALS OF UNIFORM NORMAL AND TRIANGULAR DO NOT HAVE SAME LENGTH
The confidence interval defined as [-sigma sigma] has different percentage, relative length for different probability distributions
uniform: 58%
normal: 68%
triangular: 65%
source: NIST
.
COMMENT 4:
APPROXIMATING STANDARD DEVIATION
while the continuous uniform distribution has the standard deviation
var3=1/12*(b-a)^2
and the continuous uniform distribution has standard deviation
sd3=var3^.5
= .577 % 57.7%
for discrete uniform probability distribution the exact variance is
var4=1/12*((b-a+1)^2-1)
=0.6667
but approximation, it's only an approximation, for discrete uniform distribution variance, is
sd4=(1/12*((b-a+1)^2-1))^.5
= 0.8165 % wider
COMMENT 5:
THE PROBLEM OF NOT USING ENOUGH SAMPLES
It has clear symptoms
- mean bias that is not constant
- standard deviation bias not constant
- the mean value is not centred
- confidence interval length not constant
- confidence interval beyond possible values
To avoid these problems, the more samples the better, the more constant that the mean becomes, and each standard deviation interval gets closer to the continuous uniform distribution standard deviation 57.7% confidence interval
N=50
.
N=150
.
Example of confidence interval spilling over the possible values with N=5.
The confidence interval goes beyond the possible values that the samples can take, off [-1 1]
.
COMMENT 6:
BESSEL'S CORRECTION APPLIES TO VARIANCE
Besse's correction: 1/(N-1) instead of 1/N on the variance
MATLAB function var already applies it by default.
sd=(var(r')').^.5 % var=E((X-mean)^2)
=
0.3918
0.5241
0.4789
0.1929
0.7288
0.6207
0.7547
0.5934
0.5055
0.2855
(1/(size(r,2)-1)*sum((r-m).^2,2)).^.5
=
0.3918
0.5241
0.4789
0.1929
0.7288
0.6207
0.7547
0.5934
0.5055
0.2855
without the Bessel's correction
1/size(r,2)*sum((r-m).^2,2).^.5
=
0.1567
0.2096
0.1916
0.0772
0.2915
0.2483
0.3019
0.2373
0.2022
0.1142
that matches
sd=(var(r')',1,2).^.5
note that
var(data,2)
is not the same as
var(data,1,2)
the effect of the Bessel correction is desired in the kind of short runs used here, as the following simple example illustrates
D=[2 4 4 4 5 5 7 9]
var(D) % = 4.5714
var(D)^.5 % = 2.1381
var(D,1,2) % = 4
var(D,1,2)^.5 % = 2
In h ttp://en.wikipedia.org/wiki/Standard_deviation it's mentioned that the mean value that should be as centred as possible, and the standard deviation interval over-spilling the possible values are particularly significant with low amounts of samples, N<10
there's no general formula or closed set of formulae for short runs estimation of standard deviation (in literature referred as ' unbiased sample standard deviation') that works across all probability density distributions.
COMMENT 7:
THE c4 STANDARD DEVIATION CORRECTION FACTOR
there's literature attempting to fix the standard deviation error for short runs with a fixed correction factor
c4(N)=(2/(N-1))^.5*gamma(N/2)/gamma((N-1)/2)
In this example, for N=5
N=5;
(2/(N-1))^.5*gamma(N/2)/gamma((N-1)/2)
= 0.94
my understanding is that c4 may be applied directly
sd3=sd*c4
or c4 can be further approximated with a simple -1.5
sd5=(1/(N-1.5)).^.5*(N)^.5*(var(r,1,2)).^.5
where gamma2 is the population excess kurtosis
example kurtosis
kurtosis(D)
note that var with default field applies Bessel and therefore cannot be used to manually calculate kurtosis
mean((D-mean(D)).^4)/var(D)^2
not the same as
mean((D-mean(D)).^4)/var(D,1,2)^2
the excess kurstosis is defined as
excess_kurtosis=kurtosis(r,1,2)-3
sd6=(1/(N-1.5-.25*gamma2)).^.5*(N)^.5*(var(r,1,2)).^.5
= 0.6840
sd6=(1./(N*ones(numel(gamma2),1)-1.5-.25*gamma2)).^.5.*N^.5.*(var(r,1,2)).^.5
=
0.6461
0.6265
0.6634
0.6364
0.6386
0.6840
0.6639
0.6614
0.6719
0.6919
Just once more, in related literature it's clearly mentioned that for smallest samples and highest precision these approximations may not be accurate enough.
So far, the approaches of guessing a confidence interval may have an alternative counting the closest samples to the mean.
COMMENT 8
APPLYING CONFIDENCE INTERVAL ON SAMPLES COUNT
Another option for such short runs is to directly apply the confidence interval on the count of samples closer to the sample mean and thus exclude the 'odd' samples.
Again it's just an approximation of the standard deviation, but at least in the following way there is overconfidence by rendering possible samples not included in the probability distribution function domain.
Start measuring the continuous uniform distribution [-1 1] confidence interval:
confidence_interval=2*(1/12*(b-a)^2)^.5
Then the percentage of samples to collect is
confidence_interval/(b-a) % *100
Amount of samples to include in the confidence interval,
N=5;N1=ceil(N*confidence_interval/(b-a))
Now define the confidence interval with the closest 3 samples to the short run mean
r1=[];r2=[];
for k=1:1:10
L=r(k,:)
[vals,ind]=sort(d2(L,m(k)*ones(1,N)))
r1=[r1;min(L(ind([1:1:N1])))];r2=[r2;max(L(ind([1:1:N1])))];
% L1=L(ind([1:1:N1]))
% r1=[r1;min(L1)];r2=[r2;max(L1)];
end
y=[-1*ones(10,1) r1 m-.05 m+.05 r2 ones(10,1)]
yd=diff(y,1,2);
x=[1:1:10];
figure(2);ax2=gca;
bh=barh(ax2,yd,.5,'stacked','EdgeColor','none');
bh(1).FaceColor=[.9 .9 .9];bh(5).FaceColor=[.9 .9 .9]; % outside var
bh(3).FaceColor=[.9 .4 .1];
bh(2).FaceColor=[.8 0 0];bh(4).FaceColor=[.8 0 0]; % variance interval
% add mean reference line
hold all;
figure(2);
plot(ax2,[1 1],[0 11],'k--') % 0 mean marker
for k=1:1:size(r,1) % sample mean markers
plot(ax2,[m(k)+1 m(k)+1],[k-.25 k+.25],'Color',[0 1 0])
end
for k=1:1:size(r,1) % samples
for s=1:1:size(r,2)
plot(ax2,[r(k,s)+1 r(k,s)+1],[k-.25 k+.25],'k')
end
end
dxtick=.25;
ax2.XTick=[0:dxtick:2];
ax2.XTickLabel=ax2.XTick-1
At least now there's no risk to take a confidence level that renders as reliable samples that are not even within the probability density function domain.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!