-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathstats.ml
More file actions
287 lines (253 loc) · 10.7 KB
/
stats.ml
File metadata and controls
287 lines (253 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
open Pervasives
open String
open Trader
open Marketmaker
(*
* STATISTICS ENGINE
* - Gaussian Statistics
* - Three point Linear Square Regression Model
* - Newton-Raphson Method for Curve approximation (Secant estimate)
* - Chebyschev Variance Probability Calculator
* - Markov Chain analysis using three features
* - Graph Analysis
*
* DATA ANALYSIS
* - Mean spread, bids, asks, trade count
* - Max spread, bids, asks
*
* In implementaiton 1, the cheat command currently compares previous
* bid/asks and uses a linear regression model to suggest the next move,
* referencing the actual value.
*
* In implementation II, the cheat command factors in a least-squares
* regression model to predict your next move. It compares this to
* the actual value of the dice roll. Based off of your previous ask/bids
* we apply a Gaussian blur matrix to this to find another average value.
* We then use Chebyschev's inequality and the variance of your previous
* moves to appropriately offset this second average from the expected
* rolls. Finally, we incorporate the first average, subtract the two
* and present that as a good guess for the next move.
* This is essentially the procedure a market maker would use to price
* the next bid/ask spread.
*)
(*
* DICE ROLLING SIMULATION
* - roll dice
* - calculate statistics
*)
type dice_data = {
player_roll : int;
other_rolls : int list;
sum_rolls : int;
}
let rec roll_list (num:int) (acc: int list) : (int list) =
if num = 0
then acc
else
roll_list (num - 1) ((1 + Random.int 5)::acc)
(**[get_player_roll d] gives the number the player rolled *)
let get_player_roll (d:dice_data) : int = d.player_roll
(**[mega_roll num_opp] gives dice data based on how many traders the player
is going against *)
let mega_roll (num_opp:int) : dice_data =
let playroll = (Random.int 5) + 1 in
let other_list = roll_list num_opp [] in
{
player_roll = playroll;
other_rolls = other_list;
sum_rolls = (playroll + (List.fold_left (fun acc h -> acc + h) 0 other_list)
);
}
(* END DICE *)
type graph_data = {bid_data : int list; ask_data : int list;
trade_data : string list; time_data : int list;
hidden_number : int}
(**[to_float_list_acc acc] is a list of floats from a string list. *)
let rec to_float_list_acc acc = function
|[] -> acc
|h::t -> to_float_list_acc ((float_of_string h):: acc) t
(**[to_float_list lst] is a list of floats converted from the string list
[lst] *)
let to_float_list lst =
to_float_list_acc [] lst |> List.rev
(**[sum lst] is the sum of the floats in [lst] *)
let sum lst = (List.fold_left (fun acc h -> acc +. h) 0.0 lst)
(**[get_mean] is the mean of the values in lst. *)
let get_mean = function
|[] -> failwith "empty list for getting mean"
|lst -> sum lst /. float (List.length lst)
(**[sum_squared lst] is the sum of squares of the values in [lst]. *)
let sum_squared lst =
sum (List.map (fun x -> x*.x) lst)
(**[get_variance lst] is the variance of the values in [lst]. *)
let get_variance lst =
(* sum of the squared values divided by length subtracted by mean squared *)
let mean = get_mean lst in
(sum_squared lst)/. float (List.length lst) -. (mean *. mean)
(**[last_three_val] is a list of the last three values in the lst. *)
let rec last_three_val = function
|[] -> failwith "empty list or less thasn 3 values"
|x::y::z::[] -> [float x; float y; float z]
|h::t -> last_three_val t
(**[last_three_lsr lst] is the prediction of the next values based on the least
squares of the last three values in [lst].*)
let last_three_lsr lst =
let three = last_three_val lst in
let x = sum three in
let x2 = sum_squared three in
let length = float (List.length lst) in
let last_ys = [length-. 2.0; length -.1.0; length] in
(* make a int list to float list function *)
let y = sum last_ys in
let xy = List.combine three last_ys |>
List.fold_left (fun acc (x,y) -> acc +. (x*.y)) 0.0 in
let m = ((length *. xy) -. (x *.y))/.((length *. x2) -. (x *. x)) in
let b = (y -. (m *.x))/.length in
((length +. 1.0) -. b) /. m
(** [newton_raphson_secant f start] approximates the root of a function [f]
starting at seed value [start].
Returns None if not converged or Some x where x is the converged value.
This approximates the derivative
of the function as a secant line rather than the traditional tangent
method *)
let newton_raphson_secant f start =
let dfdx fu =
fun x -> (fu (x +. 0.1) -. fu x) /. 0.1 in
let rec iter xk number =
let update = start -. (f xk /. (dfdx f) xk) in
if number > 100 then None else
if (abs_float (update -. xk) < 0.01) then Some xk else iter xk (number + 1)
in iter start 0
(**[get_max lst acc] is the max value in the list [lst]. *)
let rec get_max lst acc =
match lst with
| [] -> acc
| h::t -> if h > (List.hd acc) then get_max t [h] else get_max t acc
(**[get_data true_val bidask_lst bids asks trades times] is a record
containing the history of the market's bids, asks, trade types and the true
value of
the security. It takes in the bidask history of the market in [bidask_lst]
and the [true_val] of the security. *)
let rec get_data true_val bidask_lst bids asks trades times =
match bidask_lst with
| [] -> {bid_data = bids; ask_data = asks; trade_data = trades;
time_data = times; hidden_number = true_val}
| h::t -> get_data true_val t (h.bid::bids) (h.ask::asks)
(h.trade_type::trades) times
(**[list_of_ints strt nd] is a list of ints in ascending order from [strt] to
[nd]. *)
let rec list_of_ints strt nd lst =
if strt <= nd then list_of_ints (strt+1) nd (strt::lst)
else List.rev lst
(**[get_graph market trader] is a record of the bids and asks put by the player,
the types of trades made and the true value of the security. It takes in
a [market] type t and a [trader] type t*)
let get_graph (market : Marketmaker.t) (trader : Trader.t) =
let true_val = trader.hidden_number in
let bidask_lst = market.bid_ask_history in
let times = list_of_ints 0 (List.length bidask_lst) [] in
let bid_lst = [] in
let ask_lst = [] in
let trade = [] in
get_data true_val bidask_lst bid_lst ask_lst trade times
(**[bid_acc lst acc] is the list of bids in bidask list [lst]. *)
let rec bid_acc (lst : Marketmaker.bidask list) acc =
match lst with
| [] -> List.rev acc
| h::t -> bid_acc t (h.bid::acc)
(**[ask_acc lst acc] is the list of asks in bidask list [lst]. *)
let rec ask_acc (lst : Marketmaker.bidask list) acc =
match lst with
| [] -> List.rev acc
| h::t -> ask_acc t (h.ask::acc)
(**[text_capture market] prints out "Trace" when called *)
let text_capture (market : Marketmaker.t) =
print_endline "Trace";
()
(**[chebyshev_like_var var] Uses the normal variance of a float list, and
calculates chebyshevs variance *)
let chebyshev_like_var var =
match var with
|0. -> 0.
|_ -> 1. -. (1. -. (1./.(sqrt var)))
(**[matr_helper row vector return] is the sum of the element-wise
multiplications of [row] and [vector]. *)
let rec matr_helper row vector return =
match row with
| [] -> return
| h::t -> matr_helper t (List.tl vector) h*(List.hd vector)+return
(**[matr_mul mtr vector return] is the product of the matrices [mtr] which
should be 3x3 and [vector] which should be 3x1. *)
let rec matr_mul (mtr: int list list) (vector: int list) (return: int list) =
if List.length vector <> 3 then failwith "vector needs to be 3x1"
else
match mtr with
| [] -> return
| h::t -> if List.length h <> 3
then failwith "mtr needs to be 3x3"
else matr_mul t vector ((matr_helper h vector 0)::return)
(**[linear_reg_cheat market] is the linear regression of the bids or asks in
the market based on what the market has seen more of. *)
let linear_reg_cheat (market : Marketmaker.t ) (dice:dice_data) =
let bid_list = (bid_acc (market.bid_ask_history) []) in
let ask_list = (ask_acc (market.bid_ask_history) []) in
if List.length bid_list > List.length ask_list then
(* Linear regression for asks *)
if List.length ask_list < 3 then
-1.0 else
let last_three = ((List.nth ask_list
(List.length ask_list - 3))
::(List.nth ask_list
(List.length ask_list - 2))
::(List.nth ask_list
(List.length ask_list - 1))
::[]) in
let raw_lsr =
abs_float (
(last_three_lsr
last_three ))
in
(* Currently using a gaussian blur kernel on the last three values *)
let ans = matr_mul ((1::2::1::[])::(2::4::2::[])::(1::2::1::[])::[]) last_three [] in
abs_float ((float_of_int dice.sum_rolls) -. 0.3*.raw_lsr /. (
if (chebyshev_like_var( float_of_int ( List.nth ans 0) )) < 1.0
then 1.0/.((chebyshev_like_var ( float_of_int ( List.nth ans 0) )))
else (chebyshev_like_var ( float_of_int ( List.nth ans 0) )) ))
else
(* Linear regression for bids*)
(* Linear regression for asks *)
if List.length bid_list < 3 then
-1.0 else
let last_three = ((List.nth bid_list
(List.length bid_list - 3))
::(List.nth bid_list
(List.length bid_list - 2))
::(List.nth bid_list
(List.length bid_list - 1))
::[]) in
let raw_lsr =
abs_float (
(last_three_lsr
last_three ))
in
(* Currently using a gaussian blur kernel on the last three values *)
let ans = matr_mul ((1::2::1::[])::(2::4::2::[])::(1::2::1::[])::[]) last_three last_three in
abs_float ((float_of_int dice.sum_rolls) -. 0.3*.raw_lsr /. (
if (chebyshev_like_var( float_of_int ( List.nth ans 0) )) < 1.0
then 1.0/.((chebyshev_like_var ( float_of_int ( List.nth ans 0) )))
else (chebyshev_like_var ( float_of_int ( List.nth ans 0) )) ))
(**[count lst str acc] is the frequency of occurrence of [str] in [lst]. *)
let rec count lst str acc =
match lst with
| [] -> acc
| h::t -> if h = str then count t str acc+1
else count t str acc
(**[trade_freq market trader] is the frequency of each trade type in the market.
It prints the output to the screen. *)
let trade_freq market trader =
let info = get_graph market trader in
let trades = info.trade_data in
let hits = count trades "hit" 0 in
let lifts = count trades "lift" 0 in
let prnt = ["hits = "^(string_of_int hits); "lifts = "^(string_of_int lifts)]
in List.iter print_string prnt