```python
from sympy import *
from math import factorial
```

# Discrete Random Variables


```python
"""
Definition:
The cumulative distribution function (CDF), F(·), of a random
variable, X, is defined by

F(x) := P(X ≤ x).

"""
```


```python
#Exemplo: Jogar dado
x = (1,2,3,4,5,6)
wp = 1/len(x)
```


```python
"""
Definition:
A discrete random variable, X, has probability mass function (PMF),
p(·), if p(x) ≥ 0 and for all events A we have
P(X ∈ A) = X x∈A p(x).

"""
```


```python
#Probabilidade de ser maior ou igual a 4 P(x=>4)
px=0
seq = ''
print('Probabilidade de ser maior ou igual a 4 P(x=>4)', x[3:])
for i in x[3:]:
    px = px+wp
    seq = seq + ' ' + str(i)
    print('probabilidade: ', round(px, 2), ' valor', seq)
```

    Probabilidade de ser maior ou igual a 4 P(x=>4) (4, 5, 6)
    probabilidade:  0.17  valor  4
    probabilidade:  0.33  valor  4 5
    probabilidade:  0.5  valor  4 5 6



```python
"""
Definition:
The expected value of a discrete random variable, X, is given by
E[X] := SUM xi p(xi).

"""
```


```python
#Para o mesmo caso, o valor esperado seria:
Ex = 0
for i in x:
    Ex = Ex + wp*i
print('O valor esperado E(x) é de:', Ex)
```

    O valor esperado E(x) é de: 3.5



```python
"""
Definition. The variance of any random variable, X, is defined as
Var(X) := E[(X − E[X])2]
        = E[X2] − E[X]2

"""
```


```python
#Obtendo a variancia para o caso

varx = 0
Ex2 = 0

for i in x:
    Ex2 = Ex2 + wp*i**2

varx = Ex2 - Ex**2

print("A variancia para de um dado é de:", round(varx, 2))
```

    A variancia para de um dado é de: 2.92


# The Binomial Distribution


```python
"""

We say X has a binomial distribution, or X ∼ Bin(n, p), if
P(X = r) = (n r)p**r(1 − p)**n−r

For example, X might represent the number of heads in n independent coin
tosses, where p = P(head). The mean and variance of the binomial distribution
satisfy
E[X] = np
Var(X) = np(1 − p).

"""
```


```python
"""
(n r) = n!/(r!(n-r)!)
"""
```

### A Financial Application


```python
"""

Suppose a fund manager outperforms the market in a given year with
probability p and that she underperforms the market with probability 1 − p.
She has a track record of 10 years and has outperformed the market in 8 of
the 10 years.
Moreover, performance in any one year is independent of performance in
other years.
Question: How likely is a track record as good as this if the fund manager had no
skill so that p = 1/2?
Answer: Let X be the number of outperforming years. Since the fund manager
has no skill, X ∼ Bin(n = 10, p = 1/2) and
P(X ≥ 8) = Xnr=8(n r)p**r(1 − p)**n−r

Question: Suppose there are M fund managers? How well should the best one do
over the 10-year period if none of them had any skill?

"""
```


```python
#Resolvendo a questão a cima, temos:
n=10
p=1/2
r=8

Px = (factorial(n)/(factorial(r)*factorial(n-r)))*p**(r)*(1-p)**(n-r)

print('A probabilidade é de:', round(Px*100, 1), '%')


```

    A probabilidade é de: 4.4 %


# The Poisson Distribution


```python
"""
We say X has a Poisson(λ) distribution if
P(X = r) = λ**(r)*e**(-λ)/r!

E[X] = λ and Var(X) = λ

"""
```

# Bayes’ Theorem


```python
"""
Let A and B be two events for which P(B) 6= 0. Then
P(A | B) = P(ATB)/P(B)
         = P(B | A)P(A)/P(B)
         = P(B | A)P(A)/(SUM P(B | Aj)P(Aj))
where the Aj’s form a partition of the sample-space.

"""
```


```python
"""
Let Y1 and Y2 be the outcomes of tossing two fair dice independently of
one another.

Let X := Y1 + Y2. Question: What is P(Y1 ≥ 4 | X ≥ 8)?

"""
```


```python
#Resolvendo a questão a cima, temos:

y1 = (1,2,3,4,5,6)
y2 =(1,2,3,4,5,6)

wp=1/6

for i in x[3:]:
    pa = pa+wp


```

# Continuous Random Variables


```python
"""
Definition. A continuous random variable, X, has probability density function
(PDF), f(·), if f(x) ≥ 0 and for all events A


"""
```


```python

```

# The Normal Distribution


```python

```

6dd4e5721ddb6e000f8e25e79019951687dd25ed
ipynb
Jupyter Notebook
Financial Engineering & Risk Management/Introduction to Financial Engineering and Risk Management/probability(I).ipynb
MaikeRM/FinancialEngineering
d5881995ff3097e77cb62633ab22d25625c81ee7
MIT
Financial Engineering & Risk Management/Introduction to Financial Engineering and Risk Management/probability(I).ipynb
MaikeRM/FinancialEngineering
d5881995ff3097e77cb62633ab22d25625c81ee7
MIT
Financial Engineering & Risk Management/Introduction to Financial Engineering and Risk Management/probability(I).ipynb
MaikeRM/FinancialEngineering
d5881995ff3097e77cb62633ab22d25625c81ee7
MIT
Qwen/Qwen-72B
1. YES
2. YES