Antidilutive in EPS calculation

EPS (earnings per share) is a very important ratio in income statement, it is calculated as earnings (net income) attributed to common shareholders divide by common shares outstanding. Actually, it is so important that it is required to include EPS on the face of the income statement.


As you can see, they not only show the EPS, there is also another line right below it which is the diluted EPS. The reason that diluted EPS need to be disclosed to the public is that there are different kinds of equity like preferred stock, convertible stock that has the potential of “diluting” the EPS. How big a difference is can be? Usually it is pretty small, like for Walmart, the earnings per share is only 0.01 but in some cases, the difference can be material enough that investor want to know the potential downside.

Say for example, convertible stock sometimes got paid dividend and can also be converted to certain amount of shares. If not convert, that is the simple calculation for EPS, however, the diluted EPS to evaluate if all the convertible stocks got redeemed into common stock, on one hand, the net income will increase because the earnings that used to go to dividend now can be retained, on the other hand, the number of outstanding common shares also increased due to the conversion. In this case, there is a scenario where the diluted EPS if converted can actually be higher than the basic EPS, if this happens, the diluted EPS sort of loses its meaning of providing a good projection of the potential downside. Both IFRS and GAAP require that this kind of EPS – Antidilutive Security be excluded from the diluted EPS calculation.

Now, let’s do some simple calculation and see under what situation Antidilutive security could exist.

Say a company’s net income is I, number of common shares outstanding is C and number of preferred stock is P. The term for preferred stock is that the annual dividend paid per share is D and it can also be converted to X amount of common stock if wanted.

Basic EPS = (I – P * D) / C

Diluted EPS = I / (C + P * X)

The constrain is that Basic EPS >= Diluted EPS

(I – P * D) / C >= I / (C + P * X)

After a bit transform, we got: X*I – D*C – P * X * D >= 0

I like to rearrange it into the following format:

D <= I / (P + C / X)

This is easy to interpret, P+C/X can be interpreted as if all shared got converted into preferred shares. If the dividend is smaller than if all converted to preferred stocks, then it is dilutive. If not, then it is anti-dilutive which should be excluded. So in this case you can see, if the dividend for the preferred stock is too high, or the conversion X is too small, it is highly likely that the constrain will not hold and it will be anti-dilutive. Also, if the number of preferred stock is substantial, this will also become anti-dilutive.





Shark Tank Valuation

There is this fantastic TV show Shark Tank from ABC, where startups come to pitch their business to a group of seasoned businessmen/women. The “sharks” will decide if they should invest their own money in those startups, hence, their motivation is to seek for high return and the questions they asked are usually challenging. I have always been asking myself how can the sharks invest that much amount of money, usually hundreds of thousands of money for partial ownership – equity within just a few minutes, and one step further, how can they even tell how much a business worth in the first place? Sometimes, they call out the entrepreneurs “crazy” valuation and sometimes they take the offer the way it is, and sometimes even go beyond what was originally asked.


After watch it for a few seasons, you started to see patterns where they pretty much want to understand their income from revenue, margin (in order to calculate gross profit) and net earnings (take home). Relatively speaking, not that much questions about balance sheet or cashflow. Clearly, their valuation are based on those key financial metrics, but the question is how do they go from those metrics to how much a company worth? Thanks to some of the heated conversations every now and then, the sharks will make similar comments like “you are asking for a valuation X multiple of your earnings, you know that will never happen in Y industry, right?”.

Then you have been told the secret, the next question will be WHY a company’s worth is a multiple their earnings and why that ratio is different across industries?

To determine how much anything worth, there are usually several ways, what is the cost, what is the worth of similar product that have been sold recently, and if you ask someone in finance, they probably will threw you off by saying “the current worth of an asset is the sum of all its future cashflows discounted to today”.

Lots of buzz words, right, “cashflow”, “discount”, “asset”..etc. Do not worry, everything should be pretty straightforward after we go through a every day example.

First is to make sure that we are all on the same page that “one dollar tomorrow is not the same as one dollar today”. This is called the time value of money and can be explained using your interest paying saving account. Say you have a very good bank with an interest rate of 10%. There are lots of details go into how actual interest (yield) could different based on when interest are paid and how often are paid, let’s put it on the side and assume $10 is paid the end of the year if you put $100 at the beginning of the year. The you will have $110 at the beginning of next year and 10%*110 = $11 worth of interest will be paid end of the next year, interest will get higher and higher thanks to the interest built on top of earned interest, everyone favorite compounding. So your $100 dollar today look like “worth more” in the future.

Year 0: $100
Year 1: $110
Year 2: $121

Year N: $100*(1+10%)^N

However, in nature, they are same because you take no risk in this investment since it is the bank (I did not go through 2008 so bear with my naiveness here), you do not have to do any work other than providing capital, last, this kind of investment is open to pretty much everyone. Therefore, any reasonable investor shall “take this for granted” if they decide to park their money somewhere else.

If someone is going to ask you to invest or borrow your money, you should expect they pay you back higher than what they borrow you today, or vice versa, you should expect to lend out lower if they promise to pay you back a fixed amount in the future.

From another perspective, $100 next year will equal to $91 today, the same as $110 to $100 in the previous example; $100 two years will equal to $83 today, the same as $121 to $100. Now, we are ready to look at how a company will generate money for its investors.

A company will bear the goal of making profit, making profit to keep its day to day operations (if not, they will not live long), sometimes even to have some profit after paying for all kinds of expenses which is called the net income / earnings. Earnings sometime are reinvested back to the business to scale up its success and sometimes those extra money will be given back to its investors. Of course, depending on the stage of the company and its potential based on various reasons (industry, business model, environment, ..etc), investors will expect different use of those earnings, that is also why some companies pay dividend up to 10% and some tech companies pay literally no dividend. Let’s say the business that go to shark tank is already operating, has already attracted a fixed group of the customer with returning business, the revenue pay for everything with extra. If I am the owner of this business, we can start by assuming all earnings go to our pocket. Let’s even fixed everything else assuming that from now on, they company will make same amount of earnings ($100K) every year for the future. In that case, it is a pretty good way of collecting cash in the future. Using the idea we mentioned, let’s try to convert all those future annal net income to today’s worth. Shall we use the interest rate above? I will say yes for now, for in reality it is probably not this simple, the risk of running a small business is high, you probably will need to put into effort, so you probably should expect an return rate higher than bank interest.

Year1 -> today: $100K / (1+10%)
Year2 -> today: $100K / (1+10%)^2
Year3 -> today: $100K / (1+10%)^3

YearN -> today: $100K / (1+10%)^N

In order to give away this cashcow to you, the entrepreneur probably will trade you fairly at the price of receiving the same amount of the money in the future.

So sum will be a very simple geometric series where the sum can be represented in the following equation:

valuation = $100K/(1+10%) * (1-q^n) / (1-q) where q=1/1.1=1/(1+rate)

Now that we know the magical multiple is pretty much

valution/earning = (1+r)(1-1/(1+r)^n)/(1-1/(1+r))

I plot out a heatmap based on “multiple” using the number of years and IRR (internal rate of return). The interesting pattern is that as the rate increases, the multiple caps at certain value, the higher the rate, the sooner it flattens and the lower its cap is.

Screen Shot 2018-09-16 at 10.48.44 PM

I also expanded the limits of each variable from 1 to 100 and here is the bird view.

Screen Shot 2018-09-16 at 10.50.29 PM.png

As an investor, I might ask myself the question of how long this business might keep making money for me (dividend or capital gain due to reinvested earnings), what will be my required rate that I will expect taking the market interest rate and the premium that I want to add on top of that.

Screen Shot 2018-09-16 at 10.56.45 PM

One can easily tell that the bank rate for investors is usually the big index funds and S&P 500 has already achieved an annual return of ~ 11%. If we double that benchmark to 22% as the required rate of return. Then we can see that by holding the company forever will earn no more than 7 times the original investment. Again, the power of compounding, $100 in 7 years will only worth $25, $5 in 15 years and roughly $1 in 23 years.

As you can see, if your business is attractive enough that your investor believe whatever you do will keep existing forever, also, if you can convince your investors that your business is less risky and at the same time, there is no good equivalent on the open market for investors (low interest), then you will position yourself to the top right of the chart. Of course, this article is based on a strong assumption that the earnings is fixed which it is never, it either can operate only for a few years (less than half of the small business survive the first 5 years), or the earnings could be volatile upwards and downwards for various reasons.

But in the end, hopefully this article is helpful to understand how a company worth from the most basic perspective and what are some of the factors investors and entrepreneurs need to pay attention to fairly evaluate and determine a company’s worth.

Part words from Shark Tank again “in sales, we trust”


  1. SharkTank from ABC, screenshot, comments, copyright reserved to the original author.
  2. Yahoo Finance

Degree of * Leverage

Within Corporate Finance, there is a focus area where management optimize their cost structure. There are several key measurement to identify business risk under the name of leverage. They use the term elasticity of one percent change in one variable with regard to unit percentage change in another variable. The most commonly used degree of operating leverage and degree of financial leverage.

Attached is a screenshot of how I derived the equation based on the definition.

Screen Shot 2018-08-19 at 7.54.02 PM

In the end, the term degree of total leverage is defined as %change in net income over %change in units sold. Once should reach to the conclusion easily that DTL = DOL * DFL

Return-Generating Models: The market Model Beta

R_i = alpha_i + beta_i * R_m + e_i

where the R_m is the return of the market. You can simply get the monthly return for a given stock and the S&P 500 as the market. Run a linear regression and the slope will be your beta.


Based on this post from Quantitative Finance from Stackexchange, the Beta calculated was based on the monthly return for the past three years. Comparing with our calculation in Python, the number lines up pretty well.

Note: I was using the close price, when using the open price, it was pretty close too. However, the beta calculated using high and low is pretty different from Yahoo Finance.



Portfolio Risk – Diversification and Correlation

This article is a visualization of a portfolio of two assets where we see how portfolio return and risk changes as the weight changes and the correlation between the two underlying assets changes.


This is a very typical risk return plot where the horizontal axis represent the risk – standard deviation of the portfolio where the vertical axis represent the expected return of the portfolio. All the data points are color coded by the correlation between the two assets (eg. purple +1, 100% positive correlated). It might not be that obvious, but I set the size of each data point to be the weight for asset 1 where the biggest represent all the capital is allocated to the asset one, and vice versa.

Here is a screenshot of the R code to generate the graph above.


The key takeaway is that given same expected return, you want to diversify your portfolio in a way where underlying assets are more negatively correlated to each other to reduce the risk. This might sounds magical but the math did serve the purpose of proof. However, it is easier to say than doing, if you have an asset that has a good return, but might correlate positively which you will like to replace, in real life, it will be hard to find some alternative with the same return because if a stock is negatively correlated to what you already have, it probably has a subpar return rate (hedging is another thing if that is what you want to achieve).

Imagine – OMAX Binocular Compound Microscope

Just bought a microscope from Omax and this is a link to that model.

I do not have any professional biomedic education other than the one classes taken in high school and that was a moment where several students share a microscope so did not spend any quality time at all, I guess. Now I have my own 🙂

This model support a 1.3MP Digital camera and you only need to use a regular USB cable to connect it to your computer and you are good to go. Based on the description, the software functionality is highly limited on the MacOS platform but for a newbie like me, it is sufficient! The microscope does not come with any slide so I ordered some preprocessed slide from Amscope, now everything is ready, let’s show you a few photos that I have taken.

The first photo is a bee leg and second is actually a piece of napkin, the following are the pine leaf under different resolution.

Image recognition has already been very helpful in facial recognition, object detection and recognition in order to enable cool things like auto tagging, auto driving cars, .etc however, there is also applications at the macro and micro cell. Here is an interesting post from Google explaining how dermatologist use deep learning to detect skin cancer.

ML Demo – Drag&Drop


When I started my career, two things that I kind of shy away from are Excel and Tableau. It is a shame that how much extra effort that I have put into exploring options in Python and R by writing tens of lines of code to implement a pivot in Excel. However, on the bright side, you did become more proficient with the programming method and probably had a much deeper understanding of the underlying implementation of “pivot” because you know “reshape” could be much more powerful and predictable. The con is also very clear, now very few people can use your work. Only the coders now, and what is even worse, many coders do not read/use other people code and prefer do it themselves :). Given that, sometime you might think it is probably a good idea to put a user interface on your hardcore but beautiful applications because it is reusable, and now much accessible to a broader audience. In a series of blog posts, I am trying to build a machine learning demo that people can build a “hidden rule”, a “secret”, a “customized pattern” that we are trying to reveal by using machine learning. A key first step is how to help users easily build some rule. You can definitely ask the user to enter some rule like “if attribute_1 > 1000 then …”. However, you might be surprised at how hard it will be even to let average human being to enter that logic into computer (talk to your grandparents and give it a try). A good starting point is probably to have some drag and drop.

I am by no means a front-end developer, in this blog post, I am trying to explore some options and equip myself with some basic knowledge of the existing options out there. If I can build some drag and drop user friendly solution to build a if statement, I will declare a victory. So let’s get started.

0. Requirements

1. HTML5 Drag&Drop


A quick Google search says drag and drop is a built-in attribute in HTML5. Here is an example from W3schools showing how you can drag and drop an image in/out of an empty box.

There are a few key attributes related to drag and drop we need to highlight:


  1. draggable=”true”: the object need to be configured in order to move it
  2. ondragSTART=”drag(event)”: ev.dataTransfer.setData(, )


  1. ondragOVER=”allowDrop(event)”: preventDefault() will now show a green plus sign indicating that you can drop now
  2. ondrop=”drop(event)”: what do you expect to happen on the drop (mouse release). preventDefault() + ev.dataTransfer.getData() + will add certain content to the target object.

The example from W3Schools is very helpful, I also added a few more boxes with attributes configured to be different value so that you can understand how the objects will react accordingly. You can see my modified version here.

MDN Web Docs

At the same time, there is a much more comprehensive documentation about everything drag and drop from MDN Web Docs. They introduced the attributes above in further depth and at the same time, mentioned many other concepts like define the drag image, effect, etc.

They also included a few demos here and the one that I like the most is the parking demo, a game where you can park certain car during certain time by dragging and dropping. You can find the source code here.

2. Existing Library

jQuery Query Builder looks like a very promising library that a non-technical user will be able to build pretty sophisticated logic.

You can with with Query Builder in jsfillder by clicking here.

Screen Shot 2018-05-19 at 11.59.27 AM


Feature Construction for LM – Polynomials

In Sutton’s book “Reinforcement Learning – An introduction” draft 2017 Nov Chapter 9.5.1. The Author discussed a scenario where one can construct features for a linear model using “interactions” between different dimensions of a state.

Screen Shot 2018-02-10 at 10.43.40 AM

It is a very short math equation but not quite straightforward to fully comprehend. Let’s raise a few examples to help understand this equation more intuitively and hopefully we can understand why there are (n+1)^k different features.

Let’s assume that we have three dimensions in the state space, like the physical spatial position of an object. In this case, k=3. Let’s assign different values to n starting from value 0 and see how that math equation unfolds as we grown n.

Screen Shot 2018-02-10 at 11.00.40 AM



Sympy – Solver

Want to solve a equation using Python, Sympy solver is an easy solution. (I wish twenty years ago I had access to this library so I might do a better job for my school homework)

Screen Shot 2018-01-13 at 6.17.25 AM.png

I am taking a course fundamentals of supply chain from MIT on EDX, there is a small math equation at the beginning of the course when you need to calculate the coefficients for a SKU distribution following power law. To be more specific, the question assumes the distribution follows the power law y = a * x^b where y denotes the percentage of the  items sold and x denotes the number of products sold. At a high level, this is a law behind a few popular products encompass majority of your sales.

Consider the example of a store where 5% of the products account for 66.6% of the items sold, and 50% of the products account for 95% of the items sold. – Course

In this case, we substitute x and y with the real store data, we have

0.666 = a * (0.05)^b
0.95 = a * (0.5)^b

Two variables, two independent equations. You can solve this equation by cancelling any unknown variable. Say let’s get started with cancelling variable a first. We have:

(0.05)^b / 0.666 = (0.5)^b / 0.95

Then we take the natural logarithm of each side, we have:

b * ln(0.05) - ln(0.666) = b * ln(0.5) - ln(0.95)

Finally we got,

b = (ln(0.95) -ln(0.666)) / (ln(0.5) -ln(0.05)) = 0.154

a = 0.95 / (0.5^0.154) = 1.06

Double check, a * (0.05)^b = 1.06 * (0.05)^0.154 = 0.668 ~ 0.666 (pretty close, probably because of the rounding which tends to get amplified in non-linear equations).

Well, as you can see, this equation is not the end of the world but it took me about 10 minutes to write down the equation and punch many buttons on my calculator to get the intermediate steps and what if I just want to get the result.

Here is how you can use Solver to get the result in a faster, more consistent and accurate way:

Screen Shot 2018-01-13 at 6.50.45 AM

Here we go, 5 lines of code, do I really need to explain what happened? I do not think so, right? 🙂

Sympy seems like to have way more functionalities other than just solving elementary school algebraic problems, it also claims to cover ordinary differential equations, partial differential equations, system of polynomial equations and more than that.

(BTW, if you just want to quickly get your hands dirty, Sympy has a live code shell where you can test out the example code or your own problem)


RL Trading – code study of sl-quant

There is this very interesting post from Hackernoon where the author built a self-learning trading bot that will learn and act accordingly to maximize the reward. The post is very fun since demonstrated the learning capability under a few naive models. On the other hand, it is actually the underlying implementation that intrigued me the most which I decided write this blog and go through the notebook provided by Daniel and learn more what is happening under the hood.


Screen Shot 2018-01-08 at 10.33.37 PM

The get_reward function is probably one of the most important steps in designing a reinforcement learning system. In an trading example, it can be somehow straightforward at first glance because people can simply use the financial position ( like P&L) as the measurement, actually it is probably the one method many people will agree and adopt.

We can start by first looking into the terminal_state == 1 part of the code, which indicates that is the last step of the process. In this case, the author simply call the Backtest function straight out of box by passing in pricing information and the signal information, in this case, each element of xdata stores the state which contains the current price and the difference comparing with the previous day. Hence, [x[0] for x in xdata] is a list of all the prices. In this case, you can grab the P&L data for the last day and you are good to good. (click here to learn more about the backtest functionality within twp)

The most interesting part is actually by looking into how the intermediate steps rewards are calculated. Within the terminal_state = 0, there are two if statements all based on the value of signal[timestep] and signal[timestep-1]. Signal is “Series with capital to invest (long+,short-) or number of shares”. In that case, signal[‘timestep’] and signal[timestep-1] is the capital to invest for the current and previous step. The interesting part if the both of them are equal to 0, basically means nothing to invest, the author actually deduct 10 points from the reward variable, I think this is to penalize the activity of doing nothing probably. Then, the step of where the signal for today and yesterday are different, this is probably the most challenging part of this reward function.

It built a while loop and go backwards in time to compare the two consecutive days of signal until they are equal. Actually, if the previous step and the one before it happen to have no change. Then i = 1 and it jumped out of the loop and stays at 1. However, if this is a very active investor and every day is different. i could increase to be as large as timestep – 1.  The code screenshot got cut off and here is the complete code for line 94.

Screen Shot 2018-01-08 at 11.01.48 PM

Here I am having a hard time understanding how this reward function is established. I can understand the price difference part. And multiply by the number of shares gives you basically the profit, or loss if there is a price drop. At the beginning, I thought the author must really hate making money because he multiplied that profit by a negative 100. Later on, I realized that the signal is interesting, it could be a positive number which means the investor is in a long position of owning certain stocks and a negative number indicates the investor is in a short position cashing out his stocks. So if this person is selling, then signal is negative, and if the price increased, this actually ended up a big positive boost to the reward. +price * -share * – 100 = +100 P&L. Last but not least, he also added a component where he multiply the number of shares he owns yesterday by the number of days that he hold and divide by 10. This is also a positive number and grows linear as the number of days he holds this position (i) and by the absolute value of his position. Bigger the deal is, and this will lead to a bigger reward. In this way, how this second component in the reward function is structured probably will boost the performance of holding a big amount of stocks for a long time. This is interesting because the reward function is crafted in such a way that encompass several key data points but in the end, it will collapse back to the P&L, I am wondering why he did not use P&L just across the who journey.

All things said, this is actually one way of how reward function got implemented.


Screen Shot 2018-01-08 at 11.29.39 PM

So here, the inputs to the evaluate_Q function are a trained prediction model – eval_model and a eval_data, which is a list containing a time series of pricing information.

First, the variable got initialized into an empty panda series. and then the pricing info got to be used to initiate the xdata (a time series of all the states) in which variable state got initialized to be the first state. Next comes the grand while loop that will not terminate until the end. Each loop represents a time step, a day, a minute, or a second given how your time step is defined. At the beginning of the loop, the model will be used to predict the Q value for the given state. This will generate a value for each action taken under that state. In this case, we have only 3 different actions, buy, sell or hold. In this case, the eval_model.predict is supposed to generate a score Q value for each of those action taken. Whichever has the highest score will be deemed as the best action and be acted upon. Given the action taken, this will bring the process into the next state – new_state, time_step will increase by one and the signal variable will also be updated within the take_action function. In this case, the signal variable keep appending and appending until the end of the simulation. The eval_reward is actually calculated at every step and actually only eval_reward in the last loop got used.  In my point of view, maybe the author should move that part of the code outside the while loop to improve some efficiency.


Screen Shot 2018-01-08 at 11.50.40 PM

This is where the magic happens. First, it is the signal got initialized to be an empty panda series. Then it goes into a for loop, and the number of the loops is depending on how many epochs the user wants this model to run. Within each epoch, all the key variables got reinitiated but seems like signal variable got to escape from this process.

Within each epoch, there is this while loop looping through each state. This part of the code is actually fairly similar to evaluate_Q, you will see as we read more. First the model is being used to predict the Q value, however, before the Q value got used to pick the next optimized action. There is a random factor for exploration where there is chance that the next action will be randomly chosen to avoid local optimal or overfitting. Otherwise, actually most of the cases assuming you picked up a fairly small epsilon, the next action will be based on the estimation of the Q value. After the action being taken, all the key variables should be updated and we will land in a new state, then the new state will be used to predict the next Q value and now we can calculate the update to be the reward for the current step plus the best estimation for the next step at that time. Then this cycle will be fed to the prediction model and further enhance its capability to predicting the Q value for a given state and the right action to take.

Screen Shot 2018-01-09 at 12.06.21 AM

Again, I just want to highlight that how the recursion happened here. Probably a whole article should be contributed here to explain the mathematical reason behind how the model is updated and why the update is the reward with an attenuated future Q value. If you want something quickly and dirty, this stackoverflow is help explaining the difference between value iteration and policy iteration from the implementation perspective.

This is basically it, a more detailed explanation of the source code behind the interesting post about self learning quant.