I am trying to find the probability distribution of some stochastic data. I can generate the plot in mat开发者_Go百科lab but would find it more useful if i could get the values in tabled format so i can do a monte carlo simulation.
You can get the probabilities from stochastic data by using the optional output arguments of hist
like so:
z=randn(10000,1); %# generate 10000 trials of a normally distributed random variable.
[f,x]=hist(z,100); %# get x values and bin counts (f)
prob=f/trapz(x,f); %# divide by area under the curve to get the
You can easily verify that this gives you the probability distribution.
bar(x,prob);hold on
plot(x,1/sqrt(2*pi)*exp(-(x.^2)/2),'r','linewidth',1.25);hold off
You can create a table from the above data using uitable
.
data=num2cell([prob(:);x(:)]);
colNames={'Probability','x'};
t=uitable('Data',data,'ColumnName',colNames);
This might be a silly question, however, are you working with a discrete distribution (binomial, Poisson, ...) or a continuous distribution? If you're working with any kind of continuous distribution adding a step and representing this as discrete is going to cause trouble.
Even if you're working with a discrete distribution the tabular representation is an unnecessary step.
Here's some code that shows a pretty easy way to do what you want.
%% Parametric fitting, followed by random number generation
% Generate some random data from a normal distribution with mean = 45 and
% standard devation = 6
X = 45 + 6 * randn(1000,1);
foo = fitdist(X, 'normal')
% Use the object to generate 1000 random numbers
My_data = random(foo, 1000,1);
mean(My_data)
std(My_data)
%% Kernel smoothing, followed by random number generation
% Generate some random data
X = 10 + 5 * randn(100,1);
Y = 15 + 3 * randn(60,1);
my_dist = vertcat(X,Y);
% fit a distribution to the data
bar = fitdist(my_dist, 'kernel')
% generate 100 random numbers from the distribution
random(bar, 100, 1)
%% Fitting a discrete distribution
% Use a poisson distribution to generate a 1000 random integers with mean = 6.8
Z = poissrnd(6.8, 1000,1);
foobar = fitdist(Z, 'poisson')
% generate 100 random numbers from the distribution
random(foobar, 100, 1)
精彩评论