Using Facebook’s Prophet to Predict Mongolian Stocks

Forecasting the Mongolian Stock Exchange

Robert Ritz
May 5, 2018 · 7 min read

Disclaimer: In this post I will be making several forward looking predictions of stocks listed on the Mongolian Stock Exchange. These predictions are for information purposes only and should not be the basis for any financial decisions (positive or negative). I’m not a financial adviser and not a licensed broker of securities in Mongolia or anywhere else. In short, if you make financial decisions based off of this post you do so at your own (very high) risk. The purpose of this article is to have a bit of fun and show what Facebook’s Prophet can do.

In 2017 Facebook released a paper describing a way to make forecasts at the scale Facebook does (huge). The idea is that analysts need a way to make forecasts quickly and reliably while still maintaining the ability to add value with their experience. The outcome of this is Prophet. In the words of Facebook themselves,

“Prophet is a procedure for forecasting time series data. It is based on an additive model where non-linear trends are fit with yearly and weekly seasonality, plus holidays. It works best with daily periodicity data with at least one year of historical data. Prophet is robust to missing data, shifts in the trend, and large outliers.”

With my usual habit of applying data science and analysis tools to Mongolian data, I thought it would be interesting to try to predict 10 listed stocks on the Mongolian Stock Exchange (MSE) Top 20 index. These were chosen arbitrarily. These are:

Source: Mongolian Stock Exchange

The Python Function

Our first step is to import our libraries. I also set the chart style here to be used later (I really like the FiveThirtyEight inspired style).

Next I create the custom function that will be used to make our predictions.

This function takes two arguments, 1) the name (and path if needed) of the CSV file to be imported, and 2) the time periods forward to make predictions. The CSV file should have one column named ‘date’ and one named ‘value’. All of the data files download from the MSE website like this.

This function does several things:

  1. Imports the CSV file named.
  2. Renames the columns of our CSV to ‘ds’ and ‘y’, which is required by Prophet.
  3. Fits the prophet model on our dataframe and also adds confidence intervals.
  4. Makes future predictions for N periods forward.
  5. Removes weekend forecasts as our source data doesn’t have this and it causes incorrect predictions.
  6. Plots the time series with fitted model and future predictions.
  7. Plots separate trend graphs, overall, day of the year, and day of the week.

All of this is called and executed with one line. For example, if I want to use Prophet to forecast the following year for the Top 20 Index of the MSE I write:

This returns the following two graphs:

Top 20 Index — Output of Prophet Function

The graph on the left shows our source data (black dots), the fit trend line (solid blue line), and the confidence interval (shaded blue area). It projects out from the last data point for 365 intervals going forward (1 year).

The graph on the right shows the overall trend (with confidence interval for the prediction shaded in blue), the yearly trend (the time of year the data trends up or down), and the day of the week trend.

Our prediction here is quite optimistic (probably too much so), but it certainly follows the trend.

Forecasting MSE Stocks

Now for the fun part. I am going to use our function we defined above to predict the next year for the stocks I chose. For fun I will give a BUY or SELL recommendation for each (please see disclaimer above and don’t take this seriously).

Tavan Tolgoi — BUY

Tavan Tolgoi is one of the largest mines in Mongolia and produces large quantities of coking and thermal coal.

Aside from the weird numbers at the beginning of our data (Prophet handled these very well) we can see a very consistent trend.

Gobi Cashmere — BUY

Gobi Cashmere is a formerly state owned cashmere garment producer.

APU — BUY

APU is a beverage maker and distributor. Their new marketing slogan is Absolute. Pure. Unique. However this former state owned companies initials actually stand for Arkhi Piiv Undaa (Vodka, beer, drinks). APU is owned by Shunkhlai Group and recently merged with Heineken Asia Pacific which precipitated this huge price increase.

Talkh Chikher — BUY

This company makes some of the best bread in the country. It is my bread of choice for sandwiches (not that you care). I put this as a buy even though I’m in reality not as bullish on this company. After all, how many sandwiches can you eat?

Darkhan Nekhii — BUY

This was an unknown company to me before this article. Darkhan Nekhii is a leather processing company that also makes leather garments.

Suu (Milk) — BUY

The Mongolian household name in milk (literally). This company is the largest milk and dairy producer in the country.

Bayangol Hotel — SELL

One of the most prestigious hotels during communist times, they’ve recently renovated and are trying to modernize their offerings. Their new buffet is of particular interest. Nevertheless it receives one of the only SELL recommendations (again see the disclaimer above) on the list.

Mongolian Post — BUY

A relatively new company on the stock exchange, and also recently privatized. The confidence interval is all over the place on this one. In reality who knows what will happen. Personally I give it a buy, not for any reason backed up by data, but because I always receive great service there and they do a good job given the circumstances.

Material Impex — BUY

This wholesale and little known trade company receives a buy, but just barely.

Ulsin Ikh Delguur (State Department Store) — SELL

This department store was founded in 1924 and has everything you could possibly want (if you are a tourist and willing to spend double for whatever you want). The one exception is the grocery store on the first floor that, while cramped, has a decent selection.

Conclusion

This was a quite simple prediction for these stocks. In reality you would want to dive deeper into each company, learn their fundamentals, and then tweak the forecast parameters to give a more accurate picture. A good example of this is APU. Their recent merger is so far off the trend that Prophet smooths it out quite a bit. This prediction could be improved considerably.

Moving forward one area of improvement would be to find events that cause inflection points in the stocks. The Stocker package builds on top of Prophet and can use Google Trends to find change points and match them to search trends.

If you liked this article please hit the clap button, leave a comment, or shoot me an email at robertritz@outlook.com.

Mongolian Data Stories

Using data analysis, visualization, and machine learning to tell Mongolia’s story. Interested in writing for Mongolian Data Stories? Send an email to Robert Ritz (editor of MDS) at robertritz@outlook.com.

Robert Ritz

Written by

Data Scientist and Director of LETU Mongolia. Keen observer of Mongolia.

Mongolian Data Stories

Using data analysis, visualization, and machine learning to tell Mongolia’s story. Interested in writing for Mongolian Data Stories? Send an email to Robert Ritz (editor of MDS) at robertritz@outlook.com.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade