如何使用Matlab计算在二进制序列中得到至少$q$0的概率?

如何使用Matlab计算在二进制序列中得到至少$q$0的概率?,matlab,math,Matlab,Math,对于一个来自我朋友的项目(不是教育),我需要做一些计算。我在matlab方面的经验很少(7年前我有过一些java课程,所以我知道什么是for循环,等等) 问题 给定一个长度为m的二进制序列S,其中n不清楚(至少对我来说),n、x和q是随机数还是固定值。我写了一些东西,好像它们是随机数。如果不是这样的话,它很容易改变 代码的每一行都在代码中的注释(%)中解释 %This is the number of bits m=1000; %This is a counter needed for th

对于一个来自我朋友的项目(不是教育),我需要做一些计算。我在matlab方面的经验很少(7年前我有过一些java课程,所以我知道什么是for循环,等等)

问题

给定一个长度为m的二进制序列S,其中n不清楚(至少对我来说),n、x和q是随机数还是固定值。我写了一些东西,好像它们是随机数。如果不是这样的话,它很容易改变

代码的每一行都在代码中的注释(%)中解释

%This is the number of bits

m=1000;

%This is a counter needed for the probability calculation

count=0;

%Loop needed for the probability calculation

for i = 1:1000000

%This is a random number generator that generates m (1000 in this case)
%random numbers between 0 and 1

randomseq=rand(1,m);

%This creates a random number between 0 and 1 and then multiplies it by
%0.29 so it can actually be between 0 and 0.29. If n is not a random
%number, then change it with the fixed value that it has.

n=rand(1,1)*0.29; 

%This line is for the following:
%If a number is less than n, then make it 1, else make it 0 (this is
%needed so you can acquire a probability for the number of ones and the
%number of zeros). Here you will have around  n*1000 ones, but not
%equal since randomseq is a random vector with 1000 elements and those
%are too few numbers so it can create a real distribution.

onezerorand=randomseq<n;

%That's why this is needed, just in case if the number of ones go over
%30%. If it goes over 30%, than make the numones coef 0.29

if sum(onezerorand)>=300
   numones=0.29;
else

%This is a coef which represent the number of ones, but in probability
%terms (between 0 and 1). Mean just calculates the mean value between
%the zeros and the ones, which is essentially the probability.

    numones=mean(onezerorand);
end

%The rest of the numbers are zero, so you get the probability by
%subtracting numones from 1

numzeros=1-numones;

%This creates a random number x between 0 and 0.59. If x is not a
%random number, the same comment goes as for n.

x=rand(1,1)*0.59;

%There is a 50% chance that x of the ones will be changed to zeros and
%vice versa, so you create a random number between 0 and 0.5 and
%multiply it by x.

p0=rand(1,1)*0.5;
p1=1-p0;
x_ones=rand(1,1)*p0*x;
x_zeros=rand(1,1)*p1*x;

%You add the changed zeros to the ones and the changed ones to the
%zeros and you subtract the changed ones from the ones and the changed
%zeros from the zeros, so you can get the last value of the number of
%zeros.

new_numones=numones-numones*x_ones+numzeros*x_zeros;
new_numzeros=numzeros-numzeros*x_zeros+numones*x_ones;

%You create a random number between 0.51 and 0.99, so you can compare
%the number of zeros to it. If q is not a random number, the same
%comment as the one for x and n.

q=rand(1,1)*0.48+0.51;

%You compare the number of zeros to the new random number. If it's
%bigger than it, than you raise the counter, if not, you do nothing.
%You repeat this 1000000 times so you can get a good probability
%estimation and the number you get is the probability of this occuring.

if new_numzeros>q
    count=count+1;
end
end

%The probability of this event occuring

prob=count/1000000;
%这是位数
m=1000;
%这是概率计算所需的计数器
计数=0;
%概率计算所需的循环
对于i=1:1000000
%这是一个生成m(本例中为1000)的随机数生成器
%0到1之间的随机数
randomseq=兰特(1,m);
%这将创建一个介于0和1之间的随机数,然后将其乘以
%所以它实际上可以在0到0.29之间。如果n不是随机数
%编号,然后使用其具有的固定值进行更改。
n=兰特(1,1)*0.29;
%该行用于以下内容:
%如果数字小于n,则将其设为1,否则将其设为0(这是
%所以你可以得到一个概率的数目和
%零的数目)。在这里,您将有大约n*1000个,但不是
%由于randomseq是包含1000个元素的随机向量
%数字太少,因此它可以创建一个真实的分布。
onezerorand=randomseqq
计数=计数+1;
终止
终止
%该事件发生的概率
prob=计数/1000000;

根据我的估计,概率约为60%,随机系数为n、x和q。如果它们是固定值,我就不知道了,因为我不知道它们的确切值。

谢谢,我明天再看。这些变量是已知的,不是随机的。然后改变:n=rand(1,1)*0.29;以n=0.25为例,x=rand(1,1)*0.59;以x=0.55为例,q=rand(1,1)*0.48+0.51;例如,对于q=0.6,您将只添加实际值,而不是我的示例。我已经用这个值试过了,概率是55%,如果你需要什么,明天再写一次,或者如果这是应该的,就发一篇帖子。干杯:)