Streamline your energy analyses with Python: Sensitivity analysis
Picture retrieved fronm free-stock image

Streamline your energy analyses with Python: Sensitivity analysis

Why use a programming language for sensitivity analysis?

In this week's "Streamline your energy analysis with Python series" I will show why and how to streamline an essential tool to evaluate energy investments: sensitivity analysis.

Financial evaluation of energy investments, like developing an oil field or deploying an offshore windfarm, requires the analyst to state assumptions for several variables and calculate several economic indicators. After calculating the desired measures with the base assumptions, the analyst will engage in answering "what-if" questions. For example, what if operating costs increase or what if the corporate tax rate decreases.

As the project grows in scope and detail, tens or hundreds of such questions can appear. Managing and keeping track of the model and results becomes difficult. This is worsened if you use Excel or spreadsheet software to manage the model: new sheets are added, formulas become complex, changes need to make by hand. By the end of the analysis, it becomes unbearable to trace the changes, and handling the file to a new analyst will certainly result in broken links.

This is where a programming language such as python comes in hand. By using a programming language, you will have a:

  1. A reproducible model that can be easily changed and adjusted.
  2. Save time in future projects as you can reuse the model.
  3. Share the code without fear of breaking links.
  4. Keep your sanity in check as you will not have to trace between tens of different Excel sheets.

A note before proceeding. I am not arguing that Excel is a bad tool. On the contrary, it is a very useful tool for certain applications. However, programming languages outperform Excel in certain tasks.

Undertaking sensitivity analysis

To keep things simple suppose we are interested on installing an offshore wind turbine that has the following assumptions. 

No alt text provided for this image

Calculating the net free cash flow and net present value is straightforward. Notice how easy and clear it is to follow the calculations and read through the code. If you come from a spreadsheet heavy background it may take a while to get used to looking at calculations rather than numbers. I assure you that with practice comes comfort. 

No alt text provided for this image

While in this simple example you may think a spreadsheet can do the job, remember that this code is scalable, and requires minimum maintenance.

Now let us perform sensitivity analysis. As a first example let us check the effect of a +/- 10% price change on NPV. If you were using a spreadsheet you will have to define dummy columns or different sheets that contain the assumptions. You will then need to retrieve the result and copy it on another table for inspection, then plot the results. All these calculations of course involve lots of typing and some manual overrides.

In a programming language like Python, we can use a “for loop” to calculate the model and store the results for different assumptions. In addition, we can easily create data frames to share the information and plot the results for a quick inspection. All the procedure in python is shown in the next image. 

No alt text provided for this image

Noticed that we used the cash flow calculations and change the price variable to the defined cases. The full program only takes 13 lines of code plus the base assumptions, and package imports like NumPy and Pandas for data management. If required, you can style the plot to whatever is your need.

The best part of this program is that it is scalable. For example, what if we wanted to modify two assumptions at the same time. Tracking, storing, analysing and plotting the changes in a spreadsheet can be cumbersome, to say the least.

The next code snippet shows the effects on NPV for 10% changes in price and discount factor. Notice how we just changed a couple of lines to get the results. The complete program only required 50 lines of code including setting the assumptions and imports of the required packages. However, this code is already scalable to different settings and variables; the structure would nevertheless be the same. It is also a program that is reproducible and easy to understand.

Take the plunge and incorporate programming languages into your energy analysis toolkit. You will not regret it.  

No alt text provided for this image



I also perform similar analyses using programming languages 😊 I like them more than spreadsheets

To view or add a comment, sign in

More articles by Arturo Regalado

Others also viewed

Explore content categories