기본 콘텐츠로 건너뛰기

pandas_ta를 적용한 통계적 인덱스 지표

Continuous Possibility Distribution:PDF

Contents

  1. Probability Density Function(PDF)

Continuous Possibility Distribution

If the probability of randomly selecting a number in a certain interval [a, b] is the same, the number becomes a random variable, and since the number of the interval is infinite, a single point cannot be specified. In other words, the probability at a particular point in a continuous variable cannot be defined. Instead, if intervals with equal probabilities are grouped, the probability of selecting a group can be defined as the length of that selected portion over the length of the entire interval. This relationship can be expressed as Equation 1.

$$\begin{align}\tag{1} P(X \in [a, b])&=1\\ P(X \in [x_1, x_2])& \varpropto (x_2-x_1)\\&= \frac{x_2-x_1}{b-a}\\ a\le x_1 \le x_2\le b \end{align}$$

Based on the above expression, the cumulative distribution function (CDF) for the random variable X is written as Equation 2.

$$\begin{align}\tag{2} F(X)=\begin{cases} 0 & \quad \text{for}\; x\le a\\ \frac{x-a}{b-a} & \quad \text{for}\; a \le x\le b\\ 1 & \quad \text{for}\; x\ge b \end{cases} \end{align}$$

In fact, in the case of a continuous variable, the probability at a point cannot be defined, so the difference between the symbols '≤' and '<' cannot be defined either.

If F(x) is a continuous function for all x, then the random variable X with the cumulative distribution function F(x) can be said to be continuous.

$$F(x)=P(X \le x)$$

In other words, the cumulative distribution function, F(x), is a function that is differentiable over all ranges.

Probability Density Function(PDF)

The probability of each case in the discrete probability function can be calculated with the probability mass function (PMF) and the cumulative distribution function (CDF). Similarly, a CDF can be defined for a continuous probability function. However, for a continuous variable whose point cannot be defined, a probability mass function (PMF) cannot be specified. In other words, it is difficult to specify any part of a continuous variable due to the infinity of its range. Therefore, in the case of a continuous variable, it is possible to replace the probability mass function of a discrete variable by calculating the probability of a range including that point, not a specific point. Such a function is called a probability density function and can be mathematically calculated using the concept of differentiation as follows.

$$\begin{align}f(x)&=\lim_{\Delta \to 0}\frac{P(x \lt X \le x+\Delta)}{\Delta}\\&=\lim_{\Delta \to 0}\frac{F(x+\Delta)-F(x)}{\Delta}\\&=\frac{dF(x)}{dx}=F^\prime(x) \\\because \; & P(x \lt X \le x+\Delta)=P(X \le x+\Delta)-P(X \lt x)\\&=F(x+\Delta)-F(x) \end{align}$$
The probability density function (PDF) of a continuous random variable X with the cumulative distribution function CDF is defined as the derivative of the CDF as shown in Equation 3. $$\begin{equation}\tag{3} f(x)=\frac{dF(x)}{dx} \end{equation}$$

The PDF above can be obtained by differentiating the CDF. Conversely, it means that CDF can be computed as the integral of PDF. If $\Delta$ is a very small value, the cumulative probability in a certain interval can be expressed as Equation 4.

$$\begin{align}\tag{4} &\begin{aligned} F(x)&=P(x \le X \le x+\delta) \varpropto f(x)\delta\\ &=\int^{ x+\delta}_{x}f(x)\delta \end{aligned}\\ &\begin{aligned}P(a \le x \le b)&=F(b)-F(a)\\&=\int^b_a f(x)dx\end{aligned} \\ & \int ^\infty_{-\infty} f(x)dx =1\end{align}$$

Example 1)
Assume a continuous random variable with the following PDF.

$$ f(x) =\begin{cases} ce^x& \quad x \ge 0\\ 0 & \quad \text{otherwise} \end{cases}$$

1) Determine c
It can be easily determined by integrating the above PMF. This problem requires integration and solving the equation. This process uses sympy's integrate(), Eq() and solve() functions. coded.

import numpy as np
import pandas as pd 
from sympy import *
import matplotlib.pyplot as plt
from scipy import stats 
c, x=symbols("c x")
f=c*exp(-x)
F=f.integrate((x, 0, oo ))
F
c
solve(Eq(F, 1), c)
[1]

2) $\displaystyle F(x)=\int^x_0e^{-x}\,dx$?

F=integrate(exp(-x),(x, 0, x))
F
$\quad \color{blue}{\displaystyle 1 - e^{- x}}$

3) $\displaystyle P(1 \lt X \lt 3)=F(3)-F(1)=\int^3_1 e^{-x}\,dx$

F=integrate(exp(-x),(x, 1, 3))
F
$\quad \color{blue}{\displaystyle - \frac{1}{e^{3}} + e^{-1}}$
N(F, 4)
0.3181

Example 2)
In a continuous random variable X having the following probability density function in all intervals

$$\displaystyle f(x)=\frac{e^{-|x|}}{2}$$

P(X ≤ 2)?

$$\begin{aligned} P(X \le 2) &=F(2)\\ &= \int^2_{-\infty} \frac{e^{-|x|}}{2} \, dx \end{aligned}$$

The above integration can be executed using sympy's integrate() function. However, this module function returns an expression instead of a number if the upper or lower bound of the integral is infinity. Therefore, the lower bound is computed with a very large value instead of infinity.

x = symbols("x")
f=exp(-abs(x))/2
integrate(f, (x, -oo, 2))
$\quad \color{blue}{\displaystyle \frac{\int\limits_{-\infty}^{2} e^{- \left|{x}\right|}\, dx}{2}}$
Fless2=integrate(f, (x, -1000000, 2))
Fless2.evalf(4)
0.9323

The integrate() function of the scipy module can be applied more simply than the sympy function used in the code above. This function returns the result and margin of error.

import scipy as sp 
sp.integrate.quad(lambda x: 1/2*np.exp(-abs(x)), -np.inf, 2)
(0.9323323583816946, 2.2674850885806563e-10)

Example 3)
The CDF for a continuous random variable X where f(x)=x between $0 \le x \le 1$ and 0 elsewhere can be expressed as

$$F(X)=\begin{cases} 0 & \quad x<0\\x & \quad 0 \le x \le 1\\1 & \quad x \ge 1 \end{cases}$$

1) If a random variable is expressed as a function $y=g(x)=e^x$, the CDF of that variable can be expressed as

$$\begin{align} F(y)&=F(e^x)\\&=P(Y \le y)\\&=P(e^X \le y)\\&P(X \le \text{ln}(y))\\&=\text{ln}(y) \end{align}$$

According to this function, the range of the random variable Y can be expressed as [1, e] based on the range of X. Within the scope, the CDF can be organized as:

$$F(y)=\begin{cases} 0 & \quad y \lt 1\\ \ln(y) & \quad 1 \le y \le e\\1 & \quad y \ge e \end{cases}$$

2) The PDF of the random variable Y can be derived from the derivative of the CDF above.

$$\begin{align} f(y)&=F^\prime(y)\\&=\frac{d(\ln(y))}{dy}\\&=\frac{1}{y}, \quad 1 \le y \le e \end{align}$$

3) E(Y)?

Expected values can be calculated using random variables Y or X.

$$\begin{align} E(Y)&=\int^e_1 yf(y)\, dy\\&=\int^e_1y\frac{1}{y} \, dy \\&=e-1\\ \text{or}\\E(Y)&=E(e^x)\\&=\int^1_0 e^xF^\prime(x)\, dx\\&=e-1 \end{align}$$

The relationship between random variables Y, f(y), and E(Y) is visualized as follows.

rng=np.arange(1, np.exp(1)+1E-6, 0.1)
plt.figure(figsize=(8, 4))
p=[1/i for i in rng]
plt.plot(rng, p)
plt.axvline(x=np.exp(1)-1, color="red", label="mean")
plt.xlabel("Y", fontsize="13", fontweight="bold")
plt.ylabel("f(y)", fontsize="13", fontweight="bold")
plt.legend(loc="best")
plt.show()

Considering again the derived part of f(y) in the above example, it can be expressed as

$$\begin{align}&y=e^x=g(x) \rightarrow x=g^{-1}(y)\\ &\begin{aligned} F(y)&=P(Y \le y)\\&=P(g(X) \le y)\\&=P(X < g^{-1}(y))\\&=F(g^{-1}(y))\\ f(y)&=\frac{d}{dy}F(x)\\&=\frac{dx}{dy}\frac{d(F(x))}{dx}\\&=\frac{dx}{dy}f^\prime(x)\\&=\frac{f^\prime(x)}{\frac{dy}{dx}}\\&=\frac{f^\prime(x)}{g^\prime(x)} \end{aligned} \end{align}$$

Under the assumption that X is a continuous random variable and g is a differentiable function, if Y=g(X), then the probability density function of Y is Equation 4.

$$\begin{equation}\tag{4}f(y)=\begin{cases} \frac{f(x)}{|g^\prime(x)|}=f(x)\big{|}\frac{dx}{dy}\big{|} & \quad \text{in}\, g(x)=y \\ 0 & \quad \text{in}\, g(x) \neq y \end{cases}\end{equation}$$

Example 4)

The probability density function of the random variable x is:

$$f(x) = \begin{cases} 4x^3 \quad & 0\le x \le 1\\ 0 \quad & \text{otherwise} \end{cases}$$

PDF of another random variable $\displaystyle Y=\frac{1}{X}$ based on random variable X?

$$\begin{align} &\begin{aligned}Y&=g(x)\\&=\frac{1}{X}\\ f(y)&=\frac{f(x)}{|g^\prime(x)|}\\&=\frac{4x^3}{\big{|}-\frac{1}{x^2}\big{|}}\\&=4x^5\\&=\frac{4}{y^5}\end{aligned}\\ &\therefore f(y)=\begin{cases} \frac{4}{y^5}& \quad y \ge 1\\0 &\quad \text{otherwise} \end{cases} \end{align}$$

댓글

이 블로그의 인기 게시물

[Linear Algebra] 유사변환(Similarity transformation)

유사변환(Similarity transformation) n×n 차원의 정방 행렬 A, B 그리고 가역 행렬 P 사이에 식 1의 관계가 성립하면 행렬 A와 B는 유사행렬(similarity matrix)이 되며 행렬 A를 가역행렬 P와 B로 분해하는 것을 유사 변환(similarity transformation) 이라고 합니다. $$\tag{1} A = PBP^{-1} \Leftrightarrow P^{-1}AP = B $$ 식 2는 식 1의 양변에 B의 고유값을 고려한 것입니다. \begin{align}\tag{식 2} B - \lambda I &= P^{-1}AP – \lambda P^{-1}P\\ &= P^{-1}(AP – \lambda P)\\ &= P^{-1}(A - \lambda I)P \end{align} 식 2의 행렬식은 식 3과 같이 정리됩니다. \begin{align} &\begin{aligned}\textsf{det}(B - \lambda I ) & = \textsf{det}(P^{-1}(AP – \lambda P))\\ &= \textsf{det}(P^{-1}) \textsf{det}((A – \lambda I)) \textsf{det}(P)\\ &= \textsf{det}(P^{-1}) \textsf{det}(P) \textsf{det}((A – \lambda I))\\ &= \textsf{det}(A – \lambda I)\end{aligned}\\ &\begin{aligned}\because \; \textsf{det}(P^{-1}) \textsf{det}(P) &= \textsf{det}(P^{-1}P)\\ &= \textsf{det}(I)\end{aligned}\end{align} 유사행렬의 특성 유사행렬인 두 정방행렬 A와 B는 'A ~ B' 와 같...

유리함수 그래프와 점근선 그리기

내용 유리함수(Rational Function) 점근선(asymptote) 유리함수 그래프와 점근선 그리기 유리함수(Rational Function) 유리함수는 분수형태의 함수를 의미합니다. 예를들어 다음 함수는 분수형태의 유리함수입니다. $$f(x)=\frac{x^{2} - 1}{x^{2} + x - 6}$$ 분수의 경우 분모가 0인 경우 정의할 수 없습니다. 이와 마찬가지로 유리함수 f(x)의 정의역은 분모가 0이 아닌 부분이어야 합니다. 그러므로 위함수의 정의역은 분모가 0인 부분을 제외한 부분들로 구성됩니다. sympt=solve(denom(f), a); asympt [-3, 2] $$-\infty \lt x \lt -3, \quad -3 \lt x \lt 2, \quad 2 \lt x \lt \infty$$ 이 정의역을 고려해 그래프를 작성을 위한 사용자 정의함수는 다음과 같습니다. def validX(x, f, symbol): ① a=[] b=[] for i in x: try: b.append(float(f.subs(symbol, i))) a.append(i) except: pass return(a, b) #x는 임의로 지정한 정의역으로 불연속선점을 기준으로 구분된 몇개의 구간으로 전달할 수 있습니다. #그러므로 인수 x는 2차원이어야 합니다. def RationalPlot(x, f, sym, dp=100): fig, ax=plt.subplots(dpi=dp) # ② for k in x: #③ x4, y4=validX(k, f, sym) ax.plot(x4, y4) ax.spines['left'].set_position(('data', 0)) ax.spines['right...

부분분수의 미분

내용 방법 1 방법 2 방법 3 부분분수의 미분 분수의 미분은 일정한 공식 을 적용하여 계산할 수 있습니다. 그러나 분수 자체가 단순한 표현으로 이루어지지 않았다면 미분 과정이나 결과는 매우 복잡할 수 있습니다. 만약 복잡한 분수 함수를 간단한 분수들로 분해할 수 있다면 계산이 보다 간편해질 것입니다. 이와 같이 분해된 간단한 분수들을 부분분수 라고 합니다. 예를 들어 다음 두 분수의 합을 계산해 봅니다. $$\begin{align} \frac{1}{x+1}+\frac{2}{x-1}&=\frac{x-1+2(x+1)}{(x+1)(x-1)}\\ &=\frac{3x+1}{x^2-1} \end{align}$$ 위 과정은 3개 이상의 여러 분수에서도 이루어질 수 있습니다. 또한 역으로 진행될 수 있습니다. 즉, 분수를 부분 분수로 분할할 수 있습니다. 그러나 이러한 과정은 대수분수 (분자의 가장 큰 차수가 분모의 최고의 차수보다 작은 분수)에서만 이루어질 수 있습니다. 예를 들어 $\displaystyle \frac {x^2+2}{x^2-1}$의 경우는 분자와 분모의 차수는 2차로 같습니다. 이러한 경우 다음과 같이 분리할 수 있습니다. $$\frac{x^2+2}{x^2-1}=1+\frac{3}{x^2-1}$$ 위의 식 중 $\displaystyle \frac{3}{x^2-1}$은 분자의 차수가 분모의 차수 보다 낮은 대수 분수이므로 부분 분수로 분리할 수 있습니다. 이와같이 부분 분수로 분해하는 방법은 다음과 같이 몇 가지로 구분할 수 있습니다. 방법 1 위 예의 결과 $\displaystyle \frac{3x+1}{x^2-1}$의 경우를 역으로 생각해 봅니다. 분모의 인수분해가 가능하면 그 분모의 인수에 의해 다음과 같이 분해할 수 있습니다. $$\begin{align} \frac{3x+1}{x^2-1}&=\frac{3x+1}{(x+1)(x-1)}\\ &=\frac{A}{x+1...