0% found this document useful (0 votes)
10 views28 pages

Data Science Crash Course Overview

The document serves as a crash course on data science, covering essential topics such as computing basics, programming logic, and the use of SQL in SAS for financial studies. It discusses the limitations of SAS compared to open-source languages like R, emphasizing the importance of data manipulation and analysis techniques. Additionally, it outlines various procedures for running fixed effects and error corrections in SAS, as well as methods for creating and analyzing financial portfolios.

Uploaded by

Jack Hong
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views28 pages

Data Science Crash Course Overview

The document serves as a crash course on data science, covering essential topics such as computing basics, programming logic, and the use of SQL in SAS for financial studies. It discusses the limitations of SAS compared to open-source languages like R, emphasizing the importance of data manipulation and analysis techniques. Additionally, it outlines various procedures for running fixed effects and error corrections in SAS, as well as methods for creating and analyzing financial portfolios.

Uploaded by

Jack Hong
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

A CRASH COURSE

ON DATA SCIENCE
W E A R E R E A L D ATA S CI E N T I S T S A N D W E
DON’T KNOW IT

J AC K HO NG
2015
AGENDA

• Introduction
• Basic programming logic
• SQL in SAS
• Important procedures in Financial Studies
COMPUTING BASICS (HARDWARE)
• What does a computer do?
– A computer computes!

• Using the biological brain as an analogy

Central Processing Unit (CPU) Random Access Memory (RAM) Disk Drives
= = =
Frontal Lobe (calculates) Working (fast) memory Long term (slow) memory
COMPUTERS VS HUMANS
• Computers beat brains in terms of
– Speed (computation and recall)
• How long do you think it will take my
MacBook Pro to compute (1 + 1), 100
million times?
– Accuracy (perfect recall)
• Will a computer system return different
data the same way you call it every time?
– Working volume
• How many wikipedia (entire) can a
S$140 hard disk hold?
COMPUTERS VS HUMANS
• The common saying is that computers are
way behind humans in higher cognitive skills,
such as
– Relationship mapping
– Synthesizing different ideas
– Extracting contexts
REALLY?
• Context-based AI
– Siri
• “PWN” Jeopardy
– IBM’s Watson
• Predict human behavior
– Telling you what you want before
you know it
– Amazon, Tesco etc
• Deep Learning
– Without teaching a program what is
the definition of a cat, it can
differentiate a cat from other
animals
WE ARE EQUIPPED FOR THE FUTURE
OF DATA SCIENCE
• The core value-add of these futuristic applications lies in the
ability to make sense of what has happened, is happening, and
predict what will happen
• The tools in which this core value-add is built upon
– Insane amounts of data
– Data varieties
– Statistics, econometrics and other quantitative scientific
methods
• Computing resources and skillsets are important because
they are the only way to unlock this core value-add
• The future of data science is not tailored for computer
science specialists, but to applied scientists who are
competent in the art of computer science
WHY SAS WILL NOT BE THE DOMINANT
LANGUAGE/PLATFORM OF DATA SCIENTISTS
• SAS is a proprietary language and programming competencies in it are not fully
transferable
– Uses syntax and logic that are partially independent from mainstream programming languages
• Many common logic are not supported by SAS, or require convoluted syntax and steps
• Users have easy control over the data manipulation part, but the procedural coding part is complex
(PROC IML) – Ask MATLAB and R users

• SAS’s architecture is rigid


– It is hard to mix and match data technologies. Compatibility issues are rife.

• SAS is slow to update


– R (and Stata) has a wider variety of recent statistical procedures

• SAS has weak visualization capabilities


• R and other open-source languages have a large supporting community that improves and
extends the language and modules (anybody can do it)
• SAS cannot handle programming requirements outside its limited scope
– E.g. Go to [Link], extract the json (javascript object notation) list of historical
exchange rates for hundreds of countries, munge them into a dataset and perform analysis?
WHY DO WE STILL LEARN ?
• First word: Legacy / Second word: Convenience
• Academia legacy (Finance)
– WRDS datasets are in SAS
– WRDS server (Unix and Windows) uses PC-SAS
– Many empirical researchers in Finance uses SAS, and their SAS codes can be found in WRDS
• Industry legacy
– Many commercial entities have deployed SAS since 1970s
– However, note that these deployments are typically ‘click and run’
• SAS is pretty much focused on pre-building applications to target industry needs (e.g. revenue management)
• Jobs requiring coding in SAS are technical in nature and may have little to do with data analysis (e.g. improving
• However, you may get a job in SAS developing statistical applications
• SAS is stable and easier to code for brute-force data manipulation
– Handling large datasets using open-source software typically require more codes to link up operations
between CPU, RAM and HDD
– SAS is both a database and programming platform, and its proprietary codes (e.g. PROC MODEL) are
much shorter
ARCHITECTURE DIFFERENCES I
• A primer to software-hardware interaction
• What happens when you execute a piece of code?
– CPU computes according to programming logic
• Operators: + - / x
• Logic: if/then/else
• Flow: while x < 1
– Where does it store and read data and steps?
• Random Access Memory (RAM)
• Requires unique physical locations on the hardware (memory addresses)
– Since RAM is like a storage, can we use disk drives in place of RAM?
• Yes, OS X and Windows are doing that (virtual memory/pagefile/swap)
• But they only use it for very specific situations, why?
– RAM is 33x faster than SSD, which is 100x faster than HDD
– Disk drives have limited writes (SSD will wear out in 2 months)
– CPUs can only pre-load data into its cache (small temporary storage area) from RAM
ARCHITECTURE DIFFERENCES II
• An average laptop has 4GB of RAM
– IBES detailed earnings estimates (DETU_EPSUS) is 2.2GB in size
– CRSP daily stock file (DSF) is 3.3GB in size
– Compustat fundamental annual (FUNDA) is 5.5GB in size
• The strategy is to make sure that the size of the required
dataset for RAM-only computation is at the bare minimum
– RAM-only computation: e.g. complex econometrics procedures
• Database software helps us to munge (manipulate) data before
we reach this computation stage
– E.g. MySQL, Microsoft SQL, MongoDB, IBM Oracle
– They perform CRUD (create, read, update and delete) using disk
drive storage space
• Computation is still done by CPU and RAM, but intermediate and
final results are written to disk space
ARCHITECTURE DIFFERENCES III
• SAS is a platform that combines database and programming • Open-source platforms are free, flexible, easily customizable
logic and has a huge support community
– Users do not need to set up each component separately – However, users have to set up their own environment
– Most of our working time on SAS involves data munging – Generally deploy a popular interpreter as ‘glue’ platform
(database-like syntax) (e.g. Python, Ruby)
– Running procedures generally involve invoking existing – Separately install database servers
SAS functions (corr, reg, means, summary, univariate etc)
– Import user-contributed data analytics codes (pandas,
scikit, scipy, numpy) or BYOC (bring your own codes)
RDBS VS NOSQL
• Relation-based Database Systems
– All SQL (Structured Query Language) variants, Oracle, SAS etc
– Once properly setup and indexed
• Very fast
• Very stable
– Good support and long track record

• NoSQL Databases
– Stands for “Not Only SQL”
– MongoDB, Hadoop etc
– Very flexible
– Collections instead of datatype (e.g. store mp3 as one variable)
– Easy to work on insanely huge datasets via clustered servers (distributed computing)

• Remote database server is very useful


– One source, no sync issues
– Uses the computing resources of the remote system for CRUD
– Frees up your local computing resources for other matters

• SAS can use other RDBS as well (via ODBC drivers)


BASIC
PROGRAMMING
LOGIC
PROGRAMMING LOGIC IS UNIVERSAL
• Expressions
– Types (integer, float, string, objects)
– Operators (arithmetic)
• Variables and assignment
• Functions
• Boolean
• Flow control
• Application
T HE M O ST
IM P O R TA NT ST E P
IN D ATA
A NA LY S IS
BEFORE YOU EVEN START CODING:
VISUALIZE YOUR DATASET SCHEMA!
PROC SQL
WHAT IS SQL?
• SQL stands for Structured Query Language
– It is an ANSI (American National Standards Institute) standard
– SQL is universal across SQL/SQL-supported platforms (syntax may
vary)
• It consists of
– Data definition language
• Creating tables with pre-defined data structures (namely n x m with a
specific datatype for each column)
– Recall the ‘types’ that we have seen during the Python hands-on (integer,
float, string)
• Like SAS, the length and datatype must be pre-defined and all data
entries must adhere to it, until modified
– Integer: int, bigint
– String: length determined by number of characters (SAS max = 65535)
– Dates: date formats
– Float: single or double floating point

– Data manipulation language


• Used for selecting, inserting, deleting and updating data
PROC SQL IN SAS
• PROC SQL is not necessarily better than DATASTEP
– DATASTEP processes data row by row
• Sounds primitive and tedious
• But extremely robust
– DATASTEP cannot be ‘chained’
• May end up creating many intermediate tables
• But extremely robust
– SQL is shorter, more elegant and easier to maintain
• But eats up a lot more resources (e.g. joins require pre-loading)
YOU WILL USE THESE PROC SQL
STATEMENTS ALL THE TIME
• Create
– Make a new dataset
• Select
– Case
– Aggregation functions
• Groupby
– The axis for aggregation functions
• Where
– Slicing data
• Having
– Filters observations
• Order by
– Sorts data
• Joins
– Combine tables
– Left, right, inner, outer, full
– Multiple joins
IM P O R TA NT
P R O CE D U RE S IN
F I NA NCI A L
S TU D IE S
RUNNING FIXED EFFECTS AND ERROR
CORRECTIONS IN SAS
• PROC REG
– Remember to correct for white’s heteroskesdacity by invoking /hcc
proc reg data=car;
• White’s hetero-consistent correction
model car=sue3 bm size dispersion
• Similar to ROBUST in Stata
2002d 2003d 2004d 2005d 2006d
– Invoke /stb to get standardized coefficients to compare effects between
independent variables 2007d 2008d 2009d 2010d 2011d
– Not easy to run fixed effects (levels) 2012d 2013d/hcc stb;
• Requires dummy variables for each level run;
quit;
• PROC SURVEYREG
– Easy to invoke fixed effects
• Use CLASS proc surveyreg data = car;
– White’s hetero-consistent correction is not sufficient cluster date_id;
– Need clustered standard errors class quarter_id;
• Recall: white’s errors relate to residuals across all observations (across groups) model car = sue3 bm size dispersion
• In panel data regressions, we have within groups errors quarter_id /solution adjrsq;
• Invoke CLUSTER to correct for this run;
quit;
RUNNING FIXED EFFECTS AND ERROR
CORRECTIONS IN SAS
• PROC MODEL
– Correcting for time-series errors (NEWEY-WEST)
– Use gmm in proc model

proc model data = drift_portfolio;


endo port_ret_ewrf;
exog mktrf smb hml umd;
instruments _exog_;
parms b0 b1 b2 b3 b4;
port_ret_ewrf = b0 + b1*mktrf + b2*smb + b3*hml
+ b4*umd;
fit port_ret_ewrf /gmm kernel=(bart,5,0) vardef=n;
run;
quit;
RUNNING SELECTION MODELS IN
STATA
• Treatment models
– Use treatreg <2nd stage model>, treat(<1st stage probit model>)
• Selection models (heckman)
• Use heckman <2nd stage model>, select(<1st stage probit model>)
• Uses MLE (numerical method) as default: More efficient
• Invoke twostep option to run it the original way
• Multinomial selection models
– What happens if your 1st stage model is a logit?
– Use user-created module SELMLOG
• Fixed effects and error correction
– Always remember to run your models with error corrections (ROBUST or CLUSTER()
– Fixed effects: append i. in front of the variable ([Link] [Link])
• Many user-created stata modules do not support this
• The workaround is to create dummy variables for each fixed effect
CALENDAR TIME PORTFOLIO
• First step: Identify the innovative characteristic and form a buy-sell portfolio
– Create a risk portfolio (similar to SMB, HML)
– In event studies, group stocks by the innovative characteristics that you have found, e.g.
• Buy firms who announces a new CEO that has beard
• Sell firms who announces one that does not
• Second step: Line up events and calculate daily returns of portfolio
– Line up the events along the calendar time
• Determine the boundaries of your calendar time
• e.g. start at the earliest event date + 0, end at last event date + 90
– Determine how long you want to hold for each stock
• e.g. buy at announcement date + 2, sell at announcement date + 90
– For each day in the calendar time:
• Compute the daily returns of all the stocks that are being held in the portfolio
– Remember to adjust your prices with CFACPR
• Use 2 weighting measures: Equally-weighted, and value-weighted
– Remember to adjust your shares outstanding (SHROUT) with CFACSHR
• Third step: Use market returns for days with no stocks
– Replace with market returns for calendar days in which there are no stocks in your portfolio
CALENDAR TIME PORTFOLIO
• Fourth step: Download and convert the 5 factors (from WRDS FF database or authors’ websites)
– 5 factors (all in WRDS)
• Risk free rate (RF)
• Market returns (MKT)
• Small minus Big (SMB)
• High minus Low (HML)
• Momentum (UMD)
• Liquidity (Pastor-Stambaugh LIQ)
– For earnings surprise/ drift: Check against Satka’s liquidity measure as well
• Final step: Merge the 5 factor dataset to portfolio returns dataset by date
– Remember to adjust 2 things
• Portfolio return minus RF (LHS)
• Market premium MKT – RF (RHS)
– Use PROC MODEL (for NEWEY-WEST correction) to regress
• (Portfolio returns – RF) = alpha + b1 * (MKT – RF) + b2 * SMB + b3 * HML + b4 * UMD + b5 * LIQ
– Significant alphas implies drift (i.e. market did not fully incorporate information)
• Direction tells us if they are wrong or under-reacted or over-reacted
– Non-significant alphas implies 1. market is correct, or 2. market is not aware of this risk factor (note: this is hard to sell)
QUESTIONS AND
FURTHER
DISCUSSIONS

You might also like