代写COMSM0093: Introduction to Financial Technology (INFT) 2023-24代写R语言
- 首页 >> C/C++编程COMSM0093: Introduction to Financial Technology (INFT)
Academic Year 2023-24.
Resit and Supplementary Coursework.
This coursework requires the latest version of BSE.py (Dec 4th, 2023) from
https://github.com/davecliff/BristolStockExchange
Download the full BSE repository as ZIP folder, here:
https://github.com/davecliff/BristolStockExchange/archive/refs/heads/master.zip
Or access and download BSE.py file directly, here:
https://github.com/davecliff/BristolStockExchange/blob/master/BSE.py
WARNING: Do not use an earlier version of BSE.py that you downloaded in TB1. This coursework is in two parts:
• Part A: 40 marks
• Part B: 60 marks
You should create and submit two Jupyter Notebooks – one for each part - clearly
labelled as PartA and Part B. You should also use a *different* local copy of BSE.py file
for each part: clearly named, e.g., BSE-A.py and BSE-B.py. You should submit both BSE files along with your Jupyter Notebooks and any other code files you write.
Your Jupyter Notebook for Part A should contain no more than 1000 words of markdown. Your Jupyter Notebook for Part B should contain no more than 1000 words of markdown.
Part A: Exploration of Smith’s Test 3 using automated trading agents in BSE.
In Part A you will configure, run, visualise, and analyse market experiments based on Smith’s “Test 3”, in his seminal 1962 paper.
Source: Vernon Smith (1962), An experimental study of competitive market behavior, Journal of Political Economy, 70(2), pp. 111-137. Available online:PDF
Smith Section II describes his experimental procedure. Each experiment, human participants are divided into buyers and sellers. Buyers and sellers are allocated a card with a reservation price (maximum price to buy; or minimum price to sell) and then asked to trade. When no more trades take place,a new “period” begins. At the start of the new period, buyers and sellers are re-allocated a new reservation card and asked to trade again. The experiment repeats for P periods.
Smith performs multiple experiments with different configurations. Here, we will focus on Test 3. Smith’s visualisation of Test 3 is shown in Chart 3, copied below:
In Part A, you are required to configure a BSE experiment to closely replicate Vernon Smith’s Test 3. Rather than using human participants, you will use trading agents as participants.
You will be awarded marks for following Smith’s method closely, and marks will be lost if you deviate in method. You will also be awarded marks for quality visualisation (e.g., graphs with appropriate labelling) and for presenting graphs in a similar form to Smith’s Chart 3. Marks will be lost the more you deviate from the general form of Smith’svisualisation.
Please note: Smith uses decimal prices, ranging from $0.00 to $10.00. BSE uses integer price values, such that $1.00 in Smith’s experiment is equivalent to price 100 in BSE. BSE.py has a maximum price parameter, bse_sys_maxprice which is set to 500 (i.e., $5.00) by default:
bse_sys_maxprice = 500
To configure Smith’s experiments, it is necessary to allow price values up to $9.99.
Therefore, you must begin by editing your local copy of BSE.py to set max price 999, i.e.:
bse_sys_maxprice = 999
A.1: Configure and run a homogeneous market containing only SHVR trading agents.
Provide a graph of demand and supply, and a chart showing transaction prices for one example run. Compare your result with Smith’s result,and using your knowledge of SHVR agent logic, explain why/how results differ.
Marking scheme:
• 5 marks are available for experimental configuration code. You will make re-use of this code throughout part A.
• 5 marks are available for code that generates output figure. You will make re-use of this code throughout part A.
• 5 marks are available for results, discussion, and interpretation.
[15 marks]
A.2: Using the same experimental configuration as part A.1, run a homogeneous market containing only ZIC trading agents.
Using the same figure formatas part A.1, show a graph of demand and supply, and a chart
of transaction prices for one example run. Compare your result with Smith’s result, and using your knowledge of ZIC agent logic, explain why/how results differ.
[5 marks]
A.3: Using the same experimental configuration as part A.1, run a homogeneous market containing only ZIP trading agents.
Using the same figure formatas part A.1, show a graph of demand and supply, and a chart of transaction prices for one example run. Compare your result with Smith’s result, and using your knowledge of ZIP agent logic, explain why/how results differ.
[5 marks]
A.4: Using the same experimental configuration as part A.1, run a “balanced” market containing half ZIC traders and half SHVR traders. Using the same figure formatas part A.1, show a graph of demand and supply, and a chart of transaction prices for one example run. Compare your result with Smith’s result and your previous results, explaining why/how results differ.
[5 marks]
A.5: Using the same SHVR-ZIC “balanced test” market configuration as part A.4, run multiple experimental trials N, and analyse results to compare the average profits generated by each trader type. Marks will be awarded for selecting an appropriate number of trials N, for performing appropriate statistical test(s), for quality visualisations, and for accurately interpreting results. You may present a maximum of four graphs/figures. Marks will be lost for each additional graph/figure plotted.
[10 marks]
Part B
NOTE: To do well in Part B you will need to edit BSE.py and execute relatively
large numbers of market-session simulations using your edited code, which
collectively can use up quite a lot of CPU time. Once you understand what to do, making the necessary edits to the code might not take you very long but to
generate enough data you may then need to set simulation experiments to run on your laptop/desktop PC for several hours or more of continuous processing (e.g. overnight), during which you can be working on other things, or sleeping.
Because of this, it is important to plan ahead, and to not leave until the last
minute before starting work on this part ofthe coursework. What is required to do well here is readily achievable on a current mid-range personal computer: you do not require access to high-performance supercomputers like the University’s
BlueCrystal/BluePebble facilities, but you should expect to have to wait quite a while for enough data to be generated.
BSE edits: If you make edits to your local version of BSE.py you will need to
include a copy of your edited BSE.py in the zip file that you submit. You must also clearly comment your local BSE.py to indicate where and how you have made edits (so that we can see the changes you have made) and you should also refer to these edits in your Jupyter Notebook (include a brief description of the
changes you have made and why, and state the lines of code and function name where edits have been made so that we can easily find your changes).
A recent paper by Cliff (2023) describes the long-term co-evolutionary dynamics of markets populated by ZIP traders that are continually trying to improve their
hyperparameter values, to trade more profitably, by using a simple stochastic
hillclimber (SH) method to search for better hyperparameter values, thereby giving a
variant of the ZIP adaptive trading strategy known as ZIPSH. As described in more depth in (Cliff, 2023) the SH mechanism in a single ZIPSH trader maintains a private local
population of kdifferent candidate strategies, tests the profitability of each of them in
sequence, and then identifies the most profitable of the k as the ‘elite” strategy; it then creates k-1 new “child” strategies, each child being a mutation of the elite strategy, and replaces the non-elite members of the old population with these new children; and then it evaluates the profitability of each member of the new population to identify its elite
individual, which is then mutated to create k-1 children; and so on, repeating this process forever.
This simple SH process is referred to as a hillclimber because it is often explained via
the metaphor of a walker in the outdoors trying to find the highest part of a landscape, while blindfolded: the walker has a walking stick, and she finds the highest nearby piece
of ground by using the stick to prodk different patches of ground around her, to
determine which is the highest (here “nearby” means within prodding distance of her walking stick held from an outstretched arm); and then she takes a step to the highest point prodded; and then she starts prodding again. The SH process is stochastic
because the mutation process introduces random changes rather than
systematic/deterministic ones: returning to our walker, if her k=4 then on each iteration she could systematically always prod nearby patches of ground to the compass north,
south, east, and west – that would be a fixed, deterministic pattern; or she could
instead pick four directions at random on each iteration – that would be a stochastic choice of local sample-points.
In ZIP there are five key parameters (the trader’s initial margin, its learning rate beta, the momentum parameter used to smooth over noise in the learning process, and the
trader’s two constants ca and cr which respectively determine absolute and relative
perturbations when forming the trader’s target price used in the ZIP learning process), so the candidate strategies in ZIPSH can each be thought of as five-dimensional (5D) vectors of real numbers, and the stochastic mutation process involves adding random values to each of the five: in ZIPSH, each random mutation value is drawn from a zero- mean Gaussian distribution with a small variance. It’s not essential to use a Gaussian distribution, but it is important that the distribution for mutations is zero -mean,
because any nonzero value of the mean would introduce a systematic bias; and it’s
important that the variance is small to preserve the notion of “nearby”, i.e. to keep the k- point search local at each step.
A process as simple as SH can often work pretty well, but it has some definite
drawbacks. It works well in situations where the fitness landscape (in our ZIPSH example, this is the profitability at each point in the 5D space of possible strategy vectors) is smooth or locally correlated – that is, where the difference in
fitness/profitability of any two points in the strategy space typically grows smaller as the distance between the two points under consideration reduces. Unfortunately, many
real-world fitness landscapes are not smooth, but may instead have sudden major step-changes up or down, or may have fractal-like “ruggedness”, and in practice very often the only way to know for sure what the fitness landscape is actually like is to
sample it at many points, which can be a very time-consuming process.
One well-known problem is that simple search processes like SH can easily become trapped on local maxima – when our blindfolded walker reaches the top of a small hill, every point she prods with her stick will be lower than her current position, and so from then onwards she is stuck, even if the slopes of a much higher hill lies only a few steps away. Similarly, an individual ZIPSH trader may evolve its private population of k
strategies to the point where every mutant it generates always has lower profitability
than the current elite strategy, and so the improvement in profitability stops, even if the current elite strategy is not particularly good. Another problem is that the outcome of a SH process can be very sensitive to initial conditions: whatever values are in the
population of candidate strategies at the start of the process can have a big effect on how the ZIPSH process unfolds over time and where it ends up, what the final elite
strategy is. To revisit the metaphor: for our hillwalker, which point of high ground she finally ends up on could depend a lot on where on the map she started walking from. And, finally, if there is significant noise in the fitness function (in ZIPSH, this is the
method of evaluation of the profitability of any one candidate strategy), this can have a major destabilising effect on the efficiency and final outcome of the SH search,
because as noise levels increase, so more and more of the SH “adaptive steps” will be mis-steps, taking the search in the wrong (or, at least, a suboptimal) direction.
Using the latest BSE version (released December 2023), we can illustrate and explore
ZIPSH’s adaptive process in action by setting up a population of traders in which there is only one ZIPSH trader – here we will make that ZIPSH trader a buyer, with all other
traders in the market being the non-adaptive ZIC strategy.
Because transaction-price time series in financial markets are almost always noisy (and especially because the ZIC traders we’re using here so highly stochastic), the process of evaluating the profitability of our sole ZIPSH trader’s candidate strategies will involve
monitoring the profitability of each individual strategy over an appropriately long period of time. In BSEv1.8 the function init_stratwaittime() within ZIP’s init ()
function chooses a random evaluation period of between two and three hours (i.e. 7200 to 10800 seconds). This evaluation period determines how many ‘adaptive steps’ (i.e., cycles through the loop of evaluating the current population of strategies, identifying the elite, and mutating the elite to make children that will form the next population) can be taken in any one 24-hour period. For example, if a ZIPSH trader evaluates each
candidate strategy for two hours, and uses a population size of k=4, then evaluating the whole population takes eight hours and so in any one day of continuous round-the-
clock trading that trader can take no more than three adaptive steps. Furthermore,
because we don’t know in advance how many adaptive steps the trader will need to
demonstrate the adaptation is working well, we should in the first instance run our
experiment for a fairly large number of days, some number of weeks of continuous 24x7 trading: the graphs below show the results from BSEv1.8 market sessions lasting 30
days of continuous trading.
Figure 1 shows the profitability (measured as profit-per-second, or PPS) of the sole
ZIPSH buyer in the otherwise all-ZIC market, with 10 buyers and 10 sellers, from a single 30-day market session. Here the demand curve ran from 150 down to 125, and the
supply curve ran from 50 up to 75, which means that every trader should be expected to find a counterparty for a transaction on every order assigned to it. To highlight the effect of the progressive adaptation of hyperparameter values, in all the illustrative
experiments discussed here the initial values of the ZIP hyperparameters margin_buy (the buyer’s initial margin), beta (its learning rate), and momntm (its momentum) were all deliberately set to very low values, values so low that they result in reduced
profitability: that is, these illustrative experiments have been deliberately set up to make
it easy for the ZIPSH to improve over time: by increasing these initially low hyperparameter values, profitability is likely to increase.
It's important to remember that the situation our single ZIPSH buyer faces remains highly challenging, and is unlike any real-world market scenario: for our ZIPSH, every other trader in the market is a ZIC, and ZIC quote-prices are just (appropriately bounded) random noise values which change very frequently.
Figure 1: profitability of a sole ZIPSH buyer in a market populated by ZIC traders.
Horizontal axis is time, labelled in days; vertical axis is the profit-per-second (PPS)
generated by the ZIPSH trader.
Nevertheless, as can be seen in Figure 1, there is indeed a clear improvement in
profitability, which rises from a PPS of roughly 0.25 at Day 0 to a PPS near 1.0 at Day 30, an increase of roughly 300% in this experiment. So, the SH mechanism has to a decent extent tailored the single ZIPSH trader’s strategy hyperparameters to make it better
suited to trading in the market full of ZIC traders, with these specific supply and demand curves. This shows that, in principle, SH can perform fairly well for such a primitively simple adaptation mechanism.
Figure 2: Time-series showing evolution of the five key ZIP hyperparameters in the ZIPSH
experiment illustrated in Figure 1: in each graph the horizontal axis is time, labelled in
days, and the vertical axis is the named parameter-value.
Figure 2 shows the changes in the ZIPSH trader’s hyperparameter values over the
duration of the same experiment as was illustrated in Figure 1. As you can see, each of the hyperparameters is gradually altered to add to the improvement and maintenance of higher profitability.
But this is an inherently stochastic system: run it again and the plots of the time -
evolution of the trader’s profitability and its hyperparameter values will differ from the results seen above, and so then a key question is: how can we characterise the expected or average behaviour of the system? Put another way, how representative of the usual behaviour of the system is the single set of results chosen to be illustrated in Figures 1 and 2?
Figure 3: Profitability results from a further nine IID repetitions of the ZIPSH experiment
illustrated in Figure 1: graph axes and labels are as for Figure 1; see text for further
discussion.
To illustrate the variability, Figure 3 shows the profitability time-series from a further
nine independent and identically distributed (IID) runs of the market session that
generated Figures 1 and 2: as you can see, there is a fair amount of variation between
different IID repetitions of the same single-ZIPSH session. In each of these experiments, because key hyperparameters were initialised to very low values, initial profitability is
often in the range 0.25 to 1.5, and in half the runs shown here there is then a slow,
steady, low-variance increase in profitability scores by 50%-100% over the course of the
experiment (e.g., from PPS=1.0 at Day 0 to PPS=1.5 at Day 30; or from PPS=0.5 at Day 0 to PPS=1.0 at Day 30). In those runs that do not show a consistent low-variance
improvement in profitability, markedly higher profit scores of 1.5 to almost 3.0 can be achieved for sustained periods of time, but with manifestly higher variance and often
without enduring stability. These periods of higher scores often result from ZIPSH
finding ‘extreme’ solutions, such as switching off learning (setting beta=0) and instead relying on a very high margin_buy value, which can give an increased probability of
more profitable trades but which leaves the trader no way of adjusting to a lower margin if the initial margin_buy value turns out to be too high -- in which case the trader will
struggle to make any profit at all and then the adaptive SH process is likely to switch to strategies with nonzero beta and reduced margin_buy, thereby reverting to the lower- variance (but lower-profit) adaptive class of strategy.
These illustrative results from ZIC markets with a single ZIPSH buyer do show that the
stochastic hillclimber can indeed adapt the ZIP hyperparameters to increase
profitability; but remember this is a contrived example with the initial values of the five ZIP hyperparameter set deliberately low, and we’ve only shown the results from 10 runs here, which is a small sample-size: for statistically rigorous analysis, we would need to generate and analyse considerably more than 10 runs.
B.1: For this part of the coursework assessment, edit your local copy of the BSE.py code to give your own replication of the single-ZIPSH experiments described above, and then execute sufficiently many IID runs to be able to rigorously characterise outcomes of this system via appropriate visualizations and analyses of results, using an appropriate
statistical significance test to determine whether, in your results, there is conclusive evidence that in these all-ZIC-plus-single-ZIPSH sessions, ZIPSH does show a reliable improvement in profitability. In doing this, you may choose to run your market sessions for less than 30 days, so long as you justify that choice – shorter-duration sessions will take less time to simulate. Similarly, you may choose to alter the
init_stratwaittime()function to give each strategy less evaluation time, which will increase the number of adaptive steps per day but will increase the noise level in the profitability scores, and again if you do that then you should explain and justify your
choice. You are free to make further edits to BSE.py if you wish, so long as they also are explained and justified.
[25 marks]
B.2: Next, introduce your own variations or extensions to explore the extent to which the
single-ZIP-vs-multiple-ZIC results are affected by changes in the design of the experiment, and/or changes in the ZIP hyperparameter adaptation technique. For example, you might want to explore the effects of changes to the shapes of the supply and demand curves, and/or changes in the number of traders in the markets, and/or changes in the type of evolutionary hyperparameter optimization that the ZIP traders use (i.e., something other than SH). For any such change, you will need to select and apply appropriate visualizations, and you will need to select and apply appropriate statistical tests to see whether the change you introduced does lead to a statistically significant alteration/improvement in profitability.
[35 marks]