Data science is an important field and one that anyone can contribute to. It all starts with getting some data and visualizing it. Check this out.
Import the Necessary Libraries
We will be using Python code to help us create our visuals. First, we need to import the necessary libraries. These libraries will help us download the data, manipulate it, and create visualizations.
# Import the necessary libraries
import yfinance as yf
import pandas as pd
import matplotlib.pyplot as plt
- yfinance: This library allows us to download financial data from Yahoo Finance.
- pandas: This is a powerful library for data manipulation and analysis.
- matplotlib: This library is used for creating visualizations in Python.
Define the Date Range and List of Tickers
Next, we define the date range for our analysis and the list of tickers we want to download data for. The tickers include Bitcoin (BTC-USD) and the top tech stocks, also known as the “Magnificent Seven.”
The date ranges have been set to start in 2020 right before the Covid Pandemic and end on 5/19/24.
# Define the start and end dates
end_date = '2024-05-19'
start_date = '2020-01-01'
# List of tickers to download
tickers = ['BTC-USD', 'AAPL', 'MSFT', 'GOOGL', 'AMZN', 'NVDA', 'TSLA', 'META']```
Download Historical Data
We use the yfinance library to download the historical data for all the tickers within the specified date range.
# Download historical data for all tickers
data = yf.download(tickers, start=start_date, end=end_date)['Close']
# Handle missing data by forward filling
data.ffill(inplace=True)
Here, we download the closing prices for each ticker and handle any missing data by forward filling, which means filling missing values with the last available data.
Calculate Daily Returns and Cumulative Returns
We calculate the daily returns for each stock and then compute the cumulative returns normalized to a base of 100. This helps us visualize the growth of each asset starting from the same baseline.
# Calculate daily returns for each stock
returns = data.pct_change()
# Calculate cumulative returns normalized to 100 for each stock
cumulative_returns = (1 + returns).cumprod() * 100
Plot the Normalized Cumulative Returns
Now, let’s plot the normalized cumulative returns. Each line represents the cumulative return of an asset, starting at a base of 100. This visualization helps us see the growth trends of bitcoin compared to major tech stocks over the selected period.
# Plot the normalized cumulative returns
plt.figure(figsize=(14, 7))
colors = ['b', 'g', 'r', 'c', 'm', 'y', 'k', 'orange']
for i, ticker in enumerate(tickers):
plt.plot(cumulative_returns[ticker], label=f'{ticker} Cumulative Returns', color=colors[i], alpha=0.7)
# Set the title of the plot
plt.title('Normalized Cumulative Returns of Bitcoin vs. The Magnificent Seven since Jan 1, 2020')
# Label the x-axis as 'Date'
plt.xlabel('Date')
# Label the y-axis as 'Cumulative Returns (Base 100)'
plt.ylabel('Cumulative Returns (Base 100)')
# Add a legend to distinguish between Bitcoin and each stock
plt.legend()
# Enable grid lines on the plot for better readability
plt.grid(True)
# Show the plot
plt.show()
Why Use Cumulative Returns?
With all this code we should be able to plot the normalized cumulative returns.
This visualization helps us see the growth trends of bitcoin compared to major tech stocks over the selected period. The colors and legend make it easy to distinguish between the different assets.
Cumulative returns are used because they provide a clear picture of the total return on an investment over a period of time. By normalizing to a base of 100, we can compare different assets on the same scale, regardless of their initial value.
This is particularly useful when comparing the performance of assets with different price ranges, like bitcoin and tech stocks.
Using Google Colab
Use Google Colab (or your preferred IDE) to run the entire script as shown below. After running the code, you should get a chart that looks like the one below.
# Import the necessary libraries
import yfinance as yf
import pandas as pd
import matplotlib.pyplot as plt
# Define the start and end dates
end_date = '2024-05-19'
start_date = '2020-01-01'
# List of tickers to download
tickers = ['BTC-USD', 'AAPL', 'MSFT', 'GOOGL', 'AMZN', 'NVDA', 'TSLA', 'META']
# Download historical data for all tickers
data = yf.download(tickers, start=start_date, end=end_date)['Close']
# Handle missing data by forward filling
data.ffill(inplace=True)
# Calculate daily returns for each stock
returns = data.pct_change()
# Calculate cumulative returns normalized to 100 for each stock
cumulative_returns = (1 + returns).cumprod() * 100
# Plot the normalized cumulative returns
plt.figure(figsize=(14, 7))
colors = ['b', 'g', 'r', 'c', 'm', 'y', 'k', 'orange']
for i, ticker in enumerate(tickers):
plt.plot(cumulative_returns[ticker], label=f'{ticker} Cumulative Returns', color=colors[i], alpha=0.7)
# Set the title of the plot
plt.title('Normalized Cumulative Returns of Bitcoin vs. The Magnificent Seven since Jan 1, 2020')
# Label the x-axis as 'Date'
plt.xlabel('Date')
# Label the y-axis as 'Cumulative Returns (Base 100)'
plt.ylabel('Cumulative Returns (Base 100)')
# Add a legend to distinguish between Bitcoin and each stock
plt.legend()
# Enable grid lines on the plot for better readability
plt.grid(True)
# Show the plot
plt.show()
Analysis of the Chart
From the chart, it is evident that bitcoin (BTC-USD) has been a strong performer overall, despite its significant volatility.
At various points in time, some of the “Magnificent Seven” tech stocks have outperformed bitcoin, but none have sustained the same level of overall growth across the entire period.
Key Observations
- Bitcoin’s Performance: Bitcoin shows significant peaks and troughs, indicative of its high volatility. Despite these fluctuations, its cumulative return remains one of the highest over the period.
This demonstrates bitcoin’s potential for high returns, albeit with considerable volatility that holders must stomach to achieve these high returns.
- Tech Stocks Performance: Among the tech stocks, NVIDIA (NVDA) and Tesla (TSLA) show notable performance. In recent periods, NVIDIA has outpaced the returns of bitcoin, highlighting its strong upward trend.
This indicates that while bitcoin has substantial long-term growth, certain tech stocks can outperform it in the short term.
Investing Insight (Not Financial Advice)
Investing in bitcoin through a strategy of dollar-cost averaging (DCA) and holding over the long term has proven to be a highly successful strategy, outperforming many individual tech stocks.
However, it’s important to note the high volatility associated with bitcoin, which can be hard to handle if one has not spent enough time studying the asset. Bitcoiners tend to view their bitcoin as the world’s best saving instrument, while those not so far down the rabbit hole view it as a riskier investment.
Despite the impressive short-term performance of stocks like NVIDIA, bitcoin offers unique advantages. It has more optionality than any equity because it can be sent anywhere globally, essentially representing value over IP.
This global transferability and the decentralized nature of Bitcoin give it a distinct edge as a form of value storage and transfer.
Moreover, to achieve better returns with tech stocks, one would need to correctly pick the outperforming stock, like NVIDIA, among the “Magnificent Seven.” While indexing can mitigate this risk, it still requires active management and strategic choices.
In contrast, selecting bitcoin — often considered the “best money” in the world — simplifies the investment process. Instead of trying to predict which tech stock might outperform bitcoin in the short term, investors can choose bitcoin for its long-term potential and inherent value proposition.
While bitcoin and tech stocks offer lucrative opportunities, bitcoin’s unique characteristics and long-term growth potential make it an appealing choice for those who want to watch their capital grow without having to worry about counterparty risk or managing which equity is in their portfolio at any given time.
The Power of Curiosity and Tinkering in Both Life and Data Science
The best breakthroughs often come from curious individuals who are willing to tinker, experiment, and explore. The Wright brothers, who were bike mechanics, figured out how to fly.
Satoshi Nakamoto, an anonymous computer scientist, revolutionized the world by creating Bitcoin. Satoshi combined the insights of those who came before him to discover digital scarcity, incentivizing more cooperation and less violence through powerful cryptography.
Just as these pioneers transformed their fields through curiosity and experimentation, you too can harness the power of data science to uncover new insights and innovations.
There are so many great tools out there, including the ones we used today like Python, yfinance, pandas, and matplotlib. Combine these with AI tools and the accessibility of platforms like Google Colab right in your web browser, and it has never been easier to create and tinker.
Data science is not just for the experts — it’s a playground for anyone with curiosity and a willingness to learn. Whether you’re visualizing stock and bitcoin returns or delving into more complex analyses, the tools and knowledge are at your fingertips.
By experimenting with data and refining your skills, you can contribute to this ever-evolving field.
Remember, some of the best tools and insights will come from those who are curious, eager to test, learn, and explore. So go ahead, experiment with data, and see where your curiosity takes you.
The world of data science is vast and full of potential, and you have all the tools you need to make your own breakthroughs. The tinkerers of today will change the world for the better, just as they always have.