Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Forecaster wrapper for Prophet #191

Merged
merged 4 commits into from
Dec 10, 2024
Merged

Conversation

sd2k
Copy link
Collaborator

@sd2k sd2k commented Dec 10, 2024

Since the Forecaster traits don't pass timestamps etc, we need to provide the training data at the time the forecaster is created and pass that down to the Prophet model, after replacing the y column with whatever the forecaster gives us. This should work with transforms too.

It does require cloning the data on each call to fit, but I don't think that's avoidable since Prophet's fit takes the data by value anyway.

The alternative approach which I tried first was to use an associated type for the data, but that is pretty infectious and ends up requiring the input data to be mutable so that we can replace the y column, which ends up looking pretty ugly.

This is an alternative (and hopefully simpler) approach to #184.

Summary by CodeRabbit

  • New Features

    • Introduced a new ProphetForecaster for enhanced forecasting capabilities.
    • Added a public forecaster module for easier access to forecasting functionalities.
    • New methods for the Prophet model to improve usability, including options management and interval width settings.
    • Example provided for using the Prophet model with the WasmstanOptimizer.
  • Bug Fixes

    • Improved error handling in prediction methods to ensure robustness.
  • Documentation

    • Added comprehensive example demonstrating the use of the ProphetForecaster.

sd2k added 2 commits December 10, 2024 10:24
Since the `Forecaster` traits don't pass timestamps etc,
we need to provide the training data at the time the forecaster
is created and pass _that_ down to the Prophet model, after replacing
the `y` column with whatever the forecaster gives us. This should
work with transforms too.

It does require cloning the data, unfortunately. The alternative
approach which I tried first was to use an associated type for the
data, but that is pretty infectious and ends up requiring the input
data to be mutable so that we can replace the `y` column, which ends
up looking pretty ugly.
Copy link
Contributor

coderabbitai bot commented Dec 10, 2024

Warning

Rate limit exceeded

@sd2k has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 4 minutes and 25 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 4cabe52 and 78f1b33.

📒 Files selected for processing (1)
  • crates/augurs-prophet/src/forecaster.rs (1 hunks)

Walkthrough

The changes in this pull request introduce several enhancements to the augurs-prophet crate. A new dependency on augurs-core is added to the Cargo.toml, establishing it as a workspace dependency. The forecaster.rs file implements a new ProphetForecaster struct with methods for fitting and predicting data using the Prophet algorithm. Additionally, a new public module for forecasting is created in lib.rs. The optimizer.rs file gains a new implementation for dynamic dispatch of the Optimizer trait, and the prophet.rs file sees several new methods added to enhance the Prophet struct's functionality. An example of using the new features is provided in prophet_forecaster.rs.

Changes

File Change Summary
crates/augurs-prophet/Cargo.toml Added dependency: augurs-core.workspace = true under [dependencies].
crates/augurs-prophet/src/forecaster.rs Added ProphetForecaster struct with methods for fitting and predicting data. Introduced FittedProphetForecaster struct for prediction capabilities.
crates/augurs-prophet/src/lib.rs Added new public module: pub mod forecaster;.
crates/augurs-prophet/src/optimizer.rs Implemented Optimizer trait for Arc<dyn Optimizer>, allowing dynamic dispatch for optimization.
crates/augurs-prophet/src/prophet.rs Added new methods to Prophet struct for options handling and conversion to ProphetForecaster.
examples/forecasting/examples/prophet_forecaster.rs Introduced example demonstrating the use of the Prophet model with the wasmstan optimizer.

Possibly related PRs

🐇 In the meadow where forecasts bloom,
A Prophet shines, dispelling gloom.
With data trained and models bright,
Predictions dance in morning light.
New paths we carve with every line,
In code we trust, our futures shine! 🌼


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Experiment)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (5)
crates/augurs-prophet/src/forecaster.rs (3)

55-59: Consider avoiding unnecessary cloning of TrainingData

In the fit method, training_data is cloned to replace the y values. For large datasets, this could lead to performance issues. Consider modifying TrainingData to allow updating the y values without cloning, or refactoring to work with references to minimize overhead.


60-61: Assess the necessity of cloning the model

The model is cloned before fitting, which might be inefficient if the model is large. If possible, refactor to avoid cloning the model by leveraging interior mutability or other patterns to modify the model in place.


95-97: Handle Option without using expect to prevent potential panics

In predict_in_sample_inplace and predict_inplace, the code uses expect on Option values for predictions.yhat.lower and predictions.yhat.upper. Relying on expect may lead to panics if the assumption about uncertainty_samples being greater than zero is ever violated. Consider gracefully handling the Option using pattern matching or providing a fallback to improve robustness.

Also applies to: 134-137

examples/forecasting/examples/prophet_forecaster.rs (1)

12-15: Improve readability of timestamp data

The ds vector contains hardcoded UNIX timestamps, which might not be immediately clear to readers. Consider using date parsing functions or adding comments to clarify the dates represented, enhancing the example's readability.

crates/augurs-prophet/src/optimizer.rs (1)

302-311: Consider adding documentation for the Arc implementation

Adding documentation would help explain the purpose of this implementation, particularly its role in enabling thread-safe sharing of optimizers.

Consider adding documentation like this:

+/// Implementation for Arc<dyn Optimizer> enables thread-safe sharing of optimizers
+/// while maintaining the ability to use dynamic dispatch.
 impl Optimizer for Arc<dyn Optimizer> {
     fn optimize(
         &self,
         init: &InitialParams,
         data: &Data,
         opts: &OptimizeOpts,
     ) -> Result<OptimizedParams, Error> {
         (**self).optimize(init, data, opts)
     }
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 9652eba and 9f76c81.

📒 Files selected for processing (6)
  • crates/augurs-prophet/Cargo.toml (1 hunks)
  • crates/augurs-prophet/src/forecaster.rs (1 hunks)
  • crates/augurs-prophet/src/lib.rs (1 hunks)
  • crates/augurs-prophet/src/optimizer.rs (2 hunks)
  • crates/augurs-prophet/src/prophet.rs (3 hunks)
  • examples/forecasting/examples/prophet_forecaster.rs (1 hunks)
🔇 Additional comments (6)
crates/augurs-prophet/src/prophet.rs (2)

238-256: Accessor methods enhance flexibility of Prophet

The addition of opts, opts_mut, and set_interval_width methods provides users with convenient access to modify ProphetOptions. This enhancement improves the configurability of the Prophet model.


292-322: Dynamic optimizer conversion and forecaster integration are well-implemented

The into_dyn_optimizer method correctly converts the optimizer into a dynamic trait object, facilitating compatibility with trait objects. The into_forecaster method seamlessly integrates the Prophet model with ProphetForecaster, enhancing usability within the forecasting framework.

crates/augurs-prophet/src/lib.rs (1)

5-5: Addition of forecaster module

Introducing the forecaster module exposes the new forecasting capabilities to users, making it straightforward to access the ProphetForecaster functionality from the library's root. This aligns with the library's modular design.

examples/forecasting/examples/prophet_forecaster.rs (1)

1-53: Example effectively demonstrates ProphetForecaster usage

The example provides a clear demonstration of setting up a ProphetForecaster with data transformations and generating predictions. The code is well-structured and serves as a practical guide for users.

crates/augurs-prophet/Cargo.toml (1)

31-31: LGTM: Workspace dependency addition is appropriate

The addition of augurs-core as a workspace dependency aligns with the PR objective of implementing the Forecaster traits.

crates/augurs-prophet/src/optimizer.rs (1)

302-311: LGTM: Clean implementation of dynamic dispatch

The implementation correctly enables thread-safe sharing of optimizers through Arc while maintaining proper delegation semantics.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (3)
crates/augurs-prophet/src/forecaster.rs (3)

32-46: Consider documenting the uncertainty_samples default value.

The method correctly ensures uncertainty_samples is at least 1000, but this default value should be documented in the method's documentation.

    /// Create a new Prophet forecaster.
    ///
    /// # Parameters
    ///
    /// - `opts`: The options to use for fitting the model.
    /// - `optimizer`: The optimizer to use for fitting the model.
    /// - `optimize_opts`: The options to use for optimizing the model.
+   ///
+   /// Note: If uncertainty_samples is 0, it will be set to 1000.

53-66: Consider optimizing data cloning for large datasets.

The fit method clones the entire training data. While this ensures data isolation, it could be memory-intensive for large datasets. Consider documenting this behavior and its potential impact on performance.


91-104: Reduce code duplication in uncertainty samples handling.

The error message for uncertainty samples is duplicated. Consider extracting this into a constant or helper method.

+ const UNCERTAINTY_SAMPLES_MSG: &str = "uncertainty_samples should be > 0, this is a bug";

  // Then use it in both places:
- .expect("uncertainty_samples should be > 0, this is a bug");
+ .expect(UNCERTAINTY_SAMPLES_MSG);

Also applies to: 130-143

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 9f76c81 and 4cabe52.

📒 Files selected for processing (2)
  • crates/augurs-prophet/src/forecaster.rs (1 hunks)
  • crates/augurs-prophet/src/optimizer.rs (2 hunks)
🔇 Additional comments (4)
crates/augurs-prophet/src/forecaster.rs (3)

10-22: LGTM! Well-documented struct with clear purpose.

The struct is well-designed with appropriate fields and good documentation explaining its purpose and relationship with the augurs framework.


69-74: LGTM! Clean struct design with appropriate use of RefCell.

The struct correctly uses RefCell for interior mutability of the model, which is necessary for the predict implementations.


108-145: LGTM! Robust implementation with good error handling.

The predict_inplace method has good error handling, particularly for the zero horizon case, and properly manages the model borrowing scope.

crates/augurs-prophet/src/optimizer.rs (1)

302-314: LGTM! Clean implementation enabling thread-safe optimizer sharing.

The implementation correctly delegates to the inner type while maintaining thread safety through Arc. The documentation clearly explains its purpose.

@sd2k sd2k merged commit 9689307 into main Dec 10, 2024
24 checks passed
@sd2k sd2k deleted the prophet-forecaster-simple branch December 10, 2024 15:54
@sd2k sd2k mentioned this pull request Dec 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant