Data Science in Finance: A Python Portfolio Analysis
Data science in finance covers everything from fraud detection to trading, but its most durable idea is simple: measure risk as well as return. This article uses real monthly prices of five well-known stocks to compute returns, volatility, correlation and drawdowns in pandas, and shows what diversification actually does. It is an educational analysis of the past, not investment advice. Computed with pandas 3.0.5 and NumPy 2.5.3.

The data
The file is stocks.csv from the open vega-datasets collection: monthly prices of Microsoft, Amazon, IBM, Apple and Google from January 2000 to March 2010 (560 rows). Google only appears from August 2004, so for a fair comparison we use the 67 months when all five exist (September 2004 to March 2010). Prices in the file are as given; we did not check them against another source. Important: these are five famous companies chosen with hindsight, so the results are not representative of stocks in general.
import pandas as pd, numpy as np
s = pd.read_csv('stocks.csv', parse_dates=['date'])
prices = s.pivot(index='date', columns='symbol', values='price')
returns = prices.pct_change().dropna() # 67 common months
print(returns.mean() * 100) # average monthly return, %
print(returns.std() * 100) # monthly volatility, %
print(returns.std() * np.sqrt(12) * 100) # annualised volatilityReturn and risk
| Stock | Avg monthly return | Monthly volatility | Annualised volatility | $1 grew to | Max drawdown |
|---|---|---|---|---|---|
| Apple | 4.68% | 12.53% | 43.4% | 12.93 | -56.9% |
| 3.23% | 11.97% | 41.5% | 5.47 | -58.6% | |
| Amazon | 2.76% | 14.02% | 48.6% | 3.38 | -54.2% |
| IBM | 0.90% | 6.12% | 21.2% | 1.61 | -36.4% |
| Microsoft | 0.61% | 7.05% | 24.4% | 1.28 | -54.9% |
Higher return came with higher volatility: Apple earned the most (a dollar grew to 12.93) but IBM and Microsoft were far calmer. Every stock had a maximum drawdown, the worst fall from a previous peak, of between 36% and 59%. Each one’s worst month came in 2008 except Amazon’s (July 2006): Apple fell 33% in September 2008, Google 18.5% in November 2008, and IBM and Microsoft 20.5% and 16.3% in October 2008. Averages hide this: an investor who watched a 57% fall to the end would have needed strong nerves for the 12.9x outcome.
Diversification works, up to a point
Correlations between the five stocks’ monthly returns ranged from 0.22 (Google and IBM) to 0.55 (Apple and Google). An equal-weight mix of all five had a monthly standard deviation of 7.38%, against an average of 10.34% for the individual stocks, an annualised 25.6%, close to the calmest single stocks. The mix’s average return was 2.44% a month and a dollar grew to 4.21, with a maximum drawdown of 45.6%. So mixing removed a lot of the stock-specific risk but not the market-wide crash: drawdown was still 46%, because correlations are positive and all five fell together in 2008. Return per unit of risk (mean over standard deviation) was 0.33 for the mix, 0.37 for Apple and 0.09 for Microsoft.
Returns have fat tails
If monthly returns were normally distributed, about 4.6% of months would fall more than two standard deviations from the mean. For the equal-weight mix 6.0% did. With 67 months this is only suggestive, but it points to a known issue: extreme months are more common than a simple bell curve suggests, so models built on normal assumptions understate crash risk.
Why past returns are a weak guide
Looking at only five stocks that went on to become famous gives a heavily flattering picture, an example of survivorship and hindsight bias. Past average returns also say little about the future: we cannot say from this data which stock will do best next. Real quantitative finance tests strategies on many assets, accounts for costs and tests out-of-sample, as in our time series analysis guide, which found a stock-direction model was worse than always guessing up.
Where data science is used in finance
Common uses are credit scoring and fraud detection, risk models, forecasting demand and portfolio construction. For modelling basics see logistic regression in Python and linear regression; for fairness issues in lending-type models see the dark side of big data; and for risk from claims data see data science in insurance. The pandas pct_change documentation covers the return calculation.
Limits of this analysis
Five stocks, 67 months, monthly prices that may or may not be adjusted for dividends (not stated in the file), no transaction costs or taxes, and hindsight selection. Nothing here is a recommendation to buy or sell anything.
FAQ
How is data science used in finance?
For credit scoring, fraud detection, risk measurement, forecasting and portfolio analysis. This article focuses on measuring return and risk.
What is volatility?
The standard deviation of returns. Annualised monthly volatility is multiplied by the square root of 12. Apple’s was 43.4% and IBM’s 21.2% in our sample.
Does diversification remove risk?
Only part of it. An equal-weight mix cut volatility to 25.6%, but still fell 45.6% from its peak because all five stocks dropped together in 2008.
Is this investment advice?
No. It is a historical, educational analysis of five hand-picked stocks.
