The main objective of this project is to assist in comprehending and using the CAPM in practical situations. The relationship between systematic risk and expected return for assets, especially stocks, is described by the Capital Asset Pricing Model (CAPM), a fundamental idea in finance. Using Python, Streamlit, and a number of financial libraries, this project enables users to compute and display important metrics, such as beta and alpha, for a stock portfolio in comparison to a benchmark index (such as the S&P 500).
# Function to plot interactive plot
def intrective_plot(df, title):
fig = px.line(title=title, width=1000, height=600)
for i in df.columns[1:]:
fig.add_scatter(x=df["Date"], y= df[i], name=i)
fig.show()# Plot interactive chart
intrective_plot(df, "Stock prices")
# Plot normalized interactive chart
intrective_plot(stocks_df, "Normalized Stock prices")for i in stock_daily_return.columns:
if i != 'Date' and i != 'sp500':
# Use plotly express to plot the scatter plot for every stock vs. the S&P500
fig = px.scatter(stock_daily_return, x = 'sp500', y = i, title = i, width=1000, height=600)
# Fit a straight line to the data and obtain beta and alpha
b, a = np.polyfit(stock_daily_return['sp500'], stock_daily_return[i], 1)
# Plot the straight line
fig.add_scatter(x = stock_daily_return['sp500'], y = b*stock_daily_return['sp500'] + a)
fig.show()



