我写了以下R函数:
#initialprob <- c(0.4,0.5,0.1)
f1 <- function(n,m,priceinitial,delta,mean, sd, ninterval){
initialprob <- c(1/3, 1/3, 1/3)
traders <- vector(mode="character", length=n)
traderscurrent <- vector(mode="character", length=n)
price <- vector(mode="numeric")
pricecurrent <- vector(mode="numeric")
for(nint in 1:ninterval)
{
print(initialprob)
L = floor(rnorm(1,mean,sd))
print(L)
for(i in 1:n)
{
traders[i] = sample(c("B", "S", "N"), size=1, prob=initialprob)
}
print(table(traders))
for(step in 1:L)
{
for(i in 1:n)
{
b <- sample(traders[-i], m)
#print(b)
#table(b)
traderscurrent[i] <- sample(b,1)
}
print(table(traderscurrent))
buy = length(which(traderscurrent == "B"))
sell = length(which(traderscurrent == "S"))
neutral = length(which(traderscurrent == "N"))
total = buy+neutral+sell
buyprop = buy/total
sellprop = sell/total
neutralprop = neutral/total
pricecurrent[step] = priceinitial+buy*delta-sell*delta
priceinitial = pricecurrent[step]
traders <- traderscurrent
#print(nint)
#print
initialprob <- c(sellprop,buyprop,neutralprop)
print(initialprob)
}
a <- runif(1,0,1)
b <- runif(1,0,1)
c <- runif(1,0,1)
total = a+b+c
initialprob <- c(a/total, b/total, c/total)
print(initialprob)
price <- append(price,pricecurrent, after=length(price))
#price <- price[-step]
plot(price)
}
print(price)
#plot(price)
}
当我致电 f1(1000,100,100,10,10,1,100)
时,我会看到如下图:
我该如何解决这个问题?似乎用以前的价格追加价格存在问题。也许上一次迭代的最后价格被加两次?
答案 0 :(得分:1)
问题似乎是每次迭代后都没有重置pricecurrent向量。尝试在第一个...
<script type="text/javascript">
//Complete list of leagues and their teams:
var leagues = {
@foreach(var l in ViewBag.List)
{
@: '@l.Key': [
foreach(var team in ViewBag.List[l.Key])
{
@: '@team',
}
@: ],
}
};
...
循环声明后添加pricecurrent <- vector(mode="numeric")
:
for