# Coin Flip Probability Based On Additional Attributes

**URL:** <https://discourse.mc-stan.org/t/coin-flip-probability-based-on-additional-attributes/11271>\
**Category:** Modeling\
**Created:** [October 7, 2019, 9:16am UTC](https://discourse.mc-stan.org/t/coin-flip-probability-based-on-additional-attributes/11271 "2019-10-07T09:16:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mattiasthalen](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mattiasthalen/32/6768_2.png) [@mattiasthalen](https://discourse.mc-stan.org/u/mattiasthalen)\
**Post date:** [October 7, 2019, 9:16am UTC](https://discourse.mc-stan.org/t/coin-flip-probability-based-on-additional-attributes/11271/1 "2019-10-07T09:16:34Z")

</div>

Hi,

I’m trying to get a probability plot for several coins with different attributes.  
Let’s say that the attributes are diameter, weight, shape & thickness (all categorical).  
I’ve understood that in bayes this turns out to be the following:  
P(A | B \cap C \cap D \cap E) = \frac{P(B \cap C \cap D \cap E | A)\times P(A)}{P(B \cap C \cap D \cap E)}

If it was just a simple coin my setup would be this:  
P(A | B) = \frac{P(B | A)\times P(A)}{P(B)}

```stan
data {
	int <lower = 1> N; // number of observations
	int <lower = 0, upper = N> k; // number of successes
}
parameters {
	real<lower = 0, upper = 1> theta; // probability of success
}
model {
	theta ~ beta(1, 1); // prior
	k ~ binomial(N, theta); // likelihood
}

```

I’m a bit lost on where to start this using rstan.

---

<div class="post-metadata">

**Author:** ![nhuurre](https://avatars.discourse-cdn.com/v4/letter/n/ad7895/32.png) [@nhuurre](https://discourse.mc-stan.org/u/nhuurre)\
**Post date:** [October 7, 2019, 3:52pm UTC](https://discourse.mc-stan.org/t/coin-flip-probability-based-on-additional-attributes/11271/2 "2019-10-07T15:52:21Z")

</div>

For binary outcomes predicted by several attributes you might want to start with [a logistic regression](https://mc-stan.org/docs/2_20/stan-users-guide/logistic-probit-regression-section.html).

---

<div class="post-metadata">

**Author:** ![emiruz](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/emiruz/32/5518_2.png) [@emiruz](https://discourse.mc-stan.org/u/emiruz)\
**Post date:** [October 11, 2019, 11:05am UTC](https://discourse.mc-stan.org/t/coin-flip-probability-based-on-additional-attributes/11271/3 "2019-10-11T11:05:33Z")

</div>

If it was just one coin I guess you’re asking, what’s the chance of it landing heads given y=(y\_1,..,y\_n) observations where y\_i\in \{0,1\}. So we are interested in P(\theta\mid y). We assume y \sim bernoulli(\theta) and \theta \sim beta(\alpha,\beta). Wikipedia has a lovely page listing lots of common conjugate priors [here](https://en.wikipedia.org/wiki/Conjugate_prior). You can also calculate them yourself. In your case, Beta is a conjugate prior for the Bernoulli so the posterior form is quite simple: \theta \mid y \sim beta(\alpha + \sum\_i^n{y\_i},\beta + n - \sum\_i^n{y\_i}). You don’t need to do any Stan modelling for that. If you want to do it for several coins, I’d suggest just doing it for each coin category separately.

If on the other hand you want to create a model which enables you to guess \theta given a set of factors, you can turn your categorical factors (weight, size, etc) into dummy variables (i.e. 0 or 1 for every possible value) and then do a standard logistic regression; treating \theta as your response variable and your dummy variable factors as your binary predictors.

For example, let’s take just weight as a factor. Say it can have 3 values. We’d thus turn weight into 3 binary valued vectors w\_1,w\_2,w\_3, let o be the binary outcome. We model o \sim bernoulli\_logit(c+ \alpha w\_1 + \beta w\_2 + \gamma w\_3). The posterior parameters c, \alpha, \beta, \gamma then inform how to estimate P(o=1\mid w) = \theta for any given combination.

E.g. (untested)

```stan
data {
	int <lower = 1> N;
        int<lower=0,upper=1> o[N];
        int<lower=0,upper=1> w1[N];
        int<lower=0,upper=1> w2[N];
        int<lower=0,upper=1> w3[N];
}
parameters {
	real<lower = 0> c;
	real<lower = 0> alpha;
	real<lower = 0> beta;
	real<lower = 0> gamma;
}
model {
  for (n in 1:N)
    o[n] ~ bernoulli_logit(c + alpha*w1[n] + beta*w2[n] + gamma*w3[n]);
}

```
