-
Notifications
You must be signed in to change notification settings - Fork 9
/
behav.R
166 lines (141 loc) · 4.57 KB
/
behav.R
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
Behav <- function (
g,
ct #current time period
)
{
# package requirement
require(igraph)
######
## Useful variables and traders' variable initializations
######
n.secu <- length((V(g)$secu)[[1]])
n.traders <- length(V(g))
#n.approx <- g$n.approx
#######
## Traders pick at random a security that they will try to BUY once on the market
#######
V(g)$buy.which <- sample(n.secu, n.traders, replace = TRUE)
#######
## Traders set a PRICE to place a BUY order (must be lower OR equal the traders's money stock)
#######
for (i in 1:n.traders){
if (V(g)$approx[i] == 1){
V(g)$buy.price[i] <-min(
g$reserv.tsi[ct,V(g)$buy.which[i]]*(1-V(g)$risk.tak[i]),
V(g)$money[i]
)
}
if (V(g)$approx[i] == 2){
V(g)$buy.price[i] <-min(
g$reserv.co2[ct,V(g)$buy.which[i]]*(1-V(g)$risk.tak[i]),
V(g)$money[i]
)
}
}
#######
## Traders pick at random a security they have a positive amount of to place a SELL offer
#######
for (i in 1:n.traders){
# if trader owns no securities
if(all(V(g)$secu[[i]]==0)) V(g)$sell.which[i] <- 0
# if trader owns at least one security, here we intend to sample an INDEX, not the value at that index
if (any(V(g)$secu[[i]]>0)) V(g)$sell.which[i] <- sample(which(V(g)$secu[[i]] >=1),1)
# Safeguard
if(V(g)$secu[[i]][V(g)$sell.which[i]]<1) stop("a seller is trying to sell a security she does not own any unit of")
}
#######
## Traders set a PRICE to place a SELL order
#######
for (i in 1:n.traders){
if (V(g)$approx[i] == 1){
V(g)$sell.price[i] <-g$reserv.tsi[ct, V(g)$sell.which[i]]*(1+V(g)$risk.tak[i])
}
if (V(g)$approx[i] == 2){
V(g)$sell.price[i] <-g$reserv.co2[ct, V(g)$sell.which[i]]*(1+V(g)$risk.tak[i])
}
}
#######
## Return the network
#######
g
}
####################################
####################################
####################################
####################################
####################################
####################################
BehavPerfect <- function (
g,
ct #current time period
)
{
# package requirement
require(igraph)
######
## Useful variables and traders' variable initializations
######
n.secu <- length((V(g)$secu)[[1]])
n.traders <- length(V(g))
#n.approx <- g$n.approx
#######
## Traders pick at random a security that they will try to BUY once on the market
#######
V(g)$buy.which <- sample(n.secu, n.traders, replace = TRUE)
#######
## Traders set a PRICE to place a BUY order (must be lower than traders's money stock)
#######
for (i in 1:n.traders){
if(V(g)$approx[i] == g$true.model){
V(g)$buy.price[i] <-min(
g$reserv.best[ct,V(g)$buy.which[i]]*(1-V(g)$risk.tak[i]),
V(g)$money[i]
)
}
else {
if (V(g)$approx[i] == 1){
V(g)$buy.price[i] <-min(
g$reserv.tsi[ct,V(g)$buy.which[i]]*(1-V(g)$risk.tak[i]),
V(g)$money[i]
)
}
if (V(g)$approx[i] == 2){
V(g)$buy.price[i] <-min(
g$reserv.co2[ct,V(g)$buy.which[i]]*(1-V(g)$risk.tak[i]),
V(g)$money[i]
)
}
}
}
#######
## Traders pick at random a security they have a positive amount of to place a SELL offer
#######
for (i in 1:n.traders){
# if trader owns no securities
if(all(V(g)$secu[[i]]==0)) V(g)$sell.which[i] <- 0
# if trader owns at least one security, here we intend to sample an INDEX, not the value at that index
if (any(V(g)$secu[[i]]>0)) V(g)$sell.which[i] <- sample(which(V(g)$secu[[i]] >=1),1)
# Safeguard
if(V(g)$secu[[i]][V(g)$sell.which[i]]<1) stop("a seller is trying to sell a security she does not own any unit of")
}
#######
## Traders set a PRICE to place a SELL order
#######
for (i in 1:n.traders){
if (V(g)$approx[i] == g$true.model){
V(g)$sell.price[i] <-g$reserv.best[ct,V(g)$sell.which[i]]*(1+V(g)$risk.tak[i])
}
else {
if (V(g)$approx[i] == 1){
V(g)$sell.price[i] <-g$reserv.tsi[ct,V(g)$sell.which[i]]*(1+V(g)$risk.tak[i])
}
if (V(g)$approx[i] == 2){
V(g)$sell.price[i] <-g$reserv.co2[ct,V(g)$sell.which[i]]*(1+V(g)$risk.tak[i])
}
}
}
#######
## Return the network
#######
g
}