Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Challenge: Predict Market Direction with Logistic Regression | Building and Evaluating Trading Strategies
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Python for Traders

bookChallenge: Predict Market Direction with Logistic Regression

To put your understanding of machine learning in trading to the test, you'll tackle a hands-on challenge: predicting the next day's market direction using logistic regression. You'll begin with a hardcoded DataFrame of closing prices, engineer lagged return features, and use scikit-learn's logistic regression to classify whether the next day's return is positive or negative. You'll then evaluate the model's accuracy and display a confusion matrix to summarize its predictive performance.

Start by creating a DataFrame with daily closing prices. From these prices, calculate daily returns and then generate lagged versions of these returns to use as input features for your model. The target variable will be whether the following day's return is positive (market up) or not (market down or unchanged).

Note
Note

You can experiment by adjusting the number of lagged features or changing the train-test split to see how it affects the model's performance. For more on logistic regression and confusion matrices, consult the scikit-learn documentation.

Завдання

Swipe to start coding

You are given a DataFrame of daily closing prices for a hypothetical asset. Your task is to build a logistic regression model to predict whether the next day's return will be positive (market up) or not (market down or unchanged) using lagged return features.

  • Calculate daily returns from closing prices and add them as a new column named Return.
  • Create two new columns Lag1 and Lag2 that contain the previous day's and the day-before-previous's return, respectively.
  • Define a new column Direction as your target variable: set to 1 if the next day's return is greater than 0, otherwise 0.
  • Remove any rows containing missing values caused by shifting.
  • Use Lag1 and Lag2 as features (X) and Direction as the target (y).
  • Use all but the last 5 observations for training, and the last 5 for testing.
  • Fit a LogisticRegression model using scikit-learn on the training data.
  • Use the model to make predictions on the test set.
  • Calculate and print the test accuracy.
  • Calculate and print the confusion matrix for the test predictions.

Рішення

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 7
single

single

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Suggested prompts:

Can you show me how to create the DataFrame with closing prices?

How do I calculate lagged return features from the closing prices?

Can you explain how to set up the logistic regression model for this task?

close

bookChallenge: Predict Market Direction with Logistic Regression

Свайпніть щоб показати меню

To put your understanding of machine learning in trading to the test, you'll tackle a hands-on challenge: predicting the next day's market direction using logistic regression. You'll begin with a hardcoded DataFrame of closing prices, engineer lagged return features, and use scikit-learn's logistic regression to classify whether the next day's return is positive or negative. You'll then evaluate the model's accuracy and display a confusion matrix to summarize its predictive performance.

Start by creating a DataFrame with daily closing prices. From these prices, calculate daily returns and then generate lagged versions of these returns to use as input features for your model. The target variable will be whether the following day's return is positive (market up) or not (market down or unchanged).

Note
Note

You can experiment by adjusting the number of lagged features or changing the train-test split to see how it affects the model's performance. For more on logistic regression and confusion matrices, consult the scikit-learn documentation.

Завдання

Swipe to start coding

You are given a DataFrame of daily closing prices for a hypothetical asset. Your task is to build a logistic regression model to predict whether the next day's return will be positive (market up) or not (market down or unchanged) using lagged return features.

  • Calculate daily returns from closing prices and add them as a new column named Return.
  • Create two new columns Lag1 and Lag2 that contain the previous day's and the day-before-previous's return, respectively.
  • Define a new column Direction as your target variable: set to 1 if the next day's return is greater than 0, otherwise 0.
  • Remove any rows containing missing values caused by shifting.
  • Use Lag1 and Lag2 as features (X) and Direction as the target (y).
  • Use all but the last 5 observations for training, and the last 5 for testing.
  • Fit a LogisticRegression model using scikit-learn on the training data.
  • Use the model to make predictions on the test set.
  • Calculate and print the test accuracy.
  • Calculate and print the confusion matrix for the test predictions.

Рішення

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 7
single

single

some-alt