# Chapter 8: NumPy and Numerical Computing
# Fundamentals of Python for Financial Econometrics - NumPy and Numerical Computing
# Dataset: Ceteris Lab teaching sample

# NumPy and Numerical Computing
#
# **Opening question:** Why are numerical arrays faster and more expressive than repeatedly updating Python lists?

# %% Cell 2
import numpy as np

prices = np.array([100.0, 102.0, 101.0, 104.0])
returns = prices[1:] / prices[:-1] - 1
print(np.round(returns, 4))

# **Interpretation check:** Interpretation. Each return compares adjacent prices. The output has one fewer observation than the price array.

# %% Cell 4
import numpy as np

rng = np.random.default_rng(41202)
asset_returns = rng.normal([0.0003, 0.0002], [0.012, 0.007], size=(5, 2))
weights = np.array([0.6, 0.4])
portfolio = asset_returns @ weights
print(np.round(portfolio, 4))

# **Interpretation check:** Interpretation. Matrix multiplication applies the same portfolio weights to every simulated observation.

# %% Cell 6
import numpy as np
prices = np.array([100.0, 102.0, 101.0, 104.0])
print(np.round(returns, 4))
import numpy as np
rng = np.random.default_rng(41202)
asset_returns = rng.normal([0.0003, 0.0002], [0.012, 0.007], size=(5, 2))

# Verified source output
#
# ```text
# [ 0.02 -0.0098 0.0297]
# ```
#
# ```text
# [-0.0097 0.004 0.0097 -0.0022 -0.002 ]
# ```
#
# ```text
# [ 0.02   -0.0098  0.0297]
# ```
#
# ```text
# [-0.0097  0.004   0.0097 -0.0022 -0.002 ]
# ```
