From: Mohammed Khalilia on
"Mohammed Khalilia" <mohammedsk(a)gmail.com> wrote in message <hf2fan$kv8$1(a)fred.mathworks.com>...
> Hi,
> I am trying to understand resampling with replacement. Basically, I have a dataset (2000 instances x 2 features) and a weight vector W (2000x1) and trying to produce a bootstrap replicate of the original data according to the probability W for every instance by resampling with replacement from the original dataset.
>
> I know MATLAB has bootstrap functions, but the closest I found is randsample(), which take as arguments the vector dataset and a weight vector, but I guess it does not work in matrices.
>
> How can I do that?
>
> Thanks,

Does that make sense to anyone? Or do I need to add additional details?
From: Peter Perkins on
Mohammed Khalilia wrote:

> I am trying to understand resampling with replacement. Basically, I have a dataset (2000 instances x 2 features) and a weight vector W (2000x1) and trying to produce a bootstrap replicate of the original data according to the probability W for every instance by resampling with replacement from the original dataset.
>
> I know MATLAB has bootstrap functions, but the closest I found is randsample(), which take as arguments the vector dataset and a weight vector, but I guess it does not work in matrices.

You haven't exactly defined what you mean by, "work in matrices", but it sounds like you want to use RANDSAMPLE to generate a vector of row indices, and then use that to index into your matrix of data, with a ':' for the column index.

Hope this helps.
From: Mohammed Khalilia on
Peter Perkins <Peter.Perkins(a)MathRemoveThisWorks.com> wrote in message <hf3a6t$laf$1(a)fred.mathworks.com>...
> Mohammed Khalilia wrote:
>
> > I am trying to understand resampling with replacement. Basically, I have a dataset (2000 instances x 2 features) and a weight vector W (2000x1) and trying to produce a bootstrap replicate of the original data according to the probability W for every instance by resampling with replacement from the original dataset.
> >
> > I know MATLAB has bootstrap functions, but the closest I found is randsample(), which take as arguments the vector dataset and a weight vector, but I guess it does not work in matrices.
>
> You haven't exactly defined what you mean by, "work in matrices", but it sounds like you want to use RANDSAMPLE to generate a vector of row indices, and then use that to index into your matrix of data, with a ':' for the column index.
>
> Hope this helps.

That is close to what I want, here is what I have so far:
a = [1 2;5 6;7 8;34 5];
w = [0.1250 0.1250; 0.1250 0.1250; 0.1250 0.1250; 0.1250 0.1250];

Since randsample takes only vectors for the data and the weight, I had to reshape a
a1 = reshape(a,1,8);
w1 = reshape(w,1,8);
y=randsample(a1,8,true,w1);

Then reshape y
reshape(y,4,2);

This does not really work because reshape will completely change the shape of the data after resampling.
From: Lorenzo Guerrasio on
"Mohammed Khalilia" <mohammedsk(a)gmail.com> wrote in message <hf3beo$575$1(a)fred.mathworks.com>...
> Peter Perkins <Peter.Perkins(a)MathRemoveThisWorks.com> wrote in message <hf3a6t$laf$1(a)fred.mathworks.com>...
> > Mohammed Khalilia wrote:
> >
> > > I am trying to understand resampling with replacement. Basically, I have a dataset (2000 instances x 2 features) and a weight vector W (2000x1) and trying to produce a bootstrap replicate of the original data according to the probability W for every instance by resampling with replacement from the original dataset.
> > >
> > > I know MATLAB has bootstrap functions, but the closest I found is randsample(), which take as arguments the vector dataset and a weight vector, but I guess it does not work in matrices.
> >
> > You haven't exactly defined what you mean by, "work in matrices", but it sounds like you want to use RANDSAMPLE to generate a vector of row indices, and then use that to index into your matrix of data, with a ':' for the column index.
> >
> > Hope this helps.
>
> That is close to what I want, here is what I have so far:
> a = [1 2;5 6;7 8;34 5];
> w = [0.1250 0.1250; 0.1250 0.1250; 0.1250 0.1250; 0.1250 0.1250];
>
> Since randsample takes only vectors for the data and the weight, I had to reshape a
> a1 = reshape(a,1,8);
> w1 = reshape(w,1,8);
> y=randsample(a1,8,true,w1);
>
> Then reshape y
> reshape(y,4,2);
>
> This does not really work because reshape will completely change the shape of the data after resampling.

a = [1 2;5 6;7 8;34 5];
w = [0.1250 0.1250; 0.1250 0.1250; 0.1250 0.1250; 0.1250 0.1250];
ind=1:lsize(a,1);
n_sample=100;
Rind=randsamp(ind,n_sample,true,w);
new_sample=a(Rind,:);
From: Lorenzo Guerrasio on
"Lorenzo Guerrasio" <lorenzo.guerrasio(a)email.it> wrote in message <hf3c57$l13$1(a)fred.mathworks.com>...
> "Mohammed Khalilia" <mohammedsk(a)gmail.com> wrote in message <hf3beo$575$1(a)fred.mathworks.com>...
> > Peter Perkins <Peter.Perkins(a)MathRemoveThisWorks.com> wrote in message <hf3a6t$laf$1(a)fred.mathworks.com>...
> > > Mohammed Khalilia wrote:
> > >
> > > > I am trying to understand resampling with replacement. Basically, I have a dataset (2000 instances x 2 features) and a weight vector W (2000x1) and trying to produce a bootstrap replicate of the original data according to the probability W for every instance by resampling with replacement from the original dataset.
> > > >
> > > > I know MATLAB has bootstrap functions, but the closest I found is randsample(), which take as arguments the vector dataset and a weight vector, but I guess it does not work in matrices.
> > >
> > > You haven't exactly defined what you mean by, "work in matrices", but it sounds like you want to use RANDSAMPLE to generate a vector of row indices, and then use that to index into your matrix of data, with a ':' for the column index.
> > >
> > > Hope this helps.
> >
> > That is close to what I want, here is what I have so far:
> > a = [1 2;5 6;7 8;34 5];
> > w = [0.1250 0.1250; 0.1250 0.1250; 0.1250 0.1250; 0.1250 0.1250];
> >
> > Since randsample takes only vectors for the data and the weight, I had to reshape a
> > a1 = reshape(a,1,8);
> > w1 = reshape(w,1,8);
> > y=randsample(a1,8,true,w1);
> >
> > Then reshape y
> > reshape(y,4,2);
> >
> > This does not really work because reshape will completely change the shape of the data after resampling.
>
> a = [1 2;5 6;7 8;34 5];
> w = [0.1250 0.1250; 0.1250 0.1250; 0.1250 0.1250; 0.1250 0.1250];
> ind=1:lsize(a,1);
> n_sample=100;
> Rind=randsamp(ind,n_sample,true,w);
> new_sample=a(Rind,:);

sorry,I didn't see that in the example you provide w is a matrix, while in the initial problem w was a vector.
The code I wrote before work if w is a vector, i.e. if you write
Rind=randsamp(ind,n_sample,true,w(:,1));

If, instead, the two columns of a have different probabilities you can split matrix a in two vector a1 and a2 and matrix w in two vector w1 and w2, than resample separately.