Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Leverage Python for Quantitative Finance
Leverage Python for Quantitative Finance
The basic stock return calculation involves the calculation of the past period of daily returns of stock and taking the mean of returns which will later be either multiplied by the number of business days of a year or just compounded by the daily return to year in a more sophisticated way. In this post, we will have a look at State Contingent Expected Returns implementation in Python.
Perhaps the biggest limitation of the mean-based Expected Return is the fact that it relies solely on historic data.
We could overcome this by estimating ‘State Contingent’ Expected Returns.
With this method, we incorporate the probabilities of future events occurring. And estimate expected returns conditional on those events / ‘states’ occurring. Of course, those probabilities would be subjective to our understanding of the stock market and macroeconomic expectations.
And before we dive into the coding part we should have the required pieces for the calculation which are forecasted returns by analysts and drive some conclusions of their likelihood of materializing in one year period.
Currently, for the next 12 months, the analysts’ expectations for Apple stock;
Min: $133.00 (–8.86%)
Max: $210.00 (+43.9%)
Average: $175.09 (+19.98%)
Let’s say, you also expect the likelihood of max, average, and min states to be 25%, 35%, and 40% respectively.
Here is how that is done in Python:
import yfinance as yf def stock_history(ticker, period="3y", interval="1d", start=None, end=None, prepost=False, actions=True, auto_adjust=True, back_adjust=False, proxy=None, rounding=False, tz=None, timeout=None, **kwargs): """ Return the stock history data from yahoo finance API endpoint e.g. : ticker.history(interval='1m', start='2022-01-03', end='2022-01-10') e.g. : ticker.info(); info.keys() """ yf_data = yf.Ticker(ticker) return yf_data.history(period=period) # Getting the stock data from Yahoo Finance data = stock_history("aapl") # Calculating the mean returns for Apple data["returns"] = data["Close"].pct_change(1) print(round(data["returns"].mean()*250*100,3)) # Calculating the outcome of forecasted returns and their likelihood max_state = 0.43*0.35 min_state = -0.08*0.25 average_state = 0.19*0.40 state_contingent_expected_return = round((max_state + min_state + average_state)*100,3) print(state_contingent_expected_return)
>>>27.572
>>>5.7505
So yearly average historical return for Apple is 27.5% and the calculated State Contingent Expected Return is 20.65%
I believe we kinda stick to very conservative expectations for a company like Apple and it would make sense to expect a lower yearly return for 2023 with the given economic and financial conditions in the US. So, now it is up to you to either adjust the states of the expected returns or decide whether it is a good investment decision to risk your money for an expected return of 20.65% for the next 12-month period which is actually lower than Apple’s “3-year” mean yearly return.