Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Is it normal that interp2 changes variance?

Subject: Is it normal that interp2 changes variance?

From: Obaid Mushtaq

Date: 2 Aug, 2010 18:54:24

Message: 1 of 12

Hi all,

I have this simple question. If the answer is yes, please tell me if it is possible to preserve the variance of the interpolated data. You can take me as a noobie in Statistics.

Sample code follows:

xMin=0; xMax=1; yMin=0; yMax=1;

d=0.01;
x=xMin:d:xMax;
y=yMin:d:yMax;
[X,Y]=meshgrid(x,y);
[r,c]=size(X);
Z = 8*randn(r,c)+10;
mean(reshape(Z,1,prod(size(Z))))
var(reshape(Z,1,prod(size(Z))))

res=0.001;
xi=0:res:1;
yi=0:res:1;
[Xi,Yi] = meshgrid(xi,yi);
Zi = interp2(X,Y,Z,Xi,Yi);
[ri,ci]=size(Xi);
mean(reshape(Zi,1,prod(size(Zi))))
var(reshape(Zi,1,prod(size(Zi))))

I would appreciate any comments.

BR,

Obaid

Subject: Is it normal that interp2 changes variance?

From: Tom Lane

Date: 2 Aug, 2010 21:53:57

Message: 2 of 12

> I have this simple question. If the answer is yes, please tell me if it is
> possible to preserve the variance of the interpolated data. You can take
> me as a noobie in Statistics.

>> x = 1:2;
>> y = 1:2;
>> var(y)
ans =
    0.5000
>> xi = [1 1.5 2];
>> yi = interp1(x,y,xi)
yi =
    1.0000 1.5000 2.0000
>> var(yi)
ans =
    0.2500

This one-dimensional example shows that it's certainly possible that the
interpolation results will have a lower variance than the original data. I'm
not sure what you want to do about that. You could add noise to the output
from interp1/2, but then the results will no longer interpolate the original
data. Maybe you can explain what you're trying to accomplish.

-- Tom

Subject: Is it normal that interp2 changes variance?

From: John D'Errico

Date: 2 Aug, 2010 22:16:04

Message: 3 of 12

"Obaid Mushtaq" <obaidmushtaq@yahoo.com> wrote in message <i37490$ic7$1@fred.mathworks.com>...
> Hi all,
>
> I have this simple question. If the answer is yes, please tell me if it is possible to preserve the variance of the interpolated data. You can take me as a noobie in Statistics.
>

Yes, there is NO presumption at all that an interpolation
will preserve the variance of an array. Therefore it will
do so.

I have no idea why you wish a variance preserving
interpolant, but I would suggest that no tool does
what you want. People always want things they can't
have though.

John

Subject: Is it normal that interp2 changes variance?

From: Obaid Mushtaq

Date: 3 Aug, 2010 13:10:23

Message: 4 of 12

Hi,

Thanks for your response. Actually I get random shadowing values at a pre-defined 2D grid and these values have a particular variance as they are normally distributed.

After that I want to interpolate the values over a finer grid but the variance of the interpolated version changes as I showed you in case of ML functions.

See [1] p.p 44. They say that the linear interpolation formula guarantees the same variance. I was confused why ML's function is not preserving.

BR,

Obaid

[1] http://www.ieee802.org/16/tgm/docs/80216m-08_004r5.zip

"Tom Lane" <tlaneATmathworksDOTcom@nospam.com> wrote in message <i37epl$skj$1@fred.mathworks.com>...
> > I have this simple question. If the answer is yes, please tell me if it is
> > possible to preserve the variance of the interpolated data. You can take
> > me as a noobie in Statistics.
>
> >> x = 1:2;
> >> y = 1:2;
> >> var(y)
> ans =
> 0.5000
> >> xi = [1 1.5 2];
> >> yi = interp1(x,y,xi)
> yi =
> 1.0000 1.5000 2.0000
> >> var(yi)
> ans =
> 0.2500
>
> This one-dimensional example shows that it's certainly possible that the
> interpolation results will have a lower variance than the original data. I'm
> not sure what you want to do about that. You could add noise to the output
> from interp1/2, but then the results will no longer interpolate the original
> data. Maybe you can explain what you're trying to accomplish.
>
> -- Tom
>

Subject: Is it normal that interp2 changes variance?

From: us

Date: 3 Aug, 2010 13:27:04

Message: 5 of 12

"Obaid Mushtaq" <obaidmushtaq@yahoo.com> wrote in message <i394fv$8kd$1@fred.mathworks.com>...
> Hi,
>
> Thanks for your response. Actually I get random shadowing values at a pre-defined 2D grid and these values have a particular variance as they are normally distributed.
>
> After that I want to interpolate the values over a finer grid but the variance of the interpolated version changes as I showed you in case of ML functions.
>
> See [1] p.p 44. They say that the linear interpolation formula guarantees the same variance. I was confused why ML's function is not preserving.
>
> BR,
>
> Obaid
>
> [1] http://www.ieee802.org/16/tgm/docs/80216m-08_004r5.zip

well...
carefully(!) read the pages 44 and 45, again - and carefully(!) listen to what they really say...

us

Subject: Is it normal that interp2 changes variance?

From: John D'Errico

Date: 3 Aug, 2010 13:35:04

Message: 6 of 12

"Obaid Mushtaq" <obaidmushtaq@yahoo.com> wrote in message <i394fv$8kd$1@fred.mathworks.com>...
> Hi,
>
> Thanks for your response. Actually I get random shadowing values at a pre-defined 2D grid and these values have a particular variance as they are normally distributed.
>
> After that I want to interpolate the values over a finer grid but the variance of the interpolated version changes as I showed you in case of ML functions.
>
> See [1] p.p 44. They say that the linear interpolation formula guarantees the same variance. I was confused why ML's function is not preserving.
>

It matters not that they say this. They are wrong.

A linear interpolant is simply NOT a variance
preserving operation on a signal. For example,

n = 3;
x = linspace(0,1,n);
y = rand(size(x));

N = 100;
xN = linspace(0,1,N);
yN = interp1(x,y,xN,'linear');

var(y)
ans =
         0.137117842952513

var(yN)
ans =
        0.0404878603151538

So despite some statement that it is variance
preserving because they say it is, surely you
can believe that someone managed to publish
a paper with an incorrect statement in it?

John

Subject: Is it normal that interp2 changes variance?

From: us

Date: 3 Aug, 2010 13:52:04

Message: 7 of 12

"John D'Errico"
> > See [1] p.p 44. They say that the linear interpolation formula guarantees the same variance. I was confused why ML's function is not preserving.
> >
>
> It matters not that they say this. They are wrong.

correct - but: it's NOT what they say; it's what the OP thinks they are saying...

us

Subject: Is it normal that interp2 changes variance?

From: Matt J

Date: 3 Aug, 2010 15:46:04

Message: 8 of 12

"Obaid Mushtaq" <obaidmushtaq@yahoo.com> wrote in message <i37490$ic7$1@fred.mathworks.com>...
> Hi all,
>
> I have this simple question. If the answer is yes, please tell me if it is possible to preserve the variance of the interpolated data. You can take me as a noobie in Statistics.
==================

As the others have mentioned, interpolation will change the variance of data. To see a little bit how to analyze this, recall that interpolation can be expressed as a matrix/vector multiplication y=T*z where the matrix T represents the operation of interpolation of the 1D vector z. Also, if Zcov is the covariance matrix of z, then the covariance matrix of y is

Ycov=T*Zcov*T.'

If you have uniformly white noise, with variance Zvar=64, as in your example, the above leads to the following expression for diag(Ycov), the variances of Y,

diag(Ycov)=sum( T.^2, 2) *Zvar

So, unless sum( T.^2, 2) is uniformly 1, which it will not generally be, you will not preserve the variance.

However, if you wish, you could use the above expressions to compute the variance map diag(Ycov). You could then post-scale your results to correct the variance, restoring them to their original values.

The code below shows how you can compute the VarianceMap for a 2D image using my interpMatrix and KronProd tools:

http://www.mathworks.com/matlabcentral/fileexchange/26292-regular-control-point-interpolation-matrix-with-boundary-conditions

http://www.mathworks.com/matlabcentral/fileexchange/25969-efficient-object-oriented-kronecker-product-manipulation


sampfactor=.1;
tri=@(x) (1-abs(x)).*(abs(x)<=1);
kernel=tri(-1:sampfactor:1);
[~,origin]=max(kernel);

T=interpMatrix(kernel,origin,r,1/sampfactor); %Factor of 10 2D interpolation as matrix

Timg=KronProd({T},[1,1]); %2D version as KronProd Object

Vars = full(sum( Timg.^2 ,2)*var(Z(:))); %variances of interpolated result

VarianceMap=reshape(Vars,sqrt(numel(Vars)),[]);

Subject: Is it normal that interp2 changes variance?

From: Obaid Mushtaq

Date: 3 Aug, 2010 22:42:48

Message: 9 of 12

Thanks to all for your comments and OK I agree that variance doesn't need to be preserved but I would appreciate if someone could explain me why in the following code the variance is preserved. Here I am trying to interpolate using the linear interpolation formula given in the document I mentioned before.

@US: If you think I misunderstood something in the document, please let me know. I would appreciate.

BR,

Obaid

d=20; sigma=10;
xMin=300; xMax=900; yMin=200; yMax=600;
xmin=xMin-d; xmax=xMax+d; ymin=yMin-d; ymax=yMax+d;

% Coarse grid to be interpolated
xg=xmin:d:xmax; yg=ymin:d:ymax;
[Xg,Yg]=meshgrid(xg,yg);
[rg,cg]=size(Xg);
Zg=sqrt(sigma)*randn(rg,cg);
fprintf('Variance of original data %f\n',var(reshape(Zg,1,numel(Zg))))

% Finer grid
res=d/50;
xi=xMin:res:xMax; yi=yMin:res:yMax;
[Xi,Yi]=meshgrid(xi,yi);
[ri,ci]=size(Xi);

% Matlab Interpolation
Zm=interp2(Xg,Yg,Zg,Xi,Yi);
figure
colormap(jet)
imagesc(Zm)
set(gca,'YDir','normal')
colorbar

fprintf('Variance of ML interpolated data %f\n',var(reshape(Zm,1,numel(Zm))))

% Interpolation using IEEE doc
Zf=zeros(rg,cg);
l=1; m=1;
for ii=rg:-1:1
    for jj=1:cg
        Zf(jj,ii)=Zg(l,m);
        m=m+1;
    end
    l=l+1;
    m=1;
end

S=zeros(4);
Zi=zeros(ri,ci);
for i=1:ri
    for j=1:ci
        idx=fix((Xi(i,j)-xmin)/d);
        idy=fix((Yi(i,j)-ymin)/d);
        q=idx+1;
        p=idy+1;
        S(1)=Zf(q,p+1);
        S(2)=Zf(q+1,p+1);
        S(4)=Zf(q,p);
        S(3)=Zf(q+1,p);
        xpos=rem((Xi(i,j)-xmin),d);
        ypos=rem((Yi(i,j)-ymin),d);
        Zi(i,j) = sqrt(1-xpos/d)*(S(1)*sqrt(ypos/d) + ...
                                  S(4)*sqrt(1-ypos/d)) + ...
                                 (S(2)*sqrt(ypos/d) + ...
                                  S(3)*sqrt(1-ypos/d) )*sqrt(xpos/d);
    end
end

fprintf('Variance interpolated data %f\n',var(reshape(Zi,1,numel(Zi))))

% Flip for displaying purpose
Zii=zeros(ci,ri);
l=1; m=1;
for ii=ri:-1:1
    for jj=1:ci
        Zii(jj,ii)=Zi(l,m);
        m=m+1;
    end
    l=l+1;
    m=1;
end

figure
colormap(jet)
imagesc(Zii')
set(gca,'YDir','normal')
colorbar

Subject: Is it normal that interp2 changes variance?

From: Matt J

Date: 4 Aug, 2010 02:10:10

Message: 10 of 12

"Obaid Mushtaq" <obaidmushtaq@yahoo.com> wrote in message <i3a618$22d$1@fred.mathworks.com>...
> Thanks to all for your comments and OK I agree that variance doesn't need to be preserved but I would appreciate if someone could explain me why in the following code the variance is preserved. Here I am trying to interpolate using the linear interpolation formula given in the document I mentioned before.
==================

The interpolation formula in the IEEE doc is not remotely linear. In particular, if you rerun your code on a uniform image

Zg=ones(rg,cg);

a linear interpolation should give you a nice uniform image, as interp2 in fact does. However, you will see that the IEEE doc interpolation scheme gives a horribly artifacted, non-uniform result.

Admittedly, this nonlinear interpolation method does seem to have an interesting variance preservation property, but it sacrifices other commonly desired properties of interpolators, like uniformity-preservation, to get it.




> % Interpolation using IEEE doc
> Zf=zeros(rg,cg);
> l=1; m=1;
> for ii=rg:-1:1
> for jj=1:cg
> Zf(jj,ii)=Zg(l,m);
> m=m+1;
> end
> l=l+1;
> m=1;
> end
===============

Incidentally, this complicated for-loop looks like it can be done in one line as

Zf=fliplr(Zg.');

Subject: Is it normal that interp2 changes variance?

From: Obaid Mushtaq

Date: 5 Aug, 2010 11:00:22

Message: 11 of 12

Hi,

Thanks. I still think that the formula resembles to the linear one except for the scaling with square roots. The looped version was on purpose as I had to do this in C++ later on. I didn't know how to use 2 variables in a for loop in ML so I was doing something like that.

BR,

Obaid

Subject: Is it normal that interp2 changes variance?

From: Matt J

Date: 5 Aug, 2010 14:46:04

Message: 12 of 12

"Obaid Mushtaq" <obaidmushtaq@yahoo.com> wrote in message <i3e5k6$ju9$1@fred.mathworks.com>...

> Thanks. I still think that the formula resembles to the linear one except for the scaling with square roots.
=======

Yes, and it's pretty clear what motivated that. The formula we had from before

Ycov=T*Zcov*T.'

means that the variance vectors Var_Y=diag(Ycov)
and Var_Z=diag(Zcov) are specifically related by

Var_Y=T.^2 *Var_Z

So, if we select T=sqrt(S) where S is linear interpolation than

Var_Y=S*Var_Z

Meaning that interpolating the signal with sqrt(S) is the same as linearly interpolating
the variances Var_Z. Hence, if Var_Z is a constant Zvar than likewise Var_Y=Zvar and you've preserved variance.

Clearly, though, you can do this with any uniformity-preserving interpolator (e.g. B-splines)

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us