I try to estimate delta[j,k] on the condition sum(delta[j,1:5])=0 for each "j". but when I compile the code, software output is "multiple definitions of node W[1]". Could someone help me?
model {
for (j in 1:p){
for (k in 1:5){
Z[j, k]<- sum(delta[j,1:k])
}
for (i in 1:n){
Y[i , j] ~ dcat ( prob [i , j , 1: 5])
}}
for (i in 1:n){
theta [i] ~ dnorm (0.0 , 1.0)
}
for (i in 1:n){
for (j in 1:p){
for (k in 1:5){
eta[i , j , k] <- alpha [j] * (k*theta [i] - k*beta [j]+Z[j, k])
psum [i , j , k] <- sum(eta[i , j , 1: k])
exp.psum[i , j , k]<- exp( psum [i , j , k])
prob [i , j , k] <- exp.psum[i , j , k] / sum(exp.psum [i , j , 1:5])
} } }
for (j in 1:p){
W[j] <- sum(delta [j, 1:5])
W[j]<- 0
alpha [j] ~ dlnorm (0.83 , pr.alpha)
beta [j] ~ dnorm (-1.73 , pr.beta )
delta[j,1] <- 0.0
for (k in 2:5){
delta [j , k] ~ dnorm (0.02 , pr.delta )
} }
pr.alpha <- pow(1.2 , -2)
pr.beta <- pow(0.7, -2)
pr.delta <- pow(1.3, -2)
}
thanks
BUGS does not allow you to overwrite deterministic nodes,... you have have W[j] <- twice in the last for loop.
I guess there are many way to write the code to meet your condition. For example you could use a different distribution for delta or set delta[1] to be the remainder required to get all delta to sum to 0 after simulating delta[2] to delta[5]
Related
I'm trying to code the romberg integration method on matlab. I think I coded it right but I'm not getting why I have this error.
This is the function.
function[t , r] = Romberg (fun, a, b, nmax)
f = inline(fun);
r(1, 1) = (b - a) * (f(a) + f(b)) / 2;
for i = 1 : nmax
h(i) = (b-a) /2^(i) ;
m = 0;
for k = 1 : (2^(i))-1
m = m + f (a+k*h(i));
end
r(i + 1, 1) = (h(i) / 2) * (f(a) + f(b) + 2*m);
for j = 2 : i
r(i, j) = r(i, j-1) + (r(i, j-1) - r(i - 1, j - 1)) / (4^(j-1) - 1);
end
end
t = r (i, j)
This is its call
clc; clear all; close all;
a = 0;
b = pi;
nmax = 3;
fun ='sin (x)' ;
[t, r]= Romberg (fun, a, b, nmax)
And this is the error:
Undefined function 'Romberg' for input arguments of type 'char'.
I am trying to code Horner’s Method for Polynomial Evaluation but for some reason its not working for me and I'm not sure where I am getting it wrong.
These are the data I have:
nodes = [-2, -1, 1]
x = 2
c (coefficients) = [-3, 3, -1]
The code I have so far is:
function y = horner(x, nodes, c)
n = length(c);
y = c(1);
for i = 2:n
y = y * ((x - nodes(i - 1)) + c(i));
end
end
I am supposed to end up with a polynomial such as (−1)·(x+2)(x+1)+3·(x+2)−3·1 and if x =2 then I am supposed to get -3. But for some reason I don't know where I am going wrong.
Edit:
So I changed my code. I think it works but I am not sure:
function y = horner(x, nodes, c)
n = length(c);
y = c(n);
for k = n-1:-1:1
y = c(k) + y * (x - nodes((n - k) + 1));
end
end
This works:
function y = horner(x, nodes, c)
n = length(c);
y = 0;
for i = 1:n % We iterate over `c`
tmp = c(i);
for j = 1:i-1 % We iterate over the relevant elements of `nodes`
tmp *= x - nodes(j); % We multiply `c(i) * (x - nodes(1)) * (x -nodes(2)) * (x- nodes(3)) * ... * (x - nodes(i -1))
end
y += tmp; % We added each product to y
end
% Here `y` is as following:
% c(1) + c(2) * (x - nodes(1)) + c(3) * (x - nodes(1)) * (x - nodes(2)) + ... + c(n) * (x - nodes(1)) * ... * (x - nodes(n - 1))
end
(I'm sorry this isn't python but I don't know python)
In the case where we didn't have nodes, horner's method works like this:
p = c[n]
for i=n-1 .. 1
p = x*p + c[i]
for example for a quadratic (with coeffs a,b,c) this is
p = x*(x*a+b)+c
Note that if your language supports fma
fma(x,y,x) = x*y+z
then horner's method can be written
p = c[n]
for i=n-1 .. 1
p = fma( x, p, c[i])
When you do have nodes, the change is simple:
p = c[n]
for i=n-1 .. 1
p = (x-nodes[i])*p + c[i]
Or, using fma
p = c[n]
for i=n-1 .. 1
p = fma( (x-nodes[i]), p, c[i])
For the quadratic above this leads to
p = (x-nodes[1]*((x-nodes[2])*a+b)+c
I've developed a neural network in R to classify a set of images, namely the images in the MNIST handwritten digit database.
I use pca on the images and the nn has two hidden layers.
So far I can't get more than 95% of accuracy on the validation set.
What can I do to get a 100% of accuracy on the validation set? That is, what can I do to improve the generalization capabilities of the nn?
(I'm using a stochastic back-propagation algorithm to find the optimal weights).
I'll post the code for the function that finds the weights.
DICLAIMER: I'm so totally new to neural networks and R so this is just an attempt to come up with something.
fixedLearningRateStochasticGradientDescent <- function(X_in, Y, w_list, eta, numOfIterations){
x11();
err_data <- NULL
N <- dim(X_in)[2]
X_in <- rbind(rep(1, N), X_in) #add bias neurons to input
iter <- 0
for(i in 1:numOfIterations){
errGrad <- NULL;
iter <- i
e_in <- 0
g_list <- initGradient(w_list)
L <- length(w_list)
for(i in (1:N)){
#compute x
s_list <- list()
x_list <- list(X_in[,i, drop = FALSE])
for(l in 1:L){
S <- t(w_list[[l]]) %*% x_list[[l]]
s_list[[length(s_list) + 1]] <- S
X <- apply(S, 1:2, theta_list[[l]])
X_n <- dim(X)[2]
if(l < L){
X <- rbind(rep(1, X_n), X) #add bias neurons to input
}
x_list[[length(x_list) + 1]] <- X
}
#compute d
d_list <- list()
for(l in (1:L)){
d_list[[l]] <- NULL
}
target <- t(Y[i,,drop = FALSE])
d_list[[L]] <- 2 * (x_list[[L + 1]] - target) * theta_der_list[[L]](x_list[[L + 1]])
for(l in (L - 1):1){
T <- theta_der_list[[l]](x_list[[l + 1]])
Q <- w_list[[l + 1]] %*% d_list[[l + 1]]
D <- T * Q
D <- D[-1, , drop=FALSE] #remove bias
d_list[[l]] <- D
}
e_in <- e_in + (1/N * sum((x_list[[L + 1]] - target)^2))
for(l in 1:L){
G <- x_list[[l]] %*% t(d_list[[l]])
#print(G)
g_list[[l]] <- G
}
for(i in 1:(length(w_list))){
w_list[[i]] <- w_list[[i]] - eta * g_list[[i]]
}
}
err <- e_in
g_list <- errGrad[[2]]
err_data <- c(err_data, err)
print(paste0(iter, ": ", err))
}
plot(err_data, type="o", col="red")
print(err)
return(w_list)
}
The rest of the code is trivial:
- perform pca on input
- initialize weights
- find weights
- calculate performance on test and validation sets.
I've implemented Pollard's Rho for logarithms using Sage, as the following program stored in pollardrho.py.
def pollardrho(g, h, G):
k, m = 1, 0
t = g**k * h**m
i, j = 1, 0
r = g**i * h**j
def step(t, k, m):
if lift(t) % 3 == 0:
return (t * g, k+1, m)
if lift(t) % 3 == 1:
return (t * h, k, m+1)
if lift(t) % 3 == 2:
return (t ** 2, 2*k, 2*m)
while True:
t, k, m = step(t, k, m)
r, i, j = step(*step(r, i, j))
if t == r:
print("Found a cycle")
print("g^%s h^%s == g^%s h^%s" % (k, m, i, j))
print("g^(%s - %s) == h^(%s - %s)" % (i, k, m, j))
l = g.multiplicative_order()
print("(%s - %s) / (%s - %s) %% %s" % (i, k, m, j, l))
return (i - k) / (m - j) % l # this is where everything goes wrong.
Running this with G = GF(1013), g = G(3), h = G(245) gives the following output:
sage: pollardrho(g, h, G)
Found a cycle
g^262 h^14 == g^16870 h^1006
g^(16870 - 262) == h^(14 - 1006)
(16870 - 262) / (14 - 1006) % 1012
995
However:
sage: (16870 - 262) / (14 - 1006) % 1012
375
Note that this is a completely different result!
If I check the types of i, j, k, m, they are all of type int...
It turns out that typing an integer in the sage shell gives a different result than doing the same inside a python program that uses Sage libraries:
sage: type(1234)
<type 'sage.rings.integer.Integer'>
This isn't the same as the <type 'int'> I got inside of my own program!
Using k, m = Integer(1), Integer(0) solved my problem and I now get the correct discrete log.
To elaborate on Thom's answer, in a .py file you can't use the various preparsing things that Sage does - in particular, ints are ints. Importing from sage.rings.integer.Integer (or from sage.all) in your file could work, or (and I recommend this) just making your file .sage extension instead of .py is the easiest, and least likely to run into other subtle differences.
I would like to use the LAPACK routines for factorisation and inversion of matrices using the fully packed rectangular format, as this requires only n(n+1)/2 elements to be stored for a symmetric nxn matrix. So far, I am setting up the matrix in 'packed' format and transform it calling routine DTPTTF. However, this requires a second array. I would like to build my matrix directly in fully packed rectangular format (to save on space) - is there an 'addressing' function which will give me the position of the i,j-th element? or could somebody point me to the relevant formula?
to partly answer my own question: inspecting the source code of DTPTTF and the example given therein, I've worked out the adress for one of the four possible constellations (the only one I need), namely uplo ='L' and trans ='N'. below is my fortran function:
! ==================================== ! returns address for RFP format
integer function ijfprf( ii, jj, n ) ! for row jj and column ii
! ==================================== ! for UPLO = 'L' and TRANSR = 'N' only!
implicit none
integer, intent(in) :: ii, jj, n
integer :: i, j, k, n1, k1
if( ii <= jj ) then
i = ii; j = jj
else
i = jj; j = ii
end if
k = n/2
if( mod(n,2) == 0 ) then ! n even
n1 = n + 1
if( i <= k ) then
ijfprf = 1 + (i - 1) * n1 + j
else
ijfprf = ( j - k - 1 ) * n1 + i - k
end if
else ! n odd
k1 = k + 1
if( i > k1 ) then
ijfprf = ( j - k1 ) * n + i - k1
else
ijfprf = ( i - 1 ) * n + j
end if
end if
return
end function ijfprf