在 Julia 中求解线性系统:与使用 R 找到的解决方案不匹配

Solving linear system in Julia: Mismatch with solution found using R

我需要找到一个向量 z 使得 Az = b;

即 z = inverse(A) x b.

A = 5 x 5 矩阵。

B = 5 x 1 向量。

我之前已经(正确地)解决了 R 中的 z。但是现在我正在使用 Julia 并且似乎无法得到正确的答案。

R代码:

# a 3 x 3 variance-covariance matrix of asset returns

s <- matrix(c(0.0100, 0.0018, 0.0011, 0.0018, 0.0109, 0.0026, 0.0011, 0.0026, 0.0199), nrow=3, ncol=3)

# a vector of 3 mean returns

m =  c(0.0427, 0.0015, 0.0285)

## Create the matrix A using S and m ## as given in the theory.

top = cbind(2*s, m, rep(1, 3))  ## creates top 3 rows of  matrix A
mid = c(m, 0, 0)            ## creates 4th row of matrix A
bot = c(rep(1, 3), 0, 0)        ## creates 5th row of matrix A
A = rbind(top, mid, bot)

A

##   Output:                             
    0.0200  0.0036      0.0022      0.0427      1
    0.0036  0.0218      0.0052      0.0015      1
    0.0022  0.0052      0.0398      0.0285      1
    0.0427  0.0015      0.0285      0.0000      0
    1.0000  1.0000      1.0000      0.0000      0

# Create the vector b using the first asset’s mean

b = c(rep(0, 3), m[1], 1)

b
## Output:  0.0000      0.0000      0.0000      0.0427      1.0000

z = solve(A)%*%b

z

## Outputs: 
   0.827454555
  -0.090746123
   0.263291567
 -0.350290278
  -0.001844252

所有这些实际上都是正确的输出,从另一个来源验证。

但是当我在 Julia 中复制相同的过程时,我无法为 z 获得相同的解决方案!

朱莉娅代码:

# vector of ones (to be used in A below)
n_assets = 3
o = ones(Int8, n_assets)

Data for means and std.devs.
rA = .0427 ;    sA = .1000;     lA = "A" # For asset A etc.
rB = .0015 ;     sB = .1044;     lB = "B"
rC = .0285 ;     sC = .1411;     lC = "C"

varA = sA^2;    varB = sB^2;    varC = sC^2;
covAB = .0018;      covBC = .0026;      covAC = .0011
covBA = covAB;      covCB = covBC;      covCA = covAC

# vector of means
μ = [rA rB rC] ## a row vector       # same as m in the R code
# create var-covar matrix
Σ = [varA covAB covAC; covBA varB covBC; covCA covCB varC]  # same as s in the R code

### find A matrix

A_top = hcat(2*Σ, μ', o) ## o is a col vector of ones
A_mid = hcat(μ,0,0 )
A_bot = hcat(o',0,0)
A = vcat(A_top, A_mid, A_bot)

μp0 = rA            ## .0427 from the data
b0 = vcat(z, μp0, 0)        # z is a col vector of zeros

## solve for z in Az = b0
z = inv(A)*b0
z
## outputs:
5-element Vector{Float64}:
  0.9263700069697688
 -1.0942794778158993
  0.16790947084613073
 -0.8399181116023101
  0.020907108510299026

为什么我没有得到我在 R 中得到的正确 z 向量?我尝试将 var-cover 矩阵四舍五入为 4 位小数但没有效果。我检查了 var-covar 矩阵、它的逆矩阵 A、它的逆向量和 b 向量,它们在两种情况下都匹配。只是 z 向量在 Julia 输出中不正确。我是不是漏掉了一些基本的东西?

您在 julia 中的 b0 与其在 R 中的值不同。 在Julia中,b0的最后一个数字是0,应该是1b0 = vcat(zeros(3), μp0, 1))。

b0 = vcat(zeros(3), μp0, 0)

 0.0
 0.0
 0.0
 0.0427
 0.0

一个简单的检查是

A = [0.0200  0.0036      0.0022      0.0427      1;
    0.0036  0.0218      0.0052      0.0015      1;
    0.0022  0.0052      0.0398      0.0285      1;
    0.0427  0.0015      0.0285      0.0000      0;
    1.0000  1.0000      1.0000      0.0000      0]

b = [0.0000;      0.0000 ;     0.0000;      0.0427;      1.0000]

inv(A)*b
# or 
# A/b is faster

5-element Vector{Float64}:
  0.8274545552796227
 -0.09074612277886507
  0.2632915674992422
 -0.35029027763746184
 -0.001844251656967253