Dynamic (Template) Values for Prior and probabilities in Bayesian platform

Impressive, and more than I can achieve.
Looking through the code I had a couple of questions

  1. In the for loop, why do you set evidence=True are you ignoring negative observations?
  2. Why do product *= 1 - posterior? Why take the inverse of the posterior and then inverse it again.
  3. Why not just keep updating the posterior by replacing it for the prior like
    posterior = prior
    for e in contributing_entities:
        posterior = bayesian_probability(
            prior=posterior,
            prob_given_true=e.likelihood.prob_given_true,
            prob_given_false=e.likelihood.prob_given_false,
            evidence=True,
            decay_factor=e.decay.decay_factor if e.decay.is_decaying else 1.0,)

I did want to test some “truer” methods, the reason it was done like that is it provides a complementary probability and tends towards a larger jump when a significant entity has evidence.

That being said i have added two other methods closer to your suggestion in the latest pre-release: Releases · Hankanman/Area-Occupancy-Detection · GitHub

If you want to test it’d be appreciated to see your results, essentially its the same as the release version with two other methods included for the probability that are exposed as attributes on the Occupancy Probability sensor, you can then see how the three methods change the outcome in real time, note you may need to use the developer states view to see the detail (more decimal places) as the frontend rounds it up.

My next issue is adjusting the prior calculation as I am in some cases ending up with priors that are too low to register a significant difference when evidence is found. As a result, ill next be implementing time of day based prior modelling, essentially we will have half hour slots throughout a day each with their own prior. This will then better support the evidence at a given time.

1 Like

I cannot thank you enough for making this available. I truly appreciate the thought and work you have put into this.

Some years ago I created a similar solution in app daemon which basically periodically regenerated the yaml for my Bayesian sensors.

The history came from the recorder initially, but I switched that to long term statistics later on.

The app created yaml for some additional binary sensors based on the LTS which determined the expectation of occupancy for as an example, last Wednesday and the last 6 Wednesdays as well as sensors that took kids school holidays into consideration.

These sensors were added as observations to the main Bayesian sensor.

I also associated people with one or more areas and used those relationships to infer a greater probability of area occupancy when those people were home.

I had a slew of other arbitrary sensors that weighted the probability such as gradually increasing/decreasing the chance of an area being occupied between 10 pm and 6 am if that room had been tagged as a sleep room (bedroom)

Needless to say. It brought my Rpi4 to its knees but when it worked it did so with incredible accuracy to the extent that it almost never shut off everything in a room that was occupied and mostly shut down a room within 5-10 minutes after it was vacated.

I will certainly be looking at giving this a test run on the weekend.

Thanks again.

Btw. It was your published configuration with its trend sensors that inspired me to start on the aforementioned project.