Google과 Apple에 $1,000를 투자했을 때의 누적 수익률 I
누적 수익률 계산 능력을 실제로 활용해 봅시다. 2010년에 Google('GOOG')과 Apple('AAPL')에 각각 $1,000를 투자했다면 지금쯤 얼마가 되었을지 비교해 보겠습니다.
이 연습은 강의의 일부입니다
Python으로 시계열 데이터 다루기
연습 안내
pandas는 pd로, matplotlib.pyplot은 plt로 이미 임포트되어 있습니다. Google과 Apple의 주가 데이터도 변수 data에 로드되어 있습니다.
- 변수
investment를 정의하고 값을1000으로 설정하세요. data에.pct_change()를 적용하여returns를 계산하세요.returns에 1을 더한 결과를returns_plus_one에 할당한 후,returns_plus_one에.cumprod()를 적용하여 결과를cumulative_return에 할당하세요.cumulative_return에investment를 곱한 뒤 결과를 플롯하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Define your investment
investment = ____
# Calculate the daily returns here
returns = ____
# Calculate the cumulative returns here
returns_plus_one = ____
cumulative_return = ____
# Calculate and plot the investment return here