HCAI
HCAI

Reputation: 2263

Efficient way for variance of a continuous variable A at a point i? Matlab

I have a continuous variable A=gamrnd(5,0.4,1000,28) and an output variable Y=lognrnd(7,1.9,1000,28)

 A
    7.6472    3.4284    6.3352    8.0480    8.1021
   12.3371    5.1611    6.3986    9.3687    9.5652
    8.7700    5.2980    6.0307    2.8651   12.6011
   12.2042    4.5636    6.0570    7.1348    8.6586
    7.8960    5.5213    3.7105    6.4875    7.4891

 Y
    1.9733   14.0951   14.0951   14.0951   14.0951
    9.4284   11.7573   15.6730   25.4495   24.6680
    3.4724    4.4953    7.1237    9.4191   18.4504
    8.9548    8.9548    8.9548    8.9548    8.9548
    1.4834    2.5393    2.5393    2.5393    2.5393

I'd like to calculate the variance of Y for a specific value of A (or width) of that red box? let's say I split the domain of A into 20 red boxes, I would like to calculate the variance of Y for each box. That is to say:

$Var(Y|A=a_i)$

Any thought how I'd do that?

My thoughts so far:

[i j]=find(9.5<=A & A<=10.5)
sig=var(reshape((Y([i j])),length(i)*2,1))

but this is correct but rather ad-hoc. Let's say I had a hundred divisions in A. Is it possible to use something more efficient? enter image description here

Upvotes: 1

Views: 57

Answers (1)

Jonas
Jonas

Reputation: 74940

accumarray to the rescue!

%# split A into 100 chunks
nChunks = 100;

Aidx = round(A/maxA*(nChunks-1))+1;

%# get the number of data points just in case
nDataPoints = histc(Aidx,1:nChunks);

%# calculate the variance
varA = accumarray(Aidx,Y,[100,1],@var,NaN);

Upvotes: 1

Related Questions