0
# Statistical Functions
1
2
Statistical analysis functions for correlation, regression, and statistical measures of price data. These functions provide mathematical insights into price relationships, trends, and variability patterns.
3
4
## Capabilities
5
6
### Beta Coefficient
7
8
Measures the sensitivity of a security's returns relative to market returns, indicating systematic risk.
9
10
```python { .api }
11
def BETA(real0, real1, timeperiod=5):
12
"""
13
Beta
14
15
Calculates beta coefficient measuring correlation and relative volatility between two price series.
16
17
Parameters:
18
- real0: array-like, dependent variable (typically individual security)
19
- real1: array-like, independent variable (typically market index)
20
- timeperiod: int, number of periods for calculation (default: 5)
21
22
Returns:
23
numpy.ndarray: Beta coefficient values
24
"""
25
```
26
27
### Pearson Correlation Coefficient
28
29
Measures the linear correlation between two price series, ranging from -1 to +1.
30
31
```python { .api }
32
def CORREL(real0, real1, timeperiod=30):
33
"""
34
Pearson's Correlation Coefficient (r)
35
36
Measures linear relationship strength between two data series.
37
38
Parameters:
39
- real0: array-like, first data series
40
- real1: array-like, second data series
41
- timeperiod: int, number of periods for calculation (default: 30)
42
43
Returns:
44
numpy.ndarray: Correlation coefficient values (-1 to +1)
45
"""
46
```
47
48
### Linear Regression
49
50
Calculates linear regression line values, providing trend line based on least squares method.
51
52
```python { .api }
53
def LINEARREG(real, timeperiod=14):
54
"""
55
Linear Regression
56
57
Calculates linear regression line values using least squares method.
58
59
Parameters:
60
- real: array-like, input price data
61
- timeperiod: int, number of periods for regression (default: 14)
62
63
Returns:
64
numpy.ndarray: Linear regression line values
65
"""
66
```
67
68
### Linear Regression Angle
69
70
Calculates the angle of the linear regression line, indicating trend steepness.
71
72
```python { .api }
73
def LINEARREG_ANGLE(real, timeperiod=14):
74
"""
75
Linear Regression Angle
76
77
Calculates the angle of the linear regression line in degrees.
78
79
Parameters:
80
- real: array-like, input price data
81
- timeperiod: int, number of periods for regression (default: 14)
82
83
Returns:
84
numpy.ndarray: Regression line angle values (degrees)
85
"""
86
```
87
88
### Linear Regression Intercept
89
90
Calculates the y-intercept of the linear regression line.
91
92
```python { .api }
93
def LINEARREG_INTERCEPT(real, timeperiod=14):
94
"""
95
Linear Regression Intercept
96
97
Calculates the y-intercept of the linear regression line.
98
99
Parameters:
100
- real: array-like, input price data
101
- timeperiod: int, number of periods for regression (default: 14)
102
103
Returns:
104
numpy.ndarray: Regression line intercept values
105
"""
106
```
107
108
### Linear Regression Slope
109
110
Calculates the slope of the linear regression line, indicating trend direction and strength.
111
112
```python { .api }
113
def LINEARREG_SLOPE(real, timeperiod=14):
114
"""
115
Linear Regression Slope
116
117
Calculates the slope of the linear regression line.
118
119
Parameters:
120
- real: array-like, input price data
121
- timeperiod: int, number of periods for regression (default: 14)
122
123
Returns:
124
numpy.ndarray: Regression line slope values
125
"""
126
```
127
128
### Standard Deviation
129
130
Measures the dispersion of price data around the mean, indicating volatility.
131
132
```python { .api }
133
def STDDEV(real, timeperiod=5, nbdev=1):
134
"""
135
Standard Deviation
136
137
Calculates standard deviation of price data over specified period.
138
139
Parameters:
140
- real: array-like, input price data
141
- timeperiod: int, number of periods (default: 5)
142
- nbdev: float, number of deviations (default: 1)
143
144
Returns:
145
numpy.ndarray: Standard deviation values
146
"""
147
```
148
149
### Time Series Forecast
150
151
Projects future price values based on linear regression analysis.
152
153
```python { .api }
154
def TSF(real, timeperiod=14):
155
"""
156
Time Series Forecast
157
158
Forecasts next period value using linear regression.
159
160
Parameters:
161
- real: array-like, input price data
162
- timeperiod: int, number of periods for forecast calculation (default: 14)
163
164
Returns:
165
numpy.ndarray: Forecasted values
166
"""
167
```
168
169
### Variance
170
171
Calculates the variance of price data, measuring the average of squared deviations from the mean.
172
173
```python { .api }
174
def VAR(real, timeperiod=5, nbdev=1):
175
"""
176
Variance
177
178
Calculates variance of price data over specified period.
179
180
Parameters:
181
- real: array-like, input price data
182
- timeperiod: int, number of periods (default: 5)
183
- nbdev: float, number of deviations (default: 1)
184
185
Returns:
186
numpy.ndarray: Variance values
187
"""
188
```
189
190
### Average Deviation
191
192
Calculates the average deviation from the mean, providing a measure of dispersion that is less sensitive to outliers than standard deviation.
193
194
```python { .api }
195
def AVGDEV(real, timeperiod=14):
196
"""
197
Average Deviation
198
199
Calculates average deviation from the mean over specified period.
200
Provides a robust measure of dispersion less sensitive to outliers.
201
202
Parameters:
203
- real: array-like, input price data
204
- timeperiod: int, number of periods (default: 14)
205
206
Returns:
207
numpy.ndarray: Average deviation values
208
"""
209
```
210
211
## Usage Examples
212
213
```python
214
import talib
215
import numpy as np
216
217
# Sample data - individual stock and market index
218
stock_prices = np.array([100, 102, 101, 103, 105, 104, 106, 108, 107, 109])
219
market_prices = np.array([1000, 1020, 1010, 1025, 1040, 1035, 1050, 1065, 1055, 1070])
220
221
# Calculate statistical measures
222
beta = talib.BETA(stock_prices, market_prices, timeperiod=5)
223
correlation = talib.CORREL(stock_prices, market_prices, timeperiod=5)
224
linear_reg = talib.LINEARREG(stock_prices, timeperiod=5)
225
reg_slope = talib.LINEARREG_SLOPE(stock_prices, timeperiod=5)
226
reg_angle = talib.LINEARREG_ANGLE(stock_prices, timeperiod=5)
227
std_dev = talib.STDDEV(stock_prices, timeperiod=5)
228
variance = talib.VAR(stock_prices, timeperiod=5)
229
forecast = talib.TSF(stock_prices, timeperiod=5)
230
231
print("Statistical Analysis Results:")
232
print(f"Beta: {beta[-1]:.3f}")
233
print(f"Correlation: {correlation[-1]:.3f}")
234
print(f"Current Price: {stock_prices[-1]}")
235
print(f"Linear Regression: {linear_reg[-1]:.2f}")
236
print(f"Trend Slope: {reg_slope[-1]:.4f}")
237
print(f"Trend Angle: {reg_angle[-1]:.2f} degrees")
238
print(f"Standard Deviation: {std_dev[-1]:.3f}")
239
print(f"Variance: {variance[-1]:.3f}")
240
print(f"Next Period Forecast: {forecast[-1]:.2f}")
241
242
# Interpretation:
243
# - Beta > 1: Stock is more volatile than market
244
# - Correlation near 1: Strong positive relationship with market
245
# - Positive slope/angle: Upward trend
246
# - High std dev/variance: High volatility
247
```
248
249
## Practical Applications
250
251
### Risk Management
252
- **Beta**: Portfolio construction and risk assessment
253
- **Standard Deviation**: Position sizing and volatility measurement
254
- **Correlation**: Diversification analysis and pair trading
255
256
### Trend Analysis
257
- **Linear Regression**: Identify trend direction and strength
258
- **Regression Angle**: Measure trend steepness
259
- **TSF**: Short-term price projection
260
261
### Market Relationships
262
- **Correlation**: Identify related instruments for spread trading
263
- **Beta**: Hedge ratio calculation and market exposure measurement
264
265
### Statistical Arbitrage
266
- **Variance**: Volatility-based trading strategies
267
- **Regression Analysis**: Mean reversion strategy development