Friday, November 25, 2016

Autonomous Trading: Language Wars

In my 7th blog post, I talked about a man named William Mok who created an algorithmic trading system unlike any other (or so he claimed). But today, I want to talk more about how a beginner would approach picking a platform for actual implementation. With a plethora of languages to choose like the cult classics (Python, R, Matlab, SAS) or newer languages with a lot of potential (Julia, Scala, OCaml), it can be difficult to pick a platform for both analysis and implementation. It is first important to think about what part of the trading development process we are concerned with (Data Pipeline vs. implementation/brokerage API).

Today the focus will be on the data pipeline; how are we going to bring in, clean and analyse data? Languages such as R, Python and Julia are useful to bring data for analysis. R also has many libraries specifically made for finance such as quantmod (used for data cleaning, financial forecasting, and plotting tools) or quantlib (option pricing functions).

The issue with many of these languages (R included) is its scalability. When dealing with commercial use of trading algorithms (think billion dollar quantitative hedge funds and designated financial market makers), these algorithms do not perform with the necessary latency as is the problem with many higher level languages. One reason why this is the case is due their garbage collector features (a feature many programming languages have that automatically manages memory no longer used by a program). The solution to the low latency problem would be to use C++ instead but it is by no means an easy language to learn and even harder for rapid prototype building (important when developing financial systems).

My personal favourite right now is also gaining traction as the data analysis language to use. Julia is a relatively new language (first appearing only 4 years ago) but claims to combine both the speed of C++ with the data analysis capabilities of popular languages such as R or Python. One reason why this is possible is because Julia's core is implemented in C/C++. Below is a picture of its relative performance against other languages (where C's speed is benchmarked at a value of 1) where smaller is better.


FortranJuliaPythonRMatlabOctaveMathe-maticaJavaScriptGoLuaJITJava
gcc 5.1.10.4.03.4.33.2.2R2015b4.0.010.2.0V8 3.28.71.19go1.5gsl-shell 2.3.11.8.0_45
fib0.702.1177.76533.5226.899324.35118.533.361.861.711.21
parse_int5.051.4517.0245.73802.529581.4415.026.061.205.773.35
quicksort1.311.1532.89264.544.921866.0143.232.701.292.032.60
mandel0.810.7915.3253.167.58451.815.130.661.110.671.35
pi_sum1.001.0021.999.561.00299.311.691.011.001.001.00
rand_mat_stat1.451.6617.9314.5614.5230.935.952.302.963.273.92
rand_mat_mul3.481.021.141.571.121.121.3015.071.421.162.36
Figure: benchmark times relative to C (smaller is better, C performance = 1.0).

More details regarding the specifics of these performance tests can be found in my image reference.

Writing References:
http://www.kdnuggets.com/2014/08/four-main-languages-analytics-data-mining-data-science.html

Image References:
http://julialang.org





Friday, November 18, 2016

Machine Learning: Deep Learning

Another field of research that has grown increasingly popular in ML is deep learning. It is a supervised learning technique but has unsupervised learning features. They are useful particularly for tasks where an observation or basic unit has little meaning in and of itself but a collection of these units or certain combination has very useful meaning. These come from its ability to learn from the data that is passed to it. It is inspired by the structure of a brain much like neural network, where each node or neuron has a set of inputs with specific weights and computes some function (such as linear but can be anything we want really). The network is created when we start connecting neurons to each other, the input data and the outlets (where we store the analysed information, the "answer" to our problem). Deep networks have a specific number of neurons in each layer where each subsequent layer is able to learn the best possible representation for data from the previous one. Take the example below of a deep neural network for facial recognition:


























The figure on the right shows what a simple neural network
would look like. We have the input nodes(or neutrons), a hidden layer of neurons that process the data, and finally the output nodes that essentially give us a some sort of solution.


Image Reference:
http://stats.stackexchange.com/questions/114385/what-is-the-difference-between-convolutional-neural-networks-restricted-boltzman



Writing Reference:
http://www.kdnuggets.com/2015/01/deep-learning-explanation-what-how-why.html

Friday, November 11, 2016

Machine Learning: Bayesian Networks

Last week, I introduced the topic of Machine learning and how it was possible to train algorithms to learn from a set of data. A common technique used for supervised learning is a bayesian network. Simply, a tool to analyse large sets of multivariate probability models to discern relationships between models. What is most interesting about bayesian networks is that it combines both quantitative analysis and user intuition to "learn". These may come in the form of graphical representations such as the network seen below.

Although it is not often accurate to model stock price behaviour in this way, for the sake of simplicity, let us consider the network below. Each nodes represents a random variable. The arrow pointed from stock 1 to stock 2 and stock 4 essentially mean that stock 2 and 4 are conditionally independent given stock 1. Given a movement in the price of Stock1 (increase or decrease in price for example), if the price of Stock2 moves, it does not tell us anything about how the price of Stock4 moves. It is easy to see how extending the model to only 5 stocks starts creating fairly complex networks.



In recent years, the quantitative hedge fund industry has shifted away from using traditional predictive tools in favour for adopting more complex models such as bayesian networks and other machine learning algorithms in the hope of achieving a deeper understanding of inter market relationships. They then use these patterns to trade in inefficient financial markets. The main goal of algorithmic trading is to find statistical anomalies in the data and determine profitable ways of exploiting them. It has become increasingly difficult in the past decade however, as the rise in algorithmic trading participants have removed many of these market anomalies, making many of them unprofitable.

The potential of ML algorithms such as bayesian networks in the finance industry extends much beyond this particular example. It can be recorded to detect fraud, assist in risk management and even create models to price insurance policies.

Writing References:
https://kuscholarworks.ku.edu/bitstream/handle/1808/161/CF99.pdf;jsessionid=8EE7C78BF26BD0F349409F5AEFFE91EE?sequence=1
https://www.ics.uci.edu/~rickl/courses/cs-171/cs171-lecture-slides/cs-171-17-BayesianNetworks.pdf

Image Reference:
http://www.qminitiative.org/UserFiles/files/S_Clémençon_ML.pdf

Friday, November 4, 2016

Machine learning (A brief Introduction)

Machine learning is what we call the process of algorithms to learn from large amounts of data without being explicitly told to do so (as in from a rules based framework). One of the reasons why I'l be devoting a lot of time to talking about this concept is because the field of artificial intelligence has become much more of a reality with the recent innovations in computing power like the GPU (graphics processing unit, known mainly for their ability to make parallel computing faster, more efficient and even more powerful).

In my last post, I talked about Tech Trader Fund, a trading system developed based on multiple layers of artificial intelligence (one of which was a form of machine learning). Professor Tom Mitchell's definition of machine learning best encapsulates the process:

"A computer program is said to learn from experience E with respect to some task T and some performance measure P, if its performance E on T, as measured by P, improves with experience E. "
--Tom Mitchell, Carnegie Mellon University

The two broad fields within Machine Learning are supervised (trained on pre-defined set of data) and unsupervised (program is given data and must generate relationships with little-to-no guidance) learning. Machine learning is a form of supervised learning, and its usefulness is primarily seen in analysing very large data sets (with millions of variables one would potentially want to study and see relationships between) where traditional computational methods are no longer feasible.

Some extensions of Machine learning include decision trees, induction programming, bayesian networks and reinforcement learning, which all help the system learn from the data passed to it. But these frameworks for development do have its drawbacks. Even today, it is still prone to a wide variety of errors which need a significant amount of computing power to overcome. For example, financial markets tend to be very noisy, especially on a day-to-day timescale where volatility is multiples higher than average returns. High noise environments would potentially need overly complicated models to find relationships between asset prices for example (or risk overfitting). If the model is too simple, it may just give us plain wrong or misleading results.

Next week we will dive in to how traders in financial markets can more specifically use ML algo's to create an edge for themselves in what many are calling a crowded space.

Writing References:
https://www.toptal.com/machine-learning/machine-learning-theory-an-introductory-primer

Image References:
http://www.nsightfortravel.com/wp-content/uploads/LEARNING-MACHINE.jpg



Friday, October 21, 2016

Autonomous Trading

A few weeks ago while I was at a quantitative finance conference, I had the pleasure of meeting a young man named William Mok, one of the co-founders of Tech Trader fund. At the ripe young age of 22, he had managed to develop and implement a fully autonomous trading system. What makes his system so unique compared to the thousands of other systematic trading systems in the markets is the inherent human element he claims it possesses.

A simple way to understand this is by looking at the process of walking across the street. A quantitative system would crunch thousands of numbers assessing the probability of a car crossing the street; the time of day, the temperature, the color of the grass etc... His system, Tech Trader, would simply look down the street to see there is no car, making it physically impossible to get hit. It leverages technology to do what the best traders do at scale and at maximum efficiency. But how exactly does it do this?

He claims the system contains three layers of artificial intelligence. The first, a layer of machine learning which acts like an extra pair of senses for a human like system. The second, a layer of reinforcement learning, abstracts the majority of the data and analyses it. This layer also helps the system learn from its past experiences (but only from its past experiences). This takes the form of Q-learning, a form of reinforcement learning used to find an optimal solution for a given set of decisions. The missing piece that ties the system together is the third layer. This layer helps the computer simulate human like qualities of dreaming, imagination and reflecting, storing the data like memories. It doesn't only consider what has happened but what could happen.

What I found most surprising are the stages of evolution of the system. He had told me that when the system first rolled out, it had a tendency to take a very high risk strategy approach, trading multiple times a day in and out of large positions. But as it gained data and information, it began taking a much more conservative trading approach, flattening out its overall variance over time.

The scope computer science has on finance is boundless in its limitations. As long as there are people like William Mok who are willing to continue to innovate and think outside the box, finance will continue to move forward in more exciting ways than any could even imagine.

Image References:
https://media.licdn.com/mpr/mpr/p/4/005/09c/368/3730a17.jpg



Friday, October 7, 2016

Quantum Computing & Finance

Quantum computing is a field of research that allows us to study how algorithms and systems can apply quantum phenomena to derive solutions to complex mathematic problems. It was a field pioneered by Paul Benioff and Richard Feynman in the early 1980's. What makes quantum computers so computationally powerful is its stochasticity, i.e. on its simplest level, they operate with random variables, making them much more robust when trying to efficiently compute large complex systems involving probabilistic variables. The quantum computer relies on storing qubits, the memory elements that hold a linear superposition of the possible combinations of {(0,1)}. This allows it to evaluate and store all feasible solutions (combinations) of 0 and 1 simultaneously.

The characteristics outlined above make quantum computing ideal for solving problems that require parallel processing, the use of more than a single source of computation to execute a program or threads for example. A very useful application of quantum computing in finance is scenario analysis, where the goal is to evaluate a distribution of outcomes under an extremely large number of scenarios. The beauty of quantum computers is that they don't evaluate solutions sequentially like normal computers do, they solve them by assessing the linear combinations of possible solutions (i.e. the 0's and 1's for each qubit) all at the same time. Many current approaches to the problem require far too much time, computing power or are too restrictive in their models.

Quantum computing is no longer a thing of the past. NASA, Google, Lockheed Martin and even Berkeley have obtained their very own quantum computers. The very first company to make these computers is called D-wave systems with their flagship product the D-wave machine (pictured below). These computers are kept in chambers with temperatures below 15 millikelvin. To put that into perspective, it is 180 times colder than interstellar space making it, quite literally, one of the coolest places in the universe. The reason for keeping such low temperatures is so the solution to problems given to it automatically converge to those that require the minimum energy state.


Quantum computers are the future of finance. Many models used in finance today do not map very well onto the real world (requiring a degree of complexity and robustness simple models cannot satisfy). They help us compute solutions to models cognisant of reality's complexity and free us from an over reliance of dumb-downed models and heuristics.


Image References: 
https://static01.nyt.com/images/2013/05/16/technology/16bits-sub-quantum/16bits-sub-quantum-tmagArticle.png




Friday, September 23, 2016

Anybody can be a quantitative trader!

In part 5 of my blog, I talk about a crowd sourced online quantitative investment firm called Quantopian. The website provides a platform to write investment algorithms in the Python language by providing minute by minute stock data and a development platform for both backtesting and implementation. It then uses an API with the brokerage firm Interactive Brokers to help traders on the website receive real-time quotes and implement their trading algorithms. The best part is that the website is mostly free to use.

A basic quant algorithm essentially has the following structure. A data structure used to store data passed to it (data such as stock price information), a function written that performs technical analysis (our trading ideas) on the stock data, and an output that shows what stocks the analysis says we should trade. Since the development platform can be hooked up to an API, it can also automatically be told to make certain trades that the function has told us to make. These systems are used on a wide variety of information ranging from basic stock prices, earnings, sentiment indexes, dividend announcements and even regulatory filings.


Above is a sample of a simple trading algorithm. As shown, there are many more variables to keep in mind when creating an algorithm. These things include dynamically selecting stocks based on information that is continually being passed to it through the API or creating multi-factor models that assess stocks on a wide variety of variables as I talked about in my last post.

The world of investing is becoming more and more quantitatively driven every day and as information is becoming more widely available with the advent of the internet, speed is as valuable as ever. By creating a stock trading algorithm, it allows investors to analyse a huge database of stocks based on certain criteria in seconds instead of days.

Writing & Image References:
https://www.quantopian.com/home