I have to find 6 errors in the following code and I think I found three already, which are in p and q sample and the brakes in Y.
I changed them to:
p = sample(1:6, 1)
q = sample(1:6, 1)
Y = y[(n + 1)(2*n)]
and got the following error message afterwards:
Error in err[(t - 1):(p - q)] :
only 0's may be mixed with negative subscripts
Can you help me fix this and find the other errors?
Also I have to delete one unnecessary variable from rm (at the last line)
myMatrikel = 7081255
{
n = 500
set.seed(myMatrikel)
p = sample(1:6, n = 1)
q = sample(1:6, n = 1)
alpha = round(runif(q, min = 0, max = 1/q), digits = 3)
beta = round(runif(p, min = 0, max = 1/p), digits = 3)
initY = rnorm(max(p, q))
initErr = rnorm(max(p, q))
y = err = 1:(2*n)*0
y[1:max(p, q)] = initY
err[1:max(p, q)] = initErr
for (t in (max(p, q) + 1):length(y))
{
err[t] = rnorm(1)
y[t] = beta %*% y[(t - 1):(t - p)] + alpha %*% err[(t - 1):(p - q)] + Err[t]
}
Y = y[(n + 1)(2*n])
}
rm(list = c("err", "initErr", "initY", "y", "t", "myMatrikel"))
Here's an idea, how to narrow it down:
for (t in (max(p, q) + 1):length(y)) {
if (t %% 50) cat(t, "\n")
else cat(t)
err[t] = rnorm(1)
y[t] = beta %*% y[(t - 1):(t - p)] + alpha %*% err[(t - 1):(p - q)] + Err[t]
}
This way you can see at which iteration the error occurs and look at the specific values of that iteration. Then might easier able to see, what is causing the error.
Related
I am trying to integrate the following function using a Monte Carlo Integration. The interval I want to integrate is x <- seq(0, 1, by = 0.01) and y <- seq(0, 1, by = 0.01).
my.f <- function(x, y){
result = x^2 + sin(x) + exp(cos(y))
return(result)
}
I calculated the integral using the cubature package.
library(cubature)
library(plotly)
# Rewriting the function, so it can be integrated
cub.function <- function(x){
result = x[1]^2 + sin(x[1]) + exp(cos(x[2]))
return(result)
}
cub.integral <- adaptIntegrate(f = cub.function, lowerLimit = c(0,0), upperLimit = c(1,1))
The result is 3.134606. But when I use my Monte Carlo Integration Code, see below, my result is about 1.396652. My code is wrong by more than a factor of 2!
What I did:
Since I need a volume to conduct a Monte Carlo Integration, I calculated the function values on the mentioned interval. This will give me an estimation of the maximum and minimum of the function.
# My data range
x <- seq(0, 1, by = 0.01)
y <- seq(0, 1, by = 0.01)
# The matrix, where I save the results
my.f.values <- matrix(0, nrow = length(x), ncol = length(y))
# Calculation of the function values
for(i in 1:length(x)){
for(j in 1:length(y)){
my.f.values[i,j] <- my.f(x = x[i], y = y[j])
}
}
# The maximum and minimum of the function values
max(my.f.values)
min(my.f.values)
# Plotting the surface, but this is not necessary
plot_ly(y = x, x = y, z = my.f.values) %>% add_surface()
So, the volume that we need is simply the maximum of the function values, since 1 * 1 * 4.559753 is simply 4.559753.
# Now, the Monte Carlo Integration
# I found the code online and modified it a bit.
monte = function(x){
tests = rep(0,x)
hits = 0
for(i in 1:x){
y = c(runif(2, min = 0, max = 1), # y[1] is y; y[2] is y
runif(1, min = 0, max = max(my.f.values))) # y[3] is z
if(y[3] < y[1]**2+sin(y[1])*exp(cos(y[2]))){
hits = hits + 1
}
prop = hits / i
est = prop * max(my.f.values)
tests[i] = est
}
return(tests)
}
size = 10000
res = monte(size)
plot(res, type = "l")
lines(x = 1:size, y = rep(cub.integral$integral, size), col = "red")
So, the result is completely wrong. But if I change the function a bit, suddenly is works.
monte = function(x){
tests = rep(0,x)
hits = 0
for(i in 1:x){
x = runif(1)
y = runif(1)
z = runif(1, min = 0, max = max(my.f.values))
if(z < my.f(x = x, y = y)){
hits = hits + 1
}
prop = hits / i
est = prop * max(my.f.values)
tests[i] = est
}
return(tests)
}
size = 10000
res = monte(size)
plot(res, type = "l")
lines(x = 1:size, y = rep(cub.integral$integral, size), col = "red")
Can somebody explain why the result suddenly changes? To me, both functions seem to do the exact same thing.
In your (first) code for monte, this line is in error:
y[3] < y[1]**2+sin(y[1])*exp(cos(y[2]))
Given your definition of my.f, it should surely be
y[3] < y[1]**2 + sin(y[1]) + exp(cos(y[2]))
Or..., given that you shouldn't be repeating yourself unnecessarily:
y[3] < my.f(y[1], y[2])
I'm trying to apply KF on a simple model to estimate a signal's bias error. But the method does not seem to work. the bias error is estimated as zero and does not change. Any clue on what I'm missing and/or if KF is suited for this problem?
Here's the model presentation:
x_(k+1) = x_k + T*V_k --> Position
V_(k+1) = V_k + T*(a_k - b_k) --> Velocity
a_(k+1) = a_k --> Acceleration
b_(k+1) = b_k --> Bias
Here is my code:
# Get state space matrices
T = 0.005;
numberOfStatesVariables = 4;
A = diag(numberOfStatesVariables);
A[1,2] = T;
A[2,3] = T;
A[2,4] = -T;
C = matrix(0, 1, numberOfStatesVariables);
C[1, 3] = 1;
Q = 1*diag(numberOfStatesVariables);
R = diag(1);
P = diag(numberOfStatesVariables);
# Specify observations
numberOfSamples = 5000;
t = 1:numberOfSamples / 2 / numberOfSamples;
w1 = sample(0:10, numberOfSamples, replace = T) / 10 - 0.5 + sin(t/pi*180);
yt = rbind(w1+0.5); # 0.5 is the bias error.
estimatedT = matrix(0, numberOfStatesVariables, numberOfSamples);
for (i in 2:numberOfSamples)
{
estimatedT[, i] = A %*% estimatedT[, i-1];
P = A %*% P %*% t(A) + Q;
S = C %*% P %*% t(C) + R;
K = P %*% t(C) %*% solve(S);
estimatedT[, i] = estimatedT[, i] + K %*% (yt[,i] - C %*% estimatedT[, i]);
P = (diag(numberOfStatesVariables) - K %*% C) %*% P;
}
plot.ts(estimatedT[4,])
I am trying to program the basic Vanilla TrueSkill (3.1) algorithm in R but am getting some strange results.
My code is the following:
# A simple test between two players repeatedly laying one another
betaSq = 0.1
obs = 10000
p1_skills = 0.333
p2_skills = 0
p1_draws = rnorm(obs, p1_skills, sqrt(betaSq))
p2_draws = rnorm(obs, p2_skills, sqrt(betaSq))
p1_pred_mu = rep(NA, obs+1)
p1_pred_sigmaSq = rep(NA, obs+1)
p2_pred_mu = rep(NA, obs+1)
p2_pred_sigmaSq = rep(NA, obs+1)
# Initial values
p1_pred_mu[1] = 0
p1_pred_sigmaSq[1] = 1
p2_pred_mu[1] = 0
p2_pred_sigmaSq[1] = 1
results = p1_draws > p2_draws
probs = rep(NA, obs)
# Run TrueSkill
for (i in seq(2,obs+1)) {
probs[i-1] = predictProb(p1_pred_mu[i-1], p1_pred_sigmaSq[i-1], p2_pred_mu[i-1], p2_pred_sigmaSq[i-1], betaSq)
out = updateSkill(p1_pred_mu[i-1], p1_pred_sigmaSq[i-1], p2_pred_mu[i-1], p2_pred_sigmaSq[i-1], betaSq, results[i-1])
# Now update based on the out
p1_pred_mu[i] = out$mu1
p1_pred_sigmaSq[i] = out$sigmaSq1
p2_pred_mu[i] = out$mu2
p2_pred_sigmaSq[i] = out$sigmaSq2
}
# Output results
dev.new()
mu = p1_pred_mu
lower = qnorm(0.05, p1_pred_mu, p1_pred_sigmaSq)
upper = qnorm(0.95, p1_pred_mu, p1_pred_sigmaSq)
plot(mu, ylim = c(min(lower), max(upper)), main = "p1")
lines(lower)
lines(upper)
dev.new()
mu = p2_pred_mu
lower = qnorm(0.05, p2_pred_mu, p2_pred_sigmaSq)
upper = qnorm(0.95, p2_pred_mu, p2_pred_sigmaSq)
plot(mu, ylim = c(min(lower), max(upper)), main = "p2")
lines(lower)
lines(upper)
a = filter(probs, rep(1, 20))/20
dev.new()
plot(a)
print(sprintf("Mean p1: %g", mean(p1_pred_mu)))
print(sprintf("Mean p2: %g", mean(p2_pred_mu)))
print(sprintf("Mean results: %g", mean(results)))
print(sprintf("Mean predicted results: %g", mean(probs)))
The functions that are called are:
# Functions
updateSkill <- function(mu1, sigmaSq1, mu2, sigmaSq2, betaSq, result) {
# http://papers.nips.cc/paper/3331-trueskill-through-time-revisiting-the-history-of-chess.pdf
c = 2*betaSq + sigmaSq1 + sigmaSq2
if (result == 1) {
# Player 1 wins
v = dnorm((mu1-mu2)/c)/pnorm((mu1-mu2)/c)
w = v*(v+(mu1-mu2)/c)
mu1 = mu1 + (sigmaSq1/c)*v
mu2 = mu2 - (sigmaSq2/c)*v
sigmaSq1 = sigmaSq1 * sqrt(1 - (sigmaSq1/c^2)*w)
sigmaSq2 = sigmaSq2 * sqrt(1 - (sigmaSq2/c^2)*w)
} else if (result == 0) {
# Player 2 wins
v = dnorm((mu2-mu1)/c)/pnorm((mu2-mu1)/c)
w = v*(v+(mu2-mu1)/c)
mu1 = mu1 - (sigmaSq1/c)*v
mu2 = mu2 + (sigmaSq2/c)*v
sigmaSq1 = sigmaSq1 * sqrt(1 - (sigmaSq1/c^2)*w)
sigmaSq2 = sigmaSq2 * sqrt(1 - (sigmaSq2/c^2)*w)
}
return(list(mu1=mu1, mu2=mu2, sigmaSq1=sigmaSq1, sigmaSq2=sigmaSq2))
}
predictProb <- function(mu1, sigmaSq1, mu2, sigmaSq2, betaSq) {
# Try to predict the probability of player 1 beating player 2 using Trueskill model
mean1 = mu1
mean2 = mu2
var1 = sigmaSq1 + betaSq
var2 = sigmaSq2 + betaSq
# Now the dist of player1 - player2 is N(mean1 - mean2, sqrt(var1 + var2))
prob1Wins = pnorm(0, mean2 - mean1, sqrt(var1 + var2))
return(prob1Wins)
}
I hate to post the massive code blob but I really cannot figure out where things are going wrong.
This program runs and the predicted skills (distributed to a N(mu, sigma)) converge. However the predicted probabilities they are giving are not converging to the true probabilities for the results!
A sample output is:
[1] "Mean p1: 0.0762161"
[1] "Mean p2: -0.0762161"
[1] "Mean results: 0.7733"
[1] "Mean predicted results: 0.631424"
Any idea what is going wrong?
The reason this didn't work is because in the 3rd line of the updateSkills function it should read
c = sqrt(2*betaSq + sigmaSq1 + sigmaSq2)
not
c = 2*betaSq + sigmaSq1 + sigmaSq2
These days, I have a problem with R and RStudio. The R script cannot run in RStudio, it often is interrupted. But it can successfully run in the original console. Here is the script:
library(MASS)
simulation <- function(index) {
n = 800 #100 200 400 800 1600 3200
p = 0.5 * n #0.5*n 0.8*n 1.2*n
delta = 0.5 #0.5
k0 = 1
y = array(0, dim = c(p, n))
# simulation1 r=3
# ---------------------------------------------------------
r = 4
A = array(runif(p * r, min = -1, max = 1), dim = c(p, r))
x = array(0, dim = c(r, n)) #x = array(0, dim = c(p, n))
epsilon = array(t(mvrnorm(n, mu = rep(0, p), Sigma = diag(1, p, p))),
dim = c(p, n))
e = array(t(mvrnorm(n, mu = rep(0, r), Sigma = diag(1, r, r))), dim = c(r,
n))
Pi = diag(c(0.6, -0.5, 0.3, 0.6), r, r)
for (i in 2:n) {
x[, i] = Pi %*% x[, i - 1, drop = FALSE] + e[, i]
}
A[, 2] = A[, 2]/p^(delta/2) # weak factor
y1 = A %*% x + epsilon
y = t(scale(t(y1), center = T, scale = FALSE))
Mhat = array(0, dim = c(p, p))
for (i in 1:k0) {
covk = y[, (i + 1):n, drop = FALSE] %*% t(y[, 1:(n - i), drop = FALSE])/(n -
i)
Mhat = Mhat + covk %*% t(covk)
}
R = p/2
ratio = array(0, dim = c(R, 1))
temp = eigen(Mhat)
value = temp$values
vector = temp$vectors
for (i in 1:R) {
ratio[i] = value[i + 1]/value[i]
}
plot(ratio, type = "l")
rhat1 = which.min(ratio)
Ahat1 = vector[, 1:rhat1, drop = FALSE]
# two step
ystar = y1 - Ahat1 %*% t(Ahat1) %*% y1
ystar = t(scale(t(ystar), center = T, scale = FALSE))
Mhatstar = array(0, dim = c(p, p))
for (i in 1:k0) {
covk = ystar[, (i + 1):n, drop = FALSE] %*% t(ystar[, 1:(n - i),
drop = FALSE])/(n - i)
Mhatstar = Mhatstar + covk %*% t(covk)
}
temp1 = eigen(Mhatstar)
valuestar = temp1$values
vector1 = temp1$vectors
ratiostar = array(0, dim = c(R, 1))
for (i in 1:R) {
ratiostar[i] = valuestar[i + 1]/valuestar[i]
}
plot(ratiostar, type = "l")
rhat2 = which.min(ratiostar)
# Ahat2 = vector1[, 1:rhat2, drop = FALSE]
rhat = rhat1 + rhat2
# Ahat = cbind2(Ahat1, Ahat2)
return(rhat)
}
result=replicate(200,simulation())
The program is often interrupted. I do not know why.
My desktop is : win7
RStudio version :0.98.426
The log file is :
15 Oct 2013 11:02:50 [rsession-Administrator] WARNING Abort requested; LOGGED FROM: bool session::connection::checkForAbort(boost::shared_ptr<session::HttpConnection>, boost::function<void()>) C:\Users\Administrator\rstudio\src\cpp\session\http\SessionHttpConnectionUtils.cpp:146
15 Oct 2013 11:03:00 [rsession-Administrator] ERROR r error 5 (R symbol not found) [symbol (option)=ggvis.renderer]; OCCURRED AT: T r::options::getOption(const std::string&, const T&) [with T = std::basic_string<char, std::char_traits<char>, std::allocator<char> >] C:\Users\Administrator\rstudio\src\cpp\r\include/r/ROptions.hpp:73; LOGGED FROM: T r::options::getOption(const std::string&, const T&) [with T = std::basic_string<char, std::char_traits<char>, std::allocator<char> >] C:\Users\Administrator\rstudio\src\cpp\r\include/r/ROptions.hpp:75
binomial <- function(nmax = 100,
thr = 0.95,
alpha = 1,
beta = 1,
p_true = 0.5,
p_0 = 0.5){
for(j in seq.int(nmax, 0)){
if(pbeta(q = p_0, shape1 = alpha + j, shape2 = beta + nmax - j, lower.tail = FALSE) < thr){
targetatnmax <- j + 1
} else {
print(
break
}
}
result <- list(Success = Success, targeratnmax = targetatnmax)
return(result)
}
res = binomial(nmax,thr,alpha,beta,p_true,p_0)
res
In my program I am trying to find the number of successes needed to exceed 0.95 thr. I am trying to use a for loop with if else statements but when I run it I don't get the value I need. I know my value should be 59 but I cannot seem to get this. I know the code seems really messy but its only because I have been playing around with it for hours. PLEASE ANY HELP
Here is your code after clean-up:
binomial <- function(nmax = 100,
thr = 0.95,
alpha = 1,
beta = 1,
p_true = 0.5,
p_0 = 0.5){
targetatnmax <- 0
for(j in seq.int(0,nmax)){
if(pbeta(q = p_0, shape1 = alpha + j, shape2 = beta + nmax - j, lower.tail = FALSE) < thr){
targetatnmax <- j + 1
} else {
break
}
}
result <- list(targeratnmax = targetatnmax)
return(result)
}
res = binomial()
res
#$targeratnmax
#[1] 59
The main problem (other than the syntax errors and not existent objects) was that your loop ran from nmax to 0 instead of the other way arround.
There is probably potential for optimization, but my understanding of the statistics is not good enough to really tackle that.