How to calculate mean, standard deviation?

조회 수: 3 (최근 30일)
JFz
JFz 2015년 7월 10일
댓글: JFz 2015년 7월 10일
Hi,
I have a table myTable that has a column called Prices (column 3). I want to calculate the mean, and std, of the log return. The code is not very nice. How to make it better? Here is my code:
myarray = myTable(:, 3)
for i = 2:size(myTable, 1)
mylog = log10(myTable{i, 3)/myTable(i-1, 3));
myarray{i-1} = mylog
end
mymean = mean(myarray)
mystd = std(myarray)
Actually, I have a set of such tables, and I will create a set of means and stds and put this set into another table. I am wondering how to do it nicely since I don't want to make many for loops. And since the amount of data is huge, I want to make the speed fast in computing.
Thank you!
Jen

채택된 답변

Brendan Hamm
Brendan Hamm 2015년 7월 10일
You can index a table by the VariableNames. Furthermore we can use the properties of logs: log(a/b) = log(a) - log(b). This will allow us to utilize the diff function on the vector which will take the difference between an observation and the previous observation:
logP = log(myTable.Prices); % Takes log of all elements in the column 'Prices'
logR = diff(logP);
meanR = mean(logR);
stdR = std(logR);
Note I use the natural logarithm ( log() ) rather than base 10 ( log10() ) as this is the standard way to compute log-returns from prices. You could also combine steps 1 and 2, I just wanted to be explicit.
logR = diff(log(myTable.Prices));
  댓글 수: 1
JFz
JFz 2015년 7월 10일
Thank you! This is exactly what I need. Thank you.
Jen

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by