简体   繁体   English

在Matlab中将矩阵运算转换为R代码

[英]Converting matrix operations in Matlab to R code

I'm trying to convert Matlab code to R. I'm not familiar with Matlab matrix operations, and it appears the results from my R code do not match the results from Matlab, so any help would be greatly appreciated. 我正在尝试将Matlab代码转换为R。我不熟悉Matlab矩阵运算,并且看来我的R代码的结果与Matlab的结果不匹配,因此,我们将不胜感激。 The Matlab code I'm trying to convert is below (from this website ): 我要转换的Matlab代码如下(来自此网站 ):

% Mean Variance Optimizer

% S is matrix of security covariances
S = [185 86.5 80 20; 86.5 196 76 13.5; 80 76 411 -19; 20 13.5 -19 25]

% Vector of security expected returns
zbar = [14; 12; 15; 7]

% Unity vector..must have same length as zbar
unity = ones(length(zbar),1)

% Vector of security standard deviations
stdevs = sqrt(diag(S))

% Calculate Efficient Frontier
A = unity'*S^-1*unity
B = unity'*S^-1*zbar
C = zbar'*S^-1*zbar
D = A*C-B^2

% Efficient Frontier
mu = (1:300)/10;

% Plot Efficient Frontier
minvar = ((A*mu.^2)-2*B*mu+C)/D;
minstd = sqrt(minvar);

plot(minstd,mu,stdevs,zbar,'*')
title('Efficient Frontier with Individual Securities','fontsize',18)
ylabel('Expected Return (%)','fontsize',18)
xlabel('Standard Deviation (%)','fontsize',18)

Here is my attempt in R: 这是我在R中的尝试:

# S is matrix of security covariances
S <- matrix(c(185, 86.5, 80, 20, 86.5, 196, 76, 13.5, 80, 76, 411, -19, 20, 13.5, -19, 25), nrow=4, ncol=4, byrow=TRUE)

# Vector of security expected returns
zbar = c(14, 12, 15, 7)

# Unity vector..must have same length as zbar
unity <- rep(1, length(zbar))

# Vector of security standard deviations
stdevs <- sqrt(diag(S))

# Calculate Efficient Frontier
A <- unity*S^-1*unity
B <- unity*S^-1*zbar
C <- zbar*S^-1*zbar
D <- A*C-B^2

# Efficient Frontier
mu = (1:300)/10

# Plot Efficient Frontier
minvar = ((A*mu^2)-2*B*mu+C)/D
minstd = sqrt(minvar)

It appears that unity*S in Matlab is equivalent to colSums(S) in R. But I haven't been able to figure out how to calculate the equivalent of S^-1*unity in R. If I type this Matlab code in R ( S^-1*unity ), it calculates without error, but it gives a different answer. 看来Matlab中的unity*S等效于R中的colSums(S) 。但是我还无法弄清楚如何计算R中S^-1*unity的等效值。如果我在R( S^-1*unity ),它的计算没有错误,但是给出了不同的答案。 Because I don't understand the underlying Matlab calculation, I'm not sure how to translate it to R. 因为我不了解底层的Matlab计算,所以不确定如何将其转换为R。

I used to do matlab -> R conversions quite a bit a few years ago. 几年前我曾经做过matlab-> R转换。

My general suggestion is to open 2 terminals side by side and try to do everything going line-by-line. 我的一般建议是并排打开2个终端,并尝试逐行进行所有操作。 Then after each line you should check if what you got in MATLAB and R are equivalent. 然后,在每一行之后,您应该检查在MATLAB和R中获得的值是否相等。

This document should be handy: http://mathesaurus.sourceforge.net/octave-r.html 该文档应该方便: http : //mathesaurus.sourceforge.net/octave-r.html

In your case these appear to be the commands that you should have in mind: 在您的情况下,这些似乎是您应该记住的命令:

Matrix multiplication: 矩阵乘法:

Matlab: A*B
R: A %*% B

Transpose: 移调:

Matlab: A'
R: t(A)

Matrix inverse: 矩阵逆:

Matlab: inv(A) or A^-1
R: solve(A)

Don't try to convert everything at once because you will run into trouble. 不要尝试一次转换所有内容,否则会遇到麻烦。 When the results won't match you will not be able to tell where the error is. 当结果不匹配时,您将无法分辨错误在哪里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM