当前位置:网站首页>Probability theory: application of convolution in calculating moving average
Probability theory: application of convolution in calculating moving average
2022-07-03 11:04:00 【hflag168】
Application of convolution in calculating moving average
Convolution may often be encountered in the field of artificial intelligence , So what exactly is convolution ? How to understand ?
1. Introduction of convolution
For science and engineering students , Contact convolution is mostly in 《 Probability theory and mathematical statistics 》 Encountered in the course of , I'm learning “ Random variable function distribution ” Medium Z=X+Y in :
set up ( X , Y ) (X,Y) (X,Y) Is a two-dimensional continuous random variable , It has a probability density f ( x , y ) f(x,y) f(x,y), be Z = X + Y Z = X + Y Z=X+Y It is still a continuous random variable , The probability density is :
f X + Y ( z ) = ∫ − ∞ + ∞ f ( x , z − x ) d x f_{X+Y}(z)=\int_{-\infty}^{+\infty}f(x,z-x)dx fX+Y(z)=∫−∞+∞f(x,z−x)dx
perhaps
f X + Y ( z ) = ∫ − ∞ + ∞ f ( z − y , y ) d y f_{X+Y}(z)=\int_{-\infty}^{+\infty}f(z-y, y)dy fX+Y(z)=∫−∞+∞f(z−y,y)dy
You Ruo X and Y X and Y X and Y Are independent of each other , set up ( X , Y ) (X,Y) (X,Y) About X , Y X,Y X,Y The edge densities are f X ( x ) , f Y ( y ) f_X(x), f_Y(y) fX(x),fY(y), Then you can get it again :
f X + Y ( z ) = ∫ − ∞ + ∞ f X ( z − y ) f Y ( y ) d y ( 1 ) f_{X+Y}(z)=\int_{-\infty}^{+\infty}f_X(z-y)f_Y(y)dy \quad \quad (1) fX+Y(z)=∫−∞+∞fX(z−y)fY(y)dy(1)
and
f X + Y ( z ) = ∫ − ∞ + ∞ f X ( x ) f Y ( z − x ) d x ( 2 ) f_{X+Y}(z)=\int_{-\infty}^{+\infty}f_X(x)f_Y(z-x)dx \quad\quad(2) fX+Y(z)=∫−∞+∞fX(x)fY(z−x)dx(2)
The formula above (1) and (2) be called f X and f Y f_X and f_Y fX and fY Convolution formula , Write it down as f X ∗ f Y f_X*f_Y fX∗fY, namely
f X ∗ f Y = ∫ − ∞ + ∞ f X ( x ) f Y ( z − x ) d x = ∫ − ∞ + ∞ f X ( z − y ) f Y ( y ) d y f_X*f_Y=\int_{-\infty}^{+\infty}f_X(x)f_Y(z-x)dx=\int_{-\infty}^{+\infty}f_X(z-y)f_Y(y)dy fX∗fY=∫−∞+∞fX(x)fY(z−x)dx=∫−∞+∞fX(z−y)fY(y)dy
2. Exploration of convolution
Convolution is an important operation in analytical mathematics , There are convolution operations in both continuous and discrete cases . Here's the definition :
Definition of continuous variables : set up f ( x ) , g ( x ) f(x), g(x) f(x),g(x) yes R Two integrable functions on , To integrate :
h ( x ) = ∫ − ∞ + ∞ f ( τ ) g ( x − τ ) d τ h(x)=\int_{-\infty}^{+\infty}f(\tau)g(x-\tau)d\tau h(x)=∫−∞+∞f(τ)g(x−τ)dτ
This definite integral defines a function h ( x ) h(x) h(x), Called a function f and g Convolution of , Write it down as h ( x ) = ( f ∗ g ) ( x ) h(x)=(f*g)(x) h(x)=(f∗g)(x).
Definition of discrete variables : If the variable is a sequence x ( n ) and y ( n ) x(n) and y(n) x(n) and y(n), be
h ( n ) = ∑ i = − ∞ + ∞ x ( i ) y ( n − i ) h(n)=\sum_{i=-\infty}^{+\infty}x(i)y(n-i) h(n)=i=−∞∑+∞x(i)y(n−i)
that , h ( n ) h(n) h(n) be called x ( n ) And y ( n ) x(n) And y(n) x(n) And y(n) Convolution of .
3. Application of convolution
Convolution has many applications in engineering and Mathematics : Weighted moving average in statistics is a kind of convolution ; Two independent statistics in probability theory X and Y The probability density of the sum of is X And Y Convolution of probability density function of .
Now let's calculate the share price of apple for a period 5 Daily moving average price , And show the smoothing effect of convolution by drawing .
# step 1: load data
import numpy as np
from matplotlib.pyplot import plot
# 1.1 Here I read Apple 30 Day closing price
c = np.loadtxt('data.csv', delimiter=',', usecols=(6,), unpack=True)
# 1.2 Calculate the five-day average price , So construct an equivalent weight list ( You can also use unequal weights )
N = 5
weights = np.ones(N)/N
# step 2: calulate
# 2.1 Calculate the convolution of closing price and weight
sma = np.convolve(weithts, c)
# sma Not what we asked 5 Average daily price , Because there is only continuity 5 Day's closing price , Will produce an average price , So be right sma section ,sma[N-1:-N+1] That's what we need 5 Average daily price
sma = sma[N-1:-N+1]
# step 3: Plot
t = np.arange(N-1, len(c))
plot(t, c[N-1:], lw=1.0)
plot(t, sma, lw=2.0)
stay jupyter notebook The lower output is as follows :
As shown in the figure above , The relatively smooth thick line is the five-day moving average , The jagged thin lines depict the daily closing price .
4. summary
We calculate stocks by using convolution 5 Average daily price , It is demonstrated that the result of convolution calculation can produce a smoother output for the input . In fact, convolution plays a very important role in signal processing .
边栏推荐
- Latest sales volume of pinduoduo
- Hard goods | write all the codes as soon as you change the test steps? Why not try yaml to realize data-driven?
- Qt:qss custom qlineedit instance
- 项目组织战略管理
- 月薪过万的测试员,是一种什么样的生活状态?
- 8年测试工程师总结出来的《测试核心价值》与《0基础转行软件测试超全学习指南》
- QT: QSS custom qsplitter instance
- Interviewer: what is the internal implementation of the list in redis?
- Flink-- custom function
- Software testing (test case) writing: vulgar, native and skillful
猜你喜欢

Is it OK to test the zero basis software?

MAUI Developer Day in GCR

Pour vous amener dans le monde des bases de données natives du cloud

Game test related tests a hero's skills (spring moves are asked more questions)

你真的需要自动化测试吗?

How does MySQL find the latest data row that meets the conditions?

snownlp情感分析

Clion debug

《通信软件开发与应用》

I, a tester from a large factory, went to a state-owned enterprise with a 50% pay cut. I regret it
随机推荐
UI自动化测试如何走出困境?价值又如何体现?
Qt:qss custom qmenubar instance
Have you learned the new technology to improve sales in 2021?
Wechat applet training notes 1
QT:QSS自定义QToolBar和QToolBox实例
QT:QSS自定义QLineEdit实例
Game test related tests a hero's skills (spring moves are asked more questions)
Flink < --> Introduction to JDBC +with parameter
Buy health products for parents
After 8 years of industry thinking, the test director has a deeper understanding of test thinking
Flink chain conditional source code analysis
【Proteus仿真】74HC154 四线转12线译码器组成的16路流水灯
那些一门心思研究自动化测试的人,后来怎样了?
Logstash backup tracks the data records reported
那些一門心思研究自動化測試的人,後來怎樣了?
ConstraintLayout跟RelativeLayout嵌套出现的莫名奇妙的问题
MAUI Developer Day in GCR
Chiyou (), a specific mythical image, is also gradually abstracted as a dramatic character type "Jing". "Jing", born in Dan Dynasty and ugly at the end, is the earliest "profession" in Chinese drama
File upload and download test point
Differences among norm, normalize and normalized in eigen