如何在 Matlab 中获得概率图值的表格形式?

发布于 2024-11-07 12:24:09 字数 82 浏览 0 评论 0原文

我试图找到一些随机数据的概率分布。我可以在 matlab 中生成绘图,但如果我能获得表格格式的值,那么我会发现它更有用,这样我就可以进行蒙特卡罗模拟。

I am trying to find the probability distribution of some stochastic data. I can generate the plot in matlab but would find it more useful if i could get the values in tabled format so i can do a monte carlo simulation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

过去的过去 2024-11-14 12:24:09

您可以使用 hist 的可选输出参数从随机数据中获取概率,如下所示:

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 

您可以轻松验证这是否给出了概率分布。

bar(x,prob);hold on
plot(x,1/sqrt(2*pi)*exp(-(x.^2)/2),'r','linewidth',1.25);hold off

在此处输入图像描述

您可以使用 uitable 根据上述数据创建表格。

data=num2cell([prob(:);x(:)]);
colNames={'Probability','x'};
t=uitable('Data',data,'ColumnName',colNames);

在此处输入图像描述

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

enter image description here

You can create a table from the above data using uitable.

data=num2cell([prob(:);x(:)]);
colNames={'Probability','x'};
t=uitable('Data',data,'ColumnName',colNames);

enter image description here

猥︴琐丶欲为 2024-11-14 12:24:09

这可能是一个愚蠢的问题,但是,您使用的是离散分布(二项分布、泊松分布……)还是连续分布?如果您正在处理任何类型的连续分布,添加一个步骤并将其表示为离散分布将会引起麻烦。

即使您正在使用离散分布,表格表示也是不必要的步骤。

这里有一些代码,展示了一种非常简单的方法来完成您想要的事情。

%% 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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文