Difference between revisions of "MC3-Project-4"

From Quantitative Analysis Software Courses
Jump to navigation Jump to search
(Created page with "==DRAFT== ==Updates / FAQs== ==Overview== In this project you will implement the Q-Learning and Dyna-Q solutions to the reinforcement learning problem. You will apply them...")
 
 
(65 intermediate revisions by 2 users not shown)
Line 1: Line 1:
==DRAFT==
+
==Updates / FAQs==
  
==Updates / FAQs==
+
* '''2017-7-11'''
 +
** Project revision in progress.
 +
 
 +
* '''2017-7-17'''
 +
** Project finalized.
  
 
==Overview==
 
==Overview==
  
In this project you will implement the Q-Learning and Dyna-Q solutions to the reinforcement learning problemYou will apply them to two problems: 1) Navigation, and 2) Trading. The reason for working with the navigation problem first is that, as you will see, navigation is an easy problem to work with and understand. In the last part of the assignment you will apply Q-Learning to stock trading.
+
In this project you will design a learning trading agent.  You must draw on the learners you have created so far in the courseYour choices are:
 +
 
 +
* Create a regression or classification-based strategy using your Random Forest learner. Suggestions if you follow this approach: [[Classification_Trader_Hints]]. Important note, if you choose this method, you must set the leaf_size for your learner to 5 or greater.  This is to avoid degenerate overfitting in sample.
 +
* Create a Q-learning-based strategy using your Q-Learner. Read the [[Classification_Trader_Hints]] first, because many of the ideas there are relevant for the Q trader, then see [[Q_Trader_Hints]]
 +
* Create a scan-based strategy using an optimizer.
  
Note that your Q-Learning code really shouldn't care which problem it is solving. The difference is that you need to wrap the learner in different code that frames the problem for the learner as necessary.
+
Your learner should work in the following way:
 +
* In the training phase (e.g., addEvidence()) your learner will be provided with a stock symbol and a time period.  It should use this data to learn its strategy. For instance, for a regression-based learner it will use this data to make predictions about future price changes.
 +
* In the testing phase (e.g., testPolicy()) your learner will be provided a symbol and a date range. All learning should be turned OFF during this phase.
 +
If the date range is the same as used for the training, it is an in-sample test. Otherwise it is an out-of-sample test.  Your learner should return a set of dated trades similar to those input to your market simulator.
  
For the navigation problem we have created testqlearner.py that automates testing of your Q-Learner in the navigation problemWe also provide teststrategylearner.py to test your strategy learner.  In order to apply Q-learning to trading you will have to implement an API that calls Q-learning internally.  
+
Here are some important requirements: Your testPolicy() method should be much faster than your addEvidence() method. The timeout requirements (see rubric) will be set accordinglyMultiple calls to your testPolicy() method should return exactly the same result.
  
 
Overall, your tasks for this project include:
 
Overall, your tasks for this project include:
  
* Code a Q-Learner
+
* Devise numerical/technical indicators to evaluate the state of a stock on each day.
* Code the Dyna-Q feature of Q-Learning
+
* Build a strategy learner based on one of the learners described above that uses the indicators.
* Test/debug the Q-Learner in navigation problems
+
* Test/debug the strategy learner on specific symbol/time period problems.
* Build a strategy learner based on your Q-Learner
+
* Write a report describing your learning strategy.
* Test/debug the strategy learner on specific symbol/time period problems
 
 
 
Scoring for the project will be allocated as follows:
 
 
 
* Navigation test cases: 80% (note that we will check those with dyna = 0)
 
* Dyna implemented: 5% (we will check this with one navigation test case by comparing performance with and without dyna turned on)
 
* Trading strategy test cases: 20%
 
  
For this assignment we will test only your code (there is no report component).  Note that the scoring is structured so that you can earn a B (80%) if you implement only Q-Learning, but if you implement everything, the total possible score is 105%.  That means you can earn up to 5% extra credit.
+
Scoring for the project will be based on trading strategy test cases and a report.
  
 
==Template and Data==
 
==Template and Data==
  
* Download <tt>'''[[Media:mc3_p3.zip|mc3_p3.zip]]'''</tt>, unzip inside <tt>ml4t/</tt>
+
* Update your local repository from github.  
* Implement the <tt>QLearner</tt> class in <tt>mc3_p3/QLearner.py</tt>.
+
* Place your existing Q-Learner or RTLearner or OptimizationLearner into <tt>mc3p4_strategy_learner/</tt>.
* Implement the <tt>StrategyLearner</tt> class in <tt>mc3_p3/StrategyLearner.py</tt>
+
* Implement the <tt>StrategyLearner</tt> class in <tt>mc3p4_strategy_learner/StrategyLearner.py</tt>
* To test your Q-learner, run <tt>'''python testqlearner.py'''</tt> from the <tt>mc3_p3/</tt> directory.
+
* ALL of your code should be contained in the two files listed above.
* To test your strategy learner, run <tt>'''python teststrategylearner.py'''</tt> from the <tt>mc3_p3/</tt> directory.
+
* To test your strategy learner, follow the instructions on [[ML4T Software Setup#Running the grading scripts|Running the grading scripts]]
* Note that example problems are provided in the <tt>mc3_p3/testworlds</tt> directory
 
 
 
==Part 1: Implement QLearner==
 
 
 
Your QLearner class should be implemented in the file <tt>QLearner.py</tt>.  It should implement EXACTLY the API defined below.  DO NOT import any modules besides those allowed below.  Your class should implement the following methods:
 
 
 
* QLearner(...): Constructor, see argument details below.
 
* query(s_prime, r): Update Q-table with <s, a, s_prime, r> and return new action for state s_prime, update rar.
 
* querysetstate(s): Set state to s, return action for state s, but don't update Q-table or rar.
 
 
 
Here's an example of the API in use:
 
 
 
<PRE>
 
import QLearner as ql
 
 
 
learner = ql.QLearner(num_states = 100, \
 
    num_actions = 4, \
 
    alpha = 0.2, \
 
    gamma = 0.9, \
 
    rar = 0.98, \
 
    radr = 0.999, \
 
    dyna = 0, \
 
    verbose = False)
 
 
 
s = 99 # our initial state
 
 
 
a = learner.querysetstate(s) # action for state s
 
 
 
s_prime = 5 # the new state we end up in after taking action a in state s
 
 
 
r = 0 # reward for taking action a in state s
 
 
 
next_action = learner.query(s_prime, r)
 
</PRE>
 
 
 
<b>The constructor QLearner()</b> should reserve space for keeping track of Q[s, a] for the number of states and actions.  It should initialize Q[] with uniform random values between -1.0 and 1.0.  Details on the input arguments to the constructor:
 
 
 
* <tt>num_states</tt> integer, the number of states to consider
 
* <tt>num_actions</tt>  integer, the number of actions available.
 
* <tt>alpha</tt> float, the learning rate used in the update rule. Should range between 0.0 and 1.0 with 0.2 as a typical value.
 
* <tt>gamma</tt> float, the discount rate used in the update rule.  Should range between 0.0 and 1.0 with 0.9 as a typical value.
 
* <tt>rar</tt> float, random action rate: the probability of selecting a random action at each step. Should range between 0.0 (no random actions) to 1.0 (always random action) with 0.5 as a typical value.
 
* <tt>radr</tt> float, random action decay rate, after each update, rar = rar * radr. Ranges between 0.0 (immediate decay to 0) and 1.0 (no decay).  Typically 0.99.
 
* <tt>dyna</tt> integer, conduct this number of dyna updates for each regular update.  When Dyna is used, 200 is a typical value.
 
* <tt>verbose</tt> boolean, if True, your class is allowed to print debugging statements, if False, all printing is prohibited.
 
 
 
<b>query(s_prime, r)</b> is the core method of the Q-Learner.  It should keep track of the last state s and the last action a, then use the new information s_prime and r to update the Q table.  The learning instance, or experience tuple is <s, a, s_prime, r>.  query() should return an integer, which is the next action to take.  Note that it should choose a random action with probability rar, and that it should update rar according to the decay rate radr at each step.  Details on the arguments:
 
  
* <tt>s_prime</tt> integer, the the new state.
+
Use the following parameters for trading and evaluation:
* <tt>r</tt> float, a real valued immediate reward.
 
  
<b>querysetstate(s)</b> A special version of the query method that sets the state to s, and returns an integer action according to the same rules as query() (including choosing a random action sometimes), but it does not execute an update to the Q-table. It also does not update rar. There are two main uses for this method: 1) To set the initial state, and 2) when using a learned policy, but not updating it.
+
* Use only the data provided for this course.  You are not allowed to import external data.
 +
* Allowable positions are: 200 shares long, 200 shares short, 0 shares.
 +
* Benchmark:
 +
** The performance of a portfolio starting with $100,000 cash, then investing in 200 shares of the relevant symbol and holding that position
 +
* There is no limit on leverage.
 +
* Use the transaction cost model from [[MC2-Project-1]] when evaluating your portfolio. (IE: commissions and market impact)
  
==Part 2: Navigation Problem Test Cases==
+
==Implement Strategy Learner==
  
We will test your Q-Learner with a navigation problem as followsNote that your Q-Learner does not need to be coded specially for this task.  In fact the code doesn't need to know anything about it.  The code necessary to test your learner with this navigation task is implemented in testqlearner.py for you.  The navigation task takes place in a 10 x 10 grid world.  The particular environment is expressed in a CSV file of integers, where the value in each position is interpreted as follows:
+
For this part of the project you should develop a learner that can learn a trading policy using your learnerYou should be able to use your Q-Learner or RTLearner from the earlier project directly, with no changesIf you want to use the optimization approach, you will need to create new code or that. You will need to write code in <tt>StrategyLearner.py</tt> to "wrap" your learner appropriately to frame the trading problem for it.  Utilize the template provided in <tt>StrategyLearner.py</tt>.
 
 
* 0: blank space.
 
* 1: an obstacle.
 
* 2: the starting location for the robot.
 
* 3: the goal location.
 
 
 
An example navigation problem (CSV file) is shown below.  Following python conventions, [0,0] is upper left, or northwest corner, [9,9] lower right or southeast corner.  Rows are north/south, columns are east/west.
 
 
 
<PRE>
 
0,0,0,0,3,0,0,0,0,0
 
0,0,0,0,0,0,0,0,0,0
 
0,0,0,0,0,0,0,0,0,0
 
0,0,1,1,1,1,1,0,0,0
 
0,0,1,0,0,0,1,0,0,0
 
0,0,1,0,0,0,1,0,0,0
 
0,0,1,0,0,0,1,0,0,0
 
0,0,0,0,0,0,0,0,0,0
 
0,0,0,0,0,0,0,0,0,0
 
0,0,0,0,2,0,0,0,0,0
 
</PRE>
 
 
 
In this example the robot starts at the bottom center, and must navigate to the top centerNote that a wall of obstacles blocks its path.  We map this problem to a reinforcement learning problem as follows:
 
 
 
* State: The state is the location of the robot, it is computed (discretized) as: column location * 10 + row location.
 
* Actions: There are 4 possible actions, 0: move north, 1: move east, 2: move south, 3: move west.
 
* R: The reward is -1.0 unless the action leads to the goal, in which case the reward is +1.0.
 
* T: The transition matrix can be inferred from the CSV map and the actions.
 
 
 
Note that R and T are not known by or available to the learner.  The testing code <tt>testqlearner.py</tt> will test your code as follows (pseudo code):
 
 
 
<pre>
 
Instantiate the learner with the constructor QLearner()
 
s = initial_location
 
a = querysetstate(s)
 
s_prime = new location according to action a
 
r = -1.0
 
while not converged:
 
    a = query(s_prime, r)
 
    s_prime = new location according to action a
 
    if s_prime == goal:
 
        r = +1
 
        s_prime = start location
 
    else
 
        r = -1
 
</pre>
 
 
 
A few things to note about this code: The learner always receives a reward of -1.0 until it reaches the goal, when it receives a reward of +1.0. As soon as the robot reaches the goal, it is immediately returned to the starting location.
 
 
 
Here are example solutions:
 
 
 
[[mc3_p3_examples]]
 
 
 
[[mc3_p3_dyna_examples]]
 
 
 
==Part 3: Implement Dyna==
 
 
 
Add additional components to your QLearner class so that multiple "hallucinated" experience tuples are used to update the Q-table for each "real" experience.  The addition of this component should speed convergence in terms of the number of calls to query().
 
 
 
We will test your code on <tt>world03.csv</tt> with 50 iterations and with dyna = 200.  Our expectation is that with Dyna, the solution should be much better than without.
 
 
 
==Part 4: Implement Strategy Learner==
 
 
 
For this part of the project you should develop a learner that can learn a trading policy using your Q-Learner.  Utilize the template provided in <tt>StrategyLearner.py</tt> Overall the structure of your strategy learner should be arranged like this:
 
 
 
For the policy learning part:
 
* Select several technical features, and compute their values for the training data
 
* Discretize the values of the features
 
* Instantiate a Q-learner
 
* For each day in the training data:
 
** Compute the current state (including holding)
 
** Compute the reward for the last action
 
** Query the learner with the current state and reward to get an action
 
** Implement the action the learner returned (BUY, SELL, NOTHING), and update portfolio value
 
* Repeat the above loop multiple times until cumulative return stops improving.
 
 
 
A rule to keep in mind: As in past projects, you can only be long or short 100 shares, so if your learner returns two BUYs in a row, don't double down, same thing with SELLs.
 
 
 
For the policy testing part:
 
* For each day in the testing data:
 
** Compute the current state
 
** Query the learner with the current state to get an action
 
** Implement the action the learner returned (BUY, SELL, NOTHING), and update portfolio value
 
* Return the resulting trades in a data frame (details below).
 
  
 
Your StrategyLearner should implement the following API:
 
Your StrategyLearner should implement the following API:
Line 179: Line 57:
 
import StrategyLearner as sl
 
import StrategyLearner as sl
 
learner = sl.StrategyLearner(verbose = False) # constructor
 
learner = sl.StrategyLearner(verbose = False) # constructor
learner.addEvidence(symbol = "IBM", sd=dt.datetime(2008,1,1), ed=dt.datetime(2009,1,1), sv = 10000) # training step
+
learner.addEvidence(symbol = "AAPL", sd=dt.datetime(2008,1,1), ed=dt.datetime(2009,12,31), sv = 100000) # training phase
df_trades = learner.testPolicy(symbol = "IBM", sd=dt.datetime(2009,1,1), ed=dt.datetime(2010,1,1), sv = 10000) # testing step
+
df_trades = learner.testPolicy(symbol = "AAPL", sd=dt.datetime(2010,1,1), ed=dt.datetime(2011,12,31), sv = 100000) # testing phase
 
</PRE>
 
</PRE>
  
Line 193: Line 71:
 
The output result is:  
 
The output result is:  
  
* df_trades: A data frame whose values represent trades for each day.  Legal values are +100.0 indicating a BUY of 100 shares, -100.0 indicating a SELL of 100 shares, and 0.0 indicating NOTHING [update, values of +200 and -200 for trades are also legal so long as net holdings are constrained to -100, 0, and 100].
+
* df_trades: A data frame whose values represent trades for each day.  Legal values are +200.0 indicating a BUY of 200 shares, -200.0 indicating a SELL of 200 shares, and 0.0 indicating NOTHING.  Values of +400 and -400 for trades are also legal so long as net holdings are constrained to -200, 0, and 200.
  
 
==Contents of Report==
 
==Contents of Report==
  
There is no report component of this assignmentHowever, if you would like to impress us with your Machine Learning prowess, you are invited to submit a succinct report.
+
Write a report describing your systemThe centerpiece of your report should be the description of how you have utilized your learner to determine trades.  Describe the steps you took to frame the trading problem as a learning problem for your learner.
  
==Hints & resources==
+
In the course of creating your learning trading strategy you will probably evaluate a number of different (hyper-)parameters for your learner and your trading strategy.  Choose two of those to look at more carefully.  Conduct and report on two experiments that illustrate the methods by which you refined your learner or strategy to excel at the assigned task.
  
This paper by Kaelbling, Littman and Moore, is a good resource for RL in general: http://www.jair.org/media/301/live-301-1562-jair.pdf  See Section 4.2 for details on Q-Learning.
+
Your descriptions should be stated clearly enough that an informed reader could reproduce the results you report.
  
There is also a chapter in the Mitchell book on Q-Learning.
+
The report can be up to 2000 words long and contain up to 6 figures (charts and/or tables).
 
 
For implementing Dyna, you may find the following resources useful:
 
 
 
* https://webdocs.cs.ualberta.ca/~sutton/book/ebook/node96.html
 
* http://www-anw.cs.umass.edu/~barto/courses/cs687/Chapter%209.pdf
 
  
 
==What to turn in==
 
==What to turn in==
  
Turn your project in via t-square.  All of your code must be contained within QLearner.py and StrategyLearner.py.
+
Turn your project in via t-square.  Your submission should include exactly 3 files. All of your code must be contained within two files: your learner and StrategyLearner.py.
  
* Your QLearner as <tt>QLearner.py</tt>
+
* Your learner.
 
* Your StrategyLearner as <tt>StrategyLearner.py</tt>
 
* Your StrategyLearner as <tt>StrategyLearner.py</tt>
* Your report (if any) as <tt>report.pdf</tt>
+
* Your report as <tt>report.pdf</tt>
 
* Do not submit any other files.
 
* Do not submit any other files.
  
 
==Rubric==
 
==Rubric==
  
Only your QLearner class will be tested. 
+
'''Code: 65 points'''
  
* For basic Q-Learning (dyna = 0) we will test your learner against 10 test worlds with 500 iterations.  Each test should complete in less than 2 seconds.  For the test to be successful, your learner should find a path to the goal <= 1.5 x the number of steps our reference solution finds. We will check this by taking the min of all the 500 runs. Each test case is worth 8 points. We will initialize your learner with the following parameter values:
+
We will test StrategyLearner in the following situations:
 +
* Training / in sample: January 1, 2008 to December 31 2009.
 +
* Testing / out of sample: January 1, 2010 to December 31 2011.
 +
* Symbols: ML4T-220, AAPL, UNH, SINE_FAST_NOISE
 +
* Starting value: $100,000
 +
* Benchmark: Buy 200 shares on the first trading day, Sell 200 shares on the last day.
  
<Pre>
+
We expect the following outcomes in evaluating your system:
    learner = ql.QLearner(num_states=100,\
+
* For ML4T-220
        num_actions = 4, \
+
** addEvidence() completes without crashing within 25 seconds: 1 points
        alpha = 0.2, \
+
** testPolicy() completes in-sample within 5 seconds: 2 points
        gamma = 0.9, \
+
** testPolicy() returns same result when called in-sample twice: 2 points
        rar = 0.98, \
+
** testPolicy() returns an in-sample result with cumulative return greater than 100%: 5 points
        radr = 0.999, \
+
** testPolicy() returns an out-of-sample result with cumulative return greater than 100%: 5 points
        dyna = 0, \
+
* For AAPL
        verbose=False) #initialize the learner
+
** addEvidence() completes without crashing within 25 seconds: 1 points
</PRE>
+
** testPolicy() completes in-sample within 5 seconds: 2 points
 +
** testPolicy() returns same result when called in-sample twice: 2 points
 +
** testPolicy() returns an in-sample result with cumulative return greater than benchmark: 5 points
 +
** testPolicy() returns an out-of-sample result within 5 seconds: 5 points
 +
* For SINE_FAST_NOISE
 +
** addEvidence() completes without crashing within 25 seconds: 1 points
 +
** testPolicy() completes in-sample within 5 seconds: 2 points
 +
** testPolicy() returns same result when called in-sample twice: 2 points
 +
** testPolicy() returns an in-sample result with cumulative return greater than 200%: 5 points
 +
** testPolicy() returns an out-of-sample result within 5 seconds: 5 points
 +
* For UNH
 +
** addEvidence() completes without crashing within 25 seconds: 1 points
 +
** testPolicy() completes in-sample within 5 seconds: 2 points
 +
** testPolicy() returns same result when called in-sample twice: 2 points
 +
** testPolicy() returns an in-sample result with cumulative return greater than benchmark: 5 points
 +
** testPolicy() returns an out-of-sample result within 5 seconds: 5 points
 +
* For withheld test case
 +
** If any part of code crashes: 0 points awarded.
 +
** testPolicy() returns an in-sample result with cumulative return greater than benchmark: 5 points
  
* For Dyna-Q, we will set dyna = 200.  We will test your learner against <tt>world03.csv</tt> with 50 iterations.  The test should complete in less than 10 seconds. For the test to be successful, your learner should find a path to the goal <= 1.5 x the number of steps our reference solution finds.  We will check this by taking the min of all 50 runs. The test case is worth 5 points.  We will initialize your learner with the following parameter values:
+
We reserve the right to use different time periods if necessary to reduce auto grading time.
  
<Pre>
+
* IMPORTANT NOTES
    learner = ql.QLearner(num_states=100,\
+
** For achieving the required cumulative return, recall that <tt>cr = (portval[-1]/portval[0]) - 1.0</tt>
        num_actions = 4, \
+
** The requirement that consecutive calls to <tt>testPolicy()</tt> produce the same output for the same input means that you '''cannot''' update, train, or tune your learner in this method. For example, a solution that uses Q-Learning should use <tt>querySetState()</tt> and '''not''' <tt>query()</tt> in <tt>testPolicy()</tt>. Updating, training, and tuning (<tt>query()</tt>) is fine inside <tt>addEvidence()</tt>.
        alpha = 0.2, \
+
** Your learner should '''not''' select different hyper-parameters based on the '''symbol'''. Hyper-parameters include (but are not limited to) things like features, discretization size, sub-learning methods (for ensemble learners). Tuning using cross-validation or otherwise pre-processing the '''data''' is OK, things like <tt>if symbol=="UNH"</tt> are '''not OK'''. There may be a withheld test case that checks your code on a valid symbol that is not one of the four listed above.
        gamma = 0.9, \
+
** Presence of code like <tt>if symbol=="UNH"</tt> will result in a 20 point penalty.
        rar = 0.5, \
+
** When evaluating the trades generated by your learner, we '''will''' consider transaction costs (market  impact and commissions).
        radr = 0.99, \
 
        dyna = 200, \
 
        verbose=False) #initialize the learner
 
</PRE>
 
  
* We will test StrategyLearner in the following situations:
+
'''Report: 35 points'''
** Training: Dec 31 2007 to Dec 31 2009
 
** Testing: Dec 31 2009 to Dec 31 2011
 
** Symbols: ML4T-220, IBM
 
** Starting value: $10,000
 
** Benchmark: Buy 100 shares on the first trading day, Sell 100 shares on the last day.
 
* We expect the following outcomes in testing:
 
** For ML4T-220, the trained policy should significantly outperform the benchmark in sample (7 points)
 
** For ML4T-220, the trained policy should significantly outperform the benchmark out of sample (7 points)
 
** For IBM, the trained policy should significantly outperform the benchmark in sample (7 points)
 
  
Training and testing for each situation should run in less than 30 seconds.  We reserve the right to use different time periods if necessary to reduce auto grading time.
+
* Is the method by which the learner is utilized to create a trading strategy described sufficiently clearly that an informed reader could reproduce the result? (up to 10 point deduction if not)
 +
* Does report description match the code? (up to 10 point deduction if not)
 +
* Are the two required experiments explained well? (up to 5 points deduction each if not)
 +
* Are the two required experiments compellingly supported with tabular or graphical data? (up to 5 points deduction each if not)
 +
* Does the report contain more than 2000 words? (10 point deduction if so)
 +
* Does the report contain more than 6 figures and/or tables? (10 point deduction if so)
 +
* Is the report especially well written (up to 2 point bonus if so)
  
 
==Required, Allowed & Prohibited==
 
==Required, Allowed & Prohibited==
Line 266: Line 155:
 
Required:
 
Required:
 
* Your project must be coded in Python 2.7.x.
 
* Your project must be coded in Python 2.7.x.
* Your code must run on one of the university-provided computers (e.g. buffet02.cc.gatech.edu), or on one of the provided virtual images.
+
* Your code must run on one of the university-provided computers (e.g. buffet02.cc.gatech.edu).
 +
* All code must be your own.
 +
* No external learning libraries allowed.
  
 
Allowed:
 
Allowed:
Line 285: Line 176:
 
==Legacy==
 
==Legacy==
  
 +
*[[MC3-Project-4-Legacy-Q-trader]]
 
*[[MC3-Project-2-Legacy-trader]]
 
*[[MC3-Project-2-Legacy-trader]]
 
*[[MC3-Project-2-Legacy]]
 
*[[MC3-Project-2-Legacy]]

Latest revision as of 13:00, 18 July 2017

Updates / FAQs

  • 2017-7-11
    • Project revision in progress.
  • 2017-7-17
    • Project finalized.

Overview

In this project you will design a learning trading agent. You must draw on the learners you have created so far in the course. Your choices are:

  • Create a regression or classification-based strategy using your Random Forest learner. Suggestions if you follow this approach: Classification_Trader_Hints. Important note, if you choose this method, you must set the leaf_size for your learner to 5 or greater. This is to avoid degenerate overfitting in sample.
  • Create a Q-learning-based strategy using your Q-Learner. Read the Classification_Trader_Hints first, because many of the ideas there are relevant for the Q trader, then see Q_Trader_Hints
  • Create a scan-based strategy using an optimizer.

Your learner should work in the following way:

  • In the training phase (e.g., addEvidence()) your learner will be provided with a stock symbol and a time period. It should use this data to learn its strategy. For instance, for a regression-based learner it will use this data to make predictions about future price changes.
  • In the testing phase (e.g., testPolicy()) your learner will be provided a symbol and a date range. All learning should be turned OFF during this phase.

If the date range is the same as used for the training, it is an in-sample test. Otherwise it is an out-of-sample test. Your learner should return a set of dated trades similar to those input to your market simulator.

Here are some important requirements: Your testPolicy() method should be much faster than your addEvidence() method. The timeout requirements (see rubric) will be set accordingly. Multiple calls to your testPolicy() method should return exactly the same result.

Overall, your tasks for this project include:

  • Devise numerical/technical indicators to evaluate the state of a stock on each day.
  • Build a strategy learner based on one of the learners described above that uses the indicators.
  • Test/debug the strategy learner on specific symbol/time period problems.
  • Write a report describing your learning strategy.

Scoring for the project will be based on trading strategy test cases and a report.

Template and Data

  • Update your local repository from github.
  • Place your existing Q-Learner or RTLearner or OptimizationLearner into mc3p4_strategy_learner/.
  • Implement the StrategyLearner class in mc3p4_strategy_learner/StrategyLearner.py
  • ALL of your code should be contained in the two files listed above.
  • To test your strategy learner, follow the instructions on Running the grading scripts

Use the following parameters for trading and evaluation:

  • Use only the data provided for this course. You are not allowed to import external data.
  • Allowable positions are: 200 shares long, 200 shares short, 0 shares.
  • Benchmark:
    • The performance of a portfolio starting with $100,000 cash, then investing in 200 shares of the relevant symbol and holding that position
  • There is no limit on leverage.
  • Use the transaction cost model from MC2-Project-1 when evaluating your portfolio. (IE: commissions and market impact)

Implement Strategy Learner

For this part of the project you should develop a learner that can learn a trading policy using your learner. You should be able to use your Q-Learner or RTLearner from the earlier project directly, with no changes. If you want to use the optimization approach, you will need to create new code or that. You will need to write code in StrategyLearner.py to "wrap" your learner appropriately to frame the trading problem for it. Utilize the template provided in StrategyLearner.py.

Your StrategyLearner should implement the following API:

import StrategyLearner as sl
learner = sl.StrategyLearner(verbose = False) # constructor
learner.addEvidence(symbol = "AAPL", sd=dt.datetime(2008,1,1), ed=dt.datetime(2009,12,31), sv = 100000) # training phase
df_trades = learner.testPolicy(symbol = "AAPL", sd=dt.datetime(2010,1,1), ed=dt.datetime(2011,12,31), sv = 100000) # testing phase

The input parameters are:

  • verbose: if False do not generate any output
  • symbol: the stock symbol to train on
  • sd: A datetime object that represents the start date
  • ed: A datetime object that represents the end date
  • sv: Start value of the portfolio

The output result is:

  • df_trades: A data frame whose values represent trades for each day. Legal values are +200.0 indicating a BUY of 200 shares, -200.0 indicating a SELL of 200 shares, and 0.0 indicating NOTHING. Values of +400 and -400 for trades are also legal so long as net holdings are constrained to -200, 0, and 200.

Contents of Report

Write a report describing your system. The centerpiece of your report should be the description of how you have utilized your learner to determine trades. Describe the steps you took to frame the trading problem as a learning problem for your learner.

In the course of creating your learning trading strategy you will probably evaluate a number of different (hyper-)parameters for your learner and your trading strategy. Choose two of those to look at more carefully. Conduct and report on two experiments that illustrate the methods by which you refined your learner or strategy to excel at the assigned task.

Your descriptions should be stated clearly enough that an informed reader could reproduce the results you report.

The report can be up to 2000 words long and contain up to 6 figures (charts and/or tables).

What to turn in

Turn your project in via t-square. Your submission should include exactly 3 files. All of your code must be contained within two files: your learner and StrategyLearner.py.

  • Your learner.
  • Your StrategyLearner as StrategyLearner.py
  • Your report as report.pdf
  • Do not submit any other files.

Rubric

Code: 65 points

We will test StrategyLearner in the following situations:

  • Training / in sample: January 1, 2008 to December 31 2009.
  • Testing / out of sample: January 1, 2010 to December 31 2011.
  • Symbols: ML4T-220, AAPL, UNH, SINE_FAST_NOISE
  • Starting value: $100,000
  • Benchmark: Buy 200 shares on the first trading day, Sell 200 shares on the last day.

We expect the following outcomes in evaluating your system:

  • For ML4T-220
    • addEvidence() completes without crashing within 25 seconds: 1 points
    • testPolicy() completes in-sample within 5 seconds: 2 points
    • testPolicy() returns same result when called in-sample twice: 2 points
    • testPolicy() returns an in-sample result with cumulative return greater than 100%: 5 points
    • testPolicy() returns an out-of-sample result with cumulative return greater than 100%: 5 points
  • For AAPL
    • addEvidence() completes without crashing within 25 seconds: 1 points
    • testPolicy() completes in-sample within 5 seconds: 2 points
    • testPolicy() returns same result when called in-sample twice: 2 points
    • testPolicy() returns an in-sample result with cumulative return greater than benchmark: 5 points
    • testPolicy() returns an out-of-sample result within 5 seconds: 5 points
  • For SINE_FAST_NOISE
    • addEvidence() completes without crashing within 25 seconds: 1 points
    • testPolicy() completes in-sample within 5 seconds: 2 points
    • testPolicy() returns same result when called in-sample twice: 2 points
    • testPolicy() returns an in-sample result with cumulative return greater than 200%: 5 points
    • testPolicy() returns an out-of-sample result within 5 seconds: 5 points
  • For UNH
    • addEvidence() completes without crashing within 25 seconds: 1 points
    • testPolicy() completes in-sample within 5 seconds: 2 points
    • testPolicy() returns same result when called in-sample twice: 2 points
    • testPolicy() returns an in-sample result with cumulative return greater than benchmark: 5 points
    • testPolicy() returns an out-of-sample result within 5 seconds: 5 points
  • For withheld test case
    • If any part of code crashes: 0 points awarded.
    • testPolicy() returns an in-sample result with cumulative return greater than benchmark: 5 points

We reserve the right to use different time periods if necessary to reduce auto grading time.

  • IMPORTANT NOTES
    • For achieving the required cumulative return, recall that cr = (portval[-1]/portval[0]) - 1.0
    • The requirement that consecutive calls to testPolicy() produce the same output for the same input means that you cannot update, train, or tune your learner in this method. For example, a solution that uses Q-Learning should use querySetState() and not query() in testPolicy(). Updating, training, and tuning (query()) is fine inside addEvidence().
    • Your learner should not select different hyper-parameters based on the symbol. Hyper-parameters include (but are not limited to) things like features, discretization size, sub-learning methods (for ensemble learners). Tuning using cross-validation or otherwise pre-processing the data is OK, things like if symbol=="UNH" are not OK. There may be a withheld test case that checks your code on a valid symbol that is not one of the four listed above.
    • Presence of code like if symbol=="UNH" will result in a 20 point penalty.
    • When evaluating the trades generated by your learner, we will consider transaction costs (market impact and commissions).

Report: 35 points

  • Is the method by which the learner is utilized to create a trading strategy described sufficiently clearly that an informed reader could reproduce the result? (up to 10 point deduction if not)
  • Does report description match the code? (up to 10 point deduction if not)
  • Are the two required experiments explained well? (up to 5 points deduction each if not)
  • Are the two required experiments compellingly supported with tabular or graphical data? (up to 5 points deduction each if not)
  • Does the report contain more than 2000 words? (10 point deduction if so)
  • Does the report contain more than 6 figures and/or tables? (10 point deduction if so)
  • Is the report especially well written (up to 2 point bonus if so)

Required, Allowed & Prohibited

Required:

  • Your project must be coded in Python 2.7.x.
  • Your code must run on one of the university-provided computers (e.g. buffet02.cc.gatech.edu).
  • All code must be your own.
  • No external learning libraries allowed.

Allowed:

  • You can develop your code on your personal machine, but it must also run successfully on one of the university provided machines or virtual images.
  • Your code may use standard Python libraries.
  • You may use the NumPy, SciPy, matplotlib and Pandas libraries. Be sure you are using the correct versions.
  • You may reuse sections of code (up to 5 lines) that you collected from other students or the internet.
  • Code provided by the instructor, or allowed by the instructor to be shared.
  • Use util.py (only) for reading data.

Prohibited:

  • Any libraries not listed in the "allowed" section above.
  • Any code you did not write yourself (except for the 5 line rule in the "allowed" section).
  • Any Classes (other than Random) that create their own instance variables for later use (e.g., learners like kdtree).
  • Print statements outside "verbose" checks (they significantly slow down auto grading).
  • Any method for reading data besides util.py

Legacy