【问题标题】:Setting up an arbitrage strategy in R by solving system of inequalities subject to an equality constraint通过求解受等式约束的不等式系统在 R 中建立套利策略
【发布时间】:2020-09-17 23:10:52
【问题描述】:

我正在尝试构建套利投资组合 x 使得 Sx = 0 和 Ax>=0,其中 A 是 t=1 时的收益矩阵,S 是 t=0 时的价格。我无法手动完成,所以我尝试使用 R 中的 limSolvelpSolve 包中包含的函数,但没有成功,因为我继续获得零向量(我需要非平凡的解决方案)。我也不知道如何自己编码。任何有关如何进行的帮助或提示将不胜感激。谢谢!

    A = data.frame(
              cbind(
                 c(2,1,0),
                 c(1,1,1),
             c(0,1,2),
             c(3,2,1),
             c(1,1,0)
              )
             ) %>% as.matrix()
f.con = A
    
    S = data.frame(
              cbind(
             c(1,1,1,2,1/3)
              )
             ) %>% as.matrix()

f.obj = c(t(S))

# Set unequality signs
f.dir <- c(">",
           ">",
           ">")

# Set right hand side coefficients
f.rhs <- c(0,
           0,
           0)

# Final value 
lp("min", f.obj, f.con, f.dir, f.rhs)$solution

# Variables final values
lp("max", f.obj, f.con, f.dir, f.rhs)$solution

【问题讨论】:

    标签: r finance


    【解决方案1】:

    我们首先解释为什么问题中的代码得到了它的结果。

    请注意,存在 x >= 0 的隐式约束。

    关于引用等式约束的主题,问题中显示的代码中没有等式约束。

    关于最小值,在lp 参数中&gt; 表示&gt;= 很明显x=0 是可行的。鉴于目标向量的所有分量都是正的,它导致最小值为 0。来自?lp

    const.dir:给出方向的字符串向量 约束:每个值应该是“," 或 ">="。 (在每一对中,这两个值是相同的。)

    关于最大值,解没有上限约束,并且目标向量的分量都是正的,因此没有有限解。线性程序是否成功应始终在显示解决方案之前显示,如果不成功则不应显示解决方案,因为它没有意义。

    关于代码,cbind 已经生成了一个矩阵,因此将其转换为数据框然后再转换回矩阵是没有意义的。此外,目标可以表示为纯向量,并且约束的 rhs 可以写为标量,该标量将被回收到适当的长度。我们可以将问题中的代码等效地编写为:

    library(lpSolve)
    
    A <- cbind(2:0, 1, 0:2, 3:1, c(1, 1, 0))
    S <- c(1, 1, 1, 2, 1/3)
    
    res1 <- lp("min", S, A, ">=", 0)   
    res1
    ## Success: the objective function is 0 
    res1$solution
    ## [1] 0 0 0 0 0
    
    res2 <- lp("max", S, A, ">=", 0)
    res2
    ## Error: status 3 
    

    CVXR

    如下所示,使用 CVXR 更容易对此进行表述。

    找到满足 Ax >= 0 和 Sx == 0 的向量 x。(A 和 S 来自上方。)我们添加约束 -1

    library(CVXR)
    
    x <- Variable(5)
    objective <- Maximize(sum(x))  # arbitrary objective
    constraints <- list(A %*% x >= 0, sum(S*x) == 0, x >= -1, x <= 1)
    problem <- Problem(objective, constraints)
    soln <- solve(problem)
    

    给予:

    > soln$status
    [1] "optimal"
    
    > soln$value
    [1] 1.66666
    
    > soln$getValue(x)
               [,1]
    [1,]  0.8788689
    [2,]  0.4790521
    [3,]  0.3087133
    [4,] -0.9999857
    [5,]  1.0000113
    

    lpSolve 再次

    为此使用 lpSolve 改变变量

    x = 2*y-1
    

    或等效

    y = (x+1)/2
    

    转换

    Ax >= 0
    Sx == 0
    -1 <= x <= 1
    

    2Ay >= A1
    2Sy >= S'1
    0 <= y <= 1
    

    所以我们写:

    f.con <- rbind(2*A, 2*S, diag(5))
    f.dir <- c(rep(">=",3),  "=", rep("<=", 5))
    f.rhs <- c(rowSums(A), sum(S), rep(1, 5))
    
    res3 <- lp("max", rep(1, 5), f.con, f.dir, f.rhs)
    res3
    ## Success: the objective function is 3.333333 
    2*res3$solution-1
    ## [1]  1.000000e+00 -4.966028e-13  6.666667e-01 -1.000000e+00  1.000000e+00
    

    【讨论】:

      猜你喜欢
      • 2018-07-24
      • 1970-01-01
      • 1970-01-01
      • 2015-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-08
      相关资源
      最近更新 更多