Skip to content

Commit 2e3fb12

Browse files
author
Bob Carpenter
committed
all funshapes working, with existing modified so there is no rejection
1 parent cc585d9 commit 2e3fb12

File tree

8 files changed

+105
-57
lines changed

8 files changed

+105
-57
lines changed
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
# http://www.openbugs.info/Examples/Funshapes.html
2-
1+
/*
2+
* BUGS volume 3, fun shapes, circle
3+
* http://www.openbugs.info/Examples/Funshapes.html
4+
*/
35
parameters {
4-
real<lower=-1,upper= 1> x;
5-
real<lower=-1,upper= 1> y;
6+
real<lower=-1,upper=1> x;
7+
real<lower = -sqrt(1 - square(x)),
8+
upper = sqrt(1 - square(x))> y;
69
}
7-
810
model {
9-
// lp__ <- log(step(1 - x * x - y * y));
10-
lp__ <- log(fmax(0, 1 - x * x - y * y));
11-
11+
/* uniform within constraints */
1212
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Hollow Square
3+
* http://www.openbugs.info/Examples/Funshapes.html
4+
*
5+
* Transform a uniform rectangle
6+
* 1234
7+
* 5678
8+
* 90AB
9+
* to a uniform hollow square
10+
* 1234
11+
* 5 6
12+
* 7 8
13+
* 90AB
14+
*/
15+
parameters {
16+
real<lower=0,upper=2> x_raw;
17+
real<lower=0,upper=1.5> y_raw;
18+
}
19+
model {
20+
/* no-op; uniformity implied by parameter constraints */
21+
}
22+
generated quantities {
23+
real x;
24+
real y;
25+
if (y_raw > 1) {
26+
// cases 1, 2, 3, 4
27+
x <- x_raw - 1;
28+
y <- y_raw - 0.5;
29+
} else if (y_raw < 0.5) {
30+
// cases 9, 0, A, B
31+
x <- x_raw - 1;
32+
y <- y_raw - 1;
33+
} else if (x_raw < 0.5) {
34+
// case 5
35+
x <- x_raw - 1;
36+
y <- y_raw - 0.5;
37+
} else if (x_raw < 1.0) {
38+
// case 6
39+
x <- x_raw - 1.5;
40+
y <- y_raw - 1;
41+
} else if (x_raw < 1.5) {
42+
// case 7
43+
x <- x_raw - 0.5;
44+
y <- y_raw - 0.5;
45+
} else {
46+
x <- x_raw - 1.0;
47+
y <- y_raw - 1.0;
48+
}
49+
}

bugs_examples/vol3/funshapes/hsquare.stan.0

Lines changed: 0 additions & 14 deletions
This file was deleted.

bugs_examples/vol3/funshapes/makefile

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ PGM = circle#
33
PGM2 = squaremc#
44
PGM3 = ring#
55
PGM4 = hsquare#
6-
PGM5 = parallelagram#
6+
PGM5 = parallelogram#
77

88
CXX = g++
99
CXX = clang++
@@ -12,31 +12,32 @@ EIGENPATH = $(shell find $(STAN_HOME)/lib -path '*lib/eigen_*' -regex '.*lib\/ei
1212
CPPFLAGS = -I $(BOOSTPATH) -I$(EIGENPATH) -I $(STAN_HOME)/src
1313
LIBFLAGS = -L$(STAN_HOME)/bin -lstan
1414

15+
all : $(PGM) $(PGM2) $(PGM3) $(PGM4) $(PGM5)
16+
1517
$(PGM) :
1618
$(STAN_HOME)/bin/stanc --name=$(PGM) $(PGM).stan
1719
$(CXX) -Wall -O3 -DNDEBUG $(CPPFLAGS) $(PGM).cpp -o $(PGM) $(LIBFLAGS)
18-
./$(PGM) # --data=$(PGM).data.R
20+
./$(PGM)
1921

2022
$(PGM2) :
2123
$(STAN_HOME)/bin/stanc --name=$(PGM2) $(PGM2).stan
2224
$(CXX) -Wall -O3 -DNDEBUG $(CPPFLAGS) $(PGM2).cpp -o $(PGM2) $(LIBFLAGS)
23-
./$(PGM2) --iter=10000 --warmup=1000 # --data=$(PGM2).data.R
24-
25+
./$(PGM2)
2526

2627
$(PGM3) :
2728
$(STAN_HOME)/bin/stanc --name=$(PGM3) $(PGM3).stan
2829
$(CXX) -Wall -O3 -DNDEBUG $(CPPFLAGS) $(PGM3).cpp -o $(PGM3) $(LIBFLAGS)
29-
./$(PGM3) --iter=10000 --warmup=1000 --thin=1# --data=$(PGM3).data.R
30+
./$(PGM3)
3031

3132
$(PGM4) :
32-
$(STAN_HOME)/bin/stanc --name=$(PGM4) $(PGM4).stan.0
33+
$(STAN_HOME)/bin/stanc --name=$(PGM4) $(PGM4).stan
3334
$(CXX) -Wall -O3 -DNDEBUG $(CPPFLAGS) $(PGM4).cpp -o $(PGM4) $(LIBFLAGS)
34-
./$(PGM4) # --data=$(PGM4).data.R
35+
./$(PGM4)
3536

3637
$(PGM5) :
3738
$(STAN_HOME)/bin/stanc --name=$(PGM5) $(PGM5).stan
3839
$(CXX) -Wall -O3 -DNDEBUG $(CPPFLAGS) $(PGM5).cpp -o $(PGM5) $(LIBFLAGS)
39-
./$(PGM5) # --data=$(PGM5).data.R
40+
./$(PGM5)
4041

4142

4243
clean :

bugs_examples/vol3/funshapes/parallelagram.stan

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* BUGS Volume 3, funshapes, parallelogram
3+
* http://www.openbugs.info/Examples/Funshapes.html
4+
*/
5+
parameters {
6+
real<lower=0,upper=1> x;
7+
real<lower=0,upper=1> y_raw;
8+
}
9+
transformed parameters {
10+
real<lower=-1,upper=1> y;
11+
y <- y_raw - x;
12+
}
13+
model {
14+
/* uniform within constraints */
15+
}

bugs_examples/vol3/funshapes/ring.stan.0

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* BUGS Volume 3, funshapes, square minus circle
3+
* http://www.openbugs.info/Examples/Funshapes.html
4+
*
5+
* first draw raw samples from diamond-like shape reflected per
6+
* quadrant, then reflect in transformed parameters
7+
*
8+
* unfortunate redundancy in 1-sqrt() term because we can't
9+
* get local variables into parameters
10+
*/
11+
parameters {
12+
real<lower=-1,upper=1> x_raw;
13+
real<lower = -(1 - sqrt(1 - square(1 - fabs(x_raw)))),
14+
upper = (1 - sqrt(1 - square(1 - fabs(x_raw))))> y_raw;
15+
}
16+
transformed parameters {
17+
real<lower=-1,upper=1> x;
18+
real<lower=-1,upper=1> y;
19+
x <- if_else(x_raw > 0, 1, -1) - x_raw;
20+
y <- if_else(y_raw > 0, 1, -1) - y_raw;
21+
}
22+
model {
23+
lp__ <- lp__ + log1m(sqrt(1 - square(1 - fabs(x_raw))));
24+
}

0 commit comments

Comments
 (0)