forked from miking-lang/miking-dppl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
geometric_recursive.cu
52 lines (37 loc) · 1.15 KB
/
geometric_recursive.cu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
* File geometric_recursive.cu defines the recursive geometric distribution model.
*/
#include <stdio.h>
#include "inference/smc/smc.cuh"
// Initialize the model with program state type and number of bblocks.
INIT_MODEL(int)
BBLOCK_HELPER(geometricRecursive, {
if(SAMPLE(bernoulli, p))
return 1;
else
return BBLOCK_CALL(geometricRecursive, p) + 1;
}, int, double p)
// Define the model (or fragment of model) with a BBLOCK.
BBLOCK(geometric, {
PSTATE = BBLOCK_CALL(geometricRecursive, 0.6);
NEXT = NULL;
})
// Use result after inference. Prints frequencies that represents resulting distribution.
CALLBACK(callback, {
int frequencies[10] = {0};
for(int i = 0; i < N; i++) {
int outcome = PSTATES[i];
if(outcome <= 10)
frequencies[outcome-1]++;
}
for(int i = 0; i < 10; i++)
printf("%d: %f\n", i+1, frequencies[i] / (double)N);
})
// Wrapper for main function
MAIN({
// Initialize bblock, add it to the array of bblocks to be executed in inference
// Essentially, it adds the BBLOCK to the model
FIRST_BBLOCK(geometric);
// Run SMC inference
SMC(callback);
})