Skip to content

Commit

Permalink
reload
Browse files Browse the repository at this point in the history
  • Loading branch information
DragonflyStats committed Oct 24, 2023
1 parent b746516 commit 02bcefb
Show file tree
Hide file tree
Showing 11 changed files with 1,018 additions and 405 deletions.
149 changes: 149 additions & 0 deletions Probability/Gamblers_Ruin.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
---
title: "Gambler's Ruin"
subtitle: Simulations with R
author: DragonflyStats.github.io
output:
prettydoc::html_pretty:
theme: cayman
highlight: github
---


\section{Gambler's Ruin}

Consider a gambler who starts with an initial fortune of \$1 and then on each successive gamble
either wins \$1 or loses \$1 independent of the past with probabilities p and q = 1-p respectively.

Suppose the gambler has a starting kitty of A.
This gamblers places bets with the “Banker”, who has an initial fortune B. We will look at the game from the perspective of the gambler only.
The Banker is, by convention, the richer of the two.

\begin{itemize}
\item Probability of successful gamble for gambler : p
\item Probability of unsuccessful gamble for gambler : q (where q = 1 - p )
\item Ratio of success probability to failure success: $s = p / q$
\item Conventionally the game is biased in favour of the Banker (i.e. $q>p$ and $s<1$)
\end{itemize}

Let $R_n$ denote the Gambler’s total fortune after the $n-$th gamble.

If the Gambler wins the first game, his wealth becomes $R_n =A+1$.
If he loses the first gamble, his wealth becomes $R_n = A-1$.
The entire sum of money at stake is the “Jackpot” i.e. $A+B$.
The game ends when the Gambler wins the Jackpot ($R_n = A+B$) or loses everything ($R_n = 0$).


### Simulation a Single Gamble}


To simulate one single bet, compute a single random number between 0 and 1.
```{r}
runif(1)
```


Lets assume that the game is biased in favour of the Banker
p = 0.45 , q = 0.55.
If the number is less than 0.45, the gamble wins. Otherwise the Banker wins.


> runif(1)
[1] 0.1251274
>#Gambler Loses
>
> runif(1)
[1] 0.754075
>#Gambler wins
>
> runif(1)
[1] 0.2132148
>#Gambler Loses
>
> runif(1)
[1] 0.8306269
```
%----------------------------------------------------------------------%
\newpage
Let A be the Gambler's Kitty at the start of the gambling.
Let B be the Banker's Wealth.
The probability of A winning a gamble is p.
The vector $R_n$ records the gambler's worth on an ongoing basis. At the start, The first value is A.
```{r}
A=20;B=100;p=0.47
Rn=c(A)
probval = runif(1)
if (probval < p)
{
A = A+1; B =B-1
}else{A=A-1;B=B+1}
#Save the values from each bet
Rn=c(Rn,A)
```

\newpage
Should the Gambler win the entire jackpot (A+B). The game would also cease. We include a \textbf{\texttt{break}} statement to stop the loop if the gambler wins the entire jackpot. A break statement will stop a loop if a certain logical condition is met.

```{r}
A=20;B=100;p=0.47
Rn=c(A)
Total=A+B
while(A>0)
{
UnifVal=runif(1)
if(UnifVal <= p)
{
A = A+1; B =B-1
}else{A=A-1;B=B+1}
Rn=c(Rn,A)
if(A==Total){break}
}
```

We can construct a plot to depict the gambler's ongoing fortunes in the game. We use a \texttt{for} loop, that implements the game, recording the duration of the game each time. The duration of each game is the dimension of the \texttt{Rn} vector, i.e. \texttt{length(Rn)}.


```{r}
A.ini=20;B=100;p=0.47
M=1000
RnDist=numeric()
Total=A+B
for (i in 1:M)
{
Rn=numeric()
Rn[1]=A
A=A.ini
while(A>0)
{
UnifVal=runif(1)
if(UnifVal <= p)
{
A = A+1; B =B-1
}else{A=A-1;B=B+1}
Rn=c(Rn,A)
if(A==Total){break}
}
RnDist=c(RnDist,length(Rn))
}
```

### Distribution of Durations}
We can use for loop to get a sense of the duration (i.e. the number of rounds a game will last).
\newpage
\end{document}

206 changes: 206 additions & 0 deletions Probability/Gamblers_Ruin.html

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
---
title: "Untitled"
title: "The Monty Hall Problem"
susbtitl: Simulations with R
author: DragonflyStats.github.io
output:
html_document:
toc: true
fig_caption: true
prettydoc::html_pretty:
theme: cayman
highlight: github
---


Expand Down Expand Up @@ -208,143 +210,3 @@ The output of this program would look like this. The proportion is approximately
> SwitchCount
[1] 677
```
%---------------------------------------------------------------%
\section{Gambler's Ruin}
Consider a gambler who starts with an initial fortune of \$1 and then on each successive gamble
either wins \$1 or loses \$1 independent of the past with probabilities p and q = 1-p respectively.
Suppose the gambler has a starting kitty of A.
This gamblers places bets with the “Banker”, who has an initial fortune B. We will look at the game from the perspective of the gambler only.
The Banker is, by convention, the richer of the two.
\begin{itemize}
\item Probability of successful gamble for gambler : p
\item Probability of unsuccessful gamble for gambler : q (where q = 1 - p )
\item Ratio of success probability to failure success: $s = p / q$
\item Conventionally the game is biased in favour of the Banker (i.e. $q>p$ and $s<1$)
\end{itemize}
Let $R_n$ denote the Gambler’s total fortune after the $n-$th gamble.
If the Gambler wins the first game, his wealth becomes $R_n =A+1$.
If he loses the first gamble, his wealth becomes $R_n = A-1$.
The entire sum of money at stake is the “Jackpot” i.e. $A+B$.
The game ends when the Gambler wins the Jackpot ($R_n = A+B$) or loses everything ($R_n = 0$).
### Simulation a Single Gamble}
To simulate one single bet, compute a single random number between 0 and 1.
```{r}
runif(1)
```


Lets assume that the game is biased in favour of the Banker
p = 0.45 , q = 0.55.
If the number is less than 0.45, the gamble wins. Otherwise the Banker wins.


> runif(1)
[1] 0.1251274
>#Gambler Loses
>
> runif(1)
[1] 0.754075
>#Gambler wins
>
> runif(1)
[1] 0.2132148
>#Gambler Loses
>
> runif(1)
[1] 0.8306269
```
%----------------------------------------------------------------------%
\newpage
Let A be the Gambler's Kitty at the start of the gambling.
Let B be the Banker's Wealth.
The probability of A winning a gamble is p.
The vector $R_n$ records the gambler's worth on an ongoing basis. At the start, The first value is A.
```{r}
A=20;B=100;p=0.47
Rn=c(A)
probval = runif(1)
if (probval < p)
{
A = A+1; B =B-1
}else{A=A-1;B=B+1}
#Save the values from each bet
Rn=c(Rn,A)
```

\newpage
Should the Gambler win the entire jackpot (A+B). The game would also cease. We include a \textbf{\texttt{break}} statement to stop the loop if the gambler wins the entire jackpot. A break statement will stop a loop if a certain logical condition is met.

```{r}
A=20;B=100;p=0.47
Rn=c(A)
Total=A+B
while(A>0)
{
UnifVal=runif(1)
if(UnifVal <= p)
{
A = A+1; B =B-1
}else{A=A-1;B=B+1}
Rn=c(Rn,A)
if(A==Total){break}
}
```

We can construct a plot to depict the gambler's ongoing fortunes in the game. We use a \texttt{for} loop, that implements the game, recording the duration of the game each time. The duration of each game is the dimension of the \texttt{Rn} vector, i.e. \texttt{length(Rn)}.


```{r}
A.ini=20;B=100;p=0.47
M=1000
RnDist=numeric()
Total=A+B
for (i in 1:M)
{
Rn=numeric()
Rn[1]=A
A=A.ini
while(A>0)
{
UnifVal=runif(1)
if(UnifVal <= p)
{
A = A+1; B =B-1
}else{A=A-1;B=B+1}
Rn=c(Rn,A)
if(A==Total){break}
}
RnDist=c(RnDist,length(Rn))
}
```

### Distribution of Durations}
We can use for loop to get a sense of the duration (i.e. the number of rounds a game will last).
\newpage
\end{document}

250 changes: 250 additions & 0 deletions Probability/PREPAREA/Monty_Hall_Problem.html

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: Document
title:
username:
account: rpubs
server: rpubs.com
hostUrl: rpubs.com
appId: https://api.rpubs.com/api/v1/document/1104051/9fc3555f99504a54b62bbc8adaf7d53b
bundleId: https://api.rpubs.com/api/v1/document/1104051/9fc3555f99504a54b62bbc8adaf7d53b
url: http://rpubs.com/publish/claim/1104051/ca2a8a184dd94c818f5a6e64654bf1e2
when: 1698175835.74178
lastSyncTime: 1698175835.74178
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
---
title: "Poisson Regression - Zero-Inflated Poisson regression"
output: html_document
susbtitl: The {vcd} R Package
author: DragonflyStats.github.io
output:
prettydoc::html_pretty:
theme: cayman
highlight: github
---



```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(knitr)
Expand Down Expand Up @@ -33,30 +40,12 @@ head(fishing)
```


<table>
<thead><tr><th>X</th><th>nofish</th><th>livebait</th><th>camper</th><th>persons</th><th>child</th><th>xb</th><th>zg</th><th>count</th></tr></thead>
<tbody>
<tr><td>1 </td><td>1 </td><td>0 </td><td>0 </td><td>1 </td><td>0 </td><td>-0.8963146</td><td> 3.0504048</td><td>0 </td></tr>
<tr><td>2 </td><td>0 </td><td>1 </td><td>1 </td><td>1 </td><td>0 </td><td>-0.5583450</td><td> 1.7461489</td><td>0 </td></tr>
<tr><td>3 </td><td>0 </td><td>1 </td><td>0 </td><td>1 </td><td>0 </td><td>-0.4017310</td><td> 0.2799389</td><td>0 </td></tr>
<tr><td>4 </td><td>0 </td><td>1 </td><td>1 </td><td>2 </td><td>1 </td><td>-0.9562981</td><td>-0.6015257</td><td>0 </td></tr>
<tr><td>5 </td><td>0 </td><td>1 </td><td>0 </td><td>1 </td><td>0 </td><td> 0.4368910</td><td> 0.5277091</td><td>1 </td></tr>
<tr><td>6 </td><td>0 </td><td>1 </td><td>1 </td><td>4 </td><td>2 </td><td> 1.3944855</td><td>-0.7075348</td><td>0 </td></tr>
</tbody>
</table>




```{r}
## histogram with x axis
ggplot(fishing, aes(count)) + geom_histogram()
```

`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.






Expand All @@ -66,12 +55,6 @@ ggplot(fishing, aes(count)) + geom_histogram()
ggplot(fishing, aes(count)) + geom_histogram() + scale_x_log10()
```

`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
Warning message:
"Removed 142 rows containing non-finite values (stat_bin)."




```{r}
## Run a Zero-Inflated Poisson Regression Analysis to predict number of fish caught
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: Document
title:
username:
account: rpubs
server: rpubs.com
hostUrl: rpubs.com
appId: https://api.rpubs.com/api/v1/document/1104046/5e16dd3951c6407faab922c4597345a2
bundleId: https://api.rpubs.com/api/v1/document/1104046/5e16dd3951c6407faab922c4597345a2
url: http://rpubs.com/publish/claim/1104046/93a72405f50c43f0bbbfa41b8bd33469
when: 1698175575.65415
lastSyncTime: 1698175575.65415
Loading

0 comments on commit 02bcefb

Please sign in to comment.