Where to save the model file while using R2Winbugs? - winbugs

I'm learning R2WinBUGS. I have few doubts. Can anyone please help me in solving it? when I run this example, I get a error saying "ratsmodel1.txt" does not exists. where am I suppose to save my model file code? And how am I suppose to call it from R using R2WinBUGS?
Modelfile code in WinBUGS
setwd("C:/Program Files/R/R-3.5.1/")
ratsmodel1<-function(){
for( i in 1:N){
for(j in 1:T){
Y[i,j]~dnorm(mu[i,j],tau.c)
mu[i,j]<-alpha[i]+beta[i]+(x[j]-xbar)
}
alpha[i]~dnorm(alpha.c,alpha.tau)
beta[i]~dnorm(beta.c,beta,tau)
}
tau.c~dgamma(0.001,0.001)
sigma<-1/sqrt(tau.c)
alpha.c~dnorm(0.0,1.0E-6)
alpha.tau~dgamma(0.001,0.001)
beta.c~dnorm(0.0,1.0E-6)
beta.tau~dgamma(0.001,0.001)
alpha0<-alpha.c-xbar*beta.c
}
if(is.R()){
filename<-file.path(getwd(),"ratsmodel1.txt")
}
write.model(ratsmodel,filename)
file.show(filename)
R coding
library(R2WinBUGS)
rats.data<-list(
x=c(8.0,15.0,22.0,29.0,36.0),
xbar=22,
N=30,
T=5,
Y= structure(.Data=c(...),.Dim=c(30,5)))
Y<-rats.data$Y
T<-rats.data$T
x<-rats.data$x
xbar<-rats.data$xbar
N<-rats.data$N
rats.data<-list(Y=Y,x=x,T=T,N=N,xbar=xbar)
rats.params<-c("sigma","beta.c","alpha0")
rats.inits<-
list(alpha=c(...),beta=c(...),alpha.c=c(150),beta.c=c(10),
tau.c=c(1),beta.tau=c(100),alpha.tau=c(100))
ratsfit=bugs(rats.data,rats.inits,parameters.to.save=rats.params,
model.file="rat smodel1.txt",n.chain=7,n.iter=2000,n.burnin=1000,
n.thin=1,debug= FALSE,codaPkg=TRUE,
bugs.directory="C:/Program Files/WinBUGS14/",program=="WinBUGS")
rats.coda=read.bugs(ratsfit)
summary(rats.coda)

Related

Pedersen circom/circomlibjs inconsistency?

As a unit test for a larger use case, I am checking that indeed the pedersen hash I am doing in the frontend aligns with the expected hash done through a circom circuit. I am using a simple assert in the circuit and generating a witness and am feeding both the hashed and unhashed values to the circuit, recreating the hash to make sure that it goes through.
I am running a Pedersen hash in my frontend using circomlibjs. As a unit test, I have. a circuit with a simple assert that check whether the results from my frontend line up with the pedersen hash in the circom circuit.
The circuit I am using:
include "../node_modules/circomlib/circuits/bitify.circom";
include "../node_modules/circomlib/circuits/pedersen.circom";
template check() {
signal input unhashed;
signal input hashed;
signal output createdHash[2];
component hasher = Pedersen(256);
component unhashedBits = Num2Bits(256);
unhashedBits.in <== unhashed;
for (var i = 0; i < 256; i++){
hasher.in[i] <== unhashedBits.out[i];
}
createdHash[0] <== hasher.out[0];
createdHash[1] <== hasher.out[1];
hashed === createdHash[1];
}
component main = check();
In the frontend, I am running the following,
import { buildPedersenHash } from 'circomlibjs';
export function buff2hex(buff) {
function i2hex(i) {
return ('0' + i.toString(16)).slice(-2);
}
return '0x' + Array.from(buff).map(i2hex).join('');
}
const secret = (new TextEncoder(32)).encode("Hello");
var pedersen = await buildPedersenHash();
var h = pedersen.hash(secret);
console.log(buff2hex(secret));
console.log(buff2hex(h));
The values that are printed are:
0x48656c6c6f
0x0e90d7d613ab8b5ea7f4f8bc537db6bb0fa2e5e97bbac1c1f609ef9e6a35fd8b
Which are consistent with the test done here.
So I then create an input.json file which looks as follows,
{
"unhashed": "0x48656c6c6f",
"hashed": "0x0e90d7d613ab8b5ea7f4f8bc537db6bb0fa2e5e97bbac1c1f609ef9e6a35fd8b"
}
And lastly run the following script to create a witness, in the hopes that the assert will go through.
# Compile the circuit
circom ${CIRCUIT}.circom --r1cs --wasm --sym --c
# Generate the witness.wtns
node ${CIRCUIT}_js/generate_witness.js ${CIRCUIT}_js/${CIRCUIT}.wasm input.json ${CIRCUIT}_js/witness.wtns
However, I keep getting an assert error,
Error: Error: Assert Failed.
Error in template check_11 line: 26
Which describes the assert in the circuit, so I assume there is an inconsistency in the hash.
I am new to circom so any insights would be greatly appreciated!
For anyone who stumbles across this, it happens that the cause of issue is endianess. The issue was fixed by converting the unhashed to little endian in the input, I am not sure as to where exactly the problem is, but seems like the hasher reads it as big endian on the frontend but the input is expected little endian (or vice verse).
As I have managed to patch up a fix for this at the moment, I will stop investigating, but implore anyone who understand this further to give a better explanation.

How to run e file one by one? Not in parallel test

I am new to specman, I am now writing a testbench which i want to give many specific test cases to debug a calculator.
For example,
I have two files, the first one called "test1" and the second called "test2".
Here is my code for "test1":
extend instruction_s {
keep cmd_in_1 == ADD;
keep din1_1 < 10;
keep din2_1 < 10;
};
extend driver_u {
keep instructions_to_drive.size() == 10;
};
And here is my code for "test2":
extend instruction_s {
keep cmd_in_1 == SUB;
keep din1_1 < 10;
keep din2_1 < 10;
};
extend driver_u {
keep instructions_to_drive.size() == 10;
};
However, when I tried to test my code, specman shows error, it seems I can't do this like that.
Is there any possible way that I can let specman execute "test1" file first and then run "test2" file?
Or if there is some other way that I can achieve my goal?
Thanks for your helping.
Do you really want to have one test that executes 10 ADD instructions, and run another test that executes 10 SUB instructions?
If so, the common way to do so is to compile your testbench, and run multiple times - each time loading another test file.
For a start, try this:
xrun my_device.v my_testbench.e test1.e
xrun my_device.v my_testbench.e test2.e

How to use jeromq in MATLAB

jeromq is a Java implementation of libzmq. I have a .jar file created from the jeromq source. However, I'm unable to call a class in jeromq from MATLAB. I've used addjavaclasspath and addjavalibrarypath but am still not able to get it working. Does anyone have a simple working example in MATLAB?
I've added the answer here as for reference in case anyone else is interested.
% Author : Dheepak Krishnamurthy
% License : BSD 3 Clause
import org.zeromq.ZMQ;
ctx = zmq.Ctx();
socket = ctx.createSocket(ZMQ.REP);
socket.bind('tcp://127.0.0.1:7575');
message = socket.recv(0);
json_data = native2unicode(message.data)';
message = zmq.Msg(8);
message.put(unicode2native('Received'));
socket.send(message, 0);
socket.close()
My Matlab 9.0.0.341360 (R2016a) wanted the following code instead of import above:
javaclasspath('/path/to/jar/jeromq-0.4.3-SNAPSHOT.jar')
import org.zeromq.*
The rest worked fine.
Here is what I had to do to get things working.
javaclasspath('jeromq-0.5.1.jar')
import org.zeromq.*;
%subscribe to ZMQ feed
context = ZContext();
socket = context.createSocket(ZMQ.SUB);
success = false;
while(~success)
success = socket.connect('tcp://127.0.0.1:5996');
end
socket.subscribe("");
socket.setTCPKeepAlive(1);
%receive a message
message = socket.recv(0); %nonblocking receive uses argument (1)
%when done
socket.close();

why difference function does not work (openSCAD)?

I try to learn how to use openSCAD. I 'm reading (also watching) a lot of tutorials but I cannot get why the following code does not work. Could you please help me?
difference() {
polygon(
points=[[2,0],[1.6,2.6],[2.2,3.4],[5.6,4],[11.4,3.4],[11.4,0.6],[10,-1.6],[7.6,-2.4],[4.4,-1.8]]);
polygon(// right len holder in
points=[[2.4,0],[2,2.6],[2.5,3.1],[5.6,3.6],[11,3],[11,0.6],[9.8,-1.2],[7.6,-2],[4.4,-1.45]]);}
your top level object is a 2D-object, use linear_extrude to get 3D-objects:
h = 10;
difference() {
linear_extrude(height=h) polygon(
points=[[2,0],[1.6,2.6],[2.2,3.4],[5.6,4],[11.4,3.4],[11.4,0.6],[10,-1.6],[7.6,-2.4],[4.4,-1.8]]);
linear_extrude(height=h) polygon(// right len holder in
points=[[2.4,0],[2,2.6],[2.5,3.1],[5.6,3.6],[11,3],[11,0.6],[9.8,-1.2],[7.6,-2],[4.4,-1.45]]);
}

How to call a function of postgis-1.5.dll in c code

I'm creating an Postgresql extension in c that use Postgis Point. when i try to call a function of postgis-1.5.dll after loading it, it fails and i get no error message
Here is a small part of my code:
Point *pt =(Point*) palloc(sizeof(Point));
bool test;
HINSTANCE DLLHandle;
typedef bool(*ST_empty)(Point*);
ST_emptyPtr ST_empty;
pt->x = 0.2;
pt->y = 0.9;
DLLHandle = LoadLibrary(L"postgis-1.5.dll");
ST_empty = (ST_emptyPtr)GetProcAddress(DLLHandle,"LWGEOM_isempty");
if (DLLHandle != NULL){
if(!ST_empty)
elog(ERROR,"null ehhhh");
test = ST_empty(p);
elog(ERROR,"not empty");
}
Could anyone help me?
It may help to look at the source:
lwgeom_is_empty from PostGIS Trunk
Are you sure it's failing? The code above doesn't test the return value from the function call. What does the following do?
if (!ST_empty(p))
elog(ERROR,"not empty");
Brian