---
title: "Using Bayes Rule with Forecasting"
author: "Aditya Mehrotra"
date: "2024-02-14"
output: html_document
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE,
                      error = TRUE, fig.height = 4)
library(tidyverse)
library(lubridate)
```

## Overview
-- The following document will use R to implement the Bayes Rule with Forecasting on a given Situation

## Situation
Let's assume that Madison, Wisconsin gets about 115 days of precipitation per year. Suppose that when there is precipitation, the forecast on Channel 3000 is correct 93% of the time and when there is no precipitation in the city, they are correct 87% of the time.

## Question 1
### How many days will Channel 3000 correctly forecast in a non-leap year (365 days)

``` {r correct_pred_reg_year}
precipitation_correct_odds = 0.93
nothing_correct_odds = 0.87

precipitation_days = 115
nothing_days = 365 - precipitation_days

correct_forecast = (precipitation_days * precipitation_correct_odds) + (nothing_days * nothing_correct_odds)
round(correct_forecast)
```

#### Math
Correct Forecast = (Odds Correct for Precipitation) * (Days there is Precipitation) + (Odds Correct for Non-Precipitation) * (Days there is No Precipitation)

- Odds Correct for Precipitation = 0.93
- Days there is Precipitation = 115
- Odds Correct for Non-Precipitation = 0.87
- Days there is No Precipitation = 250

Correct Forecast = (0.93) * (115) + (0.87) * (250)

#### Answer
Correct Forecast = 324 Days

## Question 2
### If Channel 3000 predicts precipitation in Madison, what is the likelihood that there is actually precipitation?

```{r channel_3000_precipitation_pred}
forecast_pred_precipitation = ((precipitation_days / 365) * precipitation_correct_odds) + ((nothing_days / 365) * (1 - nothing_correct_odds))

pred_precipitation = ((precipitation_days / 365) * precipitation_correct_odds) / forecast_pred_precipitation

pred_precipitation
```

#### Math
Precipitation Prediction = (((Days of Precipitation) / (Total Days in a Year)) * (Odds of Correct Prediction of Precipitation)) + (((Days of No Precipitation) / (Total Days in a Year)) * (1 - Odds of Correct Prediction of No Precipitation)))

- Days of Precipitation = 115
- Total Days in a Year = 365
- Odds of Correct Prediction of Precipitation = 0.93
- Days of No Precipitation = 365 - Days of Precipitation = 365 - 115 = 250
- (1 - Odds of Correct Prediction of No Precipitation) = (1 - 0.87) = 0.13

Precipitation Prediction = (115/365) * 0.93 + (250/365) * 0.13

#### Bayes Rule
P(Rain|Forecast Rain) = (P(Forecast Rain|Rain) * P(Rain)) / (P(Forecast Rain))

#### Answer
Precipitation Prediction = 76.69%

## Question 3
Let's assume that the Channel 3000 forecast was correct yesterday. This increases the likelihood of Channel 3000's forecast for today. Specifically, if the forecast was correct yesterday, then the probability of Channel 3000 accurately predicts if there is precipitation 93% of the time, and 87% of the time when there is no precipitation. If the forecast was correct on January 29th, what is the probability it was correct on January 28th? Assume that the forecast was incorrect on the 27th.

```{r}
prob_precipitation = 115 / 365
prob_no_precipitation = 1 - prob_precipitation

precipitation_accuracy_new = 0.98
no_precipitation_accuracy_new = 0.92

precipitation_accuracy_old = 0.93
no_precipitation_accuracy_old = 0.87

prob_28_correct = (prob_precipitation * precipitation_accuracy_old) + (prob_no_precipitation * no_precipitation_accuracy_old)
prob_29_correct_28_correct = (prob_precipitation * precipitation_accuracy_new) + (prob_no_precipitation * no_precipitation_accuracy_new)
prob_29_correct_28_false = (prob_precipitation * precipitation_accuracy_old) + (prob_no_precipitation * no_precipitation_accuracy_old)

prob_29_correct = (prob_28_correct * prob_29_correct_28_correct) + ((1 - prob_28_correct) * prob_29_correct_28_false)

prob_28_correct_29 = ((prob_29_correct_28_correct * prob_28_correct) / prob_29_correct)
prob_28_correct_29
```
