Search code examples
matlabmatlab-deployment

Generate Gaussian data in matlab


I need to generate a Gaussian vector,e.g. "delta" -with arbitrary size-, with zero mean and variance of "alpha". If "alpha" is chosen such that norm(delta,2)<=0.5 with probability of e.g. 90%. How can we do that?


Solution

  • With var.*randn(1000,1) + mu you can generate a vector with a certain variance var and mean mu. Then we calculate the norm(delta,2). This operation is repeated 100000 times. In the variable B it is stored the values for which norm(delta,2)<=0.5. The probability is then Prob=length(B)/length(Normv)

    mu = 0; alpha = 0.01537;
    Normv=0;
    REP=100000
    for j=1:REP
        delta = alpha.*randn(1000,1) + mu;
        Normv(j)=norm(delta,2);
    end
    B=Normv(Normv<=0.5);
    Prob=length(B)/length(Normv);
    

    You could also include a for loop, sweeping the variance

    Normv=0;
    mu = 0; 
    aux=1;
    REP=10000;
    variance = 0.014:0.0001:0.017;
    for k=1:length(variance)
    for j=1:REP
        delta = variance(k).*randn(1000,1) + mu;
        Normv(j)=norm(delta,2);
    end
    B=Normv(Normv<=0.5);
    Prob(aux)=length(B)/length(Normv);
    aux=aux+1;
    end
    
    plot(variance,Prob)
    xlabel('Variance')
    ylabel('Probability')
    

    Here is the generated plot:

    Plot Probability(Variance)

    The alpha (variance) you are trying to find is 0.01537. The higher the REP the higher is the precision of your alpha.