Advanced NumPy | 21/100 Days of Python Algo Trading

Advanced NumPy tutorial, NumPy broadcasting, NumPy advanced indexing, NumPy performance optimization, Multidimensional arrays NumPy, NumPy matrix operations, Complex array manipulations, NumPy slicing techniques, Efficient NumPy coding, NumPy memory management, High-performance computing with NumPy, NumPy array tricks, Advanced NumPy functions, NumPy vectorization, NumPy array filtering

Congratulations on reaching this stage!

If you’re here, it means you’re truly dedicated to mastering Python. Directly below, you’ll find a quiz designed to help you reinforce what you’ve just learned. This isn’t just about recalling facts; it’s about deeply understanding the concepts. Take a moment to answer the questions and see how well you can apply the knowledge from the video. You’re doing great—every question you tackle brings you one step closer to becoming a Python expert!

Day 21: Advanced Numpy

1. Which of the following is a major advantage of NumPy arrays over Python lists in algorithmic trading?
2. Given the need to handle large datasets in algorithmic trading, why would you prefer NumPy arrays over Python lists?
3. How would you use Boolean indexing to select all positive returns from an array returns?
```python
positive_returns = returns[________]
```
4. Given an array prices, how do you use fancy indexing to select elements at indices 2, 4, and 6?
```python
selected_prices = prices[________]
```
5. If you have an array prices of shape (10,) and you want to add a constant value 5 to each element, how does broadcasting work in this case?
```python
adjusted_prices = prices + 5
```
6. How does broadcasting work when adding a 1D array of shape (3,) to a 2D array of shape (3, 3)?
```python
result = array_2d + array_1d
```
7. How do you calculate the exponential moving average (EMA) of an array prices with a smoothing factor alpha?
```python
ema = np.zeros_like(prices)
ema[0] = prices[0]
for t in range(1, len(prices)):
ema[t] = alpha * prices[t] + (1 - alpha) * ema[t-1]
```
8. How would you use NumPy to compute the standard deviation of daily returns in an array daily_returns?
```python
std_dev = np.________(daily_returns)
```
9. What is the correct NumPy implementation of the sigmoid function applied to an array x?
```python
def sigmoid(x):
return np.________(1 / (1 + np.exp(-x)))
```
10. Why is the sigmoid function useful in algorithmic trading models, particularly in logistic regression?
11. How do you calculate the Mean Squared Error (MSE) between predicted and actual returns in NumPy?
```python
mse = np.________((predicted_returns - actual_returns)**2)
```
12. Why is the Mean Squared Error important in evaluating algorithmic trading models?
13. How can you replace missing values (NaNs) in an array data with the mean of the non-missing values?
```python
mean_value = np.nanmean(data)
data[np.isnan(data)] = mean_value
```
14. Which function is used to identify missing values in a NumPy array?
```python
missing_values = np.________(data)
```
15. Which library is commonly used alongside NumPy for plotting graphs in Python?
16. How would you plot a time series of stock prices stored in a NumPy array prices using Matplotlib?
```python
import matplotlib.pyplot as plt
plt.plot(________)
plt.show()
```
17. How do you perform element-wise addition of two arrays a and b with broadcasting, given that a has shape (3, 1) and b has shape (1, 3)?
```python
result = a + b
```
18. In algorithmic trading, how can you efficiently calculate the daily returns from a time series array prices using NumPy?
```python
daily_returns = np.diff(prices) / prices[:-1]
```
19. Given an array weights representing the weights of a portfolio, how do you normalize them to sum to 1 using NumPy?
```python
normalized_weights = weights / np.sum(weights)
```
20. How can you use advanced indexing to select specific rows from a 2D array data where the first column is greater than a threshold value?
```python
selected_rows = data[data[:, 0] > threshold]
```

 

1 thought on “Advanced NumPy | 21/100 Days of Python Algo Trading”

Comments are closed.