How do I create a vectorized version of randsample in Matlab
Asked Answered
L

0

0

I have a support (supp_epsilon) and a probability mass function (pr_mass_epsilon) in Matlab, constructed as follows.

    supp_epsilon=[0.005 0.01;
                  0.01  0.015;
                  0.015 0.02;
                  0.02  0.025]; 

    suppsize_epsilon=size(supp_epsilon,1);

    pr_mass_epsilon=zeros(suppsize_epsilon,1);

    mu_epsilon=[0; 0];
    sigma_epsilon=[1 0.5; 0.5 1];
    
    pr_mass_epsilon=zeros(suppsize_epsilon,1);

    for j=1:suppsize_epsilon
 pr_mass_epsilon(j)=mvnpdf(supp_epsilon(j,:),mu_epsilon.',sigma_epsilon)/sum(mvnpdf(supp_epsilon,mu_epsilon.',sigma_epsilon));
     end

Note: supp_epsilon is 4x2. Each 1x2 row of supp_epsilon is an element of the support of interest.

Question: I want to draw n=10 vectors of size 1x2 from pr_mass_epsilon. Each of these vectors has to be a row of supp_epsilon. For n very large the empirical frequency of the drawn vectors has to be close to pr_mass_epsilon.

How can I do this?

Note: This question addresses the scalar case where the answer suggests to use randsample.

Lagan answered 30/10, 2019 at 18:37 Comment(10)
just use randsample(whatever_it_is,2)Gidgetgie
It gives me error: epsilon=randsample(supp_epsilon, n, true, pr_mass_epsilon,2);Lagan
Error using randsample (line 74) POPULATION must be a vector.Lagan
You say you want n random vectors. What is n, and why is supp_epsilon a matrix and not a vector?Nutria
Thanks. supp_epsilon is now a matrix because it represents the support of a bivariate random vector. Each 1x2 row of supp_epsilon is a possible realisation of such bivariate random vector. pr_mass_epsilon is the associated probability mass function.Lagan
Is this a use-case for arrayfun?Skimp
Is your bi-variate (or univariate) support discrete or continuous? Your use of the term probability mass function suggests discrete; invoking mvnpdf suggests continuous.Skimp
@Skimp The support is discrete and equal to supp_epsilon. I construct the probability mass function by truncating and discretising a multivariate normal, but this is just one example.Lagan
@user3285148 Ok, thanks. So sum(pr_mass_epsilon) = 1 and it is indeed a PMF, not a PDF. The idea is to develop this for any given pr_mass_epsilon, is that right?Skimp
yes, that is correct.Lagan

© 2022 - 2024 — McMap. All rights reserved.