How to Build a Factor Model in Python

Ever wonder what drives stock returns beyond just market moves? That’s where factor models come in. They break down an asset’s return into common drivers—like market risk, size, value, or momentum—so we can better understand, predict, and optimize our portfolios.

Today, we’ll learn how to build a simple multi-factor model in Python and interpret the results. If you love quant finance, regression, and nerding out over data—this is for you!


💡 What Is a Factor Model?

A factor model explains an asset’s return using a linear combination of common risk factors. The most famous example is the Capital Asset Pricing Model (CAPM):

R_i = α + β * R_m + ε

Where:

  • R_i is the return of asset i
  • R_m is the market return
  • β is sensitivity to the market (market beta)
  • α is the intercept or alpha (abnormal return)
  • ε is the residual error

But one factor isn’t always enough. Enter multi-factor models like Fama-French 3-factor or 5-factor models, which add factors like:

  • SMB: Small minus Big (size effect)
  • HML: High minus Low (value effect)
  • MOM: Momentum

Modern quant investors use even more: low volatility, quality, ESG, etc.


🔧 Step-by-Step: Build a Factor Model in Python

1️⃣ Install Required Libraries

pip install pandas numpy yfinance statsmodels matplotlib

2️⃣ Load Asset and Factor Data

We’ll get daily returns for Apple (AAPL) and the Fama-French 3 factors from Kenneth French’s website via a simplified CSV or preloaded dataset.


import pandas as pd
import yfinance as yf
import statsmodels.api as sm

# Get Apple stock price
aapl = yf.download("AAPL", start="2020-01-01", end="2023-01-01")['Adj Close']
aapl_returns = aapl.pct_change().dropna()

# Load Fama-French factors (example: already downloaded CSV)
factors = pd.read_csv("F-F_Research_Data_Factors_daily.csv", skiprows=3)
factors = factors.rename(columns={"Mkt-RF": "MKT"})
factors['Date'] = pd.to_datetime(factors['Date'], format='%Y%m%d')
factors = factors.set_index('Date') / 100  # Convert % to decimal
factors = factors.loc[aapl_returns.index]

# Merge data
data = pd.concat([aapl_returns, factors], axis=1).dropna()
data.columns = ['AAPL', 'MKT', 'SMB', 'HML', 'RF']

# Excess return
data['AAPL_EX'] = data['AAPL'] - data['RF']

3️⃣ Regress Returns on Factors


# Set up X (factors) and Y (excess returns)
X = data[['MKT', 'SMB', 'HML']]
Y = data['AAPL_EX']
X = sm.add_constant(X)

# Run regression
model = sm.OLS(Y, X).fit()
print(model.summary())

📈 Example Output

Here’s what the regression results might show:


                            OLS Regression Results                            
==============================================================================
Dep. Variable:              AAPL_EX   R-squared:                       0.425
==============================================================================
coef    std err          t      P>|t|      [0.025      0.975]
------------------------------------------------------------------------------
const     0.0003      0.000      2.101      0.037     2e-05     0.0006
MKT       1.0256      0.045     22.851      0.000     0.937     1.114
SMB       0.3221      0.067      4.791      0.000     0.190     0.454
HML      -0.1953      0.054     -3.620      0.000    -0.301    -0.089

This means:

  • β_market ≈ 1.03 → AAPL is highly sensitive to the market
  • β_SMB ≈ 0.32 → Slight tilt toward small-cap behavior
  • β_HML ≈ -0.19 → AAPL behaves like a growth stock (not value)
  • R² ≈ 0.42 → 42% of AAPL’s return is explained by these 3 factors

📌 Interpretation and Use

Factor models give you:

  • 🎯 Insights into what drives returns (e.g., is a stock momentum-driven?)
  • 🛡️ Better risk management (e.g., controlling factor exposures)
  • 📦 Portfolio construction tools (e.g., build a long/short value portfolio)

You can also use them for:

  • Attribution analysis
  • Hedging strategies
  • Smart beta or factor investing strategies

📊 Visualizing Factor Exposure


import matplotlib.pyplot as plt

coefs = model.params[1:]
coefs.plot(kind='bar')
plt.title("Factor Exposures for AAPL")
plt.ylabel("Beta")
plt.grid(True)
plt.show()

This bar chart shows AAPL’s beta to each factor. A high market beta, moderate size tilt, and negative value tilt give you a fingerprint of Apple’s factor personality.


🧠 Final Thoughts

Factor models are the backbone of modern portfolio theory. Whether you’re backtesting strategies, managing risk, or analyzing performance, understanding how to build and interpret them is an essential skill.

In this guide, you learned how to:

  • Load and clean financial and factor data
  • Build and interpret a multi-factor regression model
  • Visualize exposures to better understand asset behavior

Now it’s your turn—run this on your favorite stocks, add new factors, and get to know your portfolio better than ever. Happy modeling!


Want more finance nerd tutorials? Follow me at The Finance Nerd for more Python-powered investment insights!

How to Build a Black-Litterman Model in Python (Without Losing Your Mind)

What if Harry Markowitz and Thomas Bayes teamed up to manage your portfolio? That’s basically what the Black-Litterman model is: a quantitative investing tool that blends modern portfolio theory with your personal views. Today, we’re going to explore how to build this model from scratch in Python—with code, theory, and plenty of finance nerd humor along the way.


💡 What Is the Black-Litterman Model?

The Black-Litterman model was developed by Fischer Black and Robert Litterman at Goldman Sachs. In short, it starts with a “neutral” market portfolio—what the market implies are fair asset returns—and then lets you adjust those expectations with your own views (like “I think Apple will outperform Google by 5%”).

This results in a set of posterior expected returns you can plug into a mean-variance optimizer, giving you smarter, more intuitive portfolio weights than relying on historical returns alone.


📉 Why Not Just Use Mean-Variance Optimization?

Good question. Traditional Mean-Variance Optimization (MVO) has a problem: it’s hypersensitive to input assumptions—especially expected returns. A small tweak in expected return can flip your optimal portfolio completely. This makes it prone to overfitting and unrealistic allocations (like putting 100% into Tesla because it performed well last year).

The Black-Litterman model fixes this by:

  • Starting from the market-implied expected returns (based on CAPM theory)
  • Adjusting those with your own views (absolute or relative)
  • Blending them using a Bayesian approach, weighted by confidence

The result? Portfolios that are diversified, stable, and still aligned with your beliefs.


🛠️ Step-by-Step: Build a Black-Litterman Model in Python

1️⃣ Define Your Assets and Covariance Matrix

Let’s assume a tech-heavy portfolio:

  • Assets: AAPL, MSFT, GOOG, AMZN, TSLA
  • Volatilities: Between 16% and 30%
  • Correlations: About 0.6 between all pairs

import numpy as np
import pandas as pd

tickers = ['AAPL', 'MSFT', 'GOOG', 'AMZN', 'TSLA']
vols = np.array([0.20, 0.18, 0.16, 0.22, 0.30])
corr = 0.6
corr_matrix = np.full((5, 5), corr)
np.fill_diagonal(corr_matrix, 1)

cov_matrix = np.outer(vols, vols) * corr_matrix
cov_df = pd.DataFrame(cov_matrix, index=tickers, columns=tickers)

2️⃣ Set Market Capitalization Weights


market_caps = pd.Series({
    'AAPL': 3000,
    'MSFT': 2500,
    'GOOG': 1800,
    'AMZN': 1400,
    'TSLA': 900
})
market_weights = market_caps / market_caps.sum()

3️⃣ Calculate Implied Equilibrium Returns


risk_free = 0.02
delta = 2.5
Pi = delta * cov_df.values.dot(market_weights.values) + risk_free
prior = pd.Series(Pi, index=tickers)

These are the market-implied expected returns for each asset. You can think of this as the “default” starting point.

4️⃣ Express Your Views (P & Q Matrices)

Let’s assume two views:

  1. Absolute view: TSLA will return 20%
  2. Relative view: AAPL will outperform GOOG by 5%

P = np.array([
    [0, 0, 0, 0, 1],  # TSLA
    [1, 0, -1, 0, 0]  # AAPL vs GOOG
])
Q = np.array([0.20, 0.05])
view_conf = np.array([0.5, 0.95])

5️⃣ Combine Everything in Black-Litterman


from pypfopt.black_litterman import BlackLittermanModel

bl = BlackLittermanModel(
    cov_df,
    pi=prior,
    P=P,
    Q=Q,
    omega="idzorek",
    view_confidences=view_conf,
    tau=0.05
)
bl_rets = bl.bl_returns()

Result: A new set of expected returns, adjusted for your views and confidence levels.

6️⃣ Calculate Optimal Portfolio Weights


bl_weights = bl.bl_weights(risk_aversion=delta)
print(bl_weights)

This gives you your optimal portfolio. For example:

  • AAPL: 49%
  • TSLA: 12%
  • GOOG: 2.7%

Notice how your views shifted weight away from GOOG and toward AAPL and TSLA. Beautiful!


📈 Why Finance Nerds Love Black-Litterman

The Black-Litterman model is like giving your portfolio a brain—and a backbone. It respects the market but still lets you express strong, confident views without going overboard.

It’s especially useful if you’re:

  • Building institutional portfolios
  • Running model portfolios with tactical overlays
  • Trying to avoid overfitting in historical MVO models

Plus, it’s incredibly satisfying to watch your views translate into clean, data-driven portfolio shifts.


🧠 Final Thoughts

The Black-Litterman model gives you the best of both worlds: market rationality and personalized insights. With Python and a few libraries like PyPortfolioOpt, it’s easier than ever to implement.


Want more posts like this? Subscribe or follow me at The Finance Nerd for regular articles on quant tools, data-driven investing, and nerdy financial insights.

World Quant University (WQU)’s Financial Engineering Programme, my thoughts about the Course after 1 year.

A year ago I started studying the MSc in Financial Engineering offered by WQU and I wrote about the course here. As I am writing this post, I have now completed around 60% of the modules so I thought that an update would be helpful for prospective students.

These are the modules that I have completed:

(1) Financial Markets: this is a general introduction to financial markets and financial products. As the course caters towards students with different backgrounds, if you have studied for financial qualifications (such as CFA, FRM etc.) you will find this to be a gentle refresher. As it’s hard to test any maths skills, most of the assignments/essays will be theoretical.

(2) Econometrics: OK, you will finally get your hands dirty and start doing some work with time series. You will start with classic models such as GARCH, ARCH, ARIMA in analysing time series of end of day asset prices. Although you get the choice between R and Python, at least for this module, I strongly recommend sticking to R (lecture notes are better written for R).

(3) Discrete-time Stochastic Processes, (4) Continuous-time Stochastic Processes: these two courses in my opinion are at the heart of the programme; when it comes to asset pricing, having an understanding of stochastic processes and their properties is indispensable when deriving pricing equations of various claims. These two modules will be especially tough for candidates with a weak mathematical background. These concepts are quite abstract so they just require some time to sink in – I wouldn’t get discouraged if you see a massive knowledge gap. The assignments were focused on applying Monte Carlo methods and analytical pricing formulas on exotic derivatives. The projects from these modules were the most enjoyable so far.

(5) Computational Finance
This module builds on a lot of the concepts learned during the previous two modules by teaching students how to implement the concepts in python. It has an emphasis in applying Monte Carlo Methods in pricing exotic options and computing CVA. I found this module to be very practical.

(6) Machine Learning in Finance
They seem to cover a lot of the so called classic topics in ML: classifiers, supervised & unsupervised learning, etc. You will be using tensor-flow in training the various models, making predictions and assessing the model’s accuracy.

Continue reading

The World Quant University (WQU): Application and Overview

A free MSc in Financial Engineering sounds crazy but it looks like this is exactly what World Quant University seems to be doing. With an ethical goal of making education globally accessible to capable students, regardless of their financial situation, WQU launched their programme in 2015 by Igor Tulchinsky and has been gaining popularity, especially in the past few years.
The course is fully provided online and covers a wide range of topics/subjects that would be taught on a Masters in Financial Engineering.
Continue reading

Getting Technical with the CQF (Certificate in Quantitative Finance)

CQF

This is a programme that I had been looking at for a while; the course seemed to be very well structured and geared towards people like myself who are interested in learning about the building blocks of quantitative finance in a short amount of time. The course has renowned lecturers such as Paul Wilmott who is very well respected within the quantitative finance community. 

Information regarding this programme can be accessed here.

A bit about myself 

Before delving into the course details I would like to give some context as to why I thought this course would be beneficial for my career. I have around 10 years of experience working in the financial sector and 5 years in derivatives valuations; although I heavily rely on industry-standard pricing models, very rarely do I need to look into the derivations of such models. Although some might argue or question the practicality of delving deep into understanding SDEs, Martingales and Risk Neutral Measures, these are all topics which have been fascinating me for a while and I have always found understand them hard to understand via self-study. I don’t have a maths/physics background so before even starting with stochastic calculus, I was struggling with the basics. My goal was to develop my skills which would allow me to take a stab at an entry-level quantitative research role and progress my learning experience via a PhD (in Financial Engineering); I thought the CQF would be a good starting point and would equip me the necessary skill set.

Before the Course

If you are reading this post, chances are that you are in a situation similar to mine 6 months ago. You might be asking if this course is worth it or if there are any other cheaper and better alternatives. With a price tag of around $16K, it would be foolish to enrol on the course without any proper research. $16K is also an amount where you start thinking whether it would be just better investing a little bit more to be able to get a Masters in Financial Engineering from a prestigious institution. Having done a fair amount of research myself and having gone through the ongoing debates on Quantnet, these were the key discussions that I was seeing on the forum.

For those who have concluded that the CQF does not warrant its tuition fees and instead are just looking for some good textbooks, I can recommend the below: 

– Steven Shreve, Stochastic Calculus and Finance. http://efinance.org.cn/cn/FEshuo/stochastic.pdf

– Interest Rate Models – Theory and Practice: Damiano Brigo, Fabio Mercurio.

– Options, Futures and Other Derivatives: John Hull (this one is a classic) 

 

I could write a longer list but these are the core textbooks that I found particularly helpful.

The CQF also discloses online its core textbooks, so self-studying those materials might also be an option if you already have a certain level of maths (Paul Wilmott Introduces Quantitative Finance, is what I would recommend starting with to grasp the basics).  https://www.cqf.com/about-cqf/program-delivery/learning-resources

Course Structure and Overview 

London based students can attend classes directly. However, since most students are based outside of the UK, they will view the classes online via the learning portal. During the live webcasts, students have access to the chat functionality and can ask/answer questions. All classes are recorded and available on the portal so that delegates can view them at their own pace. 

The whole course is divided into 2 levels and the topics covered in the respective levels are as follows: 

Level 1: Building Blocks of Quant Finance, Quantitative Risk and Regulations, Equities and Currencies

Level 2: Data Science and Machine Learning, Fixed Income, Credit, Advanced Electives

(please note that this information is accurate for the June 2019 Cohort which I enrolled for) 

In my case, I had 2 exams at the start of Level 1each counting 20% towards the final score of the course and 2 more exams for Level 2, another exam counting 20% and the final project counting 40%.

All exams are open book and except for the final project, you have two weeks to complete each exam. In designing these exams, the CQF has focused on the practical part rather than testing whether delegates can just merely memorise formulae and concepts. I found the projects fun and rewarding to do. Without giving away too many details, here are the topics that I was tested on:

Exam 1: portfolio optimisation and VAR / Exam 2: option pricing exotic options / Exam 3: machine learning and fixed income pricing / Exam 4 final project (you have a range of topics to choose from).

There is also a final optional exam that you can take if you want to aim to get a distinction on the course.

Tips for people who already signed up

If you have already signed up and you are eagerly awaiting to start the course I have a few tips for you which I hope you will find beneficial in your journey. 

Tip #1 Do the maths primer

This is also very strongly encouraged by CQF faculty. A lot of the maths that is used relies on: algebra; statistics; and calculus. You will have a hard time-solving PDEs and SDEs if you are rusty on the basics. Be sure to to be comfortable with these topics if you don’t want to quickly fall behind; you should attempt the optional test to gauge your skills.

Tip #2 Choose Python as your Programming Language

Assuming you are looking to pick up a new language, I recommend using python as a lot of the course material and libraries use python. R might also be a good alternative. If you are already comfortable with a programming language then stick to that one, this advice applies to candidates who are not very advanced programmers and who are beginners in numerous programming languages (this was my situation at the start of the course).

Tip #3 There is no shame in asking for an extension for your projects

I assume that a lot of the people are working professionals which adds an extra layer of difficulty in meeting assignment deadlines. You are allowed one extension per exam for each level so you should take advantage of this to ensure that you have enough time to submit good quality work. Please note that if you don’t defer an exam and miss a deadline, you will automatically be deferred to the next cohort so it’s in your best interest to respect the deadlines if you want to complete the course in 6 months.

Conclusion

I think a lot of people might be interested in reading the conclusion. Is this course worth taking or not? I can’t provide a clear yes or no answer (I’m sorry) as I have still not completed it. What I can say at this stage is that taking the CQF made sense for me as I had been struggling a lot in picking up the required entry-level maths for a quant role. The course breaks down quantitative finance into its core areas, which helped me to grasp the basic concepts and taught me to apply them in a practical manner. My aim now is to continue working on the skills that I learned and build a portfolio of research strategies and papers whilst developing my personal views and awareness regarding this fascinating discipline.

I will list some positives and negatives that should give some more colour regarding my experience.

Negatives

– Since the classes take place during London evening hours, this ends up being very late for APAC based delegates. Due to the time difference, I ended up watching the recordings when I would have preferred being able to participate in the live classes without staying up late.

– The course did feel a bit pricy given that it only lasts for 6 months and it felt a bit rushed. I know deferring for 6 months is an option that candidates have but I thought that the content could have been spread out a bit better.

– Although overall the level of teaching was good, I saw huge gaps between the level of teaching by certain lecturers which impacted my learning experience for certain topics (this is an opinion shared by a number of delegates who I spoke with).

– A lot of people still question the rigour and I personally don’t think the CQF at this stage will hold the same weight as an MFE or PHD when applying for quant jobs. I am interested in how the CQF Institute will continue working on the brand in the upcoming years.

Positives

– The course definitely taught me a lot of maths and practical skills which I have found useful at work. This is what I was looking for so I can say that the course delivered what it promised.

– I really enjoyed using the CQF app. Being able to view videos during my commute to work helped me continue my studies, especially during busy days.

– The video library is pretty decent and I will be looking at the additional lecturers in the upcoming months. This concept of life long learning does appeal to me.

– It has a good focus on coding and I will be aiming to spend the following months in testing what I have learned. I felt like the course gave me the required tools to start doing my own research within the field of quantitative finance.

I hope you enjoyed this post and if you have any questions please feel free to email me privately. Good luck with your studies!

 

Disclaimer: Please note that I am not affiliated with the CQF Institute, the above content represents my subjective views about this course. This post was written to help people make informed decisions about the CQF programme, I strongly recommend that you do your own research prior to signing up.