# DbS Code

10 Jul 2020 - Neil

This code implements the decision-by-sampling model for risky choices reported in Stewart, Chater, and Brown (2006), Stewart and Simpson (2008) and Stewart (2009). The example R code implements the model for one example of the common ratio effect from Kahneman and Tversky (1979).

You can find the DbS_code on github.

Here is an example of the code at work:

``````> # Load the decision by sampling library
> source("DbS.R")
>
> # 80% chance of 4000 otherwise nothing
> # vs 3000 for sure
> c3 <- list(g3A=gamble(x=c(4000, 0), p=c(0.8, 0.2)),
+     g3B=gamble(x=c(3000), p=c(1))
+ )
> c3
\$g3A
x   p
[1,] 4000 0.8
[2,]    0 0.2

\$g3B
x p
[1,] 3000 1

>
> # 20% chance of 4000 otherwise nothing
> # vs 25% chance of 3000 otherwise
> c4 <- list(g4A=gamble(x=c(4000, 0), p=c(0.2, 0.8)),
+     g4B=gamble(x=c(3000, 0), p=c(0.25, 0.75))
+ )
> c4
\$g4A
x   p
[1,] 4000 0.2
[2,]    0 0.8

\$g4B
x    p
[1,] 3000 0.25
[2,]    0 0.75

>
> # Thresholds for discriminating amounts and probabilities
> thresholds=list(x=10, p=.1)
>
> # A uniform background context of amounts and probabilities
> context=list(x=seq(-6000,6000,1), p=seq(-1,1,.01))
>
> # Probability of selecting each gamble for each choice
> predictions <- DbS(choices=list(c3=c3, c4=c4),
+     thresholds=thresholds,
+     context=context,
+     prob.sample.context=.5,
+     prob.sample.amount=.5,
+     self.comparison=F,
+     choice.mechanism="accumulators",
+     choice.threshold=5
+ )
> predictions
\$c3
g3A       g3B
0.1818279 0.8181721

\$c4
g4A       g4B
0.6025243 0.3974757
``````