Scripts
February 4, 2022 ยท View on GitHub
These are mostly python scripts used for data analysis, both in the command line and by the UI component:
setup_db.py- Initialize the database and load basic dataget_stocks.py- Get stock historical dataget_regressions.py- Calculate regressions for stocks versus factorsbmg_series.py- Create your own BMG series of high versus low climate risk stock returnscorrelate.py- Calculate correlation of your BMG series versus other factors and create orthognalized seriesbmg_anlayze.py- Analyze your BMG series's effectiveness in number of climate risk stocks identified by sector
Each of them has a --help option which explains what the parameters are.
Examples
Then use get_stocks.py to get stock information for stocks table and return history for the stock_data table:
python scripts/get_stocks.py -f some_ticker_file.csvfor using a csv source filepython scripts/get_stocks.py -t ALBto load a single stock with tickerALBpython scripts/get_stocks.py -s ALBshows whether there is data stored for the tickerALB
If your stock ticker is a composite stock, it will calculate the historical returns using the weights in the stock_components table.
By default the script will get the Monthly stock values, to get Daily values use --frequency=DAILY, eg:
python scripts/get_stocks.py -f some_ticker_file.csv --frequency=DAILYfor using a csv source filepython scripts/get_stocks.py -t ALB --frequency=DAILYto load a single stock with tickerALBpython scripts/get_stocks.py -s ALB --frequency=DAILYshows whether there is data stored for the tickerALB
To run the regression, use get_regressions.py to save the output in the stock_stats table:
python scripts/get_regressions.pyfor running all the stocks in the databasepython scripts/get_regressions.py -f some_ticker_file.csvfor using a csv source filepython scripts/get_regressions.py -t ALBto run and store the regression for a given stock
Likewise for using Daily values, eg:
python scripts/get_regressions.py -t ALB --frequency=DAILYto run and store the regression for a given stock
It will use the stock returns in the database by default, or if none are found, get them first. It has some optional parameters:
-s YYYY-MM-DDto specify an optional start date, by default it will start at the earliest common date from the stocks and risk factors-e YYYY-MM-DDto specify an optional end date, by default it will end at the latest common date from the stocks and risk factors-i Nfor the regression interval in months (defaults to 60 months).-n FACTOR_NAMEto specify the BMG factor to use. If not specified,DEFAULTwill be used.-hto see all parameters available
To calculate a BMG series and store it in the database:
python scripts/bmg_series.py -n XOP-SMOG -g SMOG -b XOP
where
-n <series name>is the name of your bmg series-bis the ticker of your Brown stock-gis the ticker of your Green stock
Likewise for using Daily values, eg:
python scripts/bmg_series.py -n XOP-SMOG -g SMOG -b XOP --frequency=DAILY
Command Line Scripts
These have been deprecated but are still available and can be used to run regressions in the command line without the database:
python scripts/factor_regression.py
The inputs are:
- Stock return data: Use the
stock_data.csvor enter a ticker - Carbon data: The BMG return history. By default use
carbon_risk_factor.csv. - Fama-French factors: Use either
ff_factors.csv, which are theFama/French Developed 3 FactorsandDeveloped Momentum Factor (Mom), orff_factors_north_american.csv, which are the Fama/FrenchNorth American 3 Factorsand theNorth American Momentum Factor (Mom)series, from the Dartmouth Ken French Data Library The original CARIMA project used the data fromff_factors.csv
The output will be a print output of the statsmodel object, the statsmodel coefficient summary, including the coefficient & p-Values (to replicate that of the CARIMA paper)
stock_price_function.py adjusts this so it returns an object (which is used later)
factor_regression.py loads in the stock prices, the carbon risk factor and the Fama-French factors. The names of these CSVs are asked for. If stock data would be liked to be downloaded, then it will use stock_price_function.py to do so
- Ensure that you have the relevant modules installed
- Have stock_price_script.py in the same folder as factor_regression.py
- Have your factor CSVs saved
- Run factor_regression.py and follow the prompts and enter the names of the CSVs as asked