Guest User

Untitled

a guest
Jan 21st, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.34 KB | None | 0 0
  1. '''
  2.  
  3. Generates a company's beta based on the desired time frame using daily values
  4.  
  5. Inputs
  6. =========
  7. tic: string
  8. the company's ticker (as per Yahoo! finance)
  9. start_date: string
  10. the earliest date to be used
  11. end_date: string
  12. the last date to be used
  13.  
  14. Parameters
  15. ==========
  16. t_per: string
  17. time period for regression
  18. mkt_tic: string
  19. proxt to use for the stock market
  20. df_tic: data frame
  21. data frame that holds the Yahoo! Finance outputs for the company
  22. df_mkt: data frame
  23. data frame that holds the Yahoo! Finance outputs for the market proxy
  24. tic_hpr: float
  25. daily hpr for the stock
  26. mkt_hpr: float
  27. daily hpr for the market
  28. regress: regression output
  29. output of the beta regression
  30. beta: float
  31. company's beta
  32. df: integer
  33. degress of freedom for the significance tests and CI
  34. beta_se: float
  35. standard error of the beta result
  36. beta_tval: float
  37. the t-value based on the degrees of freedom used
  38.  
  39. Returns
  40. =======
  41. beta : float
  42. the company's beta
  43. CI_low: float
  44. the beta's confidence interval's lower bound
  45. CI_high: float
  46. the beta's confidence interval's upper bound
  47. stat_t: float
  48. the test statistic for the beta's significance
  49. stat_1per: float
  50. the critical value for 1 percent significant
  51. stat_5per: float
  52. the critical value for 5 percent significant
  53.  
  54. '''
  55.  
  56. # packages to use for the analysis
  57. import datetime as date
  58. import pandas as pd
  59. import pandas_datareader as web
  60. import statsmodels.api as sm
  61. import statsmodels.formula.apo as smf
  62. from scipy import stats
  63.  
  64. # derive the inputs from the user
  65. print("What is the company's ticker? (Please use the ticker as per Yahoo! Finance)")
  66. tic = raw_input()
  67.  
  68. print("What would you like to use as a market proxy: S&P 500 (1) or Russell 3000 (2)")
  69. mrk_tic = raw_input()
  70.  
  71. if mrk_tic = 1:
  72. mrk_tic = "GSPC"
  73. elif mrk_tic = 2:
  74. mrk_tic = "RUA"
  75. else:
  76. print("Error: please input 1 or 2 for the two options listed prior")
  77.  
  78. print("Would you like to select a trailing time frame (1) or input custom dates (2)")
  79. t_per = raw_input()
  80.  
  81. date = {-30, -90, -180, -365, -1095, -1825] # vector of days for reference
  82.  
  83. print("Please select: one month (1), three months(2), six months(3), one year(4), three year(5), five year(6) ")
  84. t_per = raw_input()
  85. t_per = date[t_per - 1]
  86. end_date = date.today()
  87. start_date = timedelta(days = t_per)
  88.  
  89. """
  90. if t_per = 1:
  91. date = {-30, -90, -180, -365, -1095, -1825] # vector of days for reference
  92. print("Please select: one month (1), three months(2), six months(3), one year(4), three year(5), five year(6) ")
  93. t_per = raw_input()
  94. t_per = date[t_per - 1]
  95. end_date = date.today()
  96. start_date = timedelta(days = t_per)
  97. elif t_per = 2:
  98. print("Please enter the start date (mm/dd/yyyy)")
  99. start_date = raw_input()
  100. sd_mm =
  101. sd_dd =
  102. sd_yyyy =
  103. print("Please enter the end date (mm/dd/yyyy)")
  104. end_date = raw_input()
  105. ed_mm =
  106. ed_dd =
  107. ed_yyyy =
  108. else:
  109. print("Error: please input 1 or 2 for the two options listed prior")
  110.  
  111. """
  112.  
  113. start_date = date.datetime(sd_yyyy,sd_mm,sd_dd)
  114. end = date.datetime(ed_yyyy,ed_mm,ed_dd)
  115.  
  116. # retrieve stock prices and calculate returns
  117. df_tic = web.get_data_yahoo(tic, start_date, end_date)
  118. adj_close_tic = df_tic['Adj Close']
  119.  
  120. for i = 0:len(adj_close_tic) - 1:
  121. tic_hpr = []
  122. hpr = (adj_close_tic[i] - adj_close_tic[i + 1]) / adj_close_tic[i + 1]
  123. tic_hpr.append(hpr)
  124. next i
  125.  
  126. # retrieve market proxy prices and calculate returns
  127. df_mkt = web.get_data_yahoo(mrk_tic, start_date, end_date)
  128. adj_close_mkt = df_mkt['Adj Close']
  129.  
  130. for i = 0:len(adj_close_mkt) - 1:
  131. mkt_hpr = []
  132. hpr = (adj_close_mkt[i] - adj_close_mkt[i + 1]) / adj_close_mkt[i + 1]
  133. mkt_hpr.append(hpr)
  134. next i
  135.  
  136. # regress the stock returns on the market returns
  137. regress = sm.OLS(tic_hpr, mkt_hpr).fit()
  138. beta = regress.params
  139.  
  140. # find the beta's standard error and compute the 95% CI
  141. beta_se = regress.bse
  142.  
  143. df = len(tic_hpr) - 1
  144. beta_tval = stats.t.ppf(1-0.025, df)
  145.  
  146. CI_low = beta - (beta_tval * beta_se)
  147. CI_high = beta + (beta_tval * beta_se)
  148.  
  149. # find the test statistics
  150. stat_t = regress.tvalues()
  151.  
  152. stat_5per = stats.t.ppf(1-0.025, df)
  153. stat_1per = stats.t.ppf(1-0.005, df)
  154.  
  155. # print results
  156. print("Results:")
  157. print("The beta for %s is %d." % (tic, beta))
  158. print("The CI for the beta is %d to %d." % (CI_low, CI_high))
  159. 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