Matlab中具有特定质量函数的随机变量生成
Random variable generation with a specific mass function in Matlab
我必须编写一个计算机程序,当给定概率质量函数时
{pj , j = 1, . . . , n} 作为输入,给出随机数的值作为输出
具有此质量函数的变量。这些是我关于这个问题的唯一信息。我试过了
%f(x)=pdf
%F(x)=cdf
%X=tixaia metavliti
f(x);
X;
F(x)= cumsum(f(x));
%firstly we want F(x) to have unique values
%we remove the non-unique values from cdf and X
[F(x),mask] = unique(F(x));
X = X(mask);
%apply inverse interpolation
%so we can produce random value X from the cdf
u = rand;
projection = interp1( F(x),X,u );
该代码是否接近于解决我的问题?谁能帮帮我?
这是一个示例,它根据定义的概率从向量 (7 8 9 10) 中采样 2 个值。它使用统计工具箱。
R = randsample([7 8 9 10],2,true,[0.15 0.35 0.35 0.15])
如果您想自己编写代码,Tasos 将您指向正确的 link。
我必须编写一个计算机程序,当给定概率质量函数时 {pj , j = 1, . . . , n} 作为输入,给出随机数的值作为输出 具有此质量函数的变量。这些是我关于这个问题的唯一信息。我试过了
%f(x)=pdf
%F(x)=cdf
%X=tixaia metavliti
f(x);
X;
F(x)= cumsum(f(x));
%firstly we want F(x) to have unique values
%we remove the non-unique values from cdf and X
[F(x),mask] = unique(F(x));
X = X(mask);
%apply inverse interpolation
%so we can produce random value X from the cdf
u = rand;
projection = interp1( F(x),X,u );
该代码是否接近于解决我的问题?谁能帮帮我?
这是一个示例,它根据定义的概率从向量 (7 8 9 10) 中采样 2 个值。它使用统计工具箱。
R = randsample([7 8 9 10],2,true,[0.15 0.35 0.35 0.15])
如果您想自己编写代码,Tasos 将您指向正确的 link。