-
Notifications
You must be signed in to change notification settings - Fork 29
Implementation of the Binomial Model for Option Pricing #1
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
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Enhanced the handling of option styles by incorporating an `OptionStyle` enum. Updated the `Payoff` trait and related methods to account for different option styles, ensuring accurate calculation of call and put payoffs. Expanded test coverage to include the new functionality and validated edge cases such as zero volatility and deep in/out of the money scenarios.
Expanded the `OptionType` enum with detailed comments describing each variant, its properties, and usage scenarios. Additionally, updated visibility settings for various modules and added thorough documentation for the binomial model pricing function, as well as including rust-toolchain configuration for nightly builds.
Centered the project logo using a styled div in the README. Added detailed information about the newly implemented binomial model for option pricing, including features, parameter descriptions, and future work.
Specified a fixed height of 100px for the logo image to ensure consistent display across different devices. This prevents the image from scaling disproportionately and maintains a uniform appearance.
Increase the height of the logo image from 100px to 200px to enhance visibility and visual appeal in the documentation.
Implemented a binomial tree pricing model with tree generation functionality in the pricing module. Added new methods in the model for binomial tree price calculation and introduced a visualization module to draw the binomial tree. Updated Cargo.toml to include the `plotters` dependency for rendering the tree.
Introduce utility functions for binomial tree calculations in `utils.rs`. Refactor `binomial_model.rs` to use these new utility functions, ensuring a cleaner and more modular code structure. Add documentation and tests to `visualization/binomial_tree.rs`.
Updated the canvas size to 1200x800 for better visibility and adjusted label positioning and font sizes for clearer text rendering. Enhanced the binomial tree visualization with more precise label placements and additional data points.
Feature/draw binomial tree
Refactored `calculate_down_factor` to use explicit volatility and time parameters. Added comprehensive unit tests for pricing utility functions and modified `generate_binomial_tree` to use the new `calculate_down_factor` signature. Enhanced visualization code readability by minor formatting adjustments.
Introduce calculate_option_node_value to streamline the binomial options pricing calculation. Refactor binomial_model.rs to use the new function, improving code clarity and maintainability.
Removed the unused `calculate_payoff` function from utils.rs in favor of directly implementing its logic within `price_binomial`. This change simplifies the code and ensures the correct payoff calculation for American and European options, reducing redundancy and potential errors. Updated tests to validate the new implementation and corrected test parameters and expected outcomes.
Removed unused and commented-out function `calculate_payoff` to improve code readability. Also added a newline for consistency in function separation.
Enhanced the documentation for various functions in `utils.rs` to provide a clearer understanding of their purposes, parameters, and return values. This includes detailed descriptions for up and down factor calculations, probability, discount factor, discounted payoff, option price, and discounted value calculations.
Refactor calculation of option node values to handle both European and American options. Add unit tests for European and American put option pricing, and a test for the calculate_probability function to ensure correctness of the implementation.
Renamed and refactored functions to improve code readability and modularity. Introduced `option_node_value` and `option_node_value_wrapper` for clearer separation of concerns in computing option node values. Updated relevant calls and adjusted parameters for consistency and clarity.
This commit updates the `payoff` function to use a new `PayoffInfo` struct instead of separate parameters for `spot`, `strike`, and `style`. This allows for greater flexibility and makes it easier to handle more complex option types that require additional information, such as spot price history for Asian options or minimum and maximum spot values for Lookback options.
Added `#[derive(Clone)]` to various enums to facilitate cloning. Updated the payoff calculation to handle optional spot prices and integrated the new `PayoffInfo` structure across the pricing module for cleaner code management and improved spot price handling.
Refactored the payoff calculation logic to use the PayoffInfo struct in various pricing functions. This change improves code readability and maintainability by consolidating the parameters into a single struct. It also restores the previously commented-out logic for Lookback option types, providing a more complete feature set.
Removed unnecessary `allow(dead_code)` attribute and added comments to clarify the usage of `spot_prices`, `spot_min`, and `spot_max` fields. These changes improve code readability and maintainability by ensuring that the intent of each field is documented.
Provide comprehensive comments explaining the fields of `PayoffInfo`, detailing each variable such as `spot`, `strike`, and option-specific attributes. This improves code readability and aids future developers in understanding the struct's purpose and usage.
Extracted common payoff calculation logic into a new `standard_payoff` function under `pricing::payoff.rs`. This reduces redundancy and simplifies the `OptionType` implementation by reusing the standardized payoff logic.
These tests cover European calls and puts, Asian arithmetic calls, barrier options, binary options, lookback options, quanto options, and power options. They verify that the `payoff` function correctly calculates the expected payoff for each option type.
Refactored the test setup by formatting struct initialization for readability. Added a Default implementation for the PayoffInfo struct to simplify creating instances with default values.
Extract calculation logic for various option types into separate functions. This improves code readability and maintainability by isolating the payoff logic for more complex option types like Asian, Barrier, Binary, and Lookback options.
Introduce `black_scholes_model.rs` for pricing options using the Black-Scholes formula. Removed `current_price` from the `Options` structure and renamed `Option` to `Options`. Added `statrs` and `greeks` utilities to support the new functionality and calculations.
Refactored the Black-Scholes model for better readability and maintainability. Enhanced documentation for utility functions and added comprehensive unit tests for `d1`, `d2`, `n`, `n_prime`, and `big_n`.
Consolidate d1 and d2 calculations into `calculate_d_values` for reuse. Extract and modularize option pricing into separate functions for calls and puts. Add comprehensive documentation and unit tests for new functions.
Introduce `constants` module with commonly used constants for time calculations. Enhance `black_scholes` function to handle multiple option types, adding detailed sub-functions for European option pricing. Comment out currently unused dependencies in `Cargo.toml` for a leaner build.
Enhanced the Options struct by adding a new field `dividend_yield` to account for dividends. Created a new module for Greek equations including delta, gamma, theta, vega, rho, and rho_d calculations, integrating the `dividend_yield` into these computations.
Updated the codebase to use the predefined constant ZERO instead of the floating-point literal 0.0. This improves code readability and maintainability by ensuring consistency and making the base value easily configurable from a single location.
This commit adds the #[allow(dead_code)] attribute to various functions, structures, and enums across src/pricing/utils.rs, src/surfaces/types.rs, and src/greeks/utils.rs. This change helps suppress compiler warnings for code that is not currently being used but could be useful in the future. Additionally, redundant computed_d1 variables are removed from test cases in src/greeks/utils.rs.
Eliminated redundant 'premium' field from Options struct and updated its usage across the codebase. Added a new Position struct to handle premium and other related attributes, along with comprehensive methods and tests for handling positions and their profits/losses.
Refactor the `unrealized_pnl` method to merge logic of realized PNL calculation. Removed realized_pnl method. Update and add unit tests to ensure correct functionality and introduce a constant ZERO for comparison.
Refactor `Position::new` to include a check that enforces positive premium values by converting negatives to positives. Added a helper method `check_premium` for this purpose and a corresponding test to validate the change.
This refactor replaces numerical literals `0.0` with a named constant `ZERO` across several modules. This improves code readability and consistency by giving a descriptive name to the zero value.
Introduced the PnL struct for representing profit and loss details along with a related implementation for calculations. Also added the PnLCalculator trait for standardized PnL calculation methods.
Integrated a new Telegraph process for option pricing within the `Options` model. This change includes the `TelegraphProcess` struct and related methods to simulate state transitions, and a new calculation method in `Options` to utilize this process.
Replaced usage of `rand::Rng` with `rand::random` for simplicity and consistency. Removed unnecessary newline at the end of the file.
Refactored the `telegraph` function to accept optional lambda parameters and updated its signature. Documented the TelegraphProcess class and added a method to estimate telegraph parameters from historical data. Added comprehensive unit tests for the TelegraphProcess class and the telegraph function, including cases with provided and estimated parameters.
Enhanced types like `OptionType`, `ExoticParams`, `ExpirationDate`, etc., with `fmt::Display` and `fmt::Debug` traits for better formatting. Added comprehensive tests to ensure correct implementation of these traits.
Rearranged and removed unnecessary imports across multiple files to enhance readability and maintainability. Also modified some formatting in `format.rs` and `tests_options` to align with Rust best practices.
Removed unnecessary debugging prints from telegraph pricing function and simplified control flow. Updated tests to use more robust datetime handling methods and marked stochastic prices with comments to deactivate unreliable assertions.
Implemented `PnLCalculator` trait for `Options` and `Position` structs. Updated `PnL` struct fields to be optional to handle different calculation scenarios. Refactored `total_cost` and `pnl_at_expiration` methods in `Position` for clarity and added relevant unit tests.
Implemented a `Graph` trait for the `Options` struct to visualize intrinsic values versus price ranges. Adjusted the `fmt` method in `format.rs` to remove redundant "Option" text and added an example to demonstrate the new graphing functionality.
Modified `position` module to be public. Updated PnL calculation functions to accept an optional underlying price parameter and added detailed graphing capabilities to visualize PnL at expiration. Introduced new tests to ensure correctness of these functionalities.
Reorder imports for clarity and adjust formatting to enhance readability. This includes aligning comments, removing redundant operations, and properly spacing expressions for better code maintainability.
Updated the break-even logic to account for option style and position side, and simplified expiration date formatting. Added detailed unit tests for break-even analysis and cleaned up the graphical representation of positions with break-even lines labeled.
Implemented fmt::Debug and fmt::Display for the Position struct to aid in better logging and debugging. Added corresponding unit tests to verify the formatted output for both Debug and Display traits.
Introduce dark green and dark red colors for positive and negative PNL values, replacing the single blue line with color-coded segments. Refactor point sizes and vertical label offsets for clarity, and improve overall visual consistency.
Refactor points of max and min PNL adjustments to include a margin for better scale visualization. Clean up redundant comments and ensure proper formatting of code blocks for improved readability.
joaquinbejar
added a commit
that referenced
this pull request
Aug 21, 2024
Merge pull request #1 from joaquinbejar/main
This was referenced Dec 25, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Recent Updates
Implementation of the Binomial Model for Option Pricing
We have successfully implemented a robust binomial model for option pricing. This implementation includes:
Flexible Option Types: Our model now supports various option types including European, American, and exotic options like Asian, Barrier, and more.
Comprehensive Pricing Parameters: We've introduced a
BinomialPricingParamsstruct that encapsulates all necessary parameters for option pricing, including asset price, volatility, interest rate, strike price, time to expiry, number of steps, option type, option style (Call/Put), and trade side (Long/Short).Efficient Pricing Algorithm: The
price_binomialfunction implements an efficient binomial tree algorithm for option pricing. It handles special cases such as zero time to expiry and zero volatility.Support for Both Call and Put Options: Our implementation allows pricing of both call and put options through the
OptionStyleenum.Long and Short Positions: The model accounts for both long and short positions in options through the
Sideenum.Payoff Trait: We've introduced a
Payofftrait that allows for easy extension to new option types in the future.Comprehensive Test Suite: We've developed a suite of unit tests to ensure the accuracy of our pricing model under various scenarios, including edge cases.
Code Optimization: We've addressed Clippy warnings and optimized our code for better performance and readability.
Detailed Documentation: We've added comprehensive documentation to our main pricing function, explaining its usage, parameters, and providing examples.
Future Work
We're excited about these improvements and look forward to further enhancing our options trading library!