금융 분석에서 히스토그램, 산점도, 선 그래프 활용
노트북 Quantopian 강의
요약
이 입문 강의는 일반적인 그래프로 금융 데이터를 살펴보고 잠재적 구조나 데이터 문제를 발견하는 방법을 설명합니다. 미국 US 주식 두 종목의 일별 가격을 예로 들어 경험적 분포용 히스토그램, 누적 빈도용 누적 히스토그램, 관측값 쌍의 관계를 보는 산점도, 시간에 따른 값을 나타내는 선 그래프를 보여줍니다. 또한 분포 그래프에는 가격 수준보다 수익률이 대체로 더 유용한 이유도 설명합니다. 가격은 비정상적이며 넓은 범위에서 움직일 수 있습니다.
예시는 시각적 탐색과 공식 검정을 구분합니다. 차트는 가설을 세우는 데 도움이 될 수 있지만, 눈에 띄는 패턴은 확증 편향을 반영할 수 있으며 과거 수익률 분포가 미래에도 같은 움직임을 보장하지 않습니다. 선 그래프는 표본 관측값을 연결해 그 사이의 움직임을 가릴 수 있고, 산점도는 값 쌍에 붙은 날짜 정보를 버립니다. 강의는 관계를 검증할 전략이나 통계 검정을 제공하지 않으므로, 모델 주장을 평가하려면 시각적 점검만으로는 부족하고 더 신중한 분석이 필요합니다.
핵심 아이디어
- 히스토그램으로 관측값의 빈도 분포를 살펴보고 누적 히스토그램으로 누적 빈도를 확인합니다.
- 금융 시계열의 분포 그래프에서는 비정상적인 가격 수준보다 수익률이 유용한 경우가 많습니다.
- 산점도는 관측값 쌍을 보여주지만 각 관측값의 날짜는 나타내지 않습니다.
- 선 그래프는 시간에 따른 흐름을 보기 쉽게 하지만 표본 점 사이의 변화를 숨길 수 있습니다.
- 그래프는 탐색과 가설 수립에 활용하고, 패턴이나 모델이 지속된다는 증거로 여기지 않습니다.
태그
전문
# Graphical Representations of Data
<a href="https://www.quantrocket.com"><img alt="QuantRocket logo" src="https://www.quantrocket.com/assets/img/notebook-header-logo.png"></a>
© Copyright Quantopian Inc.<br>
© Modifications Copyright QuantRocket LLC<br>
Licensed under the [Creative Commons Attribution 4.0](https://creativecommons.org/licenses/by/4.0/legalcode).<br>
<a href="https://www.quantrocket.com/disclaimer/">Disclaimer</a>
***
[Quant Finance Lectures (adapted Quantopian Lectures)](Introduction.ipynb) › Lecture 5 - Plotting Data
***
# Graphical Representations of Data
By Evgenia "Jenny" Nitishinskaya, Maxwell Margenot, and Delaney Granizo-Mackenzie
<a href="https://youtu.be/nKq_wz3Qk8w?si=gtDPpJ3fxhfEyY4d&t=91" target="_blank">Quantopian video for this lecture ↗</a>
Representing data graphically can be incredibly useful for learning how the data behaves and seeing potential structure or flaws. Care should be taken, as humans are incredibly good at seeing only evidence that confirms our beliefs, and visual data lends itself well to that. Plots are good to use when formulating a hypothesis, but should not be used to test a hypothesis.
We will go over some common plots here.
```python
# Import our libraries
# This is for numerical processing
import numpy as np
# This is the library most commonly used for plotting in Python.
# Notice how we import it 'as' plt, this enables us to type plt
# rather than the full string every time.
import matplotlib.pyplot as plt
```
## Getting Some Data
If we're going to plot data we need some data to plot. We'll get the pricing data of Apple (AAPL) and Microsoft (MSFT) to use in our examples. First, we will look up the sids for AAPL and MSFT:
```python
from quantrocket.master import get_securities
securities = get_securities(symbols=["MSFT", "AAPL"], fields=["Sid", "Symbol"], vendors='usstock')
securities
```
### Data Structure
Knowing the structure of your data is very important. Normally you'll have to do a ton of work molding your data into the form you need for testing. QuantRocket has done a lot of cleaning on the data, but you still need to put it into the right shapes and formats for your purposes.
In this case the data will be returned as a pandas dataframe object. The rows are timestamps, and the columns are the sids for AAPL and MSFT.
```python
from quantrocket import get_prices
start = '2014-01-01'
end = '2015-01-01'
data = get_prices("usstock-free-1min", data_frequency="daily", sids=securities.index.tolist(), fields='Close', start_date=start, end_date=end)
data = data.loc["Close"]
data.head()
```
For convenience, let's rename the columns to be symbols instead of sids.
```python
sids_to_symbols = securities.Symbol.to_dict()
data = data.rename(columns=sids_to_symbols)
data.head()
```
Indexing into the 2D dataframe will give us a 1D series object. The index for the series is timestamps, the value upon index is a price. Similar to an array except instead of integer indices it's times.
```python
data['MSFT'].head()
```
## Histogram
A histogram is a visualization of how frequent different values of data are. By displaying a frequency distribution using bars, it lets us quickly see where most of the observations are clustered. The height of each bar represents the number of observations that lie in each interval. You can think of a histogram as an empirical and discrete Probability Density Function (PDF).
```python
# Plot a histogram using 20 bins
plt.hist(data['MSFT'], bins=20)
plt.xlabel('Price')
plt.ylabel('Number of Days Observed')
plt.title('Frequency Distribution of MSFT Prices, 2014');
```
### Returns Histogram
In finance rarely will we look at the distribution of prices. The reason for this is that prices are non-stationary and move around a lot. (For more info on non-stationarity please see Lecture 43: *Integration, Cointegration, and Stationarity*.) Instead we will use daily returns. Let's try that now.
```python
# Remove the first element because percent change from nothing to something is NaN
R = data['MSFT'].pct_change()[1:]
# Plot a histogram using 20 bins
plt.hist(R, bins=20)
plt.xlabel('Return')
plt.ylabel('Number of Days Observed')
plt.title('Frequency Distribution of MSFT Returns, 2014');
```
The graph above shows, for example, that the daily returns of MSFT were above 0.03 on fewer than 5 days in 2014. Note that we are completely discarding the dates corresponding to these returns.
IMPORTANT: Note also that this does not imply that future returns will have the same distribution.
### Cumulative Histogram (Discrete Estimated CDF)
An alternative way to display the data would be using a cumulative distribution function, in which the height of a bar represents the number of observations that lie in that bin or in one of the previous ones. This graph is always nondecreasing since you cannot have a negative number of observations. The choice of graph depends on the information you are interested in.
```python
# Remove the first element because percent change from nothing to something is NaN
R = data['MSFT'].pct_change()[1:]
# Plot a histogram using 20 bins
plt.hist(R, bins=20, cumulative=True)
plt.xlabel('Return')
plt.ylabel('Number of Days Observed')
plt.title('Cumulative Distribution of MSFT Returns, 2014');
```
## Scatter plot
A scatter plot is useful for visualizing the relationship between two data sets. We use two data sets which have some sort of correspondence, such as the date on which the measurement was taken. Each point represents two corresponding values from the two data sets. However, we don't plot the date that the measurements were taken on.
```python
plt.scatter(data['MSFT'], data['AAPL'])
plt.xlabel('MSFT')
plt.ylabel('AAPL')
plt.title('Daily Prices in 2014');
```
```python
R_msft = data['MSFT'].pct_change()[1:]
R_aapl = data['AAPL'].pct_change()[1:]
plt.scatter(R_msft, R_aapl)
plt.xlabel('MSFT')
plt.ylabel('AAPL')
plt.title('Daily Returns in 2014');
```
## Line graph
A line graph can be used when we want to track the development of the y value as the x value changes. For instance, when we are plotting the price of a stock, showing it as a line graph instead of just plotting the data points makes it easier to follow the price over time. This necessarily involves "connecting the dots" between the data points, which can mask out changes that happened between the time we took measurements.
```python
plt.plot(data['MSFT'])
plt.plot(data['AAPL'])
plt.ylabel('Price')
plt.legend(['MSFT', 'AAPL']);
```
```python
# Remove the first element because percent change from nothing to something is NaN
R = data['MSFT'].pct_change()[1:]
plt.plot(R)
plt.ylabel('Return')
plt.title('MSFT Returns');
```
## Never Assume Conditions Hold
Again, whenever using plots to visualize data, do not assume you can test a hypothesis by looking at a graph. Also do not assume that because a distribution or trend used to be true, it is still true. In general much more sophisticated and careful validation is required to test whether models hold. Plots are mainly useful when initially deciding how your models should work.
---
**Next Lecture:** [Means](Lecture06-Means.ipynb)
[Back to Introduction](Introduction.ipynb)
---
*This presentation is for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation for any security; nor does it constitute an offer to provide investment advisory or other services by QuantRocket LLC ("QuantRocket"). Nothing contained herein constitutes investment advice or offers any opinion with respect to the suitability of any security, and any views expressed herein should not be taken as advice to buy, sell, or hold any security or as an endorsement of any security or company. In preparing the information contained herein, the authors have not taken into account the investment needs, objectives, and financial circumstances of any particular investor. Any views expressed and data illustrated herein were prepared based upon information believed to be reliable at the time of publication. QuantRocket makes no guarantees as to their accuracy or completeness. All information is subject to change and may quickly become unreliable for various reasons, including changes in market conditions or economic circumstances.*






출처의 라이선스에 따라 출처를 표시하고 전문을 공개합니다. 라이선스: CC BY 4.0
이 요약은 원문을 바탕으로 Stratmill의 리서치 에이전트가 작성했으며, 원문을 복사한 것이 아닙니다.