RAM issues in QuantFin, Sample based Approximation in CVXR

I have recently come across the newly uploaded slides by Boyd et. al. (see Convex Optimization in Quantitative Finance), and wanted to replicate the example for sample based CRRA maximization in R, since I mainly use that. Unfortunately, I am getting an Error message because it attempts to allocate a 298 GB vector somewhere…

The mad thing is that the script ran fine for my brother who uses Linux (and does not have 300GB Ram either).

Below is a minimal working example. If anyone has any advice on things I can try, or can tell me what caused this problem, I would be thankful.

################################################################################

library(CVXR)

################################################################################

Calculate Return Matrix

StartValue ← 100

N ← 100000

Returns ← rnorm(N, .07, .2)

#That is annual mean 0.07 and annual vol 0.2

Riskfree ← 1.02

TerminalValues ← StartValue * (Returns +1)

#Just use ATM options

CallPayoff ← ifelse(TerminalValues-100 >=0, TerminalValues-100, 0)

sum(CallPayoff == 0)

PutPayoff ← ifelse(!(TerminalValues-100 >=0), 100 - TerminalValues, 0)

sum(PutPayoff == 0)

Call_Return ← CallPayoff/8.92
Put_Return ← PutPayoff/6.94

Return_Matrix ← cbind(Bond_Return = rep(Riskfree,N),
Stock_Return =Returns + 1,
Call_Return,
Put_Return)

################################################################################

“Calculate” Probabilities

probabilities ← matrix(rep(1/N, N))

################################################################################

Problem formulation

U ← function(x, rho = 3){

if(rho == 1){
y ← log(x)
}else{
y ← (x^(1-rho))/(1-rho)
}
return(y)
}

w ← Variable(4)

const ← list(sum(w) == 1, w >= 0)

obj ← Maximize(t(probabilities) %% U(Return_Matrix %% w, rho = 3))

prob ← Problem(obj, const)

result ← solve(prob)

Despite the URL for this forum being cvxr, this forum is for CVX, which runs under MATLAB.

For support for CVXR, which runs under R, see Frequently Asked Questions — CVXR .

1 Like