I've got large matrix of parameters.
The point is that among many parameters some parameters with arbitrary indexes induce value errors and I'd like to fix them.
The toy example is as follows:
foo[3, 2] <- mu[3, 2] # mu is some (4,4) matrix from data input
for (r in 1:4) {
for (c in 1:4) {
foo[r, c] ~ dnorm( mu[r, c], .01 )
}
}
I see some examples like 15414303 and 46730232, but I cannot wrap my head around the problem how to apply those tricks (or similar) in my case.
Is there a simple way to implement such a logic in JAGS / BUGS?
The simplest way would be to supply foo in the data where all entries beside foo[3,2] are missing and foo[3,2] is mu[3,2]. Then, the code you have above should work fine (if you remove the definition of foo[3,2] in your code). The alternative would be to define the loops around the fixed cell. For example:
for(r in c(1,2,4)){
for(c in 1:4){
foo[r, c] ~ dnorm( mu[r, c], .01 )
}
}
for(c in c(1,3,4)){
foo[3, c] ~ dnorm( mu[r, c], .01 )
}
Related
The Boost inversion_chi_square_distribution shared different value than chi2inv from Matlab, Input parameters(1 - 1e-3, 2).
Can someone explain what i did wrong ?
I used the gamma inversion distribution.
Example :
inputs :
shape param : n/2
scale : 2
p = 1 - 1e-3;
Matlab -> chi2inv(1-1e-3, 2);
Matlab -> gaminv(1-12-3, n/2, 2);
Result -> 13.8155. The same result for both functions.
C++ Boost
cdf(boost::math::inverse_gamma_distribution<double> (n/2,2), a); -> a = 1-1e-3; n = 2;
Result : 0.13506461276045323
Thank you in advance.
Boost implements inverse-gamma distribution, whereas you need the inverse cumulative distribution function (icdf) of the standard gamma distribution.
The former is the gamma distribution calculated at 1/x, whereas the latter is the inverse of the cdf for the gamma distribution, see https://en.wikipedia.org/wiki/Quantile_function . You use two completely different functions with similar names, hence different results.
I'm afraid the function you need does not have a closed form in a general case (though, for example, for shape = 1 scale = 2 it is easy to derive its closed form: -2.0*log(1.0 - x)). One can implement it using some numerical inversion scheme, like bisection, Newton etc.
Not knowing much about the maths/statistics side, I can guess that Matlab's idea of gaminv is the special function, not a distribution.
According to Wolfram Alpha you should probably be using the "inverse regularized gamma function": https://www.wolframalpha.com/input/?i=inverse+cdf+chi2
Or in plaintext:
ConditionalExpression[Piecewise[{{2 InverseGammaRegularized[ν/2, 0, x], 0 < x < 1}, {0, x <= 0}}, Infinity], 0 <= x <= 1]
I don't immediately know how to apply this idea to candidate code - but it was too large to post in a comment
With Prolog I want to simplify algebra expression represented as as list of list:
algebra equation
f = 3x+2
list of list
[[3,1],[2,0]]
3 and 2 are coefficients
1 and 0 are exponents
That should be obvious.
I am looking for some tips or suggestions on how to code the simplifications for this example:
f = 3x+2x+1+2
[[3,1],[2,1],[1,0],[2,0]]
simplified:
f = 5x+3
[[5,1],[3,0]]
I have tried some built in functions but did not get the proper idea about how to use them.
One liner, similar to what's proposed by joel76:
simplify(I,O) :-
bagof([S,E],L^(bagof(C,member([C,E],I),L),sum_list(L,S)),O).
The inner bagof collects C (coefficients) given E (exponents), the resulting list L is summed into S, and paired with E becomes [S,E], an element (monomial) of O.
If you omit the universal quantification specifier (that is L^) you get single monomials on backtracking.
You can solve your problem in this way:
simplify(_,_,S,S,[]):- !.
simplify(L,I,Sum,NTot,[[I,S]|T]):-
Sum =< NTot,
findall(X,member([X,I],L),LO),
length(LO,N),
S1 is Sum + N,
sum_list(LO,S),
I1 is I+1,
simplify(L,I1,S1,NTot,T).
write_function([]).
write_function([[D,V]|T]):-
write(' + '),write(V),write('x^'),write(D),
write_function(T).
test:-
L = [[3,1],[2,1],[1,0],[2,0]],
length(L,N),
simplify(L,0,0,N,LO),
LO = [[D,V]|T],
write('f='),write(V),write('x^'),write(D),
write_function(T).
The main predicate is simplify/5 which uses findall/3 to find all the coefficients with the same degree and then sums them using sum_list/2. Then you can write the result in a fancy way using write_function/1.
In SWI-Prolog You can use aggregate :
pred(>, [_,X], [_,Y]) :- X > Y.
pred(<, [_,X], [_,Y]) :- X < Y.
pred(=, [_,X], [_,X]).
simplify(In, Out) :-
aggregate(set([S,X]), aggregate(sum(P), member([P,X], In), S), Temp),
predsort(pred, Temp, Out).
For example :
?- simplify([[3,1],[2,1],[1,0],[2,0]], Out).
Out = [[5, 1], [3, 0]] ;
false.
I have the following function that calculates an area.
It receives three parameters, the first is an n representing the number of cases, the 2nd representing the radio of circumferences, and the 3rd is l giving me back the result.
The problem I have is that when the 1st input is greater than 1 it does not work.
This my code:
as(1, [X], A) :-
A is (sqrt(3.0) * (X*X)) - (3.14 * (X*X))/2.
as(N, [H|_T], A) :-
A is (sqrt(3.0) * (H*H)) - (3.14 * (H*H))/2,
N1 is N-1,
as(N1-1, T, A).
An example of how it should work is:
?- as(4, [1,1,1,1], R).
R = 0.162050807568877130000 ;
R = 0.162050807568877130000 ;
R = 0.162050807568877130000 ;
R = 0.162050807568877130000.
If you could help me, I would be grateful ...
Is there a reason this version isn't sufficient to your needs?
as([H|_], A):-
A is (sqrt(3.0) * (H*H)) - (3.14 * (H*H))/2.
as([_|T], A) :- as(T, A).
Or maybe this?
area(H, Area) :-
Area is (sqrt(3.0) * (H*H)) - (3.14 * (H*H))/2.
as(List, Area) :- member(Ratio, List), area(Ratio, Area).
I don't understand why you need to worry about N at all.
Matching both N and [X] leads to redundancy. You shouldn't have to repeat your formula.
You have a lot of singleton errors: _T in the head, and then T in the body which will not work.
You are passing N1-1 to the recursive call, which will not cause it to be evaluated—but you already evaluated N-1 in the previous expression, so just pass N1 here. Again, I don't see the point of this at all.
I think it's a good idea to use succ(N1, N) instead of adding or subtracting one, because it works in both directions (not relevant here, of course).
It feels a bit icky to be combining the list traversal with the calculation to me. I would in general break things down so that the calculation is separate from the data structure insofar as that can be done. This is a universal maxim of programming.
Since you want to compute the area for every measurement anyway, would it not be opportune to get a list of areas corresponding to the list of radio measurements? The structure of your predicate as/3 seems to indicate that you were thinking along those lines. And you could easily achieve that by using maplist/3:
:- use_module(library(apply)). % needed for maplist
% a single measurement and the corresponding area
area(X, A) :-
A is (sqrt(3.0) * (X*X)) - (3.14 * (X*X))/2.
areas(Xs,As) :-
maplist(area,Xs,As). % area/2 mapped to Xs results in As
Querying this predicate yields the desired results but in a list:
?- areas([1,1,5,3],As).
As = [0.16205080756887713, 0.16205080756887713, 4.051270189221931, 1.4584572681198935].
I am attempting to conduct operations on rows of matrices in a list format to calculate portions of a Hessian matrix (mixed partial derivatives matrix) for some software I am writing. I found that I could only do this so fast in R (even with parallelization), and so have switched over to Rcpp for quicker speed and RcppEigen for the high level matrix operations provided. When I have relied on the List type for representing lists of matrices/vectors passed from R, my Cpp code slows down tremendously as the length of the lists (each element being a matrix or vector) increases. I am not sure exactly why, but it may be because of dynamically sized objects? My questions is: Can I pass a list from R into a vector container from the Standard Template Library (STL) using RcppEigen via something like the following?
vector<Eigen::Map<Eigen::MatrixXd>> A(as<vector<Eigen::Map<Eigen::MatrixXd>> >(AA))
The reason I want to do so is because I have read that accessing vectors is much faster than accessing lists. However, I may have misinterpreted this and I apologize if so.
The idea is to pass in a list of vectors (B2) and a list of matrices (A2). Within each index of these lists, I iterate over the rows of the matrix (A) in the current index of A2 and the vector (b) in the current index of B2, calculating:
b[j] * t(A[j,]) %*% A[j,]
for j from 0 to rows-1. I would end up with a list with size equal to the number of rows of the matrix in that index, then move on to the next index of the outer limit, etc.
Here is a reproducible example of what I have been able to do using List:
library(inline)
library(RcppEigen)
library(microbenchmark)
## Create function which takes list into Rcpp and does all manipulations internally (no lapply outside)
A2 <- lapply(1:2, function(t) matrix(rnorm(10 * t), nrow = t, ncol = 10))
B2 <- lapply(1:2, function(t) rnorm(t))
## This becomes slower relative to R as the size increases.
## Something is not right in how I am programming this.
retLLMat <- "using Eigen::VectorXd;
typedef Eigen::Map<Eigen::MatrixXd> MapMatd;
typedef Eigen::Map<Eigen::VectorXd> MapVecd;
List A(AA), B(BB);
int listSize = A.size(), ncol, sublistSize;
List outList;
double sub;
for (int i = 0; i < listSize; i++)
{
List subList;
MapMatd subMat(as<MapMatd >(A[i]));
MapVecd subVec(as<MapVecd >(B[i]));
ncol = subMat.cols();
VectorXd currRow(ncol);
sublistSize = subMat.rows();
for (int j = 0; j < sublistSize; j++)
{
currRow = subMat.row(j);
sub = subVec[j];
subList[String(j)] = (sub * currRow) * currRow.transpose();
}
outList[String(i)] = subList;
}
return wrap(outList);"
## Compile Cpp code
retLLMatC <- cxxfunction(signature(AA = "List", BB = "List"), retLLMat, plugin = "RcppEigen")
## R version
retLLMat <- function(A, B) mapply(function(a, b) mapply(function(a, b) b * a, lapply(apply(a, 1, function(t) list(tcrossprod(t))), "[[", 1), b, SIMPLIFY = FALSE), A, B, SIMPLIFY = FALSE)
## Test R vs Rcpp version
microbenchmark(retLLMat(A2, B2), retLLMatC(A2, B2))
The above works, but as I increase the length of A2 and B2 to be closer to what I have in my actual real application, which is more than 1000, the Cpp version slows down relative to the R implementation and eventually is slower. To overcome this, I thought of trying to use the standard template library vector format. I didn't know how to do this, so I figured I'd start simple, and just pass a List, try to convert to
vector<Eigen::Map<Eigen::MatrixXd>>
and then send back to R. This is what I tried:
## Testing using a vector of Map<MatrixXd>
## Simplified by trying to return the list after reading it in
VectorMat <- "using Eigen::MatrixXd;
using std::vector;
typedef Eigen::Map<Eigen::MatrixXd> MapMatd;
vector<MapMatd> A(as<vector<MapMatd> >(AA);
return wrap(A);"
## This produces errors
test <- cxxfunction(signature(AA = "List"), VectorMat, plugin = "RcppEigen")
I thank all in advance for insights, and sincerely apologize if this question is not designed well for StackOverflow. I looked around at previous StackOverflow questions, read the posting guide, and googled beforehand for quite a while to try to find an answer to my problem, but it seemed I was just outside of the scope of what had been already asked. I am more than willing to make any changes necessary to make this example more reproducible and also to make what I want to do more clear. I know that you all are very busy, and I do not want to waste your time.
At Dirk's suggestion, I tried passing a List, then defining a ListOf, which should be faster given the explicit nature of the objects within (please correct me if this is wrong!)
Here's what the snippet then looked like:
ListMat1 <- "using Eigen::MatrixXd;
typedef Eigen::Map<Eigen::MatrixXd> MapMatd;
ListOf<MapMatd> A(as<ListOf<MapMatd> >(AA));
return wrap(A);"
This passed on my machine by calling:
ListMat <- cxxfunction(signature(AA = "List"), ListMat1, plugin = "RcppEigen")
res <- ListMat(A2)
However, it seems to still be problematic to access elements within this ListOf. I know this normally works, because I tested it with a numeric vector, i.e.
b2 <- lapply(1:5, function(t) numeric(t))
vecTest <- "ListOf<NumericVector> b(as<ListOf<NumericVector> >(bb));
NumericVector res = b[0];
return wrap(res);"
vecTestfn <- cxxfunction(signature(bb = "List"), vecTest, plugin = "RcppEigen")
vecTestfn(b2)
If I try to do the same thing with ListOf where each element is a MatrixXd, I seem to have an issue:
ListMatInd <- "using Eigen::MatrixXd;
typedef Eigen::Map<Eigen::MatrixXd> MapMatd;
ListOf<MapMatd> A(as<ListOf<MapMatd> >(AA));
MatrixXd res = A[0];
return wrap(res);"
This creates an error upon trying:
ListMatIndfn <- cxxfunction(signature(AA = "List"), ListMatInd, plugin = "RcppEigen")
I will continue trying things. I just want to update regarding where I am right now. At this moment, before having read the Rcpp book in full (I am about to order it with my personal development fund), this is the only way I knew to pass each element of the list as a Eigen Map MatrixXd. Thank you for your time!
You may be able to do it the other way around: use a standard List (which we know passes, obviously) where each element (which has to be SEXP anyway) passes an Eigen Map<MatrixXd> (which we also know pass individually).
So I would start simple and make it more complicated step by step til it breaks.
I'm trying to make an exercise which gets a list of numers, an shows a list of elements like this: if A=[a0,a1,a2] then there is U=[u0,u1,u2], knowing that a0*u0 + a1*u1 + a2*u2 = d and d is the gcd of A.
For 2 elements is a pretty simple thing, as Sage has a function to retrieve u0 and u1 out of a0 and a1:
A=[15,21]
(d,u0,u1)=xgcd(a[0],a[1])
I just don't understand how could I do this with a list of n elements.
Note that gcd(a, b, c) = gcd((gcd(a, b), c). This means that you can use the built-in function repeatedly to calculate the coefficients that you want.
You helped me a lot, came to this:
x1=[1256,5468,5552,1465]
n=-1
for i in x1:
n=n+1
(d,w,x)=xgcd(x1[n-1],x1[n])
u1=[w,x]
n=n-2
while n>=0:
div=d
(d,u,v)=xgcd(x1[n],div)
position=0
for j in u1:
a=j*v
u1[position]=a
position=position+1
u1=[u]+u1
n=n-1
u1
And it works ;)