IBKR Quant Blog


1 2 3 4 5 2 35


Quant

The Beautiful Binomial Logistic Regression


By: Uday Keith, Byte Academy

The Logistic Regression is an important classification model to understand in all its complexity. There are a few reasons to consider it:

  1. It is faster to train than some other classification algorithms like Support Vector Machines and Random Forests.
  2. Since it is a parametric model, one can infer causal relationships between the response and explanatory variables.
  3. Since it can be viewed as a single layer neural network, it is useful to understand and gain a deeper understanding of Neural Networks.

This article seeks to clearly explain the Binomial Logistic Regression, the model used in a binary classification setting.

The Logistic Regression has been around for a long, long time and while it is not going to single-handedly win any kaggle competitions, it is still widely used in the analytics industry. So, what can be a useful point of departure to understand the Logistic Regression intuitively? I actually like to begin with the Naive Bayes model and assume a binary classification setting. In Naive Bayes, we are trying to compute the posterior probability, or in simple terms, the probability of observing that our response variable (Y) is categorized as 1, given some specific values for our variables. The solution to the problem is expressed as:

 

 

Byte Academy

 

The goal of the Binomial Logistic Regression (Logistic Regression or LR for the remainder of this article) is exactly the same, to compute the posterior probability. However, in LR we will compute this probability directly.

The response variables (Y) follows a Binomial distribution (a special case of the Bernoulli distribution) and the probability of observing a certain value of Y (0 or 1) is:

Byte Academy

Note that if yi =1 we obtain πi, and if yi =0 we obtain 1−πi. Now, we would like to have the probabilities πi depend on a vector of observed covariates xi. The simplest idea would be to let πi be a linear function of the covariates, say πi = x′iβ.

One problem with this model is that the probability πi on the left-hand- side has to be between zero and one, but the linear predictor x′iβ on the right-hand-side can take any real value, so there is no guarantee that the predicted values will be in the correct range unless complex restrictions are imposed on the coefficients.

A simple solution to this problem is to transform the probability to remove the range restrictions and model the transformation as a linear function of the covariates. Essentially what we are saying is that we want to map the output of our covariates (not necessarily including the intercept) to a continuous range which corresponds to {0-1}. We use the odds ratio to do this.

The odds ratio gives the probability of an event occurring (πi) over

 

Byte Academy

At this point it is useful to express  πi , the probability of Y as a conditional probability of P(Y|X). If we were to solve for P(Y|X) we would arrive at the following expression:

Byte Academy

Now our posterior probability has been mapped to our covariates and the form of this function is called the sigmoid curve. Here is a visual representation.

Byte Academy

As you can see, the outputs are constrained between the range (0, 1).

Now, what we seek to do is maximize the predicted probabilities as expressed above. Specifically, we will want to maximize P(Y=0|X) when we actually observe Y=0 AND maximize P(Y=1|X) when we observe a 1. Let us call this our objective function L(β). If we were to take the negative log of this expression we would arrive at

 

Byte Academy

 

If we were to plot the two Cost equations above we would see:

 

Byte Academy

 

Since we have taken the negative log likelihood, we are now minimizing our loss function as opposed to maximizing it.

To fit (find the optimum coefficients) for our model we would take the derivative of the loss function with respect to β we would arrive at:

 

Byte Academy

 

The term P here is our predicted probability P(Y|X).

 

Unfortunately, our loss function does not have a closed-form solution and we must arrive at our minimum via stochastic gradient descent. We already have the expression above that tells us now sensitive our Loss/error function is to a small change in β. So now we can plug that expression into the standard gradient update rule.

 

Byte Academy

 

Voila! Now we have everything we want. Let’s put it all together in Python now.

 

Byte Academy

 

 

Byte Academy is based in New York, USA. It offers coding education, classes in FinTech, Blockchain, DataSci, Python + Quant. If you are interested in learning Python, visit their Full Stack Python.

This article is from Byte Academy and is being posted with Byte Academy’s permission. The views expressed in this article are solely those of the author and/or Byte Academy and IB is not endorsing or recommending any investment or trading discussed in the article. This material is for information only and is not and should not be construed as an offer to sell or the solicitation of an offer to buy any security. To the extent that this material discusses general market activity, industry or sector trends or other broad-based economic or political conditions, it should not be construed as research or investment advice. To the extent that it includes references to specific securities, commodities, currencies, or other instruments, those references do not constitute a recommendation by IB to buy, sell or hold such security. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.


20854




Quant

Python Tip - Four Key Syntax Attributes to Define a Contract in IBKR API


Python
 

The ContractSamples.py sheet from our Python1 Testbed sample2 will help you learn the syntax needed to define contracts in algo trading systems. These four are considered key class attributes:
 

contract.symbol
contract.secType
contract.currency
contract.exchange


Examples 1: EUR/GBP FX contract

Examples 2: CFD contract

   @staticmethod
    def EurGbpFx():
        #! [cashcontract]
        contract = Contract()
        contract.symbol = "EUR"
        contract.secType = "CASH"
        contract.currency = "GBP"
        contract.exchange = "IDEALPRO"
        #! [cashcontract]
        return contract

    @staticmethod
     def CFD():
        #! [cfdcontract]
        contract = Contract()
        contract.symbol = "IBDE30"
        contract.secType = "CFD"
        contract.currency = "EUR"
        contract.exchange = "SMART"
        #! [cfdcontract]
        return contract




For more examples, download the ContractSamples.py from IBKR website:  
http://interactivebrokers.github.io/


We invite our avid Python users to:

  • follow us on GitHub for more advanced contract syntax techniques

    GitHub-Octocat
  • test your trading strategies with the FREE Trial Simulated account

    FREE TRIAL

 


  1. Python 3.0 version required.
  2. Copyright (C) 2016 Interactive Brokers LLC. All rights reserved.  This code is subject to the terms and conditions of the IB API Non-Commercial License or the IB API Commercial License, as applicable.



Information posted on IBKR Quant that is provided by third-parties and not by Interactive Brokers does NOT constitute a recommendation by Interactive Brokers that you should contract for the services of that third party. Third-party participants who contribute to IBKR Quant are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.


20797




Quant

Deep Learning - Artificial Neural Network Using TensorFlow In Python - Part 2


By Umesh PalaiQuantInsti

In the first article of this series, Umesh gave an overview of the Python system requirements, libraries and data set structure.

 

Scaling data & Building X,Y

Another important step in data pre-processing is to standardize the dataset. This process makes the mean of all the input features equal to zero and also converts their variance to 1. This step ensures that there is no bias while training the model due to the different scales of all input features. Skipping this part might confuse the neural network and give a higher weight to those features which have a higher average value than others. Also, most common activation functions of the network’s neurons such as tanh or sigmoid are defined on the [-1, 1] or [0, 1] interval respectively. Currently, rectified linear unit (ReLU) activations are commonly used, which are unbounded on the axis of possible activation values. However, we will scale both the inputs and targets. We will do Scaling using sklearn’s MinMaxScaler. We instantiate the variable sc with the MinMaxScalerr() function. Then, we use the fit and transform function to train and test the dataset.

Code

We then split the train and test data set into X_train, y_train and X_test, y_test. Splitting the data set selects the target and predictors from data_train and data_test. The target, which is price rise (y_train and y_test), is located in the last column of the data_train/test. The predictors are 8 features (X_train and X_test) from the 1st column to 8th column data_train/test. The whole data set will look like figure below.

Code

Code

This is an essential part of any machine learning algorithm; training data is used by the model to arrive at the weights of the model. The test dataset is used to see how the model will perform on new data fed into the model. The test dataset also has the actual value for the output, which helps us understand how efficient the model is. Now that the datasets are ready, we may proceed with building the Artificial Neural Network using the TensorFlow library.
 

Building The Artificial Neural Network

Input, Hidden & Output Layers

The model consists of three major building blocks. The input layer, hidden layer and output layer. The input layer is the 8 features. After the input layers are 3 hidden layers. The first hidden layer contains 512 neurons. Subsequent hidden layers are always half the size of the previous layer, which means the 2nd hidden layers contains 256 and finally 3rd one 128 neurons. After 3 hidden layers, there is an output layer.  The net is creating

Code
 

Placeholders & Variables

The Artificial Neural Network starts with placeholders. We need two placeholders to fit our model: X contains the network’s inputs (features of the stock at time T = t) and Y contains the network’s output (movement of the stock at T+1). The shape of the placeholders correspond to None, and n_features, with None meaning that the inputs are a 2-dimensional matrix and the outputs are a 1-dimensional vector. It is crucial to understand which input and output dimensions the neural net needs to design it properly. The none argument indicates that we do not yet know the number of observations that flow through the neural net graph in each batch, so we keep if flexible. We will later define the variable batch size that controls the number of observations per training batch.

Code

Besides placeholders, variables are another cornerstone of the TensorFlow universe. While placeholders are used to store input and target data in the graph, variables are used as flexible containers within the graph that are allowed to change during graph execution. Weights and Biases are represented as variables in order to adapt during training. Variables need to be initialized, prior to model training.

Initializers

Initializers are used to initialize the network’s variables before training. Since neural networks are trained using numerical optimization techniques, the starting point of the optimization problem is one the key factors to find good solutions to the underlying problem. There are different initializers available in Tensor flow, each with different initialization approaches. Here, we will use which is one of the default initialization strategies the tf.variance_scaling_initializer() for two variables weight & bias.

Code

Designing the network architecture

In designing the network architecture, 1st we need to understand the required variable dimensions between input, hidden and output layers. In case of a multilayer perceptron (MLP), the network type we use here is: the second dimension of the previous layer is the first dimension in the current layer for weight matrices. It means each layer is passing its output as an input to the next layer. The biases dimension equals the second dimension of the current layer’s weight matrix, which corresponds the number of neurons in this layer.

Code

After defining the required weight and bias variables and network topology, the architecture of the network needs to be specified. Placeholders (data) and variables (weights and biases) need to be combined into a system of sequential matrix multiplications. Furthermore, the hidden layers of the network are transformed by activation functions. Activation functions are important elements of the network architecture because they introduce non-linearity to the system. There are many possible activation functions out there, and one of the most common is the rectified linear unit (ReLU), which we are using in this model.

Code

 

In the next article, Umesh will show us how to use cost function to optimize the model.

 

Disclaimer: All investments and trading in the stock market involve risk. Any decisions to place trades in the financial markets, including trading in stock or options or other financial instruments is a personal decision that should only be made after thorough research, including a personal risk and financial assessment and the engagement of professional assistance to the extent you believe necessary. The trading strategies or related information mentioned in this article is for informational purposes only.

If you want to learn more about  Decision Tree and Neural Network methods in trading strategies, or to download the code in this article, visit QuantInsti website and the educational offerings at their Executive Programme in Algorithmic Trading (EPAT™).

This article is from QuantInsti and is being posted with QuantInsti’s permission. The views expressed in this article are solely those of the author and/or QuantInsti and IB is not endorsing or recommending any investment or trading discussed in the article. This material is for information only and is not and should not be construed as an offer to sell or the solicitation of an offer to buy any security. To the extent that this material discusses general market activity, industry or sector trends or other broad-based economic or political conditions, it should not be construed as research or investment advice. To the extent that it includes references to specific securities, commodities, currencies, or other instruments, those references do not constitute a recommendation by IB to buy, sell or hold such security. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.


20789




Quant

Join IBKR for a Free Webinar with qplum - Modeling of Risk in Quantitative Portfolios


Thursday, November 29, 2018 at 12:00 PM EST
 

Register


qplum - Modeling of Risk in Quantitative Portfolios


While “standard deviation” of returns is the most common definition of risk, it is not as well-defined as one might think. Many portfolio managers attempt to quantify risk using other measures like drawdown, max loss, etc., which have intuitive appeal to clients but we believe lacks common standards. Investors typically feel more pain when markets go down in comparison to the satisfaction when markets are going up. Which risk measure would be able to capture this utility function of the investor most fairly?

In this webinar, we will discuss various ways to capture risk, especially how to account for the asymmetrical behavior on the downside. We will also link it to the practical questions, like, “What causes a client to change managers and if is there a difference of opinion between retail vs institutional clients when it comes to perception and modeling of risk?”.

 

Speaker: Anshul Sirohiya, Quantitative Portfolio Manager at qplum

 

Sponsored by:  qplum

 

Information posted on IBKR Quant that is provided by third-parties and not by Interactive Brokers does NOT constitute a recommendation by Interactive Brokers that you should contract for the services of that third party. Third-party participants who contribute to IBKR Quant are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results


20790




Quant

Summing up the Potential Benefits and Pitfalls of Diversification in 3 Slides


The post  Summing up the Potential Benefits and Pitfalls of Diversification in 3 Slides appeared first on Alpha Architect Blog https://alphaarchitect.com/blog/

By Wesley Gray, PhD | October 4th, 2018

 

Not long ago I used to teach investment management courses to Master’s students (MBAs and MS Finance types).

A core aspect of my course was so-called modern portfolio theory. We did a lot of math and problem-sets to make it feel like we were doing something useful.

But I can summarize the core muscle movements of portfolio theory in 3 slides.

 

Slide 1: The Math Works.

Why do academics love diversification? Well, they love the math. Let’s explain…

Let’s say you walk up to someone on the street and say the following:

 

“Hey, I have 1000 assets for you: Each assets has a 10% expected return, but these assets are insanely volatile.

Also, all these bets are structurally uncorrelated with anything in the world.”

 

That sounds OK in theory, but do who wants to own a bunch of incredible risky assets? Nobody!

But wait, what if someone with a PhD told you that this offering has no risk and is essentially a guaranteed 10% return!

Say what? How can a bunch of “insanely volatile” assets create a guaranteed 10% return?

Well, math can help explain how this works. An old course slide…

Wes-Gray-AlphaArchitect

Source: Wes old course slide

 

Pooling uncorrelated bets lead to no risk as the number of bets goes to infinity.

Many of us in finance now generally understand this concept (even if we don’t know the math). Of course, some smart people have gotten in trouble for relying on this concept. But before we get to the “why” on how blindly following the diversification math can get you in trouble, we’ll attempt to make the concept even clearer via a picture.
 

Slide 2: Diversification Works…

Let’s “prove” the math with a picture.

We’ll have our spreadsheet conducts a 1000 simulations. In each simulation run, a portfolio system can do the following:

  • Take 1 bet
  • Take 10 bets
  • Take 100 bets

Some bet parameters:

  • All bets are uncorrelated.(1)
  • Each bet has a 10% mean and a 20% standard deviation (roughly equivalent to owning the S&P 500 stock market bet).

We then plot the distribution of outcomes for these 1,000 simulations to highlight the possible return outcomes for the 3 different portfolio systems.

In the chart below, black represents the outcome distribution for a 1,000 simulations where the portfolio holds 1 bet, blue reflects the 10 bet system, and green reflects the 100 bet system.

Wes-Gray-AlphaArchitect

Source: Wes old course slide

 

As the math above suggests, one can eliminate risk by pooling uncorrelated bets together. The single bet portfolio has a huge distribution, whereas the 100 bet system (green) is almost centered perfectly around 10%. If we had a 10,000 bet system you’d start to see a large spike around the 10% mark. Long story short, more uncorrelated 10% bets mean less volatility, but the same 10% expected return.
 

Slide 3: Diversification Works…Until it Doesn’t

The math is beautiful, but there are a lot of embedded assumptions in portfolio theory. Specifically the assumption that correlations are not regime-dependent. (See Elisabetta’s piece on the topic)

But what happens if we change the simulation design from above and include a 10% possibility of a “bad state of the world” where correlations go from 0 across all bets to 80%. Think about almost every major meltdown the world has witnessed.

Well, in this world, many of the so-called benefits of diversification go out the window…Note the extreme right tail outcome distribution is just as big for a single bet portfolio or a 30 bet portfolio.

Wes-Gray-AlphaArchitect

 

 

Relying on diversification without considering the possibility of correlated fat left tails is a sneaky situation. Diversification, which is supposed to help an investor, ends up hurting the investor by “diversifying” away all their upside potential while leaving them with a steaming pile of downside risk when the world blows up. Yuck!

Bottom Line?

The world is not a simple bell curve. The world cannot be modeled via expected returns and covariance matrices. The world is too dynamic, driven by sentiment, and fraught with integrated funding sources (i.e., leverage channels). The only thing we can say is things that have never happened seem to happen all the time.

Diversification, like all concepts in investing, is simple, but not easy. Thinking is still required.

  • The views and opinions expressed herein are those of the author and do not necessarily reflect the views of Alpha Architect, its affiliates or its employees. Our full disclosures are available here. Definitions of common statistics used in our analysis are available here (towards the bottom).
  • Join thousands of other readers and subscribe to our blog.
  • This site provides NO information on our value ETFs or our momentum ETFs. Please refer to this site.

 

 

Visit Alpha Architect Blog to read more articles on quant finance. 

 

Alpha Architect empowers investors through education. The company designs affordable active management strategies for Exchange-Traded Funds and Separately Managed Accounts. Visit their website to learn more: https://alphaarchitect.com

This article is from Alpha Architect Blog and is being posted with Alpha Architect’s permission. The views expressed in this article are solely those of the author and/or Alpha Architect and IB is not endorsing or recommending any investment or trading discussed in the article. This material is for information only and is not and should not be construed as an offer to sell or the solicitation of an offer to buy any security. To the extent that this material discusses general market activity, industry or sector trends or other broad-based economic or political conditions, it should not be construed as research or investment advice. To the extent that it includes references to specific securities, commodities, currencies, or other instruments, those references do not constitute a recommendation by IB to buy, sell or hold such security. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.


20765




1 2 3 4 5 2 35

Disclosures

We appreciate your feedback. If you have any questions or comments about IBKR Quant Blog please contact ibkrquant@ibkr.com.

The material (including articles and commentary) provided on IBKR Quant Blog is offered for informational purposes only. The posted material is NOT a recommendation by Interactive Brokers (IB) that you or your clients should contract for the services of or invest with any of the independent advisors or hedge funds or others who may post on IBKR Quant Blog or invest with any advisors or hedge funds. The advisors, hedge funds and other analysts who may post on IBKR Quant Blog are independent of IB and IB does not make any representations or warranties concerning the past or future performance of these advisors, hedge funds and others or the accuracy of the information they provide. Interactive Brokers does not conduct a "suitability review" to make sure the trading of any advisor or hedge fund or other party is suitable for you.

Securities or other financial instruments mentioned in the material posted are not suitable for all investors. The material posted does not take into account your particular investment objectives, financial situations or needs and is not intended as a recommendation to you of any particular securities, financial instruments or strategies. Before making any investment or trade, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice. Past performance is no guarantee of future results.

Any information provided by third parties has been obtained from sources believed to be reliable and accurate; however, IB does not warrant its accuracy and assumes no responsibility for any errors or omissions.

Any information posted by employees of IB or an affiliated company is based upon information that is believed to be reliable. However, neither IB nor its affiliates warrant its completeness, accuracy or adequacy. IB does not make any representations or warranties concerning the past or future performance of any financial instrument. By posting material on IB Quant Blog, IB is not representing that any particular financial instrument or trading strategy is appropriate for you.