## LVQ and Machine Learning for Algorithmic Traders – Part 3

In the last two posts, LVQ and Machine Learning for Algorithmic Traders – Part 1, and LVQ and Machine Learning for Algorithmic Traders – Part 2, we demonstrated how to use:

1. Linear Vector Quantization
2. Correlation testing

..to determine the relevance/importance of and correlation between strategy parameters respectively.

Yet another technique we can use to estimate the best features to include in our trading strategies or models, is called Recursive Feature Elimination, an automatic feature selection approach.

## What is Automatic Feature Selection?

It enables algorithmic traders to construct multiple quantitative models using different segments of a given dataset, allowing them to identify which combination of features or strategy parameters results in the most accurate model.

Recursive Feature Elimination

One such method of automatic feature selection is Recursive Feature Elimination (RFE).

To evaluate the best feature-space for an accurate model, the technique iteratively applies a Random Forest algorithm to all possible combinations of the input feature data (strategy parameters).

The end-outcome is a list of features that produce the most accurate model.

Using RFE, algorithmic traders can refine and speed up trading strategy optimization significantly (subject to this list being smaller than the total number of input parameters of course).

R (Statistical Computing)

We’ll make use of the caret (Classification and Regression Training) package in R once again.

It contains functions to perform RFE conveniently, allowing us to spend more time in analysis instead of writing the functionality ourselves.

## Recursive Feature Elimination – Step by Step Process

1. As before, run “raw” backtests without any optimization, employing all features (parameters), and save your results in a suitable data structure (e.g. CSV table) + load the caret and randomForest libraries.
2. Specify the algorithm control using a Random Forest selection method.
3. Execute the Recursive Feature Elimination algorithm.
4. Output the algorithm’s chosen features (strategy parameters).

### Step 1: Load the data + “randomForest” and “caret” machine learning libraries in R

```> library(caret) > library(randomForest) > train.blogpost <- read.csv("data.csv", head=T, nrows=1000) > train.blogpost <- train.blogpost[,grep("feature|target",names(train.blogpost))]```

### Step 2: Specify the control using Random Forest selection function

`> rfe.control <- rfeControl(functions=rfFuncs, method="cv", number=10)`

### Step 3: Execute the Recursive Feature Elimination algorithm

`rfe.output <- rfe(train.blogpost[,1:21], train.blogpost[,22], sizes=c(1:21), rfeControl = rfe.control)`

### Step 4: Output chosen features (strategy parameters)

```> print(rfe.output) > predictors(rfe.output) > plot(rfe.output, type=c("o", "g"))```

Recursive Feature Elimination – Output Predictors

Recursive Feature Elimination – RMSE Plot

## Conclusion

From these results, it is easily apparent that a model with:

1. The first two parameters only, generates the most inaccurate model.
2. The algorithm’s 5 selected parameters (out of a total of 21) produces the most accurate model.
3. Any number of parameters greater than 5 produces lower but comparable accuracy, therefore choosing any greater a number of parameters would add zero value to the model.

Based on this, an algorithmic trader could significantly reduce his/her optimization overhead, by culling the number of strategy parameters employed in backtesting and optimization.

Additional Resource: Measuring Investments’ Risk: Value at Risk (VIDEO)
* please activate CC mode to view subtitles.

Do you have what it takes? – Join the Darwinex Trader Movement!

Darwinex – The Open Trader Exchange

## LVQ and Machine Learning for Algorithmic Traders – Part 2

Highly Correlated Parameter Removal

In LVQ and Machine Learning for Algorithmic Traders – Part 1, we discussed and demonstrated a technique (Linear Vector Quantization) to decipher the relevance and relative importance of each feature variable in the dataset under study.

In doing so, algorithmic traders would be able to isolate which of a dataset’s features (read: strategy parameters) had a minor impact on the final target, thereby aiding faster strategy optimization.

Another technique we can use for the same objective (isolating features that have little to no impact on end outcomes), involves studying the correlation between the dataset’s feature variables.

### Why Correlation?

When a trading strategy has highly correlated parameters, algorithmic traders not only run the risk of overfitting, but also that of introducing avoidable latency in execution.

While the latter may be more of a concern for short-term / intraday traders, over the longer term it may introduce considerably higher transactional costs even for swing traders -> intended vs. actual fills.

Algorithmic traders can therefore benefit from the removal of such highly correlated parameters, prior to any optimization.

R (Statistical Computing Environment)

The procedure to follow for removing such redundant features is quite simple (see below).

We will once again, make use of the caret (Classification and Regression Training) package in R, that contains a suite of convenient functions for this particular task.

N.B. It is just as simple to replicate this process in C++, Java, MQL or Python.

### Step by Step Process

1. Run “raw” backtests without any optimization, employing all features (parameters), and save your results in a suitable data structure (e.g. CSV table) for further analysis.
2. Construct a correlation matrix of the data’s features (read: strategy’s parameters).
3. Run the correlation matrix through caret’s findCorrelation() function to determine which features (parameters) are highly correlated, and can hence be removed.
4. Set the correlation threshold at 60% (can be higher or lower if you prefer), to remove features.

#### Step 1: Load the “caret” machine learning library in R.

`> library(caret)`

#### Step 2: Load and process the backtest dataset.

For the purposes of this example, we will use the same “feature|target” backtested dataset of 1,000 records employed in LVQ and Machine Learning for Algorithmic Traders – Part 1.

`> train.blogpost <- read.csv("data.csv", head=T, nrows=1000)`

`> train.blogpost <- train.blogpost[,grep("feature",names(train.blogpost))]`

#### Step 3: Calculate and print the correlation matrix

`> correlation.matrix <- cor(train.blogpost)`

`> print(correlation.matrix)`

Correlation Matrix using cor() in R

#### Step 4: Detect and print highly correlated features (threshold > 60%)

`> high.corr <- findCorrelation(correlation.matrix, cutoff=0.6)`

`> print(high.corr)`

The features (parameters) printed as a result of this process, have an absolute correlation of 60% or higher, and thus should be removed before any optimization is conducted.

Additional Resource: Measuring Investments’ Risk: Value at Risk (VIDEO)
* please activate CC mode to view subtitles.

Do you have what it takes? – Join the Darwinex Trader Movement!

Darwinex – The Open Trader Exchange