Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Generates a company's beta based on the desired time frame using daily values
- Inputs
- =========
- tic: string
- the company's ticker (as per Yahoo! finance)
- start_date: string
- the earliest date to be used
- end_date: string
- the last date to be used
- Parameters
- ==========
- t_per: string
- time period for regression
- mkt_tic: string
- proxt to use for the stock market
- df_tic: data frame
- data frame that holds the Yahoo! Finance outputs for the company
- df_mkt: data frame
- data frame that holds the Yahoo! Finance outputs for the market proxy
- tic_hpr: float
- daily hpr for the stock
- mkt_hpr: float
- daily hpr for the market
- regress: regression output
- output of the beta regression
- beta: float
- company's beta
- df: integer
- degress of freedom for the significance tests and CI
- beta_se: float
- standard error of the beta result
- beta_tval: float
- the t-value based on the degrees of freedom used
- Returns
- =======
- beta : float
- the company's beta
- CI_low: float
- the beta's confidence interval's lower bound
- CI_high: float
- the beta's confidence interval's upper bound
- stat_t: float
- the test statistic for the beta's significance
- stat_1per: float
- the critical value for 1 percent significant
- stat_5per: float
- the critical value for 5 percent significant
- '''
- # packages to use for the analysis
- import datetime as date
- import pandas as pd
- import pandas_datareader as web
- import statsmodels.api as sm
- import statsmodels.formula.apo as smf
- from scipy import stats
- # derive the inputs from the user
- print("What is the company's ticker? (Please use the ticker as per Yahoo! Finance)")
- tic = raw_input()
- print("What would you like to use as a market proxy: S&P 500 (1) or Russell 3000 (2)")
- mrk_tic = raw_input()
- if mrk_tic = 1:
- mrk_tic = "GSPC"
- elif mrk_tic = 2:
- mrk_tic = "RUA"
- else:
- print("Error: please input 1 or 2 for the two options listed prior")
- print("Would you like to select a trailing time frame (1) or input custom dates (2)")
- t_per = raw_input()
- date = {-30, -90, -180, -365, -1095, -1825] # vector of days for reference
- print("Please select: one month (1), three months(2), six months(3), one year(4), three year(5), five year(6) ")
- t_per = raw_input()
- t_per = date[t_per - 1]
- end_date = date.today()
- start_date = timedelta(days = t_per)
- """
- if t_per = 1:
- date = {-30, -90, -180, -365, -1095, -1825] # vector of days for reference
- print("Please select: one month (1), three months(2), six months(3), one year(4), three year(5), five year(6) ")
- t_per = raw_input()
- t_per = date[t_per - 1]
- end_date = date.today()
- start_date = timedelta(days = t_per)
- elif t_per = 2:
- print("Please enter the start date (mm/dd/yyyy)")
- start_date = raw_input()
- sd_mm =
- sd_dd =
- sd_yyyy =
- print("Please enter the end date (mm/dd/yyyy)")
- end_date = raw_input()
- ed_mm =
- ed_dd =
- ed_yyyy =
- else:
- print("Error: please input 1 or 2 for the two options listed prior")
- """
- start_date = date.datetime(sd_yyyy,sd_mm,sd_dd)
- end = date.datetime(ed_yyyy,ed_mm,ed_dd)
- # retrieve stock prices and calculate returns
- df_tic = web.get_data_yahoo(tic, start_date, end_date)
- adj_close_tic = df_tic['Adj Close']
- for i = 0:len(adj_close_tic) - 1:
- tic_hpr = []
- hpr = (adj_close_tic[i] - adj_close_tic[i + 1]) / adj_close_tic[i + 1]
- tic_hpr.append(hpr)
- next i
- # retrieve market proxy prices and calculate returns
- df_mkt = web.get_data_yahoo(mrk_tic, start_date, end_date)
- adj_close_mkt = df_mkt['Adj Close']
- for i = 0:len(adj_close_mkt) - 1:
- mkt_hpr = []
- hpr = (adj_close_mkt[i] - adj_close_mkt[i + 1]) / adj_close_mkt[i + 1]
- mkt_hpr.append(hpr)
- next i
- # regress the stock returns on the market returns
- regress = sm.OLS(tic_hpr, mkt_hpr).fit()
- beta = regress.params
- # find the beta's standard error and compute the 95% CI
- beta_se = regress.bse
- df = len(tic_hpr) - 1
- beta_tval = stats.t.ppf(1-0.025, df)
- CI_low = beta - (beta_tval * beta_se)
- CI_high = beta + (beta_tval * beta_se)
- # find the test statistics
- stat_t = regress.tvalues()
- stat_5per = stats.t.ppf(1-0.025, df)
- stat_1per = stats.t.ppf(1-0.005, df)
- # print results
- print("Results:")
- print("The beta for %s is %d." % (tic, beta))
- print("The CI for the beta is %d to %d." % (CI_low, CI_high))
- print("The t-stat is %d, which compares to the 5 percent and 1 percent critical values of %d and %d, respectively." % (stat_t, stat_5per, stat_1per))
Add Comment
Please, Sign In to add comment