Class: FinModeling::FamaFrench::EquityCostOfCapital

Inherits:
Object
  • Object
show all
Defined in:
lib/finmodeling/fama_french_cost_of_equity.rb

Class Method Summary collapse

Class Method Details

.from_ticker(company_ticker, num_days = 6*365) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/finmodeling/fama_french_cost_of_equity.rb', line 93

def self.from_ticker(company_ticker, num_days=6*365)
  company_historical_data = EquityHistoricalData.new(company_ticker, num_days)
  mkt_historical_data = MarketHistoricalData.new
  
  common_dates = company_historical_data.year_and_month_strings & mkt_historical_data.year_and_month_strings
  mkt_historical_data.filter_by_date!(common_dates)
  company_historical_data.filter_by_date!(common_dates)
  
  monthly_excess_returns = company_historical_data.monthly_excess_returns(mkt_historical_data.rf)
  y = GSL::Vector.alloc(monthly_excess_returns)
  x = GSL::Matrix.alloc([1.0]*y.length,
                        mkt_historical_data.rm_rf.first(y.length),
                        mkt_historical_data.smb  .first(y.length),
                        mkt_historical_data.hml  .first(y.length)).transpose
  c, cov, chisq, status = GSL::MultiFit.linear(x, y)
  
  avg_rm_rf = mkt_historical_data.rm_rf.inject(:+) / mkt_historical_data.rm_rf.length.to_f
  avg_smb   = mkt_historical_data.smb.inject(:+)   / mkt_historical_data.smb.length.to_f
  avg_hml   = mkt_historical_data.hml.inject(:+)   / mkt_historical_data.hml.length.to_f
  
  monthly_cost_of_equity = mkt_historical_data.rf.last + (c[1] * avg_rm_rf) + (c[2] * avg_smb) + (c[3] * avg_hml)
  annual_cost_of_equity  = ((monthly_cost_of_equity+1.0)**12)-1.0
  Rate.new(annual_cost_of_equity)
end