First matlab codes for statistics

This commit is contained in:
2015-10-16 00:17:00 +02:00
parent 6cf7d01663
commit 92ed818419
4 changed files with 216 additions and 78 deletions

View File

@@ -0,0 +1,25 @@
% generate data:
x = randn( 1, 100000 );
% histogram:
[h,b] = hist( x, 100 );
% normalize:
bs = b(2)-b(1);
h = h/sum(h)/bs;
% plot:
bar( b, h );
xlabel( 'x' );
% median, quartile:
xs = sort( x )
q = [ xs(length(xs)/4), xs(length(xs)/2), xs(3*length(xs)/4) ];
%q = quantile( x, [0.25, 0.5, 0.75 ] );
% plot:
bar( b(b<q(1)), h(b<q(1)), 'FaceColor', [0.5 0 0.5] );
hold on;
bar( b((b>=q(1)) & (b<q(2))), h((b>=q(1)) & (b<q(2))), 'FaceColor', [0.9 0 0] );
bar( b((b>=q(2)) & (b<q(3))), h((b>=q(2)) & (b<q(3))), 'FaceColor', [0 0 0.9] );
bar( b(b>=q(3)), h(b>=q(3)), 'FaceColor', [0.5 0 0.5] );
hold off;

View File

@@ -0,0 +1,6 @@
function x = randomwalk(n,p)
r = rand(n,1);
r(r<p) = -1.0;
r(r>=p) = +1.0;
x = cumsum(r);
end

View File

@@ -0,0 +1,25 @@
p = 0.5;
nsteps = 100;
nwalks = 1000;
y = zeros( nwalks, nsteps/10 );
for k = 1:length( y )
x = randomwalk( nsteps, p );
for j = 1:nsteps/10
y(k,j) = x((j-1)*10+1);
end
%plot( x )
%pause( 1 )
if rem(k,100) == 0
%[h1,b1] = hist( y(1:k,1), [-50:2:50] );
%[h2,b2] = hist( y(1:k,2), [-50:2:50] );
%bar( b1, h1, 1.0, 'b' );
%hold on;
%bar( b2, h2, 'FaceColor', 'r' );
%hold off;
sdev = var( y(1:k,:), 1 );
plot( sdev )
pause( 1.0 );
end
end