SlowGuess/NeurIPS_2023_OCR huggingface dataset:
Philippe Chatigny*
Riskfuel
pc@riskfuel.com
Ivan Sergienko†
Beacon Platform
ivan.sergienko@beacon.io
Ryan Ferguson
rf@riskfuel.com
Jordan Weir
jw@riskfuel.com
Maxime Bergeron
mb@riskfuel.com
The efficient frontier (EF) is a fundamental resource allocation problem where one has to find an optimal portfolio maximizing a reward at a given level of risk. This optimal solution is traditionally found by solving a convex optimization problem. In this paper, we introduce NeuralEF: a fast neural approximation framework that robustly forecasts the result of the EF convex optimization problem with respect to heterogeneous linear constraints and variable number of optimization inputs. By reformulating an optimization problem as a sequence to sequence problem, we show that NeuralEF is a viable solution to accelerate large-scale simulation while handling discontinuous behavior.
Making the least risky decision to maximize a cumulative reward over time is a central problem in machine learning and at the core of resource allocation optimization problems [26]. In modern portfolio theory [16], this problem is commonly referred to as the efficient frontier (EF) [36]. It involves distributing resources among  $n$  risky assets to maximize return on investment while respecting various constraints. These constraints (e.g., maximum allocation allowable for an asset) are set to prevent aggressive or unrealistic allocations in practice. Finding the optimal solution to this optimization problem given the expected risk and return of each asset under a set of constraints can be done by solving a convex optimization problem of quadratic programs (QP) and second-order cone programs (SOCP). Although a single optimization problem is not time-consuming to solve, its current computational cost remains the most significant bottleneck when performing the simulations necessary for financial applications [22, 7].
Indeed, inputs to the EF problem such as future expectations of asset returns, co-variances, and even simulated client preferences on asset allocation are stochastic (c.f. Table 1). Thus, one needs to repeatedly solve the optimal allocation problem under a large number of different scenarios and Monte Carlo (MC) simulations [39] are commonly used to estimate the expected reward over time. Given an allocation function  $g$  and stochastic input  $Z$ , we need to compute  $\mathbb{E}(g(Z)) = \int g(z)f_Z(z)dz$  where  $f_{Z}$  is the density function of  $Z$ . Sampling the empiric mean of  $g(Z)$  approximates  $\mathbb{E}(g(Z))$  with a convergence rate of  $\mathcal{O}(1 / \sqrt{N})$  where  $N$  denotes
$^{\dagger}$ Contribution was made when working at Riskfuel
the number of samples. The computational cost of this simulation is therefore influenced by the size of  $N$  and the cost of computing  $g$ . In spite of the vast literature of variance reduction techniques [5, 17] and the use of low-discrepancy sampling methods [10, 21] that aim to reduce  $N$ , the results of the simulation will be misleading if the assumptions are not valid, leaving the cost of running  $g(z)$  as the principal bottleneck. This makes it practically impossible to run multiple MC simulations on different candidate assumptions in real time unless significant computational resources are available. Applications that heavily depend on MC simulations of the EF problem like basket option derivatives pricing face this bottleneck because their valuation depends on accurate estimates of the expected returns  $\mathbb{E}(R)$ , portfolio volatility  $\mathbb{E}(V)$  and/or allocations  $\mathbb{E}(X)$ . The roll-out of new regulatory frameworks for financial applications of the EF problem (e.g. portfolio management) requiring more rigorous testing of  $\mathbb{E}(g(Z))$  further increases the minimal acceptable  $N$ , exacerbating the need for speed [37, 3].
The core problem then becomes finding a way to reduce the computational cost of  $g$ , making it possible to run a large number of MC simulations in a few seconds [27, 25]. Accelerating the optimization on highly-parallel hardware like graphical processing units (GPUs) can accelerate some convex optimization problems [13, 44, 11, 12], but not enough to run MC simulation in real-time effectively. Instead of only exploiting hardware, others have proposed to approximate the optimization step using deep neural networks (DNNs) and infer the result directly [18, 28, 48, 19]. However, proposed approaches fall short since they fail to simultaneously satisfy the following key requirements: (1) they do not provide theoretical guarantees that their forecast is within the domain set by constraints, (2) they do not show that their results can be applied at large scale faster than the optimization itself, (3) they can't handle a variable amount of heterogeneous inequality constraints and optimization inputs, and, (4) they are not robust to discontinuous behavior of optimization problems.
Figure 1: Illustration of the EF problem. The optimal allocations are at the frontier of the plot where the expected returns is maximized for a level of volatility. The yellow dot denotes the best allocation for eq. 2 for  $\nu_{\mathrm{target}} = 0.06$ . The location of the yellow dot will vary under different conditions, which can be approximated under different scenarios  $(Z)$  using MC simulations.
This work addresses all the shortcoming of DNN-based approaches mentioned above. We propose NeuralEF, a DNN-based model to approximate the computation of the EF problem robustly. Our model is up to 615X times faster than the baseline convex optimization when used with hardware acceleration. It is able to handle a variable number of assets and inequality constraints with consistent accuracy. In particular, we introduce a dynamic greedy allocation module to respect the constraints robustly that is agnostic to the DNN architecture. The remainder of this paper is organized as follows. Section 2 describes the EF problem and presents related works on convex optimization acceleration. Section 3 introduces NeuralEF and its training methodology. Section 4 outlines our empirical evaluations on the EF optimization problem. Finally, section 5 presents our conclusions.
2.1 The Efficient Frontier
We seek an optimal allocation  $\pmb{x} = [x_{1},\dots x_{n}]$  over  $n$  risky assets given a soft volatility target  $\mathcal{V}_{\mathrm{target}}$ . This means a portfolio with higher achieved volatility than  $\mathcal{V}_{\mathrm{target}}$  is valid only if the resulting allocation has the minimum volatility that can be achieved on the feasible domain. Finding the optimal allocation maximizing portfolio return at a given volatility target is conditional on the assets' expected returns  $\pmb{R} = [r_1,\dots ,r_n]$ , volatilities  $\pmb{V} = [v_{1},\dots ,v_{n}]$ , and correlations  $\pmb{P} = [[\rho_{1,1},\dots ,\rho_{1,n}],\dots ,[\rho_{n,1},\dots \rho_{n,n}]]$ . Constraints that must be satisfied include upper and lower bounds on total allocation of risky assets  $\alpha_{\mathrm{MAX}}$ ,  $\alpha_{\mathrm{MIN}}$ ,
and a minimum and maximum asset allocation limit per asset  $\mathbf{X}_{\mathrm{MIN}} = [x_1^{\mathrm{MIN}}, \dots, x_n^{\mathrm{MIN}}]$ ,  $\mathbf{X}_{\mathrm{MAX}} = [x_1^{\mathrm{MAX}}, \dots, x_n^{\mathrm{MAX}}]$ . As is typical in finance, assets can belong to a set of  $m$  classes  $\mathbf{C} = [c_1, \dots, c_n]$ ;  $c_i \in [1, \dots, m]$ , and allocations are subject to a class maximum  $\zeta_{\mathrm{MAX}} = [\zeta_{c_1}, \dots, \zeta_{c_m}]$ ;  $\sum_{x \in c_j} x_i \leq \zeta_{c_j}$ . We denote the set of convex optimization inputs by  $\mathbf{Z}_{\mathrm{input}} = [R, V, P, X_{\mathrm{MAX}}, X_{\mathrm{MIN}}, C, \zeta_{\mathrm{MAX}}, \alpha_{\mathrm{MIN}}, \alpha_{\mathrm{MAX}}, \mathcal{V}_{\mathrm{target}}]$  and the resulting allocation by  $Z_{\mathrm{output}} = \operatorname{EF}(Z_{\mathrm{input}})$ .
This problem is illustrated in fig. 1 using random allocations where the EF is located at the left-most frontier of the plots. We can find this frontier along a range of volatility targets by solving a two-step conditional convex optimization problem where we first find the optimal weights that minimizes risk in a portfolio of  $n$  assets subject to linear weight constraints and then maximize the return if the risk of the portfolio is below  $\mathcal{V}_{\mathrm{target}}$ .
Finding this minimum variance portfolio is equivalent to solving a QP problem of the form
$$
\boldsymbol {\psi} := \text {m i n i m i z e} \frac {1}{2} \boldsymbol {x} ^ {\top} \boldsymbol {Q} \boldsymbol {x} \text {s u b j e c t t o} \boldsymbol {a} _ {i} ^ {\top} \leq \boldsymbol {b} _ {i} \forall i \in 1, \dots , w, \tag {1}
$$
where  $\pmb{x} \in \mathbb{R}^n$  is a column vector of weights representing the allocation,  $\pmb{Q}$  is the covariance matrix of the portfolio's assets,  $\pmb{a}_i$  is a row vector representing the  $i$ -th linear constraint (obtained from  $\alpha_{\mathrm{MIN}}, \alpha_{\mathrm{MAX}}, X_{\mathrm{MAX}}, X_{\mathrm{MIN}}, \zeta_{\mathrm{MAX}}$ ) and  $\pmb{b}_i$  is the maximum required value for the  $i$ -th constraint. The result of eq. 1 allows us to calculate the minimum volatility  $\mathcal{V}_{\mathrm{min}} = \pmb{x}^\top \pmb{Q}\pmb{x}$  that can be achieved on the feasible domain.
If the resulting portfolio risk  $\mathcal{V}_{\mathrm{min}} < \mathcal{V}_{\mathrm{target}}$ , then we can afford to maximize portfolio return. In this case, the objective function of the first problem becomes one of the constraints of a SOCP problem of the form
$$
\phi := \text {m i n i m i z e} - \boldsymbol {R} ^ {\top} \boldsymbol {x} \text {s u b j e c t t o} \frac {1}{2} \boldsymbol {x} ^ {\top} \boldsymbol {Q} \boldsymbol {x} \leq \mathcal {V} _ {\text {t a r g e t}} \text {a n d} \boldsymbol {a} _ {i} ^ {\top} \leq \boldsymbol {b} _ {i} \forall i \in 1, \dots , w. \tag {2}
$$
Thus, the efficient frontier can be summarized by
$$
\boldsymbol {Z} _ {\text {o u t p u t}} = \operatorname {E F} \left(\boldsymbol {Z} _ {\text {i n p u t}}\right) = \psi \text {i f} \mathcal {V} _ {\min } > \mathcal {V} _ {\text {t a r g e t}} \text {e l s e} \phi . \tag {3}
$$
The EF optimization is sensitive at inflection points where one asset becomes more attractive than another. All else held equal, an infinitesimal difference in expected returns can cause a jump in optimal allocation for all assets. Modeling the EF problem is challenging due to the presence of such discontinuities, which grows factorially  $(\mathcal{O}(N!))$  as the number of assets increases.
2.2 Accelerating Convex Optimizations
The simplest way to speed up eq. 3 is to implement the solver for direct use with highly parallelizable hardware. This has been done using GPUs for various convex optimizations, particularly for large problems with numerous unknowns [13, 44, 12]. In particular, [11] showed that solving numerous optimizations simultaneously in a single batch leads to significant speedup. We replicated this experiment by writing a vectorized implementation of the optimization of eq. 3 in Pytorch to solve multiple EF problems at once. We observed accelerations which are consistent with the order of magnitude achieved in these works, but not sufficient to allow the completion of a full simulation in a matter of few seconds unless the GPU used has high memory capacity (e.g. 40 GB).
Several works [4, 1, 20] embed differentiable optimization problems in DNNs, offering a solution to robustly approximate the EF problem including the handling of the inequality constraints in eq. 1 and eq. 2. Their approaches aim to solve a parameterized optimization problem ensuring a constrained layer's output to align with homogeneous linear inequality constraints established by  $Z_{\mathrm{input}}$ . This is done either by solving the optimization problem during training [4, 1], or when initializing the model [20]. To support a heterogenous set of constraints, one would need to specify a set of linear inequality constraints ahead of time. This approach is impractical as it cannot generalize to unspecified sets of constraints.
Other existing supervised learning (SL) approaches to approximate convex optimization with DNNs do not provide the desired flexibility and robustness for large-scale generalization
[48, 2, 18, 28]. They use a DNN that takes  $Z_{\mathrm{input}}$  as input and outputs  $Z_{\mathrm{output}}$ , treating inequality constraints as soft during training. Reinforcement learning (RL) approaches like POMO [31] and PPO [35] have also been applied variable length input combinatorial optimization problems with high label retrieval complexity where their learned policy tries to reduce an optimally gap by measuring the regrets from the optimal allocation. These DNN approaches do not guarantee that predicted values remain within the feasible domain and are often unable to handle the changing dimensionality of  $Z_{\mathrm{input}}$  and  $Z_{\mathrm{output}}$  based on  $n$  and the set of constraints selected. Our proposed method addresses these issues, resulting in a fast and accurate approximation of the convex optimizer that respects constraints.
3 Neural Approximation of the Efficient Frontier
Figure 2: Illustration of the Transformer Encoder architecture of NeuralEF. On the left side the projection to a sequence representation from the optimization inputs considering 3 assets is shown as an example.
To approximate the computation of the QP and conditional SOCP optimization described in sec. 2.1, we use a stacked transformer encoder architecture [46, 29] as shown in fig. 2. The principal idea behind NeuralEF is to consider the optimization problem as a sequence-to-sequence (SEQ2SEQ) problem [45] and use the self attention mechanism to consider the relationships between the optimization inputs when approximating eq. 3. Contrary to large language models (LLMs) that solve quantitative mathematical problem by parsing the problem using a mix of natural language and mathematical notation where a forecast is built using the same notation [33, 15], our SEQ2SEQ formulation explicitly parse the whole optimization problem by considering the optimization input directly and forecasts the output in the solution domain. We setup the SEQ2SEQ problem such that the inputs are a set of tokens, each representing a single asset, and the outputs are a one dimensional sequence of the optimal allocation. To convert the
optimization problem in eq. 3 to a sequence representation, we divide the optimization input parameters in two types of features: global optimization inputs  $(P, \zeta_{\mathrm{MAX}}, \alpha_{\mathrm{MIN}}, \alpha_{\mathrm{MAX}}, \mathcal{V}_{\mathrm{target}})$  and asset specific inputs  $(R, V, \Upsilon_{\mathrm{MAX}}, X_{\mathrm{MIN}}, C)$ . These features along with a sequence of asset identifier ( $n$  values evenly spaced within  $[0,1]$ ) are used to rearrange the optimization input parameters into a set of vectors and are linearly projected into higher dimensional
Figure 3: Illustration with two assets of the domain output space that the dynamic greedy allocation rebalancing module will enforce. The output domain is highlighted by the cross hatch patterns and the constraints are highlighted by the red lines.
tokens  $\pmb{t}_1, \dots, \pmb{t}_n$  as shown in the left part of fig. 2<sup>3</sup>. Since the projection is made on numerical features instead of categorical embeddings like language model [9], the linear projection can express an infinity of tokens and this can induce learning difficulties during training.
Preprocessing optimization inputs before sequence projection is crucial to improving model performance and overcoming two training difficulties when considering EF as a SEQ2SEQ problem. First, there are various input combinations that represent the same optimization problem. For example, when an asset's maximum weight surpasses its class constraint, the asset's maximum weight is effectively constrained by its class limit. We address this issue by preprocessing all inputs before being projected to remove any unnecessary complexity during training. Second, the model must be invariant to the sequence ordering [41, 49, 40] given as input. Building on insights from [47], NeuralEF sorts assets by returns, adding a positional embedding before the transformer encoder. This approach accelerates convergence to higher accuracy because assets with higher returns are more likely to receive greater allocation. When both of these approaches are combined, model training becomes significantly easier.
We introduce DGAR, a dynamic greedy allocation strategy, to constrain the encoder's forecast to stay within the domain set by the constraints, as shown in fig. 3. It consists of first clipping the forecast  $X = [x_{1},\dots ,x_{N}]$  with its upper and lower bounds  $\mathbf{X}_{\mathrm{max}}$  and  $\mathbf{X}_{\mathrm{min}}$  as in eq. 4. We extract an ordering of the assets  $\mathcal{K} = [k,\dots ]\coloneqq \mathrm{argsort}(-X;N)$  where  $k\in [1,\dots ,N]$  such that assets with higher allocation are prioritized in the reallocation. We also compute an estimate of the total allocation based on the upper bound and lower bound of the domain using eq. 5. We then identify whether the total allocation is above  $\alpha_{\mathrm{MAX}}$  by computing a cumulative sum following the ordering established previously and then altering the allocation on assets such that it does not exceed  $\alpha_{\mathrm{MAX}}$  as in eq. 6. A similar process is applied to cases where  $\tilde{\alpha}_{\mathrm{ALLOC}}^{\prime} < \alpha_{\mathrm{MIN}}$ , where we inflate the allocation of the assets greedily following  $\mathcal{K}$  until we reach a total allocation  $\tilde{\alpha}_{\mathrm{ALLOC}}$  which we compute by eqs. 7-10. The dynamic nature of the greedy allocation strategy allows using dynamic programming to perform the allocation in  $O(\mathrm{Nlog(N)})$ . DGAR guarantees that the forecast is within the allowed domain of linear constraints except for the class constraints  $\zeta_{\mathrm{MAX}}$  mentioned in sec. 2.1, and the volatility constraint (non-linear) which are both limitations of DGAR.
$$
x _ {i} ^ {\prime} = \min  \left(\max  \left(x _ {i}, x _ {i} ^ {\mathrm {M I N}}\right), x _ {i} ^ {\mathrm {M A X}}\right) \tag {4}
$$
$$
\tilde {\alpha} ^ {\text {A L L O C}} = \min  \left(\max  \left(\sum_ {i = 1} ^ {N} x _ {i} ^ {\prime}, \alpha_ {\mathrm {M I N}}\right), \alpha_ {\mathrm {M A X}}\right) \tag {5}
$$
$$
x _ {k} ^ {\prime \prime} = \min  \left(\max  \left(\left\{ \begin{array}{l l} x _ {k} ^ {\prime} + \left(\alpha_ {\max } - \sum_ {i = 1} ^ {k} x _ {i}\right)\right), & \text {i f} \sum_ {i = 1} ^ {k} x _ {i} > \alpha_ {\max }, \\ x _ {k} ^ {\prime}, & \text {o t h e r w i s e} \end{array} , 0\right), \alpha_ {\operatorname * {M A X}}\right) \quad (6)
$$
$$
m = \left\{ \begin{array}{l l} 1 & \text {i f} \tilde {\alpha} _ {\mathrm {A L L O C}} ^ {\prime} <   \alpha_ {\mathrm {M I N}} \\ 0, & \text {o t h e r w i s e} \end{array} ; \tilde {\alpha} _ {\mathrm {A L L O C}} ^ {\prime} = \sum_ {i = 1} ^ {N} x _ {i} ^ {\prime \prime} \right. \tag {7}
$$
$$
a = \left\| \tilde {\alpha} _ {\text {A L L O C}} - \tilde {\alpha} _ {\text {A L L O C}} ^ {\prime} \right\| * m \quad (8) \quad b _ {k} = \min  \left(x _ {k} ^ {\operatorname {M A X}} - x _ {k} ^ {\prime \prime}, a\right) \tag {9}
$$
$$
x _ {k} ^ {\prime \prime \prime} = x _ {k} ^ {\prime \prime} + \max  \left(0, b _ {k} - \max  \left(0, - \left(a - \sum_ {i = 1} ^ {k} b _ {k}\right)\right)\right) \tag {10}
$$
We used a MC sampling scheme to cover the entire domain in table. 1 uniformly to generate a dataset of approximately one billion samples  $\mathcal{D}_{\mathrm{train}} = [(Z_{\mathrm{input},1},\mathrm{EF}((Z_{\mathrm{input},1}),\dots)]$ , which we used to train NeuralEF in a supervised fashion:
$$
\theta_ {\text {N e u r a l E F}} = \underset {\theta_ {\text {N e u r a l E F}} ^ {*}} {\operatorname {a r g m i n}} \frac {1}{N} \sum_ {i = 0} ^ {N} \mathcal {L} \left(\text {N e u r a l E F} \left(\boldsymbol {Z} _ {\text {i n p u t}, i}; \theta_ {\text {N e u r a l E F}}\right), \mathrm {E F} \left(\boldsymbol {Z} _ {\text {i n p u t}, i}\right)\right). \tag {11}
$$
There are many ways to set up an optimization problem into a SEQ2SEQ problem. Similar to prompt engineering on LLMs [8], different formulations influence the model accuracy. We do not focus on finding the best formulation to convert optimization problem into input tokens.
We generated two test datasets  $\mathcal{D}_{\mathrm{test}}$ ,  $\mathcal{D}_{\mathrm{validation}}$  of 1 million samples each on the the same domain as the training set described in table 1. All synthetic datasets mimic real-life distributions for the volatility and correlation inputs, encompassing area of the optimization domain around the discontinuity areas. Around  $84\%$  of the test samples feature at least two optimization inputs within  $\epsilon$  proximity of each other to target discontinuity areas of eq. 3. This synthetic data allows for precise control over optimization inputs and encompasses a broad spectrum of scenarios, ranging from extreme to real-world cases targeting mainly the discontinuity regions. Consequently, it facilitates a comprehensive evaluation of the model's performance across various settings. All datasets have varying numbers of assets and asset classes, and include two allocation scenarios: full allocation  $(\alpha_{\mathrm{MAX}} = \alpha_{\mathrm{MIN}} = 1)$  and partial allocation  $(\alpha_{\mathrm{MAX}} > \alpha_{\mathrm{MIN}})$  where there is no obligation to allocate the full budget. Only valid optimization inputs  $Z_{\mathrm{input}}$ , i.e. where  $\mathrm{EF}(Z_{\mathrm{input}})$  does not fail because of numerical difficulties in the solver or that no solution exists, where considered.
Feature	Range	Feature	Range
(Vartarget) volatility target	[0.05, 0.15]	(V) volatility	[0, 2]
(P) Correlation matrix	[-1, 1]	(R) returns	[-1, 2]
(ζMAX) maximum class allocation	[0.2, 1.0]	(wtMAX) maximum asset allocations	[0.01, 1.0]
(αMIN) Allocation lower bound	[0.6, 1.0]	(αMAX) Allocation upper bound	1.0
(n) Number of asset sampled	[2,12]	(m) Possible class	[0,1,2]
NeuralEF is a 7.9M parameter DNN and was trained on a single NVIDIA A100 GPU with stochastic gradient descent using the AdamW optimizer [34] and the  $L^2$  loss. We also used an annealing learning rate decay starting from  $5.5e^{-5}$  to  $1.0e^{-6}$ . As stated in sec. 2, we also implemented a baseline EF optimization in PyTorch that we used solely for comparing the evaluation throughput (evaluations/seconds) between NeuralEF over the base pricer which was implemented using CVXOPT [14]. The hyperparameters of NeuralEF are described in table. 2 and were selected by estimated guesses from the accuracy measured on  $\mathcal{D}_{\mathrm{validation}}$ .
Table 1: Input Domain of optimization input used for training.
Name	Value	Name	Value
Token dimension	320	(D) Transformer depth	8
Transformer # heads	8	Transformer heads dimension	32
Feed forward projection	1024	Output activation	Sigmoid
Embedding method	[30, 24]	Hidden activation	Swish [42]
Table 2: HyperParameters of NeuralEF. Adjusting accuracy at the expense of throughput can be easily done by increasing the token dimension.
4.1 In-domain Interpolation
Figure 4: Cumulative distributions of the sum of absolute allocation error of allocations and portfolio returns per number of assets