Fama French Regression in Python

Поділитися
Вставка
  • Опубліковано 17 січ 2025

КОМЕНТАРІ • 89

  • @julianrenner7154
    @julianrenner7154 4 роки тому +8

    Awesome - thanks for your great videos :) This is exactly what I was looking for. I would be very happy if you could create even more videos for the 'Python for Finance' playlist

    • @Algovibes
      @Algovibes  4 роки тому

      Thanks a lot for your comment. Great to hear :-)
      Happy to announce that there will be more videos in the future for the Python for finance playlist.
      Best regards

    • @julianrenner7154
      @julianrenner7154 3 роки тому

      @@Algovibes I'm trying to understand your code also from a theoretical point of view. However, I have some problems (e.g., why are the returns calculated as you show). Do you know a good reference? Maybe in a tutorial style? I skimmed through the original paper by Fama and French, and it was not detailed enough for me :-/

  • @dorothya.knudson9095
    @dorothya.knudson9095 4 роки тому +2

    Thank you so much for explaining all this! It is a very helpful demonstration!

    • @Algovibes
      @Algovibes  4 роки тому

      Thank you very much for your kind comment :) I am glad it was helpful.

  • @rraul
    @rraul 2 роки тому +1

    Hello, I'm doing this regression on my strategy today. Very easy to undestand. Thanks you, master !

    • @rraul
      @rraul 2 роки тому

      _________coef std err t P>|t| [0.025 0.975]
      const 0.0475 0.010 4.764 0.000 0.028 0.067
      quaterly results. pretty good

    • @Algovibes
      @Algovibes  2 роки тому +1

      Thx a lot rraul

  • @lilian47-x
    @lilian47-x 3 роки тому +1

    Followed the video and was able to replicate it. Thanks very much!

    • @Algovibes
      @Algovibes  3 роки тому

      Awesome, thanks a lot for the feedback Lisa.

  • @ashokpandey57
    @ashokpandey57 2 роки тому +1

    Amazing!

  • @joycesun7293
    @joycesun7293 3 роки тому +1

    Thanks so much for this! added to my favourites haha

    • @Algovibes
      @Algovibes  3 роки тому

      Thanks for watching :-)

  • @birolyildiz
    @birolyildiz 2 роки тому +1

    great video. thak you very much for your sharing.

  • @sanjaymamidipaka3546
    @sanjaymamidipaka3546 4 роки тому +2

    what a g. great video man.

    • @Algovibes
      @Algovibes  4 роки тому

      Hi mate,
      thank you very much for your kind comment. Very happy that you like it :-)

  • @ihorhurnyak7666
    @ihorhurnyak7666 3 роки тому +1

    very good work ... thank you

    • @Algovibes
      @Algovibes  3 роки тому

      Thanks a lot for watching and your comment. I appreciate it :-)

  • @jenshalsberghe
    @jenshalsberghe 4 роки тому

    really good video! well explained and not leaving out any details

    • @Algovibes
      @Algovibes  4 роки тому

      Hi Jens,
      thank you so much for your feedback!
      Happy to have you on my channel :)

  • @MrGallocard
    @MrGallocard 2 роки тому +2

    Amazing work, is it possible to also create a time series graph of the factor loading?

    • @Algovibes
      @Algovibes  2 роки тому

      Thanks buddy. Sure, you could just plot the columns containing the factors.

  • @TheTrialRun
    @TheTrialRun 3 роки тому +1

    wonderful video. can you please suggest the python codes to add MOM factor into this (Carhart model)

    • @Algovibes
      @Algovibes  3 роки тому +1

      Thanks mate :-)
      The MOM factor can be found on Ken Frenchs page. You will need to add it as an additional column and run the regression again.
      BTW I have some videos on momentum. You might find them interesting as well.

    • @TheTrialRun
      @TheTrialRun 3 роки тому +1

      @@Algovibes Thanks brother.

  • @BinhKieu82
    @BinhKieu82 3 роки тому +2

    Great video man, very straightforward & easy to digest your stuffs :).
    I have concerns about the case if all three beta of 3 factors are significant (p-value

    • @Algovibes
      @Algovibes  3 роки тому +1

      Thanks man, appreciate your kind words.
      Regarding your questions: They are all covered with examples here -> www.efficientfrontier.com/ef/101/roll101.htm
      Additionally: The higher (positive) the alpha - the better.

    • @BinhKieu82
      @BinhKieu82 3 роки тому

      @@Algovibes Thanks for the link, I can easily answer myself after reading it :).
      Could you make a video about combining other alpha such as momentum, mean reversion with the 3FM

  • @sonik88
    @sonik88 Рік тому +1

    Hi, perhaps a silly question, but do you have a video/resources as to how someone can make the factors data from raw data (price data, size, etc.)? I am referring to the SMB, HML F_F dataset that you have downloaded with directly with the DataReader? - Thank you in advance

    • @Algovibes
      @Algovibes  Рік тому

      I don't but I keep my fingers crossed you setting this up. That would be quite interesting!

  • @НеллиАбдрахманова-ф4ы

    Thank you!

    • @Algovibes
      @Algovibes  4 роки тому +1

      привет and thanks for watching!
      пожалуйста :-)

  • @neerajkumarsharma8853
    @neerajkumarsharma8853 2 роки тому +2

    When the data is time series, can we use normal regression for performing this analysis?

    • @Algovibes
      @Algovibes  2 роки тому

      Not quite sure about the question as the data shown in the video is a time series. Can you elaborate?

  • @hasanerkilic6617
    @hasanerkilic6617 2 роки тому +1

    Hey could you maybe send me the code, Im really struggling and I really need it for my thesis, thanks in advance, I would really appreciate your help

  • @stefanovacca4475
    @stefanovacca4475 2 роки тому +1

    Thanks for the video, clear explanation.
    One question. Do you think we consider more variables in the model? Let's think about Amazon, we can add features like number of products sold per month, number of positive reviews per month, and so on? So the theory could be to expand the model with new features not related with finance.

    • @Algovibes
      @Algovibes  2 роки тому

      Welcome man! Thanks for watching. Sure, but then it wouldn't be the Fama French model anymore :P There are already extensions to the model (Carhart, FF5, etc.) but ofc you can build a regression model with other factors. Let me know when you find a good one! ;-)

  • @undermygarage
    @undermygarage 3 роки тому

    You are fantastic, thank you 😊

    • @Algovibes
      @Algovibes  3 роки тому

      Thanks a lot for your comment. I really appreciate it :-)

  • @ekaterinakozello3219
    @ekaterinakozello3219 Рік тому +1

    Thank you for your video! Is this code applicable to a separate stock (not fund)? If I am doing this for AAPL, for example, the SMB factor becomes insignificant

  • @chrisjackowski591
    @chrisjackowski591 Рік тому +1

    IS there a simpler version to calculate a monthly stock return other than using resample("M").agg(lambda x:(x+1).prod()-1) ? Thank you.

    • @Algovibes
      @Algovibes  Рік тому +1

      Yes,
      explained that in detail here:
      ua-cam.com/video/fWHQwqT3lNY/v-deo.html
      cheers buddy

  • @WaiWaiG0r
    @WaiWaiG0r 3 роки тому +1

    Thanks for your fantastic video, which fully explained the Fama-french coding in python. However, do you mind digging deeper into the interpretation of the OLS results?

    • @Algovibes
      @Algovibes  3 роки тому +1

      Thanks a lot for watching and your kind comment :-)
      Sure, what do you want to know or where did I not go into deep enough?

    • @WaiWaiG0r
      @WaiWaiG0r 3 роки тому +1

      @@Algovibes the coding is perfect, I’m just wondering if you can talk more about the last part of your video which is the interpretation of the OLS result. Because I found it difficult to analyze and interpret the result. For example, what is the meaning of 275.3 on F-statistics? Why the p values of Mkt-RF and HML are zero?...

    • @Algovibes
      @Algovibes  3 роки тому

      @@WaiWaiG0r Thanks a lot :)
      I noted a video on the interpretation for the OLS regression. Might take some time as I am planning on some other stuff before but I think that could be pretty interesting.
      The p-values show that both the market and the HML factor are highly significant.
      In general it holds: The lower the p-value the more significant a coefficient.
      As we try to explain the return of the funds at least our pulled data suggests that it is nearly impossible to say that we have 0 effect on the return considering these two coefficients.
      So why are they zero in simple words? Because they have an effect on the assets (funds) return.

  • @marcuseinstulen2655
    @marcuseinstulen2655 3 роки тому +1

    Thank you for a great video!
    However I have some questions. I'm currently doing a project where I'm going to test the model on two market conditions, unstable and stable on the Swedish Stock Market. Unlike you I will not use an ETF but instead I want to use returns of individual assets (about 200-250 per year) who are listed on the Large Cap list of Stockholm. The factor-values will be obtained from Stockholm School of Economics data base (SHoFDB). My question is, how do I get all of this stock returns in to Python? And when I have the stock returns in python, can I follow the steps in your video or will the procedure looks different?
    Again, thanks for a great video!

    • @Algovibes
      @Algovibes  3 роки тому

      Hi, thanks a lot for watching and your kind comment :-)
      Your project sounds really cool. To get the returns you could work with a SQL database maybe? Use the return files (I assume they are csvs), read them in with pandas and store them in a DB. Or just read them in within the script but this will be pretty inefficient.
      Regarding the intersection between python and SQL you can check out my vid on building a finance database: ua-cam.com/video/5bUn-D4eL4k/v-deo.html
      Without knowing what exactly your approach will be: the steps should be pretty similar if you have the factor values yes.
      Only thing you would need to amend is to functionalize it so that you can run the regression for every stock in a loop or similar.

    • @marcuseinstulen2655
      @marcuseinstulen2655 3 роки тому

      Thank you for your answear!
      Is it possible for me to contact you via email?

    • @Algovibes
      @Algovibes  3 роки тому +1

      @@marcuseinstulen2655 Not yet but I will set up a mail in the info tab soon.

  • @Cunetch
    @Cunetch 3 роки тому +1

    Hi, could you explain to me how to find all the available datasets (not only this one from Fama-French) using the get_available function? Thank you very much!!

    • @Algovibes
      @Algovibes  3 роки тому +1

      Jep they are listed in the docs. Chapter 2.1.8 buildmedia.readthedocs.org/media/pdf/pandas-datareader/latest/pandas-datareader.pdf

    • @Cunetch
      @Cunetch 3 роки тому

      ​@@Algovibes Thank you for your help! Learning a lot of new things, very useful reading and trying to understand documentations for every package. Could you please consider making a video on trading strategies with multiple conditions for signals (like entry trigger, take profit, and SL)?

  • @lee_badda
    @lee_badda 3 роки тому +1

    sorry for the simple question, but can you explain more about the alpha? my algorithm came out alpha negative. What does it imply??

    • @lee_badda
      @lee_badda 3 роки тому +1

      what does it mean to have "a positive performance over the risk of 3 fama-french factor"?

    • @Algovibes
      @Algovibes  3 роки тому +1

      Never apologize for a question and this question is not necessarily an easy one!
      We set up an equation which is explaining the return of an asset. The return can be explained by the factors but also with an unexplained part which is the alpha.
      In Asset Management this alpha is important as it basically shows how the funds manager was doing his job.
      To break it down really simple: If he was performing well it could be due to the fact that the market performed good (Market factor). But it could also be due to the fact he overweighted growth stocks or small caps (the other two factors). But if he generated alpha even though we account for those factors he would probably doing a pretty good job.
      I hope this makes sense to you!

    • @lee_badda
      @lee_badda 3 роки тому

      @@Algovibes So your saying is that (to put it to the extreme,) if the market didn't performed well and he didn't overweighted growth stocks nor the small caps and yet if there was still an excess return, this excess return is explained by the alpha? Thank you so much for this amazing video. Can't wait for more contents:)

  • @ihebbibani7122
    @ihebbibani7122 3 роки тому +1

    Hi ,
    First of all thanks a loot for your videos.
    Second , would like to know about the fama French factors are they always to the same values of factors (of course depending on the number of factors...). In other terms , can I use the same factor data for other assets involving 3 factors fama French , is it like a fixed data that we could use with any asset ..?
    Thanks for your help.
    ps: Great content and very good explanations..;)

    • @Algovibes
      @Algovibes  3 роки тому

      Thanks a lot for watching and your kind comment. Appreciate it :-)
      Usually you need to calculate the factors yourself. As the FDGRX is mainly invested in American stocks I am going the easy way and take the factors provided by Ken French. These factors are calculated over all CRSP stocks (more info: www.crsp.org/resources/data). To be 100% correct you would need to calculate the factors for a given dataset (in this case the FDGRX components). Taking a broad ETF mainly invested in America you can take Ken French factors as an estimate. The results will be comparable.
      Best regards

    • @ihebbibani7122
      @ihebbibani7122 3 роки тому

      @@Algovibes "Usually you need to calculate the factors yourself"

    • @Algovibes
      @Algovibes  3 роки тому

      @@ihebbibani7122 ? :-D

  • @MA-qm7yv
    @MA-qm7yv 2 роки тому

    Hello, thanks for the video ! I have tested several ticker but I keep having a R² below 50% :( Any idea how to improve the model ?

  • @bleacherz7503
    @bleacherz7503 3 роки тому +1

    Super generous

    • @Algovibes
      @Algovibes  3 роки тому

      :-) Thanks for watching and your comment. Appreciate it!

  • @kaskos
    @kaskos 4 роки тому +1

    Hello there, first of all, thank you very much for this demonstration! It is very helpful.
    I do have a problem, though. When I type fundsret_mtl, I do not get the name of the company as you do. How can I fix this problem?

    • @kaskos
      @kaskos 4 роки тому

      And also, when I type Merge I get Adj Close instead of the stock name

    • @Algovibes
      @Algovibes  4 роки тому +1

      Hi Mustafa :-)
      Thanks a lot for your kind comment. Totally happy that it was helpful!
      The solution to both of your problems:
      When you are requesting the data do it with a list.
      So in my example you see that I am using funds = ["^FDGRX"]
      right under defining the start and end date in the very beginning.
      If you are not doing that you are getting a Series instead of a Dataframe and getting the undesired consequences.
      Please let me know if that was solving your problem.
      Best regards :)

    • @kaskos
      @kaskos 4 роки тому +1

      Thank you very much for the fast reply. I actually managed to fix the problem just after I commented :)

  • @hansfriedrich7324
    @hansfriedrich7324 2 роки тому

    So if I want to annualize the alpha of the fund I just multiply the value of the constant by 12, because it is monthly data? And if I would have made the regression with daily data I would have to multiply the value of the constant with the number of the trading days in the year?

  • @andreso4116
    @andreso4116 4 роки тому

    Hi there. need some help!
    I'm following your script to the letter with Python 3.8
    Not sure why when I call famafench it doesn't call the file
    "reader.DataReader('F-F_Reasearch_Data_Factors', 'famafrench', start, end)"
    gives me an error:
    "RemoteDataError: Unable to read URL: mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/F-F_Reasearch_Data_Factors_CSV.zip"
    perhaps, explain the data reader call, it may have changed source or name. I couldn't figure it out!

    • @Algovibes
      @Algovibes  4 роки тому

      Hi mate,
      I just checked your problem and rewrote the code myself : No problems.
      Try using this exact syntax and check if you are getting the table:
      import pandas_datareader.data as reader
      import datetime as dt
      end = dt.date(2020,12,31)
      start = dt.date(end.year - 5, end.month, end.day)
      reader.DataReader('F-F_Research_Data_Factors','famafrench', start, end)[0]
      Please let me know if you still have problems. Sometimes it is btw just that you have no internet connection. Experienced that many times.
      best regards and thank you very much for watching :-)

    • @andreso4116
      @andreso4116 4 роки тому

      @@Algovibes
      Below is the whole thing and try it again and same error.
      I gather that It cannot read the file for some reason. Perhaps I am missing a module???
      ****************** CODE ************************
      import pandas_datareader.data as reader
      import pandas as pd
      import datetime as dt
      import statsmodels.api as sm
      end = dt.date(2020,12,31)
      start = dt.date(end.year - 5, end.month, end.day)
      fund = ['FDGRX']
      fundsret = reader.get_data_yahoo(fund, start, end)['Adj Close'].pct_change()
      fundsret_mtl = fundsret.resample('M').agg(lambda x: (x+1).prod() - 1)
      # up to here all runs good!
      factors = reader.DataReader('F-F_Reasearch_Data_Factors', 'famafrench', start, end)
      ****************** CODE ************************
      OUTPUT:
      Traceback (most recent call last):
      File "", line 1, in
      factors = reader.DataReader('F-F_Reasearch_Data_Factors', 'famafrench', start, end)
      File "C:\Users\andres\anaconda3\lib\site-packages\pandas\util\_decorators.py", line 199, in wrapper
      return func(*args, **kwargs)
      File "C:\Users\andres\anaconda3\lib\site-packages\pandas_datareader\data.py", line 465, in DataReader
      return FamaFrenchReader(
      File "C:\Users\andres\anaconda3\lib\site-packages\pandas_datareader\famafrench.py", line 75, in read
      return super(FamaFrenchReader, self).read()
      File "C:\Users\andres\anaconda3\lib\site-packages\pandas_datareader\base.py", line 100, in read
      return self._read_one_data(self.url, self.params)
      File "C:\Users\andres\anaconda3\lib\site-packages\pandas_datareader\famafrench.py", line 101, in _read_one_data
      data = self._read_zipfile(url)
      File "C:\Users\andres\anaconda3\lib\site-packages\pandas_datareader\famafrench.py", line 55, in _read_zipfile
      raw = self._get_response(url).content
      File "C:\Users\andres\anaconda3\lib\site-packages\pandas_datareader\base.py", line 181, in _get_response
      raise RemoteDataError(msg)
      RemoteDataError: Unable to read URL: mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/F-F_Reasearch_Data_Factors_CSV.zip
      Response Text:
      b'



      404 - File or directory not found.





      Server Error


      404 - File or directory not found.
      The resource you are looking for might have been removed, had its name changed, or is temporarily unavailable.




      '

    • @Algovibes
      @Algovibes  4 роки тому

      @@andreso4116
      First of all sorry for the inconveniences you experienced but I think you just have a typo in your syntax:
      factors = reader.DataReader('F-F_Reasearch_Data_Factors', 'famafrench', start, end)
      -> get rid of the a -> Research_Data_Factors
      Let me know if this has solved your issue.

  • @jngl1371
    @jngl1371 4 роки тому

    hey, any idea how we could create the SMB HML factors on our own (in python) from a excel spreadsheet with the relevant firm data, instead of copying it from french's website? If you have a tip where i can learn this i would be grateful, thanks

    • @Algovibes
      @Algovibes  4 роки тому +2

      Hi
      First of all thank you very much for watching.
      To calculate the factors I would recommend sticking to the procedure from Fama French (alert: this is not going to be easy!).
      Check this link first:
      mba.tuck.dartmouth.edu/pages/faculty/ken.french/Data_Library/f-f_factors.html
      For an exact description of how you are building the portfolios check the paper 'Common risk factors in the returns on stocks and bonds' from Fama French:
      rady.ucsd.edu/faculty/directory/valkanov/pub/classes/mfe/docs/fama_french_jfe_1993.pdf
      Page 9 contains a pretty straightforward description on how portfolios are constructed.
      As a practical tip in Python to form portfolios I would suggest to use quantiles (I actually did that in my most recent video (Momentum strategy) with pandas if you need a technical hint on a very basic level).
      Would be nice to know if that was a helpful hint for you.
      I wish you good luck on your project :-)
      Best regards

    • @jngl1371
      @jngl1371 4 роки тому

      @@Algovibes thanks for the answer, i already read the fama french papers and understand the theory, however the practical part is where im having a hard time. i just startet learning python for this and am still not sure how to code a model that sorts the stocks every year new, for multiple years and computes the return. Your momentum stategy video was helpful and i will definitely have a second closer look into the code you used.

  • @shakthiprasadpai9584
    @shakthiprasadpai9584 Рік тому +1

    WHY SHOULD WE DO THE FACTORING

  • @johannesambaye7710
    @johannesambaye7710 4 роки тому

    I have the problem to import pandas_datareader. On the homepage of "datareader" was explained that pandas_datareader is no longer compatible to retrieve the data from Fama french. Do you have more information and do you have an alternative for me?
    Thanks in advance

    • @Algovibes
      @Algovibes  4 роки тому

      Hi :)
      Well it works totally fine for me - just checked. Also checked the documentation again - data reader still compatible with Ken French data library.
      Could you link your source or just show your error message which is popping up?
      Thanks a lot in advance!

    • @johannesambaye7710
      @johannesambaye7710 4 роки тому

      @@Algovibes
      Thanks for your quick message. Fortunately I was able to solve the problem. I now have a wide problem which I am currently trying to solve. I use several shares and my database is from Excel. All steps have worked so far except for the merge part.
      Line 50 in minute 14:56 -You write " merge["FDGRX-Rf"=merge.FDGRX-merge.RF
      My variant: merge ["BAC-RF", "FRD-RF", "FDX-RF", "WMT-RF"]=merge.BAC-merge.RF,merge.FRD-merge.RF,merge.FRD-merge.RF,merge.WMT-merge.RF
      Here you can see that I use several shares
      Here I get an error message: ValueError: Length of values does not match length of index
      localhost:8888/notebooks/Untitled1.ipynb
      Thanks for your help
      Johannes

    • @Algovibes
      @Algovibes  4 роки тому +1

      @@johannesambaye7710
      Could be a ton of possible reasons but I think in this case it is a simple syntax error.
      Use double brackets to solve it:
      merge [["BAC-RF", "FRD-RF", "FDX-RF", "WMT-RF"]] =merge.BAC-merge.RF,merge.FRD-merge.RF,merge.FRD-merge.RF,merge.WMT-merge.RF
      Please let me know if that has solved your problem. Otherwise share your full code so that I can help you out.
      Best regards :)

    • @johannesambaye7710
      @johannesambaye7710 4 роки тому

      @@Algovibes
      Hi :),
      Unfortunately, it did not work. Here is the code and the error message, I hope you can do something with it. Thank you very much for the support.
      github.com/Johannes-90/Fama-French-/blob/main/Fama%20French%20-Copy1.ipynb

    • @johannesambaye7710
      @johannesambaye7710 4 роки тому

      I have managed to run individual regressions with the different stocks, but packing all stocks into one regression was not possible.

  • @fartelenglebert2431
    @fartelenglebert2431 3 роки тому +1

    Variable not wariable.
    Subtract not substract

    • @Algovibes
      @Algovibes  3 роки тому

      I waited too long for this comment, honestly!
      This video is pretty old, let me know if I improved on my English on my newer ones.
      Your feedback is appreciated!