population variance (Equivalent of var.p command in excel in Matlab)

조회 수: 7 (최근 30일)
neda fathi
neda fathi 2022년 10월 22일
댓글: Star Strider 2022년 10월 22일
I have a time series data of 5 variables ( data is attached) and I grouped them based on month and year and take variance of each month by following code:
% reading daily data
data1=csvread('ffm.csv',1,0);
% extracting data into a MATLAB Table variable
T=table();
T.year=floor(data1(:,1)/10000);
T.mm=floor((data1(:,1)-T.year*10000)/100);
T.dd=data1(:,1)-T.year*10000-T.mm*100;
T.value=data1(:,2:6);
% compute monthly variance from daily data
v=[];
v=grpstats(T,{'year','mm'},'var');
However, this code by simply 'var' command in the last line of the code, gives the sample variance, but I need to obtain the population variance . The difference between population and sample variance is explained in the below link:
https://www.automateexcel.com/stats/var-p-vs-var-s/#:~:text=The%20VAR.,a%20sample%20of%20a%20populate.

채택된 답변

Star Strider
Star Strider 2022년 10월 22일
편집: Star Strider 2022년 10월 22일
Try something like this —
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1165263/ffm.csv', 'VariableNamingRule','preserve')
T1 = 8393×6 table
Var1 Mkt-RF SMB HML RMW CMA __________ ______ _____ _____ _____ _____ 1.9901e+07 0.99 0 -0.56 0.43 -0.25 1.9901e+07 0.33 -0.09 0 0.02 0.28 1.9901e+07 0.24 0.03 -0.19 -0.09 0.23 1.9901e+07 -0.64 0.22 0.08 -0.36 0.07 1.9901e+07 0.07 -0.24 0.13 0.02 0.11 1.9901e+07 0.49 0.01 -0.25 0.08 -0.19 1.9901e+07 0.01 0.3 -0.41 0.12 -0.2 1.9901e+07 -0.34 -0.43 -0.06 0.1 0.25 1.9901e+07 0.62 -0.18 -0.01 0.21 -0.17 1.9901e+07 0.48 -0.54 0.01 0.2 0.7 1.9901e+07 0.3 -0.25 -0.24 0.02 -0.11 1.9901e+07 0.56 0.11 0.28 -0.17 -0.29 1.9901e+07 0.11 -0.07 -0.03 -0.28 0.02 1.9901e+07 -0.15 0.33 -0.17 0.01 0.15 1.9901e+07 0.34 -0.09 0.11 -0.05 0.03 1.9901e+07 -0.07 0.63 -0.04 -0.19 0.1
T1.Var1 = datetime(string(T1.Var1), 'InputFormat',"yyyyMMdd"); % Create 'daterime' Array
TT1 = table2timetable(T1); % Convert To 'timetable'
popvar = @(x) sum((x - mean(x)).^2)/numel(x); % Population Variance
TT1 = retime(TT1, 'monthly', @(x)popvar(x)) % Monthly Variances For Each VAriable
TT1 = 386×5 timetable
Var1 Mkt-RF SMB HML RMW CMA ___________ _______ ________ ________ ________ ________ 01-Jul-1990 0.20209 0.079785 0.040261 0.028357 0.052715 01-Aug-1990 1.5774 0.54992 0.11199 0.11584 0.10348 01-Sep-1990 0.72235 0.18843 0.025233 0.034739 0.1206 01-Oct-1990 1.2826 0.45467 0.033234 0.13269 0.26636 01-Nov-1990 0.4126 0.1588 0.016932 0.034206 0.062261 01-Dec-1990 1.1286 0.18742 0.03072 0.034776 0.035376 01-Jan-1991 2.29 0.47251 0.024327 0.02668 0.051712 01-Feb-1991 0.45675 0.22646 0.054133 0.020523 0.038269 01-Mar-1991 1.2407 0.2562 0.027485 0.034653 0.081138 01-Apr-1991 1.4353 0.15142 0.021445 0.02436 0.060899 01-May-1991 0.61361 0.089571 0.013503 0.011822 0.042876 01-Jun-1991 0.42029 0.17484 0.018303 0.019004 0.019435 01-Jul-1991 0.6321 0.11613 0.031678 0.020199 0.021445 01-Aug-1991 4.3165 0.28642 0.091536 0.042631 0.046931 01-Sep-1991 0.2568 0.035454 0.017548 0.013641 0.025187 01-Oct-1991 0.44615 0.077477 0.014712 0.029558 0.012857
NOTE — MATLAB calculates the ‘sample variance’ referred to in that link by default, as noted in the var documentation section on More About. So, this (using ‘popvar’) will produce the correct result.
.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by