Here are the facts for the question:

  • you dont know the phone number but have an idea for a guess is is XYZ
  • if correct about number will get busy signal
  • if incorrect probability of busy signal is 1 in 100
  • however the above is based on phone working
  • if phone is broken, you will always hear a busy signal

and (the bit im not so keen on because im not sure how anyone would ever come up with these) here are our prior probabilities:

Hypothesis Description Prior Probability
H_1 Phone Working and Number Correct 0.4
H_2 Phone Working and Number Wrong 0.4
H_3 Phone Broken and Number Correct 0.1
H_4 Phone Broken and Number Wrong 0.1

report.html

In the example we get a busy signal.

DataFrame 1

  prior likelihood marginal_likelihood_i_think posterior
0 0.400000 1.000000 0.400000 0.662252
1 0.400000 0.010000 0.004000 0.006623
2 0.100000 1.000000 0.100000 0.165563
3 0.100000 1.000000 0.100000 0.165563
Total 1.000000 3.010000 0.604000 1.000000

considering a few things

The prior probability the phone is working:

\[P(\text{phone working}) = P(H_1 \lor H_2)\]

The posterior probability the probability the phone is working given data line is busy:

\[P(\text{phone working} | \text{line busy} ) = P(H_1 | \text{line busy}) + P(H_2 | \text{line busy})\]
  • the prior probability the phone is working is 0.8
  • but with additional information that the line is busy
  • revised estimate is 0.6689

code.py


from jinja2 import Template
import pandas as pd



# Step 1: Create DataFrames

df1 = pd.DataFrame({'prior': [0.4, 0.4, 0.1, 0.1]})
df1['likelihood'] = [1, 0.01, 1, 1]
df1['marginal_likelihood_i_think'] = df1['prior'] * df1['likelihood']
df1['posterior'] = None
# Calculate totals
totals = df1.sum().to_frame().T
totals.index = ['Total']

# Append totals to the DataFrame
df1 = pd.concat([df1, totals])
df1['posterior'] = df1['marginal_likelihood_i_think'] / df1.loc['Total']['marginal_likelihood_i_think']


def highlight_total_row(row):
    if row.name == "Total":
        return ['font-weight: bold; border-top: 2px solid black;' for _ in row]
    return ['' for _ in row]



def hide_cells(row):
    styles = ['' for _ in row]
    if row.name == "Total":
        # Hide column "B" in total row
        styles[row.index.get_loc("likelihood")] = "color: transparent;"
        styles[row.index.get_loc("prior")] = "color: transparent;"
    return styles

styled = df1.style.apply(highlight_total_row, axis=1).apply(hide_cells, axis=1)










html = styled.to_html(index = False)








# Step 2: Define HTML Template
html_template = """
    <h2>DataFrame 1</h2>
    
"""

# Step 3: Render HTML
template = Template(html_template)
html_output = template.render(html=html)

# Step 4: Save or Display HTML
with open('report.html', 'w') as f:
    f.write(html_output)


shell.nix



{ pkgs ? import <nixpkgs> {} }:

pkgs.mkShell {
  buildInputs = [
    pkgs.python3
    pkgs.python3Packages.pandas
    pkgs.python3Packages.jinja2
    pkgs.python3Packages.ipython
  ];
}




This site uses Just the Docs, a documentation theme for Jekyll.