SlowGuess/NeurIPS_2023_OCR huggingface dataset:
def efficient_frontier(maxweights, vol_target, conditions, condition_max, vol, ret, correl):
    n_assets = len(maxweights)
    minweights = [0] * n_assets
    v_t = vol_target * vol_target * scalar
    G = np.vstack([np.eye(n_assets), -np.eye(n_assets), conditions])
    H = np.hstack([maxweights, minweights, condition_max])
    cov = scalar * np.matmul(np.matmul(np.diag(vol), correl), np.diag(vol))
    wt = cvxopt.solve_qp(cov, np.zeros_like(ret), G, H) #eq 1
    wt = np.minimum(list(np.maximum(list(wt), minweights)), maxweights)
    wt1d = wt.reshape([n_assets, 1])
variance  $=$  np.matmul(np.matmul(wt1d.T, cov), wt1d)[0, 0]  
if variance  $<$  v_t:  
ret  $=$  np.array(ret)  
chol  $=$  np.linalg.cholesky Cov).T  
Gq  $=$  np.vstack([np.zeros(n_assets), chol])  
hq  $=$  np.zeros(n_assets + 1)  
hq[0]  $=$  np.sqrt(v_t)  
wt  $=$  cvxopt.solve_socp(-ret, G1=G, hl=H, Gq=Gq, hq=hq) #eq 2  
wt  $=$  np.minimum(list(npmaximum(list(wt), minweights)), maxweights)  
return wt
B Preprocessing
We encounter ambiguity in optimization problems due to various combinations of inputs representing the same problem. To address this, we provide three examples where we discuss the ambiguity and propose a standardized solution for processing inputs in an optimized manner prior to token projection.
When the  $i$ -th asset belongs to the  $j$ -th asset class and  $x_{i}^{\mathrm{MAX}} > \zeta_{c_{j}}$ , the constraint  $x_{i}^{\mathrm{MAX}}$  is overridden by  $\zeta_{c_{j}}$ . This means that there is no combination of assets where the allocation of the  $i$ -th asset can be higher than  $\zeta_{c_{j}}$ . To address this constraint, we clip  $x_{i}^{\mathrm{MAX}}$  to  $\zeta_{c_{j}}$  by using the formula:  $x_{i}^{\prime \mathrm{MAX}} = \min (\max (x_{i}^{\mathrm{MAX}},\zeta_{c_{j}}),0)$  for all  $i$ -th assets belonging to the  $j$ -th class.
The remaining two cases are additional edge cases related to the previous condition. If only one asset is assigned to the  $j$ -th class,  $\zeta_{c_j}$  and  $x_j^{\mathrm{MAX}}$  should be equal because it is equivalent to having no class constraint for that class. Also, if a class constraint is set but no assets belong to that class, it is equivalent to setting  $\zeta_{c_j} = 0$ . By processing the optimization inputs in this manner, we ensure that any ambiguity on the class constraints are standardized, allowing for equivalent linear projections into token before the transformer encoder part of the network.
C Experimental Section
Dataset name	Size	Description
Dtrain	1.2B samples	Sampled at random over the domain in table. 1
Dtest	990K samples	Sampled at random over the domain in table. 1
Dood	990K samples	Sampled following the indication of sec. 4.3
Table 5: Description of the dataset used
The size and description of the dataset we used are presented in table. 5. We used an asymmetric weighting scheme to generate all datasets, favoring more complex optimizations (see Table 6). As the number of assets increases, the number of unstable regions also increases, where allocation can significantly change. To ensure training and evaluation encompass these unstable regions, we generated a higher proportion of optimization inputs with more assets.
NeuralEF comprises three parts. First, the preprocesses step (1.1) sort requests by asset returns, (1.2) reduce optimization input ambiguity (Appendix B), and (1.3) project them into tokens for a SEQ2SEQ representation. Secondly there is a bidirectional encoder transformer that solve EF as SEQ2SEQ. Finally, there is DGAR to enforce constraints. The preprocessing and DGAR help accurate forecasting. (1.1) and (1.2) are optional for training if accounted in the training data but help at inference to ensure the sorting requirement of NeuralEF and clean request of some optimization input ambiguities.
To show the effectiveness of DGAR, we've trained two NeuralEF models (w/ and w/o DGAR) trained for 5 days with a fixed learning rate of  $5.5e - 5$  and present the distributional error an
Asset case	Proportion (%)
2	2.517
3	2.551
4	2.559
5	2.603
6	6.834
7	6.879
8	10.346
9	10.377
10	13.826
11	13.850
12	27.658
Table 6: Proportion of assets in each datasets.
(a)  $\mathcal{D}_{\mathrm{test}}$  distribution error NeuralEF w/o DGAR trained w/ constant lr=5e-5 for 5 days
(b)  $\mathcal{D}_{\mathrm{test}}$  distribution error NeuralEF w/ DGAR trained w/ constant lr=5e-5 for 5 days
Figure 10: Accuracy W/ DGAR vs W/O DGAR
optimality gap of the metrics that are not respected on fig. 10-11 highlighting that model with DGAR achieves better MAE. Also, DGAR exhibits a reduced optimally gap of constraint adherence, further motivating the use of DGAR.
The cleaning module of NeuralEF were attached before the transformer part of the model to address the possible ambiguities provided in the optimization inputs. Using the same example of fig. 5 where the 10-th asset is associated to class  $C_2$ , which is its only member, we changed the value of the constraints to be ambiguous relative to another and show in fig. 12 how accuracy deteriorates when the model encounter request that are ambiguous.
C.3 Accuracy with half-precision floating-point format
The results obtained using single-precision floating-point (FP32) and the model quantized to half-precision floating-point (FP16) are on the same order of accuracy as shown in table 7 and fig. 13. The quantization process to FP16 maintains the necessary precision for the calculations, resulting in equivalent outcomes as the FP32 counterpart. While a degradation
(a)  $\mathcal{D}_{\mathrm{test}}$  optimality gap NeuralEF trained w/o DGAR w/ constant  $\mathrm{lr} = 5\mathrm{e} - 5$  for 5 days
Figure 11: Optimality gap W/ DGAR vs W/O DGAR
(b)  $\mathcal{D}_{\mathrm{test}}$  optimality gap NeuralEF w/ DGAR trained w/ constant  $\mathrm{lr} = 5\mathrm{e} - 55$  for 5 days
in the ability to rank assets<sup>7</sup> and respect the volatility and class constraint occurs, we observe that this does not impact the overall distributional properties and the downstream applications that would benefit from it. As such, there is no discrepancy in the results between the two representations, demonstrating the viability of using the lower-precision FP16 for computational efficiency.
The ranking of the results has been computed with a tolerance of  $1e - 4$ , where a slight deviations are permissible and don't hurt the accuracy. This was made such that negligible allocation made by NeuralEF which can be disregarded for practical purpose are neglected.
Asset case	Portfolio weights MSE	Portfolio weights MAE	95 quantile	99.865 quantile	99.997 quantile	Ranking precision
2	9.54e-07	9.77e-04	1.25e-02	3.37e-02	1.09e-01	93.012 %
3	7.95e-08	1.63e-04	1.51e-02	3.78e-02	1.06e-01	98.394 %
4	6.85e-07	5.34e-04	1.65e-02	4.59e-02	1.39e-01	97.231 %
5	2.52e-06	1.25e-03	1.48e-02	4.08e-02	1.77e-01	94.064 %
6	1.94e-05	2.00e-03	1.51e-02	4.28e-02	1.68e-01	89.798 %
7	2.84e-06	1.19e-03	1.67e-02	4.50e-02	1.95e-01	85.224 %
8	1.06e-05	2.40e-03	1.61e-02	4.49e-02	1.57e-01	81.598 %
9	7.72e-06	1.65e-03	1.99e-02	5.20e-02	2.08e-01	77.217 %
10	1.16e-05	2.21e-03	2.00e-02	5.25e-02	1.71e-01	74.899 %
11	1.11e-05	1.72e-03	2.22e-02	5.85e-02	2.36e-01	71.646 %
12	3.48e-08	1.30e-04	1.98e-02	5.31e-02	2.11e-01	68.804 %
Portfolio return MSE	Portfolio return MAE	95 quantile	99.865 quantile	99.997 quantile	\( \zeta_{MAX} \)precision
2	2.69e-06	1.64e-03	7.87e-03	2.26e-02	6.98e-02	97.555 %
3	9.48e-06	3.08e-03	1.24e-02	3.19e-02	1.12e-01	90.949 %
4	3.58e-06	1.89e-03	1.48e-02	4.05e-02	1.77e-01	87.576 %
5	8.79e-06	2.96e-03	1.49e-02	3.57e-02	9.32e-02	86.583 %
6	1.60e-13	3.99e-07	1.52e-02	3.58e-02	1.21e-01	85.592 %
7	2.15e-09	4.63e-05	1.68e-02	3.94e-02	1.51e-01	82.323 %
8	5.74e-06	2.40e-03	1.73e-02	4.25e-02	1.49e-01	84.808 %
9	2.11e-06	1.45e-03	2.13e-02	5.19e-02	1.74e-01	83.589 %
10	2.38e-09	4.88e-05	2.31e-02	5.36e-02	1.55e-01	83.707 %
11	1.31e-07	3.62e-04	2.49e-02	5.75e-02	1.75e-01	83.190 %
12	1.28e-06	1.13e-03	2.49e-02	5.75e-02	1.84e-01	83.197 %
Volatility MSE	Volatility MAE	95 quantile	99.865 quantile	99.997 quantile	\( \nu_{target} \)precision
2	2.69e-06	1.64e-03	7.87e-03	2.26e-02	6.98e-02	87.649 %
3	9.48e-06	3.08e-03	1.24e-02	3.19e-02	1.12e-01	81.729 %
4	3.58e-06	1.89e-03	1.48e-02	4.05e-02	1.77e-01	83.229 %
5	8.79e-06	2.96e-03	1.49e-02	3.57e-02	9.32e-02	79.704 %
6	1.60e-13	3.99e-07	1.52e-02	3.58e-02	1.21e-01	80.056 %
7	2.15e-09	4.63e-05	1.68e-02	3.94e-02	1.51e-01	80.270 %
8	5.74e-06	2.40e-03	1.72e-02	4.25e-02	1.49e-01	76.472 %
9	2.11e-06	1.45e-03	2.13e-02	5.19e-02	1.74e-01	77.205 %
10	2.38e-09	4.88e-05	2.31e-02	5.36e-02	1.55e-01	79.268 %
11	1.31e-07	3.62e-04	2.49e-02	5.75e-02	1.75e-01	81.682 %
12	1.28e-06	1.13e-03	2.49e-02	5.75e-02	1.84e-01	79.517 %
Table 7: Accuracy of portfolio weights, implied return and resulting volatility for the FP16 quantized NeuralEF.