currently I am trying to implement a prototype for the following problem. I have data for machines, which sends me how long they have operated in seconds. Further, they have one sensor, which might have a value. So it would look like this

Duration Sensor Value
37       -      -
31       se1    A
12       -      -
29       se1    A
140      se1    A,B,C

normally, I would expect the sensor to have small variation, but the longer the duration is, the more variation would be expected. In my toy example, I would expect my sensor se1 to have 1 value for average duration, but it would be ok to have 3 distinct values if the duration is significantly longer.

Now, I would like to model it as a Bayesian problem

X := number of distinct values for sensor se1 
Y := duration length in seconds

P(X = x | Y = y) would be my inference such as "how probable is it to get 3 distinct values for a duration of 140 seconds?"

My approach is

  1. from the full dataset estimate P(X) e.g. via scipy.fit()
  2. from the full dataset estimate P(Y) e.g. via scipy.fit()
  3. now filter the dataset, such that only observations of se1 are in the filtered set. Consider it as evidence and estimate P(Y | X) from it.
  4. use Bayes Theorem to calculate P(X | Y)

I am not so sure about the 3)

Do I have to filter for "se1 present" or do I have to filter for "se1 has 1 distinct value" then fit, "se1 has 2 distinct values" and fit again, etc.?


Following the ideas from the answer below I have looked into the relationship of duration and occurrence of values: enter image description here It is clear that a certain duration is required for higher count of distinct values, but it is not the linear relationship as proposed (I know only as example) in the answer.

Any ideas how I could model this?

  • $\begingroup$ I'm not sure what you mean by "I would expect the sensor to have small variation" - you only have one sensor measuring a particular duration value, there is no measure of variation for that value. Are all of the machines supposed to have the same runtime? Also, I don't think it's possible to answer the question of "how likely is it to get distinct values" without knowing anything about how many data points you have. For a very small dataset, you're very likely to get all unique values, but with arbitrarily large data, you'll almost surely get none at all. $\endgroup$ Jun 2 at 16:46

1 Answer 1


It sounds like your end-goal is to come up with a pmf $P(X|Y)$, i.e. for any signal duration Y, what are the probabilities that you get 0 values, 1 value, 2 values, etc.

The only reason to invoke Bayes' theorem for this analysis is if for some reason you know the functional form of $P(Y|X)$. I.e., given the number of distinct values for sensor 1, what is the conditional pdf of signal duration. It doesn't sound to me like you know this.

To that end, I would suggest you just try to model $P(X|Y)$ directly. Seeing as X is an integer which is likely to take a relatively small value, you could (just throwing ideas out) try to model it as a conditional Poisson distribution, that is to say that $X_{i} \sim Poisson(\lambda(Y_{i}))$ and then you'll need to come up with some sensible model for $\lambda$ as a function of Y. You might think that you'd on average expect the number of distinct signals to be proportional to the length of the signal in which case you'd model it as a simple linear. Or you might think it would be some sort of diminishing return and you could model it as a power law or a logarithm.

To work through the simple case (linear without intercept), your log-likelihood would look like

$\sum_{i=1}^{N}\ln \left[\frac{(a\cdot y_{i})^{x_{i}}e^{-a\cdot y_{i}}}{x_{i}!} \right]=\sum_{i=1}^{N}x_{i}\ln a - ay_{i}+ consts$

and you can now use gradient descent or some other optimisation algorithm to find the maximum of this wrt a

The key take-home here is that using Bayes' theorem only helps when you have some information about the inverse problem. For example, if you toss a coin 10 times and it comes up heads 6 times and you want to know the inherent probability of that coin coming up heads, you have more information about the inverse problem. If you knew the inherent probability of a coin coming up heads, then you would know (via the binomial distribution) the probability of seeing 6 heads out of 10 tries. So inverting the problem is helpful.

In your case though, you don't have any inherent understanding of how the number of distinct values depends on the signal duration or how the signal duration depends on the number of distinct values (as far as I can tell anyway), so framing one in terms of the other won't help you/is circular. To make progress, unless you have tonnes of data, or Y doesn't have many distinct values relative to the amount of data you have, you won't be able to model $P(X|Y)$ non-parametrically, so you'll have to make some parametric assumptions about the functional form of the distribution of X|Y, and then you can estimate those parameters via maximum-likelihood (or even calculate the full Bayesian posterior on those parameters)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.