Raghotham Sripadraj

14 Dec 2022

Forecasting for Humans

Introduction

For the past 3 years, I have had this idea of building a better forecasting system for business users. Business users mostly live on spreadsheets and the goal I had was to take advanced forecasting techniques to business users. Forecasting is one of the most challenging and hard problems in ML given the wide variety of domains it is applied to and the various ways available to solve this problem. Truth is - No one algorithm works for all cases. Hence, I had the itch to build something to help this large section of users who do not have expert ML understanding to tune the forecasting algorithm for data. For folks who can write code, there are tools like Prophet, Greykite, etc.

The Hack

Last weekend, I finally got a chance to carve out some time to work on this problem and challenged myself to solve this end-to-end in a day. Google Sheets comes with a default forecast function that performs simple linear regression which might not be very helpful to users. Hence, the idea was to create a custom function for Google Sheets that will enable advanced forecasting techniques.

Google has created a very rich ecosystem for developers to build custom functions/add-ons to Google Workspace and mostly this is enabled by their AppScript engine. Developers can create custom functions using AppScript, which essentially is javascript, and this can be integrated with Google Sheets, Docs, and other products. This was the first time I created a custom function and I had to refer to a bunch of blog posts that helped me create this. The custom function was simple and took 3 input parameters –

  1. a range of dates values
  2. a range of values that needs to be forecasted
  3. a variable representing the forecast period (1 time period, 2 or 3, etc)

The simplest way to enable advanced forecasting is to integrate Prophet with the custom function being built. AppScript is a simple javascript engine and hence it will be hard for us to build native forecasting using just AppScript. This seemed like a big blocker and I had to look for alternatives. There are two ways to integrate Prophet with custom function:

  1. Bundle Prophet forecasting as WebAssembly (WASM) module and run forecasting natively in the browser.
  2. Make an API call from AppScript to an external service that offers Prophet forecasting as a Service.

While WASM sounded exciting, the challenge is running a compute-heavy task on the browser. With no prior experience with WASM (pyiodide being the easiest way), this seemed that I would not be able to complete the end-to-end solution in a day. Luckily, AppScript can make API calls to external web services, which meant going with option 2 was easier. The next step was to check if there are any easy, free-tier cloud offerings for running Prophet as a Service. Turned out nothing was easy to integrate or offered a free tier, which meant I had to create a service and host it on a cloud provider. Having built enough Python web services and prior knowledge of Prophet assured me that creating a Flask API wrapper around Prophet was a breeze. However, I wanted to learn something new! This was the right opportunity for me to try FastAPI, which was on my list for 2 years now.

The FastAPI docs are very well designed with most of the user guides having copy-paste code which will work out of the box. FastAPI has a great philosophy of enabling static typing with pydantic. A great plus is the swagger docs generated by default. By following the user guide, I was able to quickly create the FastAPI application that accepts a POST request with the same 3 parameters mentioned above. I ran the web server on my localhost and exposed it to the external world via ngrok to test the integration with AppScript function.

It took a few quick fixes to set the date type formatting issues and then, 💥 the AppScript was able to fetch the forecasted values and populate the sheet with the forecasted values!

Wrap Up

To me, it is important to know what tools are emerging in the market and how we can leverage them to build micro-products as quick hacks. To get this idea up and running, I learnt a bunch of new tools:

  1. AppScript ecosystem
  2. danfo.js
  3. fastapi
  4. pydantic
  5. render.com

Overall, it was a very fulfilling experience building this working proof of concept with end-to-end integrations. The plan is to find the right cloud worker which can handle the compute for low-latency forecasting and then launch the custom function as a free add-on on Google Workspace Marketplace.