Standard portfolio allocation algorithms like Markowitz mean-variance optimization or Choueffati diversification ratio optimization usually take in input asset information (expected returns, estimated covariance matrix…) as well investor constraints and preferences (maximum asset weights, risk aversion…) to produce in output portfolio weights satisfying a selected mathematical objective like the maximization of the portfolio Sharpe ratio or Diversification ratio. Chevalier et al.1 introduces a non-standard portfolio allocation framework - represented in Figure 1 - under which the same input is first used to “learn” in-sample optimized portfolio weights in a supervised training phase and then used to produce out-of-sample optimized portfolio weights in an inference phase. Figure 1. Standard v.s. supervised portfolio allocation framework. Source: Adapted from Chevalier et al. In this blog post, I will provide some details about that framework when used with the kk-nearest neighbors supervised machine learning algorithm, which is an idea originally proposed in Varadi and Teed23. As an example of usage, I will compare the performances of a kk-nearest neighbors supervised portfolio with those of a “direct” mean-variance portfolio in the context of a monthly tactical asset allocation strategy for a 2-asset class portfolio made of U.S. equities and U.S. Treasury bonds. Mathematical preliminaries Supervised machine learning algorithms Let (X1,Y1)\left( X_1, Y_1 \right), …, (Xn,Yn)\left( X_n, Y_n \right) be nn pairs of data points in4 Rm×R\mathbb{R}^m \times \mathbb{R}, m1m \geq 15, where: Each data point X1,,XnX_1, …, X_n represents an object - like the pixels of an image - and is called a feature vector Each data point Y1,,YnY_1,…,Y_n represents a characteristic of its associated object - like what kind of animal is depicted in an image (discrete characteristic) or the angle of the rotation between a rotated image and its original version (continuous characteristic) - and is called a label Given a feature vector xRmx \in \mathbb{R}^m, the aim of a supervised machine learning algorithm is then to estimate the “most appropriate” label associated to xx - y^R\hat{y} \in \mathbb{R} - thanks to the information contained in the training dataset (X1,Y1)\left( X_1, Y_1 \right), …, (Xn,Yn)\left( X_n, Y_n \right). kk-nearest neighbors regression algorithm Let dd be a distance metric6 on Rm\mathbb{R}^m, like the standard Euclidean distance. The kk-nearest neighbor (kk-NN) regression algorithm is an early7 [supervised] machine learning algorithm8 that uses the “neighborhood” of a feature vector in order to estimate its label. In more details, let (X(i)(x),Y(i)(x))\left( X_{(i)}(x), Y_{(i)}(x) \right), i=1..ni=1..n denotes the ii-th training data point closest to xx among all the training data points (X1,Y1)\left( X_1, Y_1 \right), …, (Xn,Yn)\left( X_n, Y_n \right) such that the distance of each training data point to xx satisfies d(x,X(1)(x))d \left(x, X_{(1)}(x) \right) \leq … \leq d(x,X(n)(x))d \left(x, X_{(n)}(x) \right). By definition, the kk-NN estimate for the label associated to xx is then9 the uniformly or non-uniformly weighted average label of the k{1,,n}k \in \{ 1,…,n \} nearest neighbors Y(1)(x)Y_{(1)}(x),…,Y(j)(x)Y_{(j)}(x) [\hat{y} = \frac{1}{k} \sum_{i=1}^k Y_{(i)}(x)] or [\hat{y} = \sum_{i=1}^k w_i Y_{(i)}(x)] , where wi0w_i \geq 0 is the weight associated to the ii-th nearest neighbor Y(i)(x)Y_{(i)}(x) and all the weights wiw_i, i=1..ki=1..k sum to one, that is, j=1kwk\sum_{j=1}^k w_k. For illustration purposes, the process of selecting the 2 nearest neighbors X(1)(x)X_{(1)}(x) and X(2)(x)X_{(2)}(x) of a data point xx in R2\mathbb{R}^2 is outlined in Figure 2. Figure 2. Example of kk-NN nearest neighbors selection process in m = 2 dimensions, with n = 3 training data points and k = 2 nearest neighbors. Notes: It additionally exists the kk-NN classification algorithm, which is a variant of the kk-NN regression algorithm where the label space is not R\mathbb{R} but a finite subset of N\mathbb{N}. Theoretical guarantees Since the seminal paper of Cover and Hart10 - proving under mild conditions that the kk-NN classification algorithm achieves an error rate that is at most twice the best error rate achievable8 -, several convergence results have been established for kk-NN methods. For example, under an asymptotic regime where the number of training data points nn and the number of nearest neighbors kk both go to infinity, it has been demonstrated9 that the kk-NN regression algorithm is able to learn any functional relationship of the form Yi=f(Xi)+ϵiY_i = f \left( X_i \right) + \epsilon_i, i=1..ni=1..n, where ff is an unknown function and ϵi\epsilon_i represents additive noise. As another example, this time under a finite11 sample regime, Jiang12 derives the first sup-norm finite-sample [“convergence”] result12 for the kk-NN regression algorithm and shows that it achieves a maximum error rate that is equal to the best maximum error rate achievable up to logarithmic factors12, with high probability. In addition to these convergence results, kk-NN methods also exhibit interesting properties w.r.t. the dimensionality of the feature space Rm\mathbb{R}^m. For example, while the curse of dimensionality forces non-parametric methods such as kk-NN to require an exponential-in-dimension sample complexity12, the kk-NN regression algorithm actually adapts to the local intrinsic dimension without any modifications to the procedure or data12. In other words, if the feature vectors belong to Rm\mathbb{R}^m but have a “true” dimensionality equal to \mathbb{R}^p, p < m, then the kk-NN regression algorithm will [behave] as if it were in the lower dimensional space [of dimension pp] and independent of the ambient dimension [mm]12. Further properties of kk-NN methods can be found in Chen and Shah8 and in Biau and Devroye9. Practical performances Like all supervised machine learning algorithms, the practical performances of the kk-NN regression algorithm heavily depend on the problem at hand. Yet, in general, it often yields competitive results [v.s. other more complex algorithms like neural networks], and in certain domains, when cleverly combined with prior knowledge, it has significantly advanced the state-of-the-art13. Beyond these competitive performances, Chen and Shah8 also highlights other important practical aspects of kk-NN methods that contributed to their empirical success over the years8: Their flexibility in choosing a problem-specific definition of “near” through a custom distance metric14 Their computational efficiency, which has enabled these methods to scale to massive datasets (“big data”)8 thanks to approaches like approximate nearest neighbor search15 or random projections16 Their non-parametric nature, in that they make very few assumptions on the underlying model for the data8 Their ease of interpretability, since they provide evidence for their predictions by exhibiting the nearest neighbors found8 kk-NN based supervised portfolios Supervised portfolios Chevalier et al.1 describes an asset allocation strategy that engineers optimal weights before feeding them to a supervised learning algorithm1, represented in the lower part of Figure 1. Given a training dataset of past [financial] observations1 like past asset returns, past macroeconomic indicators, etc., it proceeds as follows: For any relevant date17 t=t1,t=t_1,… in the training dataset Compute optimal (in-sample) future portfolio weights wt+1w_{t+1} over a (also in-sample) desired future horizon18, using a selected portfolio optimization algorithm with financial observations up to the time t+1t+1 These optimal future portfolio weights are the labels YtY_t, t=t1,t=t_1,…, of the training data points. To be noted that by lagging the data, we can use the in-sample future realized returns to compute all the [returns-based] estimates1 required by the portfolio optimization algorithm like the expected asset returns, the asset covariance matrix, etc. This allows to be forward-looking in the training sample, while at the same time avoiding any look-ahead bias1. During this step, constraints can of course be added in order to satisfy targets and policies1. Compute a chosen set of predictors supposed to be linked to the in-sample future portfolio weights wt+1w_{t+1}, using financial observations up to the time tt These predictors are the feature vectors XtX_t, t=t1,t=t_1,…, of the training data points. Train and tune a supervised machine learning algorithm using the training data points (Xt,Yt)\left( X_t, Y_t \right), t=t1,t=t_1,…. Once the training phase is completed, the supervised portfolio allocation algorithm is ready to be used with test data19. For any relevant (out-of-sample) test date t=t1,t’=t’_1,… Compute the set of predictors chosen during the training phase, using financial observations up to the time tt’ These predictors are the test feature vectors xtx_{t’}, t=t1,t’=t’_1,…. Provide that set of predictors as an input test feature vector to the supervised machine learning algorithm to receive in output the estimated optimal portfolio weights w^t+1\hat{w}_{t’+1} over the (out-of-sample) future horizon These estimated optimal portfolio weights are the estimated labels y^t\hat{y}_{t’}, t=t1,t’=t’_1,…. Here, depending on the exact supervised machine learning algorithm, the estimated portfolio weights w^t+1\hat{w}_{t’+1} might not satisfy the portfolio constraints20 imposed in the training phase, in which case a post-processing phase would be required. The portfolio allocation framework of Chevalier et al.1 described above allows the algorithm to learn from past time series of in-sample optimal weights and to infer the best weights from variables such as past performance, risk, and proxies of the macro-economic outlook1. This contrasts with the standard practice of directly forecasting the input of a portfolio optimization algorithm, making that framework rather original. In terms of empirical performances, Chevalier et al.1 finds that predicting the optimal weights directly instead of the traditional two step approach leads to more stable portfolios with statistically better risk-adjusted performance measures1 when using mean-variance optimization as the selected portfolio optimization algorithm and gradient boosting decision trees as the selected supervised machine learning algorithm21. Some of these risk-adjusted performance measures are displayed in Figure 3 in the case of 4 asset classes22, for the 3 horizons of predicted returns and the 3 risk aversion levels used in Chevalier et al.1. Figure 3. Performances of supervised portfolios v.s. direct mean-variance optimized portfolios, 4 asset classes. Source: Adapted from Chevalier et al. Notes: Additional information can be found in the follow-up paper Chevalier et al.23 and in a video of Thomas Raffinot for QuantMinds International. kk-NN-based supervised portfolios Theoretically, the supervised machine learning model used in the portfolio allocation framework of Chevalier et al.1 is trained to learn the following model1: [w_{t+1} = g_t \left(X_t \right) + \epsilon_{t+1}] , where: XtX_t is the feature vector made of the chosen set of predictors computed at time tt wt+1w_{t+1} is the vector of optimal portfolio weights over the desired future horizon t+1t+1 gg is an unknown function Because such a model describes a functional relationship compatible with a kk-NN regression algorithm, it is reasonable to think about using that algorithm as the supervised machine learning algorithm in the above framework. Enter kk-NN-based supervised portfolios, a portfolio allocation framework originally introduced in Varadi and Teed2 as follows: This naturally leads us down the path of creating algorithms that can learn from past data and evolve over time to change the method for creating portfolio allocations. The simplest and most intuitive machine-learning algorithm is the K-Nearest Neighbor method (kk-NN) […, which] is a form of “case-based” reasoning. That is, it learns from examples that are similar to current situation by looking at the past [and says: “what happened historically when I saw patterns that are close to the current pattern?”]. It shares a lot in common with how human beings make decisions. When portfolio managers talk about having 20 years of experience, they are really saying that they have a large inventory of past “case studies” in memory to make superior decisions about the current environment. As a side note, Varadi and Teed2 is not the first paper to apply a kk-NN regression algorithm to the problem of portfolio allocation, c.f. for example Gyorfi and al.24 in the setting of online portfolio selection, but Varadi and Teed2 is - to my knowledge - the first paper about the same “kind” of supervised portfolios as in Chevalier et al.1. A couple of practical advantages of kk-NN-based supervised portfolios v.s. for example “gradient boosting decision trees”-based supervised portfolios as used in Chevalier et al.1 are: The simplicity of the training Since nearest neighbor methods are lazy learners, there is strictly speaking no real training phase. The simplicity of the tuning There can be no tuning at all if no “advanced” technique (automated features selection, distance learning…) is used. The guarantee that (convex) portfolio constraints learned during the training phase are satisfied during the test phase In kk-NN regression25, the estimate for the label associated to a test point is a convex combination of that point nearest neighbors. As a consequence, the estimated portfolio weights w^t+1\hat{w}_{t’+1} are guaranteed25 to satisfy any learned convex portfolio constraints, thereby avoiding any post-processing that could degrade the “quality” of the estimated weights. The ease of interpretability Due to algorithm aversion, Chevalier et al.23 highlights the need to be able to transform a black box nonlinear predictive algorithm [like gradient boosting decision trees] into a simple combination of rules23 in order to make it interpretable for humans. With a kk-NN regression algorithm, which is one of the most transparent supervised machine learning algorithm in existence, that step is probably not useful26. In terms of empirical performances, Varadi and Teed2 concludes that kk-NN-based supervised portfolios consistently outperformed [vanially maximum Sharpe ratio portfolios] on both heterogeneous and homogenous data sets on a risk-adjusted basis2, with the kk-NN-based approach [exhibiting] a Sharpe ratio [… up to] over 30% higher than [the direct maximum Sharpe ratio approach]2. Average performance measures for the kk-NN-based supervised portfolios in Varadi and Teed2 are reported in Figure 4. Figure 4. Performances of kk-NN-based supervised portfolios v.s. direct mean-variance optimized portfolios. Source: Adapted from Varadi and Teed. Implementing kk-NN-based supervised portfolios Features selection Biau and Devroye9 describes features selection as: […] the process of choosing relevant components of the [feature] vector XX for use in model construction. There are many potential benefits of such an operation: facilitating data visualization and data understanding, reducing the measurement and storage requirements, decreasing training and utilization times, and defying the curse of dimensionality to improve prediction performance. , and provides some rules of thumb that should be followed9: Noisy measurements, that is, components that are independent of YY, should be avoided9, especially because nearest neighbor methods are extremely sensitive to the features used27 Adding a component that is a function of other components is useless9 Beyond these generic rules, and although it has been an active research area in the statistics, machine learning, and data mining communities1, features selection is unfortunately strongly problem-dependent. In the context of supervised portfolios, Chevalier et al.1 and Varadi and Teed2 both propose to use: Past asset returns over different horizons28 so as to assess momentum and reversals1 Past asset volatilities over different horizons28, to approximate asset-specific risk1 Varadi and Teed2 additionally proposes to include past asset correlations over different horizons28 to ensure that [the] kk-NN algorithm [doesn’t] have access to any information that the [direct mean-variance optimization] [doesn’t] have, but merely use it differently2. Chevalier et al.1, building on stocks asset pricing litterature, does not suggest to include other returns-based indicator than past asset returns and volatilities but suggests instead to include various macroeconomic indicators (yield curve, VIX…). Features scaling Typical distance metrics29 used with nearest neighbor methods like the Euclidean distance are said to be scale variant, meaning that the definition of a nearest neighbor is influenced by the relative and absolute scale of the different features. For example, when using the Euclidean distance with features such as a person’s height and a person’s age: The height feature disproportionally infuences the definition of a neighbor if the height feature is measured in millimeters and age in years The age feature disproportionally infuences the definition of a neighbor if the height feature is measured in meters and age in days For this reason, features are usually scaled to a similar range before being provided in input to a kk-NN algorithm30, which is a pre-processing step called features scaling. A couple of techniques for features scaling are described in Arora et al.31: Min-max scaling, which scales all the values of a feature (Xi)j\left( X_i \right)_j, j{1,,m}j \in \{ 1,…,m \}, i=1..ni=1..n to a given interval - like [0,1][0,1] -, based on the minimum and the maximum values of that feature: [\left( X_i \right)_j' = \frac{\left( X_i \right)_j - \min_j \left( X_i \right)_j }{\max_j \left( X_i \right)_j - \min_j \left( X_i \right)_j }, i=1..n] Standardization, also called z-score normalization, which transforms all the values of a feature (Xi)j\left( X_i \right)_j, j{1,,m}j \in \{ 1,…,m \} , i=1..ni=1..n into values that are approximatively standardly normally distributed: [\left( X_i \right)_j' = \frac{ \left( X_i \right)_j - \overline{\left( X_i \right)j}}{ \sigma{\left( X_i \right)_j} }] In the context of supervised portfolios, additional techniques are described in Chevalier et al.1: Quantile normal transformation for a “time series”-like feature, which standardizes the time-series into quantile and then map the values to a normal distribution1 It is important to note that at any given date, the quantiles should be computed using information up to that date only to avoid forward looking leakage1. In addition, a lookback window over which to compute the quantiles should be chosen, with possible impacts on the performances of the supervised machine learning algorithm. Cross sectional normalization for a regular feature, which scales the cross sectional values between 0 and 1 using the empirical cumulative distribution function1 At any given date, this normalization can be performed fully in the cross-section at that date if there are enough assets or in the cross-section at that date using information up to that date to compute the empirical cumulative distribution function. In the latter case, c.f. the previous point. Hyperbolic tangent function (tanh\tanh) scaling for labels, in order to center [them] and make them more comparable by taming outliers1: [Y' = 0.5 \tanh{\left( 0.01 \frac{Y − \overline{Y}}{ \sigma_Y } \right) }] Naturally, the reverse transformation is performed after the prediction to transform back the labels into its original values1. Finally, in the specific context of kk-NN-based supervised portfolios, 2 additional techniques are described in Varadi and Teed2, that are variations of the techniques of Chevalier et al.1. Distance metric selection As already mentioned in the previous sub-section, the distance metric used with a nearest neighbor method influences the definition of a neares