SlideShare a Scribd company logo
1 of 39
Download to read offline
Predicting the Stock Market
with Genetic Programming
David Moskowitz, Ph.D.
Infoblazer LLC
Data Scientists in Stamford Westport
January 25, 2016
Disclaimer
• The following is my opinion only
• It is not the opinion of my employer
• It is not related to any work done at my employer
You should not make any decision, financial, investments, trading or otherwise,
based on any of the information presented without undertaking independent due
diligence and consultation with a professional broker or competent financial
advisor. You understand that you are using any and all information presented at
your own risk.
Agenda
• What is Genetic Programming?
• Time Series Prediction
• Stock Market Prediction
• Other Issues
• Modularity
• Linear GP
• Genetic Algorithms
• Demonstrations
What is Genetic Programming?
• Get a computer to do something without telling it how to do it
• Breeds a population of computer programs that improve over time
• Evolution
• Genetic Operators
• Survival of the Fittest
• Stochastic component
• Non-Greedy
• Creativity
• Novel solutions
• Size and shape of solution unknown
• Limited only by what can be represented as a computer program
(Koza et al., 2006, p. 11)
Example: Design of a Satellite Boom
• Designed using a genetic algorithm
• 20,000+% improvement in frequency averaged energy levels
(Keane, 1996)
History
• Visionaries
• Samuel 1959 – Goal of AI
• Turing 1948 – Evolutionary search, gene combination, survival of the fittest
• Evolutionary Algorithms , 1962-
• mutation , populations,
• Genetic Algorithms, 1973-
• John Holland
• Crossover
• Genetic Programming, 1989-
• John Koza
• Best way to represent a computer program is a computer program
How GP Works
• Preparatory Steps
• Primitives
• Fitness Function(s)
• Initialize Population
• Evolve Population
• Calculate population fitness
• Select next generation
• Termination Condition
(Poli et al.,2008, p. 2)
GP Representation
• LISP
• (max (+ x x) (+ x (* 3 y)))
• max(x+x,x+3*y)
MAX
X
+ +
*X X
3 y
GP Operations
• Probabilistically select an operation
• Crossover
• Switch two nodes on different individuals
• Mutation
• Randomly modify an individual (node)
• Reproduction
• Copy parent, as is, to next generation
GP Selection
• Need to select one or two individuals for genetic operations
• Selection is probabilistic
• Fitness Proportional Selection
• Tournament Selection
Crossover
+
+
X y
3
*
+
y 1
/
X 2
+
+
X y
3
*
+
y 1
/
X 2
Parents
Children
Mutation
*
+
y 1
/
X 2
Parent
Child
*
+
y 1
*
2 MIN
X y
Demo – Symbolic Regression
• Curve Fitting
• 𝑥3 − 𝑥2 + 𝑥 − 4
• Prove that GP works
-200000
0
200000
400000
600000
800000
1000000
1200000
0 10 20 30 40 50 60 70 80 90
Y
X
Demo a, b
Chaotic Series
• Look random, but are deterministic
• Highly dependent on initial conditions
• Difficult to predict
𝑌𝑡 = 𝑠𝑖𝑛 𝑥 − 130 + 𝑥 + 130 x=0: 𝑌0 = 0.9
X>0: 𝑌 𝑡+1 = 4𝑌𝑡(1 − 𝑌𝑡)
0
0.2
0.4
0.6
0.8
1
1.2
0 10 20 30 40 50 60 70 80 90
Y
X
9
10
11
12
13
14
15
16
17
0 10 20 30 40 50 60 70 80 90
Y
X
Demo- Chaotic Series Symbolic Regression
Demo c, d, e, f
Regime Change
• Goal is to uncover underlying data generating process
• This can change over time
-15
-10
-5
0
5
10
0
14
28
42
56
70
84
98
112
126
140
154
168
182
196
Y
X
0<=x<70: 𝑌𝑡 = 𝑠𝑖𝑛 𝑥 + 𝑥
70<=x<130: 𝑌𝑡 = 𝑐𝑜𝑠 𝑥 − 𝑥
130<=x<200: 𝑌𝑡 = 𝑠𝑖𝑛 𝑥 − 130 + 𝑥 − 130
-3
-2
-1
0
1
2
3
0
20
40
60
80
100
120
140
160
180
200
220
240
260
280
300
320
340
360
380
400
Y
X
x<200, x>=300: 𝑌 𝑡+1 = 4𝑌𝑡(1 − 𝑌𝑡)
200<=x<300: 𝑌 𝑡+1 = 1.8708𝑌𝑡 − 𝑌𝑡−1
S&P 500 index close price during the stock market
crash of 2008 (Yahoo, 2013).
𝑌𝑡 = 𝑓 𝑊𝑇𝐹 ?
Demo- Symbolic Regression Regime Change
Demo g
Time Series Prediction
• Train on past values
• Predict future values
• Retrain periodically
Demo- Chaotic Series Prediction
Demo h, I, j
Market Prediction
• S&P 500 Long-Flat (Invest-don’t invest)
• Ignore transaction costs
• Ignore out of market returns
• Predictors
• S&P 500 Price
• S&P 500 Volume
• 250-day MA normalized
0
0.2
0.4
0.6
0.8
1
1.2
1.4
0
500
1000
1500
2000
2500
S&P 500 Index Series
Index Value 250 Day Moving Average 250-day MA Normalized
GP is a Perfect Match for Market Prediction
• “The interrelationships among the relevant variables is unknown or poorly understood
(or where it is suspected that the current understanding may possibly be wrong).”
• “Finding the size and shape of the ultimate solution is a major part of the problem.”
• “Significant amounts of test data are available in computer-readable form.”
• “There are good simulators to test the performance of tentative solutions to a problem,
but poor methods to directly obtain good solutions.”
• “Conventional mathematical analysis does not, or cannot, provide analytic solutions”
• “An approximate solution is acceptable (or is the only result that is ever likely to be
obtained)”
• “Small improvements in performance are routinely measured (or easily measurable) and
highly prized.”
(Poli et al.,2008, pp. 111-113)
Primitive Set
• Hundreds of indicators
• Ex. (http://www.investopedia.com/active-trading/technical-indicators/)
• Include common technical analysis indicators
• Momentum- compare to recent average
• Breakout- compare to recent minimum/maximum
• Ex. Buy if current price risen by 2% over minimum price last 30 days
• Prefer low level functions
• Better results possible with higher level , packaged indicators?
Primitive set
• Functions
• Add
• Subtract
• Multiply
• Divide
• Gt
• Lt
• And
• Or
• Not
• offsetValue
• ifElseBoolean
• movingAverage
• periodMaximum
• periodMinimum
• AbsoluteDifference
• Terminals
• randomInteger(low high)
• randomDouble(low high)
• True
• False
• offsetValue(0)
• Hundreds of other technical indicators
(http://www.investopedia.com/active-
trading/technical-indicators/)
Training Approaches
• Train-Predict-Retrain
• Train-Test-Predict
• Multiple Runs
Train M
Test
N
Predict
1
Train
Predict
M
N (1)
Demo k, l, m
Experiment- Market Prediction
• Investment decisions in S&P 500 Index
• Modeled after (Chen et al., 2008), 1988-2004
• Long-Flat decisions
• Normalized by 250-day moving average
• Fitness = investment gain
0
200
400
600
800
1000
1200
1400
1600
1988-01-04
1988-08-16
1989-03-31
1989-11-10
1990-06-27
1991-02-08
1991-09-24
1992-05-07
1992-12-18
1993-08-04
1994-03-17
1994-10-31
1995-06-15
1996-01-29
1996-09-11
1997-04-25
1997-12-08
1998-07-24
1999-03-10
1999-10-21
2000-06-06
2001-01-19
2001-09-04
2002-04-25
2002-12-06
2003-07-24
2004-03-09
2004-10-21
2005-06-07
S&P 500
0.7
0.8
0.9
1
1.1
1.2
1.3
1988-12-27
1989-08-10
1990-03-26
1990-11-06
1991-06-21
1992-02-04
1992-09-17
1993-05-03
1993-12-14
1994-07-29
1995-03-14
1995-10-25
1996-06-10
1997-01-22
1997-09-05
1998-04-22
1998-12-03
1999-07-21
2000-03-03
2000-10-16
2001-06-01
2002-01-22
2002-09-05
2003-04-22
2003-12-03
2004-07-21
2005-03-04
2005-10-17
2006-06-02
Normalized
Experiment- Market Prediction
• Training-validation-prediction approach (T-V-P)
• Training-prediction approach (T-P)
• Training - 1989-1998
• Prediction- 1999-2004
(Chen et al., 2008, p. 110)
Results T-V-P w/Trans Cost
Method Mean Std. Dev. Min Max 95% CI
# beating
benchmark
1999-2000
Buy & Hold 0.0751
GP 0.0434 0.0664 -0.1917 0.1197 [0.0250 ... 0.0618] 5/50
ADF 0.0309 0.0798 -0.3054 0.0845 [0.0088 ... 0.0530] 3/50
ADT 0.0510 0.0519 -0.1974 0.1042 [0.0366 ... 0.0654] 5/50
2001-2002
Buy & Hold -0.3144
GP -0.3693 0.1306 -0.8087 -0.2885 [-0.4055 ... -0.3331] 1/50
ADF -0.3347 0.0887 -0.7290 -0.1777 [-0.3593 ... -0.3102] 2/50
ADT -0.3697 0.1390 -0.7450 -0.0134 [-0.4082 ... -0.3312] 1/50
2003-2004
Buy & Hold 0.3332
GP 0.2945 0.0497 0.1432 0.3291 [0.2807 ... 0.3083] 0/50
ADF 0.3139 0.0390 0.1170 0.3539 [0.3031 ... 0.3247] 1/50
ADT 0.3247 0.0150 0.2349 0.3522 [0.3205 ... 0.3289] 2/50
(Moskowitz, 2016, p. 119)
Results T-V-P wo/Trans Cost
Method Mean Std. Dev. Min Max 95% CI
# beating
benchmark
1999-2000
Buy & Hold 0.0751
GP 0.1494 0.1088 -0.0438 0.4525 [0.1192 ... 0.1795] 35/50
ADF 0.1418 0.1238 -0.0399 0.5112 [0.1075 ... 0.1761] 35/50
ADT 0.1567 0.1099 -0.0068 0.4796 [0.1262 ... 0.1871] 37/50
2001-2002
Buy & Hold -0.3144
GP -0.3121 0.0573 -0.4081 -0.0348 [-0.3280 ... -0.2962] 17/50
ADF -0.3023 0.0848 -0.5153 0.0196 [-0.3258 ... -0.2788] 18/50
ADT -0.2843 0.0635 -0.3924 -0.1245 [-0.3020 ... -0.2667] 32/50
2003-2004
Buy & Hold 0.3332
GP 0.3045 0.0929 0.0463 0.5045 [0.2788 ... 0.3303] 15/50
ADF 0.3395 0.1171 -0.0016 0.5597 [0.3070 ... 0.3719] 22/50
ADT 0.3329 0.1202 0.0775 0.6443 [0.2996 ... 0.3663] 29/50
(Moskowitz, 2016, p. 122)
Results T-P w/Trans Cost
(Moskowitz, 2016, p. 120)
Method Mean Std. Dev. Min Max 95% CI
# beating
benchmark
1999-2000
Buy & Hold 0.0634
ADT 0.0018 0.1372 -0.4290 0.2388 [-0.0362 ... 0.1010] 17/50
DyFor GP -0.0157 0.1101 -0.2690 0.1426 [-0.0463 ... 0.0639] 15/50
2001-2002
Buy & Hold -0.3339
ADT -0.1364 0.1014 -0.2964 0.1601 [-0.1645 ... -0.0631] 50/50
DyFor GP -0.1018 0.0819 -0.2810 0.0817 [-0.1245 ... -0.0426] 50/50
2003-2004
Buy & Hold 0.2970
ADT 0.1035 0.0653 -0.0603 0.2529 [0.0854 ... 0.1507] 0/50
DyFor GP 0.0489 0.0723 -0.1780 0.2156 [0.0289 ... 0.1012] 0/50
1999-2004
Buy & Hold -0.0189
ADT -0.0349 0.1933 -0.5395 0.4592 [-0.0884 ... 0.0187] 24/50
DyFor GP -0.0698 0.1413 -0.3597 0.2136 [-0.1089 ... -0.0306] 15/50
Results T-P wo/Trans Cost
Method Mean Std. Dev. Min Max 95% CI
# beating
benchmark
1999-2000
Buy & Hold 0.0634
ADT 0.0788 0.1071 -0.1106 0.3576 [0.0491 ... 0.1562] 27/50
DyFor GP 0.0807 0.1323 -0.1904 0.3408 [0.0440 ... 0.1763] 26/50
2001-2002
Buy & Hold -0.3339
ADT -0.0524 0.1026 -0.2674 0.1521 [-0.0808 ... 0.0218] 50/50
DyFor GP -0.0594 0.0862 -0.2314 0.1020 [-0.0833 ... 0.0029] 50/50
2003-2004
Buy & Hold 0.2970
ADT 0.1246 0.0782 -0.0132 0.3739 [0.1029 ... 0.1811] 2/50
DyFor GP 0.1233 0.0702 -0.0297 0.2783 [0.1038 ... 0.1740] 0/50
1999-2004
Buy & Hold -0.0189
ADT 0.1683 0.2005 -0.1946 0.6959 [0.1128 ... 0.2239] 39/50
DyFor GP 0.1568 0.1887 -0.2618 0.5762 [0.1045 ... 0.2091] 41/50
(Moskowitz, 2016, p. 124)
ADT vs DyFor GP vs Buy and Hold
750
850
950
1050
1150
1250
1350
1450
1550
1/4/1999 1/4/2000 1/4/2001 1/4/2002 1/4/2003 1/4/2004
With Transaction Costs, 50 run mean
index value Dyfor.y_predicted ADT Sliding.y_predicted
750
850
950
1050
1150
1250
1350
1450
1550
1/4/1999 1/4/2000 1/4/2001 1/4/2002 1/4/2003 1/4/2004
Without Transaction Costs, 50 run mean
index value Dyfor.y_predicted ADT Sliding.y_predicted
ADT: +0.1683%
DyFor GP: +0.1568%
B&H: -0.0189%
ADT: -0.349%
DyFor GP: -0.698%
B&H: -0.0189%
(Moskowitz, 2016, p. 208) (Moskowitz, 2016, p. 213)
Advanced GP
• Modularity
• Automatically Defined Functions
• Strongly-typed GP
• Closure
• Advanced techniques
• Looping
• Memory store
• Lambdas
• Recursion
• Time series regimes (Moskowitz, 2016)
• Design patterns (Moskowitz, 2016)
(Koza, 1994, p. 74)
Genetic Algorithms
• Non-differentiable / nonlinear optimization problem
• Search for parameters, rules
• Size and shape prescribed
• Bit, Numeric, or other representations
• Ex. Minimize 𝑥2
− 50𝑦 + 𝑧3
, x={0-31},y={0-31},z={0-15}
10011 11000 1011
00110 11010 1100
10010 11010 1100
00111 11000 1011
192
− 50 ∗ 24 + 113
=492
62
− 50 ∗ 26 + 123
=464
182 − 50 ∗ 26 + 123=752
72 − 50 ∗ 24 + 113=180
Linear GP
• Sequence of imperative instructions
• Register-based operations
• Machine code, GPU Instructions
(Poli et al.,2008, pp. 61-65)
Demo- Symbolic Regression Regime Change
• Regime determining branch
• Regime specific functions
• Implements template method design pattern
Demo o
Next Steps
• MATLAB (GA only)
• JGAP (Java)
• DEAP (Python)
• Roll your own
• Evolutionary Signals
Not So Shameless Plug
• Evolutionary Signals
• Develop Buy/Sell signals using genetic programming
• Minimal financial knowledge and no programming experience required
• Goals
• Much larger number of predictor series
• Crowdsource models
• Earn revenue from high performing models
• Open beta testing
• Visit www.gpsignals.com for more information
Questions?
• Thank you!
• Contact info:
LinkedIn: infoblazer
@infojester
References
• Chen, S. H., Kuo, T. W., & Hoi, K. M. (2008). Genetic Programming and Financial Trading:
How Much About “What We Know.” In Handbook of financial engineering (pp. 99–154).
Springer US. doi:10.1007/978-0-387-76682-9
• Keane, A. J. (1996). THE DESIGN OF A SATELLITE BOOM WITH ENHANCED VIBRATION
PERFORMANCE USING GENETIC ALGORITHM TECHNIQUES. The Journal of the Acoustical
Society of America, 99(4), 2599–2603.
• Koza, J. R. (1994). Genetic programming II: automatic discovery of reusable programs.
MIT press.
• Koza, J. ., Keane, M. A., Streeter, M. J., Mydlowec, W., Yu, J., & Lanza, G. (2006). Genetic
programming IV: Routine human-competitive machine intelligence (Vol. 5). Springer.
• Moskowitz, D. (2016). Automatically Defined Templates for Improved Prediction of Non-
stationary, Nonlinear Time Series in Genetic Programming. Doctoral dissertation. Nova
Southeastern University. Retrieved from http://nsuworks.nova.edu/gscis_etd/953.
• Poli, R., Langdon, W. B., McPhee, N. F., & Koza, J. R. (2008). A field guide to genetic
programming. Lulu. com.

More Related Content

Viewers also liked

Genetic programming
Genetic programmingGenetic programming
Genetic programmingMeghna Singh
 
Introduction to Genetic Programming
Introduction to Genetic ProgrammingIntroduction to Genetic Programming
Introduction to Genetic Programmingadil raja
 
できる!遺伝的アルゴリズム
できる!遺伝的アルゴリズムできる!遺伝的アルゴリズム
できる!遺伝的アルゴリズムMaehana Tsuyoshi
 
Genetic Algorithm by Example
Genetic Algorithm by ExampleGenetic Algorithm by Example
Genetic Algorithm by ExampleNobal Niraula
 
Matlab Introduction
Matlab IntroductionMatlab Introduction
Matlab Introductionideas2ignite
 
Genetic algorithm
Genetic algorithmGenetic algorithm
Genetic algorithmgarima931
 
Mobile Ad hoc Networks
Mobile Ad hoc NetworksMobile Ad hoc Networks
Mobile Ad hoc NetworksJagdeep Singh
 

Viewers also liked (9)

Genetic programming
Genetic programmingGenetic programming
Genetic programming
 
Genetic Programming in Python
Genetic Programming in PythonGenetic Programming in Python
Genetic Programming in Python
 
Introduction to Genetic Programming
Introduction to Genetic ProgrammingIntroduction to Genetic Programming
Introduction to Genetic Programming
 
できる!遺伝的アルゴリズム
できる!遺伝的アルゴリズムできる!遺伝的アルゴリズム
できる!遺伝的アルゴリズム
 
Genetic Algorithm by Example
Genetic Algorithm by ExampleGenetic Algorithm by Example
Genetic Algorithm by Example
 
Matlab Introduction
Matlab IntroductionMatlab Introduction
Matlab Introduction
 
Genetic algorithm
Genetic algorithmGenetic algorithm
Genetic algorithm
 
Ad-Hoc Networks
Ad-Hoc NetworksAd-Hoc Networks
Ad-Hoc Networks
 
Mobile Ad hoc Networks
Mobile Ad hoc NetworksMobile Ad hoc Networks
Mobile Ad hoc Networks
 

Similar to Prediction the stock market with genetic programming

6 data envelopment_analysis
6 data envelopment_analysis6 data envelopment_analysis
6 data envelopment_analysisFEG
 
Rutgers Governor School - Six Sigma
Rutgers Governor School - Six Sigma  Rutgers Governor School - Six Sigma
Rutgers Governor School - Six Sigma Brandon Theiss, PE
 
Engine90 crawford-decision-making (1)
Engine90 crawford-decision-making (1)Engine90 crawford-decision-making (1)
Engine90 crawford-decision-making (1)Divyansh Dokania
 
Introduction to Machine Learning - An overview and first step for candidate d...
Introduction to Machine Learning - An overview and first step for candidate d...Introduction to Machine Learning - An overview and first step for candidate d...
Introduction to Machine Learning - An overview and first step for candidate d...Lucas Jellema
 
What are the odds of making that number risk analysis with crystal ball - O...
What are the odds of making that number   risk analysis with crystal ball - O...What are the odds of making that number   risk analysis with crystal ball - O...
What are the odds of making that number risk analysis with crystal ball - O...p6academy
 
The Art of Intelligence – A Practical Introduction Machine Learning for Oracl...
The Art of Intelligence – A Practical Introduction Machine Learning for Oracl...The Art of Intelligence – A Practical Introduction Machine Learning for Oracl...
The Art of Intelligence – A Practical Introduction Machine Learning for Oracl...Lucas Jellema
 
2022/10/30 BESC2022: Parameter Tuning Method for Multi-agent Simulation using...
2022/10/30 BESC2022: Parameter Tuning Method for Multi-agent Simulation using...2022/10/30 BESC2022: Parameter Tuning Method for Multi-agent Simulation using...
2022/10/30 BESC2022: Parameter Tuning Method for Multi-agent Simulation using...Masanori HIRANO
 
Simple rules for building robust machine learning models
Simple rules for building robust machine learning modelsSimple rules for building robust machine learning models
Simple rules for building robust machine learning modelsKyriakos Chatzidimitriou
 
Price movement prediction in Hong Kong equity market
Price movement prediction in Hong Kong equity marketPrice movement prediction in Hong Kong equity market
Price movement prediction in Hong Kong equity marketTc. Ying
 
SPC Training by D&H Engineers
SPC Training by D&H EngineersSPC Training by D&H Engineers
SPC Training by D&H EngineersD&H Engineers
 
Statistics for analytics
Statistics for analyticsStatistics for analytics
Statistics for analyticsdeepika4721
 
Introduction To Six Sigma
Introduction To Six SigmaIntroduction To Six Sigma
Introduction To Six Sigmaskoscielak
 
Feature Engineering
Feature Engineering Feature Engineering
Feature Engineering odsc
 
Scott Clark, Software Engineer, Yelp at MLconf SF
Scott Clark, Software Engineer, Yelp at MLconf SFScott Clark, Software Engineer, Yelp at MLconf SF
Scott Clark, Software Engineer, Yelp at MLconf SFMLconf
 
Optimal Learning for Fun and Profit with MOE
Optimal Learning for Fun and Profit with MOEOptimal Learning for Fun and Profit with MOE
Optimal Learning for Fun and Profit with MOEYelp Engineering
 
Lec-3 Forecasting.pdf Data science college
Lec-3 Forecasting.pdf Data science collegeLec-3 Forecasting.pdf Data science college
Lec-3 Forecasting.pdf Data science collegemsherazmalik1
 
EvoFIN2015
EvoFIN2015EvoFIN2015
EvoFIN2015mausolos
 

Similar to Prediction the stock market with genetic programming (20)

6 data envelopment_analysis
6 data envelopment_analysis6 data envelopment_analysis
6 data envelopment_analysis
 
Rutgers Governor School - Six Sigma
Rutgers Governor School - Six Sigma  Rutgers Governor School - Six Sigma
Rutgers Governor School - Six Sigma
 
Engine90 crawford-decision-making (1)
Engine90 crawford-decision-making (1)Engine90 crawford-decision-making (1)
Engine90 crawford-decision-making (1)
 
Introduction to Machine Learning - An overview and first step for candidate d...
Introduction to Machine Learning - An overview and first step for candidate d...Introduction to Machine Learning - An overview and first step for candidate d...
Introduction to Machine Learning - An overview and first step for candidate d...
 
What are the odds of making that number risk analysis with crystal ball - O...
What are the odds of making that number   risk analysis with crystal ball - O...What are the odds of making that number   risk analysis with crystal ball - O...
What are the odds of making that number risk analysis with crystal ball - O...
 
The Art of Intelligence – A Practical Introduction Machine Learning for Oracl...
The Art of Intelligence – A Practical Introduction Machine Learning for Oracl...The Art of Intelligence – A Practical Introduction Machine Learning for Oracl...
The Art of Intelligence – A Practical Introduction Machine Learning for Oracl...
 
2022/10/30 BESC2022: Parameter Tuning Method for Multi-agent Simulation using...
2022/10/30 BESC2022: Parameter Tuning Method for Multi-agent Simulation using...2022/10/30 BESC2022: Parameter Tuning Method for Multi-agent Simulation using...
2022/10/30 BESC2022: Parameter Tuning Method for Multi-agent Simulation using...
 
Simple rules for building robust machine learning models
Simple rules for building robust machine learning modelsSimple rules for building robust machine learning models
Simple rules for building robust machine learning models
 
Price movement prediction in Hong Kong equity market
Price movement prediction in Hong Kong equity marketPrice movement prediction in Hong Kong equity market
Price movement prediction in Hong Kong equity market
 
Introduction overviewmachinelearning sig Door Lucas Jellema
Introduction overviewmachinelearning sig Door Lucas JellemaIntroduction overviewmachinelearning sig Door Lucas Jellema
Introduction overviewmachinelearning sig Door Lucas Jellema
 
SPC Training by D&H Engineers
SPC Training by D&H EngineersSPC Training by D&H Engineers
SPC Training by D&H Engineers
 
ppt2.pptx
ppt2.pptxppt2.pptx
ppt2.pptx
 
Statistics for analytics
Statistics for analyticsStatistics for analytics
Statistics for analytics
 
Introduction To Six Sigma
Introduction To Six SigmaIntroduction To Six Sigma
Introduction To Six Sigma
 
Feature Engineering
Feature Engineering Feature Engineering
Feature Engineering
 
Scott Clark, Software Engineer, Yelp at MLconf SF
Scott Clark, Software Engineer, Yelp at MLconf SFScott Clark, Software Engineer, Yelp at MLconf SF
Scott Clark, Software Engineer, Yelp at MLconf SF
 
Optimal Learning for Fun and Profit with MOE
Optimal Learning for Fun and Profit with MOEOptimal Learning for Fun and Profit with MOE
Optimal Learning for Fun and Profit with MOE
 
Lec-3 Forecasting.pdf Data science college
Lec-3 Forecasting.pdf Data science collegeLec-3 Forecasting.pdf Data science college
Lec-3 Forecasting.pdf Data science college
 
When Should I Use Simulation?
When Should I Use Simulation?When Should I Use Simulation?
When Should I Use Simulation?
 
EvoFIN2015
EvoFIN2015EvoFIN2015
EvoFIN2015
 

Prediction the stock market with genetic programming

  • 1. Predicting the Stock Market with Genetic Programming David Moskowitz, Ph.D. Infoblazer LLC Data Scientists in Stamford Westport January 25, 2016
  • 2. Disclaimer • The following is my opinion only • It is not the opinion of my employer • It is not related to any work done at my employer You should not make any decision, financial, investments, trading or otherwise, based on any of the information presented without undertaking independent due diligence and consultation with a professional broker or competent financial advisor. You understand that you are using any and all information presented at your own risk.
  • 3. Agenda • What is Genetic Programming? • Time Series Prediction • Stock Market Prediction • Other Issues • Modularity • Linear GP • Genetic Algorithms • Demonstrations
  • 4. What is Genetic Programming? • Get a computer to do something without telling it how to do it • Breeds a population of computer programs that improve over time • Evolution • Genetic Operators • Survival of the Fittest • Stochastic component • Non-Greedy • Creativity • Novel solutions • Size and shape of solution unknown • Limited only by what can be represented as a computer program (Koza et al., 2006, p. 11)
  • 5. Example: Design of a Satellite Boom • Designed using a genetic algorithm • 20,000+% improvement in frequency averaged energy levels (Keane, 1996)
  • 6. History • Visionaries • Samuel 1959 – Goal of AI • Turing 1948 – Evolutionary search, gene combination, survival of the fittest • Evolutionary Algorithms , 1962- • mutation , populations, • Genetic Algorithms, 1973- • John Holland • Crossover • Genetic Programming, 1989- • John Koza • Best way to represent a computer program is a computer program
  • 7. How GP Works • Preparatory Steps • Primitives • Fitness Function(s) • Initialize Population • Evolve Population • Calculate population fitness • Select next generation • Termination Condition (Poli et al.,2008, p. 2)
  • 8. GP Representation • LISP • (max (+ x x) (+ x (* 3 y))) • max(x+x,x+3*y) MAX X + + *X X 3 y
  • 9. GP Operations • Probabilistically select an operation • Crossover • Switch two nodes on different individuals • Mutation • Randomly modify an individual (node) • Reproduction • Copy parent, as is, to next generation
  • 10. GP Selection • Need to select one or two individuals for genetic operations • Selection is probabilistic • Fitness Proportional Selection • Tournament Selection
  • 11. Crossover + + X y 3 * + y 1 / X 2 + + X y 3 * + y 1 / X 2 Parents Children
  • 13. Demo – Symbolic Regression • Curve Fitting • 𝑥3 − 𝑥2 + 𝑥 − 4 • Prove that GP works -200000 0 200000 400000 600000 800000 1000000 1200000 0 10 20 30 40 50 60 70 80 90 Y X Demo a, b
  • 14. Chaotic Series • Look random, but are deterministic • Highly dependent on initial conditions • Difficult to predict 𝑌𝑡 = 𝑠𝑖𝑛 𝑥 − 130 + 𝑥 + 130 x=0: 𝑌0 = 0.9 X>0: 𝑌 𝑡+1 = 4𝑌𝑡(1 − 𝑌𝑡) 0 0.2 0.4 0.6 0.8 1 1.2 0 10 20 30 40 50 60 70 80 90 Y X 9 10 11 12 13 14 15 16 17 0 10 20 30 40 50 60 70 80 90 Y X
  • 15. Demo- Chaotic Series Symbolic Regression Demo c, d, e, f
  • 16. Regime Change • Goal is to uncover underlying data generating process • This can change over time -15 -10 -5 0 5 10 0 14 28 42 56 70 84 98 112 126 140 154 168 182 196 Y X 0<=x<70: 𝑌𝑡 = 𝑠𝑖𝑛 𝑥 + 𝑥 70<=x<130: 𝑌𝑡 = 𝑐𝑜𝑠 𝑥 − 𝑥 130<=x<200: 𝑌𝑡 = 𝑠𝑖𝑛 𝑥 − 130 + 𝑥 − 130 -3 -2 -1 0 1 2 3 0 20 40 60 80 100 120 140 160 180 200 220 240 260 280 300 320 340 360 380 400 Y X x<200, x>=300: 𝑌 𝑡+1 = 4𝑌𝑡(1 − 𝑌𝑡) 200<=x<300: 𝑌 𝑡+1 = 1.8708𝑌𝑡 − 𝑌𝑡−1 S&P 500 index close price during the stock market crash of 2008 (Yahoo, 2013). 𝑌𝑡 = 𝑓 𝑊𝑇𝐹 ?
  • 17. Demo- Symbolic Regression Regime Change Demo g
  • 18. Time Series Prediction • Train on past values • Predict future values • Retrain periodically
  • 19. Demo- Chaotic Series Prediction Demo h, I, j
  • 20. Market Prediction • S&P 500 Long-Flat (Invest-don’t invest) • Ignore transaction costs • Ignore out of market returns • Predictors • S&P 500 Price • S&P 500 Volume • 250-day MA normalized 0 0.2 0.4 0.6 0.8 1 1.2 1.4 0 500 1000 1500 2000 2500 S&P 500 Index Series Index Value 250 Day Moving Average 250-day MA Normalized
  • 21. GP is a Perfect Match for Market Prediction • “The interrelationships among the relevant variables is unknown or poorly understood (or where it is suspected that the current understanding may possibly be wrong).” • “Finding the size and shape of the ultimate solution is a major part of the problem.” • “Significant amounts of test data are available in computer-readable form.” • “There are good simulators to test the performance of tentative solutions to a problem, but poor methods to directly obtain good solutions.” • “Conventional mathematical analysis does not, or cannot, provide analytic solutions” • “An approximate solution is acceptable (or is the only result that is ever likely to be obtained)” • “Small improvements in performance are routinely measured (or easily measurable) and highly prized.” (Poli et al.,2008, pp. 111-113)
  • 22. Primitive Set • Hundreds of indicators • Ex. (http://www.investopedia.com/active-trading/technical-indicators/) • Include common technical analysis indicators • Momentum- compare to recent average • Breakout- compare to recent minimum/maximum • Ex. Buy if current price risen by 2% over minimum price last 30 days • Prefer low level functions • Better results possible with higher level , packaged indicators?
  • 23. Primitive set • Functions • Add • Subtract • Multiply • Divide • Gt • Lt • And • Or • Not • offsetValue • ifElseBoolean • movingAverage • periodMaximum • periodMinimum • AbsoluteDifference • Terminals • randomInteger(low high) • randomDouble(low high) • True • False • offsetValue(0) • Hundreds of other technical indicators (http://www.investopedia.com/active- trading/technical-indicators/)
  • 24. Training Approaches • Train-Predict-Retrain • Train-Test-Predict • Multiple Runs Train M Test N Predict 1 Train Predict M N (1) Demo k, l, m
  • 25. Experiment- Market Prediction • Investment decisions in S&P 500 Index • Modeled after (Chen et al., 2008), 1988-2004 • Long-Flat decisions • Normalized by 250-day moving average • Fitness = investment gain 0 200 400 600 800 1000 1200 1400 1600 1988-01-04 1988-08-16 1989-03-31 1989-11-10 1990-06-27 1991-02-08 1991-09-24 1992-05-07 1992-12-18 1993-08-04 1994-03-17 1994-10-31 1995-06-15 1996-01-29 1996-09-11 1997-04-25 1997-12-08 1998-07-24 1999-03-10 1999-10-21 2000-06-06 2001-01-19 2001-09-04 2002-04-25 2002-12-06 2003-07-24 2004-03-09 2004-10-21 2005-06-07 S&P 500 0.7 0.8 0.9 1 1.1 1.2 1.3 1988-12-27 1989-08-10 1990-03-26 1990-11-06 1991-06-21 1992-02-04 1992-09-17 1993-05-03 1993-12-14 1994-07-29 1995-03-14 1995-10-25 1996-06-10 1997-01-22 1997-09-05 1998-04-22 1998-12-03 1999-07-21 2000-03-03 2000-10-16 2001-06-01 2002-01-22 2002-09-05 2003-04-22 2003-12-03 2004-07-21 2005-03-04 2005-10-17 2006-06-02 Normalized
  • 26. Experiment- Market Prediction • Training-validation-prediction approach (T-V-P) • Training-prediction approach (T-P) • Training - 1989-1998 • Prediction- 1999-2004 (Chen et al., 2008, p. 110)
  • 27. Results T-V-P w/Trans Cost Method Mean Std. Dev. Min Max 95% CI # beating benchmark 1999-2000 Buy & Hold 0.0751 GP 0.0434 0.0664 -0.1917 0.1197 [0.0250 ... 0.0618] 5/50 ADF 0.0309 0.0798 -0.3054 0.0845 [0.0088 ... 0.0530] 3/50 ADT 0.0510 0.0519 -0.1974 0.1042 [0.0366 ... 0.0654] 5/50 2001-2002 Buy & Hold -0.3144 GP -0.3693 0.1306 -0.8087 -0.2885 [-0.4055 ... -0.3331] 1/50 ADF -0.3347 0.0887 -0.7290 -0.1777 [-0.3593 ... -0.3102] 2/50 ADT -0.3697 0.1390 -0.7450 -0.0134 [-0.4082 ... -0.3312] 1/50 2003-2004 Buy & Hold 0.3332 GP 0.2945 0.0497 0.1432 0.3291 [0.2807 ... 0.3083] 0/50 ADF 0.3139 0.0390 0.1170 0.3539 [0.3031 ... 0.3247] 1/50 ADT 0.3247 0.0150 0.2349 0.3522 [0.3205 ... 0.3289] 2/50 (Moskowitz, 2016, p. 119)
  • 28. Results T-V-P wo/Trans Cost Method Mean Std. Dev. Min Max 95% CI # beating benchmark 1999-2000 Buy & Hold 0.0751 GP 0.1494 0.1088 -0.0438 0.4525 [0.1192 ... 0.1795] 35/50 ADF 0.1418 0.1238 -0.0399 0.5112 [0.1075 ... 0.1761] 35/50 ADT 0.1567 0.1099 -0.0068 0.4796 [0.1262 ... 0.1871] 37/50 2001-2002 Buy & Hold -0.3144 GP -0.3121 0.0573 -0.4081 -0.0348 [-0.3280 ... -0.2962] 17/50 ADF -0.3023 0.0848 -0.5153 0.0196 [-0.3258 ... -0.2788] 18/50 ADT -0.2843 0.0635 -0.3924 -0.1245 [-0.3020 ... -0.2667] 32/50 2003-2004 Buy & Hold 0.3332 GP 0.3045 0.0929 0.0463 0.5045 [0.2788 ... 0.3303] 15/50 ADF 0.3395 0.1171 -0.0016 0.5597 [0.3070 ... 0.3719] 22/50 ADT 0.3329 0.1202 0.0775 0.6443 [0.2996 ... 0.3663] 29/50 (Moskowitz, 2016, p. 122)
  • 29. Results T-P w/Trans Cost (Moskowitz, 2016, p. 120) Method Mean Std. Dev. Min Max 95% CI # beating benchmark 1999-2000 Buy & Hold 0.0634 ADT 0.0018 0.1372 -0.4290 0.2388 [-0.0362 ... 0.1010] 17/50 DyFor GP -0.0157 0.1101 -0.2690 0.1426 [-0.0463 ... 0.0639] 15/50 2001-2002 Buy & Hold -0.3339 ADT -0.1364 0.1014 -0.2964 0.1601 [-0.1645 ... -0.0631] 50/50 DyFor GP -0.1018 0.0819 -0.2810 0.0817 [-0.1245 ... -0.0426] 50/50 2003-2004 Buy & Hold 0.2970 ADT 0.1035 0.0653 -0.0603 0.2529 [0.0854 ... 0.1507] 0/50 DyFor GP 0.0489 0.0723 -0.1780 0.2156 [0.0289 ... 0.1012] 0/50 1999-2004 Buy & Hold -0.0189 ADT -0.0349 0.1933 -0.5395 0.4592 [-0.0884 ... 0.0187] 24/50 DyFor GP -0.0698 0.1413 -0.3597 0.2136 [-0.1089 ... -0.0306] 15/50
  • 30. Results T-P wo/Trans Cost Method Mean Std. Dev. Min Max 95% CI # beating benchmark 1999-2000 Buy & Hold 0.0634 ADT 0.0788 0.1071 -0.1106 0.3576 [0.0491 ... 0.1562] 27/50 DyFor GP 0.0807 0.1323 -0.1904 0.3408 [0.0440 ... 0.1763] 26/50 2001-2002 Buy & Hold -0.3339 ADT -0.0524 0.1026 -0.2674 0.1521 [-0.0808 ... 0.0218] 50/50 DyFor GP -0.0594 0.0862 -0.2314 0.1020 [-0.0833 ... 0.0029] 50/50 2003-2004 Buy & Hold 0.2970 ADT 0.1246 0.0782 -0.0132 0.3739 [0.1029 ... 0.1811] 2/50 DyFor GP 0.1233 0.0702 -0.0297 0.2783 [0.1038 ... 0.1740] 0/50 1999-2004 Buy & Hold -0.0189 ADT 0.1683 0.2005 -0.1946 0.6959 [0.1128 ... 0.2239] 39/50 DyFor GP 0.1568 0.1887 -0.2618 0.5762 [0.1045 ... 0.2091] 41/50 (Moskowitz, 2016, p. 124)
  • 31. ADT vs DyFor GP vs Buy and Hold 750 850 950 1050 1150 1250 1350 1450 1550 1/4/1999 1/4/2000 1/4/2001 1/4/2002 1/4/2003 1/4/2004 With Transaction Costs, 50 run mean index value Dyfor.y_predicted ADT Sliding.y_predicted 750 850 950 1050 1150 1250 1350 1450 1550 1/4/1999 1/4/2000 1/4/2001 1/4/2002 1/4/2003 1/4/2004 Without Transaction Costs, 50 run mean index value Dyfor.y_predicted ADT Sliding.y_predicted ADT: +0.1683% DyFor GP: +0.1568% B&H: -0.0189% ADT: -0.349% DyFor GP: -0.698% B&H: -0.0189% (Moskowitz, 2016, p. 208) (Moskowitz, 2016, p. 213)
  • 32. Advanced GP • Modularity • Automatically Defined Functions • Strongly-typed GP • Closure • Advanced techniques • Looping • Memory store • Lambdas • Recursion • Time series regimes (Moskowitz, 2016) • Design patterns (Moskowitz, 2016) (Koza, 1994, p. 74)
  • 33. Genetic Algorithms • Non-differentiable / nonlinear optimization problem • Search for parameters, rules • Size and shape prescribed • Bit, Numeric, or other representations • Ex. Minimize 𝑥2 − 50𝑦 + 𝑧3 , x={0-31},y={0-31},z={0-15} 10011 11000 1011 00110 11010 1100 10010 11010 1100 00111 11000 1011 192 − 50 ∗ 24 + 113 =492 62 − 50 ∗ 26 + 123 =464 182 − 50 ∗ 26 + 123=752 72 − 50 ∗ 24 + 113=180
  • 34. Linear GP • Sequence of imperative instructions • Register-based operations • Machine code, GPU Instructions (Poli et al.,2008, pp. 61-65)
  • 35. Demo- Symbolic Regression Regime Change • Regime determining branch • Regime specific functions • Implements template method design pattern Demo o
  • 36. Next Steps • MATLAB (GA only) • JGAP (Java) • DEAP (Python) • Roll your own • Evolutionary Signals
  • 37. Not So Shameless Plug • Evolutionary Signals • Develop Buy/Sell signals using genetic programming • Minimal financial knowledge and no programming experience required • Goals • Much larger number of predictor series • Crowdsource models • Earn revenue from high performing models • Open beta testing • Visit www.gpsignals.com for more information
  • 38. Questions? • Thank you! • Contact info: LinkedIn: infoblazer @infojester
  • 39. References • Chen, S. H., Kuo, T. W., & Hoi, K. M. (2008). Genetic Programming and Financial Trading: How Much About “What We Know.” In Handbook of financial engineering (pp. 99–154). Springer US. doi:10.1007/978-0-387-76682-9 • Keane, A. J. (1996). THE DESIGN OF A SATELLITE BOOM WITH ENHANCED VIBRATION PERFORMANCE USING GENETIC ALGORITHM TECHNIQUES. The Journal of the Acoustical Society of America, 99(4), 2599–2603. • Koza, J. R. (1994). Genetic programming II: automatic discovery of reusable programs. MIT press. • Koza, J. ., Keane, M. A., Streeter, M. J., Mydlowec, W., Yu, J., & Lanza, G. (2006). Genetic programming IV: Routine human-competitive machine intelligence (Vol. 5). Springer. • Moskowitz, D. (2016). Automatically Defined Templates for Improved Prediction of Non- stationary, Nonlinear Time Series in Genetic Programming. Doctoral dissertation. Nova Southeastern University. Retrieved from http://nsuworks.nova.edu/gscis_etd/953. • Poli, R., Langdon, W. B., McPhee, N. F., & Koza, J. R. (2008). A field guide to genetic programming. Lulu. com.