forked from miking-lang/miking-dppl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoin_flip.cu
46 lines (31 loc) · 812 Bytes
/
coin_flip.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
/*
* File coin_flip.cu defines a coin flip example model.
*/
#include <stdio.h>
#include <math.h>
#include <string>
#include <fstream>
#include "inference/smc/smc.cuh"
// Initialize the model with program state type and number of bblocks.
INIT_MODEL(double)
// Define the model (or fragment of model) with a BBLOCK.
BBLOCK(coinFlip, {
double x = SAMPLE(beta, 2, 2);
OBSERVE(bernoulli, x, true);
PSTATE = x;
NEXT = NULL;
})
// Use result after inference.
CALLBACK(mean, {
double weightedSum = 0;
for(int i = 0; i < N; i++)
weightedSum += PSTATES[i] * exp(WEIGHTS[i]);
printf("Estimated Mean: %f\n", weightedSum);
})
// Wrapper for main function
MAIN({
// Add the bblock to the model
FIRST_BBLOCK(coinFlip);
// Run SMC inference
SMC(mean);
})