SlideShare a Scribd company logo
R Programming
for Quantitative Finance
(Jerry Wu)
(R & RStudio)
quantmod forecast Rbitcoin igraph plyr
What is R
• R is one of the most popular platforms for data analysis
and visualization currently available.
dsjerry2017@gmail.com
Some news about R
http://spectrum.ieee.org/computing/software/the-2016-top-programming-languages
dsjerry2017@gmail.com
A brief history of R
Time Topic
1993 Research	project	in	Auckland,	NewZealand
Ross	Ihaka and	Robert	Gentlemen
1995 Released	as	open-source	software
Generally	compatible	with	the	‘S’	language
1997 R	core	group	formed
2000 R	1.0.0	released
2004 First	international	user	conference	in Vienna
2012 R	2.15.2	released
2013 R	3.0.2	released
The	CRAN	package	repository	features	5,026	available	packages.
2017 R	3.4.1	released
The	CRAN	package	repository	features	10,875available	packages.dsjerry2017@gmail.com
Data analysts captivated by R’s
Power
dsjerry2017@gmail.com
Why “R”? Not “SAS”? Or others?
• 2009
• R (lingua franca)
• Google, Microsoft, Facebook, Johnson & Johnson,Pfizer, Merck, BOA,
InterContinental Hotels and Shell…, including New York Times now use it !
•
•
•
• To give researchers of all stripes an accessible data
analysis tool.
• 10,875 (2017)
dsjerry2017@gmail.com
R may be the disruptor !
dsjerry2017@gmail.com
The Tools We’re Using – Ranked by
Usage
• R is the tool used by the most data miners (70%)
Rexer Analyticsdsjerry2017@gmail.com
The Popularity of R Software is
Skyrocketing
• 70% R
• 24% R
dsjerry2017@gmail.com
Discussion lists and sites
dsjerry2017@gmail.com
KDnuggets
Four of the top five packages were open source
dsjerry2017@gmail.com
Hits on Google Scholar
• Google R
dsjerry2017@gmail.com
Why use R
• (Free/open source software)
•
•
•
•
•
dsjerry2017@gmail.com
Task Views
Bayesian
ChemPhys ,
ClinicalTrials
Cluster
DifferentialEquations
Distributions
Econometrics
Environmetrics
ExperimentalDesign
Finance
dsjerry2017@gmail.com
Task Views
Genetics
Graphics
HighPerformanceComputing
MachineLearning
MedicalImaging
MetaAnalysis
Multivariate
NaturalLanguageProcessing
NumericalMathematics
OfficialStatistics
Optimization dsjerry2017@gmail.com
Task Views
Pharmacokinetics
Phylogenetics
Psychometrics
ReproducibleResearch
Robust
SocialSciences
Spatial
SpatioTemporal
Survival
TimeSeries
WebTechnologies
gR dsjerry2017@gmail.com
Outline
• The Basics
• Vectors and Matrices, Data Frames, Factors, Lists
• Exploratory time series data analysis
• Stock Trading
• Quantitative Analysis
• Correlation analysis
• MA Model Estimation and Forecasting
• Querying the Bitcoin blockchain with R
dsjerry2017@gmail.com
Stock Returns
• $90 worth of Foxconn Stock
• 10% return in March
• 110% = 100% + 10%
• 1.10 = 1 + 0.10
• 99 = 90 * 1.10
+
-
Chapter1_stock_returns.R dsjerry2017@gmail.com
Stock Returns – Multiple periods
• $90 worth of Foxconn Stock
• 10% return in March
• 110% = 100% + 10%
• 1.10 = 1 + 0.10
• 99 = 90 * 1.10
• 5% return in April
• 110% = 100% + 5%
• 1.05 = 1 + 0.05
• 94.5 = 90 * 1.05
• 103.95 = 90 * 1.10 * 1.05
Chapter1_stock_returns.R dsjerry2017@gmail.com
Fun Time
• $100 worth of Stock
• 10% return in March
• 5% return in April
dsjerry2017@gmail.com
The Basics
• Basic Data Types
• Vectors and Matrices
• Data Frames
• Factors
• Lists
dsjerry2017@gmail.com
Basic Data Types
• Numeric
• Character
• Logical
Chapter1_stock_returns.R dsjerry2017@gmail.com
Basic Data Types
• Variables and data types
• Class()
Chapter1_stock_returns.R dsjerry2017@gmail.com
The Basics
• Basic Data Types
• Vectors and Matrices
• Data Frames
• Factors
• Lists
dsjerry2017@gmail.com
Vector – Vectors and stock prices
• Vectors and stock prices
• Vector names()
Chapter2_vectors_and_matrices.R
dsjerry2017@gmail.com
Vector manipulation
Chapter2_vectors_and_matrices.R
dsjerry2017@gmail.com
Fun Time
• Try
• + – * /
Chapter2_vectors_and_matrices.R
dsjerry2017@gmail.com
A 2D vector
Row
Col
Chapter2_vectors_and_matrices.R
dsjerry2017@gmail.com
cbind() and rbind()
Chapter2_vectors_and_matrices.R
dsjerry2017@gmail.com
Subsetting
Chapter2_vectors_and_matrices.R
dsjerry2017@gmail.com
Fun Time –
What’s incorrect about subsetting
• Tsmc and foxconn columns
A. M_tsmc_foxconn[,c(“tsmc”, “foxconn”)]
B. M_tsmc_foxconn[c(“tsmc”, “foxconn”)]
C. M_tsmc_foxconn[,]
dsjerry2017@gmail.com
Cor()relation
• +1 perfect positive linear relationship
• -1 perfect negative linear relationship
• 0 no linear relationship
Chapter2_vectors_and_matrices.R
dsjerry2017@gmail.com
Visualize your matrix
Chapter2_vectors_and_matrices.R
dsjerry2017@gmail.com
Visualize your matrix (Time Series)
Chapter2_vectors_and_matrices.R
dsjerry2017@gmail.com
The Basics
• Basic Data Types
• Vectors and Matrices
• Data Frames
• Factors
• Lists
dsjerry2017@gmail.com
Data Frames and Name that frame
Chapter3_data_frame.R
dsjerry2017@gmail.com
Data frame subsets and subset()
Chapter3_data_frame.R
dsjerry2017@gmail.com
Present value( )
•
Cash / (1 + X /100) ^ Periods( )
Cash * (1 + X / 100) ^ -Periods( )
Chapter3_data_frame.R
dsjerry2017@gmail.com
1000 6%
839.62 [1000	/	(1	+	6%)^3]
X	=
Present value( )
Chapter3_data_frame.R
dsjerry2017@gmail.com
Present value( )
• Fun Time ( 5%) 4
Chapter3_data_frame.R
dsjerry2017@gmail.com
The Basics
• Basic Data Types
• Vectors and Matrices
• Data Frames
• Factors
• Lists
dsjerry2017@gmail.com
Stock and Bond
Chapter4_factor.R
dsjerry2017@gmail.com
Cut()
Chapter4_factor.R
dsjerry2017@gmail.com
Plot factor
Chapter4_factor.R
dsjerry2017@gmail.com
Order()
Chapter4_factor.R
dsjerry2017@gmail.com
Subsets
Chapter4_factor.R
dsjerry2017@gmail.com
The Basics
• Basic Data Types
• Vectors and Matrices
• Data Frames
• Factors
• Lists
dsjerry2017@gmail.com
List
Chapter5_list.R
dsjerry2017@gmail.com
Split()
Chapter5_list.R
dsjerry2017@gmail.com
Fun time
• 1 1000 ?
Chapter5_list.R
dsjerry2017@gmail.com
Fun time
List
DF
Chapter5_list.R
dsjerry2017@gmail.com
Attributes
Chapter5_list.R
dsjerry2017@gmail.com
Data Input and output
Chapter6_io.R
dsjerry2017@gmail.com
Outline
• The Basics
• Vectors and Matrices, Data Frames, Factors, Lists
• Exploratory time series data analysis
• Stock Trading
• Quantitative Analysis
• Correlation analysis
• MA Model Estimation and Forecasting
• Querying the Bitcoin blockchain with R
dsjerry2017@gmail.com
Exploratory time series data analysis
• (Time Series)
• GDP ..
dsjerry2017@gmail.com
Exploratory time series data analysis
•
•
dsjerry2017@gmail.com
Exploratory time series data analysis
•
•
dsjerry2017@gmail.com
Exploratory time series data analysis
• : , ,
• (Data Granularity)
• : , ,
•
dsjerry2017@gmail.com
Exploratory time series data analysis
•
• {yt }
• (state space) S
• (Discrete)
• (Continuous)
dsjerry2017@gmail.com
Exploratory time series data analysis
• (index set)
•
• (Discrete) T={0,1,2……}
•
• (Continuous) T=[0, ∞)
dsjerry2017@gmail.com
Exploratory time series data analysis
Chapter7_EDA.R
dsjerry2017@gmail.com
Exploratory time series data analysis
0501
0501
Chapter7_EDA.R
dsjerry2017@gmail.com
Fun Time
Chapter7_EDA.R
dsjerry2017@gmail.com
Exploratory time series data analysis
Chapter7_EDA.R
dsjerry2017@gmail.com
Exploratory time series data analysis
Chapter7_EDA.R
dsjerry2017@gmail.com
Exploratory time series data analysis
• Moving Average Convergence / Divergence, MACD
DIF-MACD
MACD
DIF( )
Chapter7_EDA.R
dsjerry2017@gmail.com
Fun time
addADX
addATR
addBBands
addCCI
addCMF
addCMO
addDEMA
addDPO
addEMA
addEnvelope
addEVWMA
addExpiry
addMACD
addMomentum
addROC
addRSI
addSAR
addSMA
addSMI
addTRIX
addVo
addWMA
addZLEMAdsjerry2017@gmail.com
Basic Time Series Objects
Chapter7_EDA.R
dsjerry2017@gmail.com
Basic Time Series Objects
Chapter7_EDA.R
dsjerry2017@gmail.com
Outline
• The Basics
• Vectors and Matrices, Data Frames, Factors, Lists
• Exploratory time series data analysis
• Stock Trading
• Quantitative Analysis
• Correlation analysis
• MA Model Estimation and Forecasting
• Querying the Bitcoin blockchain with R
dsjerry2017@gmail.com
Stock Trading
Chapter7_EDA.R
dsjerry2017@gmail.com
Stock Trading
• 20ma 60ma
• 20ma 60ma ( )
Lag
ROC log( / ) *	Strategy (0,1)
Cl OHLC
cumsum
exp
Chapter7_EDA.R
dsjerry2017@gmail.com
Fun time
•
dsjerry2017@gmail.com
Outline
• The Basics
• Vectors and Matrices, Data Frames, Factors, Lists
• Exploratory time series data analysis
• Stock Trading
• Quantitative Analysis
• Correlation analysis
• MA Model Estimation and Forecasting
• Querying the Bitcoin blockchain with R
dsjerry2017@gmail.com
Correlation analysis
dsjerry2017@gmail.com
Correlation analysis
dsjerry2017@gmail.com
Correlation analysis
• Rapid Growth
dsjerry2017@gmail.com
Correlation analysis
• Periodic
dsjerry2017@gmail.com
Correlation analysis
• (linearized)
dsjerry2017@gmail.com
Correlation analysis
• (Periodic)
dsjerry2017@gmail.com
Correlation analysis
Diff( )
Log( )(linearized)
(Periodic)
Chapter8_QA.R
Diff 12 x 13 y =1+(y-x)/x
dsjerry2017@gmail.com
Correlation analysis - Scatterplots
Chapter8_QA.R
dsjerry2017@gmail.com
Correlation analysis – correlation coefficient
http://www.stat.nuk.edu.tw/prost/content2/statics_6.htm
+1	
0	
-1	
dsjerry2017@gmail.com
Correlation analysis – correlation coefficient
P-value 0.05
df
Chapter8_QA.R
dsjerry2017@gmail.com
Fun time
•
•
dsjerry2017@gmail.com
Outline
• The Basics
• Vectors and Matrices, Data Frames, Factors, Lists
• Exploratory time series data analysis
• Stock Trading
• Quantitative Analysis
• Correlation analysis
• MA Model Estimation and Forecasting
• Querying the Bitcoin blockchain with R
dsjerry2017@gmail.com
MA Model Estimation and Forecasting
• ARIMA Autoregressive
Integrated Moving Average model,
)
• ARIMA p d q
• p
• d TS
• q
• ARIMA ACF
PACF p,d,q
• ACF(Sample Autocorrelation Function,
)
• PACF (Sample Partial Autocorrelation
Function, )
ACF
PACF
Chapter8_QA.R
dsjerry2017@gmail.com
MA Model Estimation and Forecasting
Lag=1
Lag=3
arima(x,	order	=	c(0,0,1))
arima(x,	order	=	c(3,0,1))
AIC( )
(Goodness	of	Fit)
AICChapter8_QA.R
dsjerry2017@gmail.com
MA Model Estimation and
Forecasting(Non-Auto ARIMA)
Chapter8_QA.R
dsjerry2017@gmail.com
MA Model Estimation and
Forecasting(Non-Auto ARIMA)
Chapter8_QA.R
dsjerry2017@gmail.com
MA Model Estimation and
Forecasting(Auto ARIMA)
Chapter8_QA.R
dsjerry2017@gmail.com
MA Model Estimation and Forecasting
dsjerry2017@gmail.com
Outline
• The Basics
• Vectors and Matrices, Data Frames, Factors, Lists
• Exploratory time series data analysis
• Stock Trading
• Quantitative Analysis
• Correlation analysis
• MA Model Estimation and Forecasting
• Querying the Bitcoin blockchain with R
dsjerry2017@gmail.com
Bitcoin
dsjerry2017@gmail.com
Bitcoin
• (Blockchain)
•
• 0.00000001 1
• 2140 2100
• = 90,375
https://zh.wikipedia.org/wiki/
dsjerry2017@gmail.com
Blockchain
Blockchain is	a distributed	database that	is	used	to	maintain	a	
continuously	growing	list	of records,	called blocks.	Each	block	contains	
a timestamp and	a	link	to	a	previous	block.
A	blockchain is	typically	managed	by	a	peer-to-peer network	collectively	
adhering	to	a	protocol	for	validating	new	blocks.
https://medium.com/@micheledaliessi/how-does-the-blockchain-work-98c8cd01d2ae
dsjerry2017@gmail.com
Querying the Bitcoin blockchain with R
Chapter9_Blockchain.R
dsjerry2017@gmail.com
Querying the Bitcoin blockchain with R
Chapter9_Blockchain.R
dsjerry2017@gmail.com
Conclusion
• Concept of R basic
• Exploratory time series data
• Learn stock trading with R
• Quantitative analysis with forecasting
• Querying the Bitcoin Blockchain Visualization with R
dsjerry2017@gmail.com
dsjerry2017@gmail.com
jerry@mail.ntust.edu.tw

More Related Content

PDF
Python網站框架絕技: Django 完全攻略班
PPTX
AI與大數據數據處理 Spark實戰(20171216)
PPTX
Graph Analytics: Graph Algorithms Inside Neo4j
PDF
The Ring programming language version 1.5.4 book - Part 7 of 185
PDF
Combine Spring Data Neo4j and Spring Boot to quickl
PPTX
Applying big data thinking to normal size data
PDF
The Ring programming language version 1.5.3 book - Part 7 of 184
PDF
The Ring programming language version 1.5.1 book - Part 6 of 180
Python網站框架絕技: Django 完全攻略班
AI與大數據數據處理 Spark實戰(20171216)
Graph Analytics: Graph Algorithms Inside Neo4j
The Ring programming language version 1.5.4 book - Part 7 of 185
Combine Spring Data Neo4j and Spring Boot to quickl
Applying big data thinking to normal size data
The Ring programming language version 1.5.3 book - Part 7 of 184
The Ring programming language version 1.5.1 book - Part 6 of 180

What's hot (18)

PPTX
GraphQL - The new "Lingua Franca" for API-Development
PDF
GraphGen: Conducting Graph Analytics over Relational Databases
PPTX
A whirlwind tour of graph databases
PDF
Practical Graph Algorithms with Neo4j
PDF
Graphs & Neo4j - Past Present Future
PDF
Data Day Seattle 2017: Scaling Data Science at Stitch Fix
PDF
"Deployment for free": removing the need to write model deployment code at St...
PDF
OVH-Change Data Capture in production with Apache Flink - Meetup Rennes 2019-...
PDF
Building a data processing pipeline in Python
PPT
Hands on Training – Graph Database with Neo4j
PPTX
Big Data-Driven Applications with Cassandra and Spark
PDF
A quick review of Python and Graph Databases
PDF
Data Science in the Cloud @StitchFix
PDF
Designing and Building a Graph Database Application – Architectural Choices, ...
PDF
Webinar about Spring Data Neo4j 4
PDF
Congressional PageRank: Graph Analytics of US Congress With Neo4j
PPTX
Building Community APIs using GraphQL, Neo4j, and Kotlin
PDF
Next-generation API Development with GraphQL and Prisma
GraphQL - The new "Lingua Franca" for API-Development
GraphGen: Conducting Graph Analytics over Relational Databases
A whirlwind tour of graph databases
Practical Graph Algorithms with Neo4j
Graphs & Neo4j - Past Present Future
Data Day Seattle 2017: Scaling Data Science at Stitch Fix
"Deployment for free": removing the need to write model deployment code at St...
OVH-Change Data Capture in production with Apache Flink - Meetup Rennes 2019-...
Building a data processing pipeline in Python
Hands on Training – Graph Database with Neo4j
Big Data-Driven Applications with Cassandra and Spark
A quick review of Python and Graph Databases
Data Science in the Cloud @StitchFix
Designing and Building a Graph Database Application – Architectural Choices, ...
Webinar about Spring Data Neo4j 4
Congressional PageRank: Graph Analytics of US Congress With Neo4j
Building Community APIs using GraphQL, Neo4j, and Kotlin
Next-generation API Development with GraphQL and Prisma
Ad

Similar to 廣宣學堂: R programming for_quantitative_finance_0623 (20)

PDF
An R primer for SQL folks
PDF
Introduction to R for data science
PDF
SQLBits Module 2 RStats Introduction to R and Statistics
PDF
1.3 introduction to R language, importing dataset in r, data exploration in r
PPTX
Programming with R in Big Data Analytics
PPTX
A Workshop on R
PPTX
Introduction to R for Learning Analytics Researchers
PDF
R programming groundup-basic-section-i
PDF
Recommender Systems, Matrices and Graphs
PPTX
PPTX
Webinar | Accessing Your Data Lake Assets from Amazon Redshift Spectrum
PPT
從行動廣告大數據觀點談 Big data 20150916
PPTX
Ggplot2 v3
PPTX
Step By Step Guide to Learn R
PDF
R language, an introduction
PPTX
Introduction to basic statistics
 
PDF
Graph databases and the #panamapapers
PDF
محاضرة برنامج التحليل الكمي R program د.هديل القفيدي
PPT
Slides on introduction to R by ArinBasu MD
PPT
Basics of R-Progranmming with instata.ppt
An R primer for SQL folks
Introduction to R for data science
SQLBits Module 2 RStats Introduction to R and Statistics
1.3 introduction to R language, importing dataset in r, data exploration in r
Programming with R in Big Data Analytics
A Workshop on R
Introduction to R for Learning Analytics Researchers
R programming groundup-basic-section-i
Recommender Systems, Matrices and Graphs
Webinar | Accessing Your Data Lake Assets from Amazon Redshift Spectrum
從行動廣告大數據觀點談 Big data 20150916
Ggplot2 v3
Step By Step Guide to Learn R
R language, an introduction
Introduction to basic statistics
 
Graph databases and the #panamapapers
محاضرة برنامج التحليل الكمي R program د.هديل القفيدي
Slides on introduction to R by ArinBasu MD
Basics of R-Progranmming with instata.ppt
Ad

More from Paul Chao (11)

PDF
企業導入微服務實戰 - updated
PDF
企業導入微服務實戰 - updated
PDF
廣宣學堂: 企業導入微服務實戰
PDF
廣宣學堂: 機器視覺初探 10152017
PDF
開放運算&GPU技術研究班
PDF
廣宣學堂: 容器進階實務 - Docker進深研究班
PDF
Docker workshop 0507 Taichung
PDF
20170430 python爬蟲攻防戰-攻防與金融大數據分析班
PDF
廣宣學堂Python金融爬蟲原理班 20170416
PDF
Introduction to Golang final
PDF
手把手帶你學Docker 03042017
企業導入微服務實戰 - updated
企業導入微服務實戰 - updated
廣宣學堂: 企業導入微服務實戰
廣宣學堂: 機器視覺初探 10152017
開放運算&GPU技術研究班
廣宣學堂: 容器進階實務 - Docker進深研究班
Docker workshop 0507 Taichung
20170430 python爬蟲攻防戰-攻防與金融大數據分析班
廣宣學堂Python金融爬蟲原理班 20170416
Introduction to Golang final
手把手帶你學Docker 03042017

Recently uploaded (20)

PDF
System and Network Administration Chapter 2
PPTX
Essential Infomation Tech presentation.pptx
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PPTX
ai tools demonstartion for schools and inter college
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
Nekopoi APK 2025 free lastest update
PDF
Digital Strategies for Manufacturing Companies
PPTX
Introduction to Artificial Intelligence
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
medical staffing services at VALiNTRY
PPTX
Reimagine Home Health with the Power of Agentic AI​
PPTX
Transform Your Business with a Software ERP System
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
System and Network Administraation Chapter 3
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
System and Network Administration Chapter 2
Essential Infomation Tech presentation.pptx
Adobe Illustrator 28.6 Crack My Vision of Vector Design
ai tools demonstartion for schools and inter college
Wondershare Filmora 15 Crack With Activation Key [2025
Odoo Companies in India – Driving Business Transformation.pdf
Nekopoi APK 2025 free lastest update
Digital Strategies for Manufacturing Companies
Introduction to Artificial Intelligence
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
medical staffing services at VALiNTRY
Reimagine Home Health with the Power of Agentic AI​
Transform Your Business with a Software ERP System
Which alternative to Crystal Reports is best for small or large businesses.pdf
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Internet Downloader Manager (IDM) Crack 6.42 Build 41
System and Network Administraation Chapter 3
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
How to Migrate SBCGlobal Email to Yahoo Easily

廣宣學堂: R programming for_quantitative_finance_0623