Error messgae in JAGS when defining constraints for priors Attempt to redefine node - mcmc

I am trying to sample from prior distributions and constrain some of the sums and differences to be greater than 0, however, when I run this code that usually works in OpenBugs/WinBugs, I get the following error 'Attempt to redefine node '. I was wondering if there is any possible way to define my constraints in order to prevent this error from occurring. Any help would be massively appreciated.
Thanks
S[1,1] ~ dbeta(8.10, 32.81)
d[1,1] ~ dbeta(4.36, 32.61)
d[1,2] ~ dnorm(0.097, 250)
d[2,2] ~ dbeta(2.23, 20.10)
Z1 <- 1
Z1 ~ dbern(constraint)
#Constraints
constraint <- step(S[1,1] + d[1,2])*
step(1 - S[1,1] - d[1,2])*
step(S[1,1] - d[1,1])*
step(S[1,1] + d[1,2] - d[2,2])

One way would be to define Z1as 1 in the data and take it out of the model. Then, you can leave in the Z1~dbern(constraint) in the model.

Related

Can I use interaction terms with psem in piecewiseSEM?

I am trying to use interaction terms with psem in the piecewiseSEM package but I get the following error:
Error in names(B) <- numVars :
'names' attribute [3] must be the same length as the vector [2]
lme(response1 ~ predictor1 * predictor2, data),
lme(response2 ~ predictor1 * predictor2, data),
...)```
lme is in the nlme package
predictor1 is an integer of 7 levels
predictor2 is a factor of 2 levels
The model alone runs fine outside of psem, though. I could not find a reference as to why interaction terms will not work in the function psem.
I appreciate any insights on this. Thanks!
Update:
this SEM only worked if predictor2 is an integer

How to print a particular constraint in pulp for large problems

I am running a OR problem in Pulp with 600*159 decision variables and large set of constraints . every time if i add new constraint to problem to check the correctness of constraint, i put print(model) . my program not able to display due to high memory to show in console. so i use 'writelp' to download file and verify which is cumbersome. can someone tell how to print only the constraint ,i need to see in console or any other option available.
When I want to do something like this, it's easy to just store the constraint to a temporary variable, print that, and then add it to the problem:
>>> x = pl.LpVariable(f"x", lowBound=0, upBound=None)
>>> constraint = x <= 3
>>> print(constraint)
x <= 3
>>> prob += constraint
A constraint is an instance of pulp.LpConstraint, which can be printed, since it has __str__() and __repr__() methods. In other words, it's just a python object.

AWK (or similar) - change 2 lines below the matching pattern

I have a problem that I think it's easiest to solve with awk but I wrapped my head around it.
Inside a file I have repeating output like this:
....
Name="BgpIpv4RouteConfig_XXX">
<Ipv4NetworkBlock id="13726"
StartIpList="x.y.z.t"
PrefixLength="30"
NetworkCount="10000"
... other output
then this block will repeat.
a)I want to match on BGPIpv4Route.*, then skip 2 lines (the "n" keyword of awk), then when reaching Prefix Length:
- either replace it with random (25,30)
or
- better but I guess harder (no idea came to mind for keeping track of what was used and looping among /25../30) -> first occurrence /25, second one /26...till /30, then rollback to /25
b) then next line with NetworkCount depending on the new value of PrefixCount calculate it as 65536 / 2^(32-Prefix Count)
eg: if PrefixCount on this occurrence was replaced with /25, then NetworkCount on the line following it = 65536 / 2 ^ 7 = 65536 / 128 = 512
I found some examples with inserting/changing a line after one that matched (or with a counter variable X lines below the match) but I got a bit confused with the value generation part and also with the changing of two lines where one is depending on the other.
Not sure I made any sense...my head is a bit overwhelmed with what I'm finding everywhere right now.
Thanks in advance!
this should do
$ awk 'BEGIN {q="\""; FS=OFS="="; n=split("25=26=27=28=29=30",ps)}
/BgpIpv4Route/ {c=c%n+1}
/PrefixLength/ {$2=q ps[c] q}
/NetworkCount/ {$2=q 65536/2^(32-ps[c]) q}1' file
perhaps minimize computation by changing to 2^(ps[c]-16)
If there are free standing PrefixLength and NetworkCount attributes perhaps you need to qualify them for each BgpIpv4Route context.

Display polynomials in reverse order in SageMath

So I would like to print polynomials in one variable (s) with one parameter (a), say
a·s^3 − s^2 - a^2·s − a + 1.
Sage always displays it with decreasing degree, and I would like to get something like
1 - a - a^2·s - s^2 + a·s^3
to export it to LaTeX. I can't figure out how to do this... Thanks in advance.
As an alternative to string manipulation, one can use the series expansion.
F = a*s^3 - s^2 - a^2*s - a + 1
F.series(s, F.degree(s)+1)
returns
(-a + 1) + (-a^2)*s + (-1)*s^2 + (a)*s^3
which appears to be what you wanted, save for some redundant parentheses.
This works because (a) a power series is ordered from lowest to highest coefficients; (b) making the order of remainder greater than the degree of the polynomial ensures that the series is just the polynomial itself.
This is not easy, because the sort order is defined in Pynac, a fork of Ginac, which Sage uses for its basic symbolic manipulation. However, depending on what you need, it is possible programmatically:
sage: F = 1 + x + x^2
sage: "+".join(map(str,sorted([f for f in F.operands()],key=lambda exp:exp.degree(x))))
'1+x+x^2'
I don't know whether this sort of thing is powerful enough for your needs, though. You may have to traverse the "expression tree" quite a bit but at least your sort of example seems to work.
sage: F = a + a^2*x + x^2 - a*x^2
sage: "+".join(map(str,sorted([f for f in F.operands()],key=lambda exp:exp.degree(x))))
'a+a^2*x+-a*x^2+x^2'
Doing this in a short statement requires a number of Python tricks like this, which are very well worth learning if you are going to use Sage (or Numpy, or pandas, or ...) a fair amount.

Multiset Partition Using Linear Arithmetic and Z3

I have to partition a multiset into two sets who sums are equal. For example, given the multiset:
1 3 5 1 3 -1 2 0
I would output the two sets:
1) 1 3 3
2) 5 -1 2 1 0
both of which sum to 7.
I need to do this using Z3 (smt2 input format) and "Linear Arithmetic Logic", which is defined as:
formula : formula /\ formula | (formula) | atom
atom : sum op sum
op : = | <= | <
sum : term | sum + term
term : identifier | constant | constant identifier
I honestly don't know where to begin with this and any advice at all would be appreciated.
Regards.
Here is an idea:
1- Create a 0-1 integer variable c_i for each element. The idea is c_i is zero if element is in the first set, and 1 if it is in the second set. You can accomplish that by saying that 0 <= c_i and c_i <= 1.
2- The sum of the elements in the first set can be written as 1*(1 - c_1) + 3*(1 - c_2) + ... +
3- The sum of the elements in the second set can be written as 1*c1 + 3*c2 + ...
While SMT-Lib2 is quite expressive, it's not the easiest language to program in. Unless you have a hard requirement that you have to code directly in SMTLib2, I'd recommend looking into other languages that have higher-level bindings to SMT solvers. For instance, both Haskell and Scala have libraries that allow you to script SMT solvers at a much higher level. Here's how to solve your problem using the Haskell, for instance: https://gist.github.com/1701881.
The idea is that these libraries allow you to code at a much higher level, and then perform the necessary translation and querying of the SMT solver for you behind the scenes. (If you really need to get your hands onto the SMTLib encoding of your problem, you can use these libraries as well, as they typically come with the necessary API to dump the SMTLib they generate before querying the solver.)
While these libraries may not offer everything that Z3 gives you access to via SMTLib, they are much easier to use for most practical problems of interest.

Resources