Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
BasedOnStyle: LLVM
IndentWidth: 4
ColumnLimit: 100
13 changes: 13 additions & 0 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
name: Bug Report
about: Create a bug report to help us improve
---

**Describe the bug**
A clear and concise description of what the bug is.

**To Reproduce**
Steps to reproduce the behavior.

**Expected behavior**
A clear and concise description of what you expected to happen.
5 changes: 5 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## Summary
- Provide a concise description of changes.

## Testing
- `ctest -V`
15 changes: 15 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: CI

on: [push, pull_request]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Configure
run: cmake -S . -B build
- name: Build
run: cmake --build build
- name: Test
run: ctest --test-dir build --output-on-failure
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

include_directories(${PROJECT_SOURCE_DIR}/include)

add_subdirectory(src)

Expand Down
3 changes: 3 additions & 0 deletions CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Code of Conduct

Be respectful and considerate in all interactions. Harassment or abusive behavior will not be tolerated. If you encounter any issues, please contact the maintainers.
8 changes: 8 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Contributing

Thank you for considering contributing to this project! Please open an issue first to discuss any major changes. Pull requests should:

1. Build successfully with CMake.
2. Run `ctest` and pass all tests.

Feel free to propose enhancements and bug fixes.
79 changes: 8 additions & 71 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

Bootstrap for implementing code snippets from Duffy's *Financial Instrument Pricing Using C++*.

### Quick Start
1. `mkdir build && cd build`
2. `cmake .. && cmake --build .`
3. `ctest -V`

## Requirements

- CMake >= 3.15
Expand Down Expand Up @@ -107,76 +112,8 @@ ctest -V

This will execute the `option_pricing` unit tests (normal PDF/CDF checks and basic call/put pricing validations).

## Getting Started Guide for Java Developers

If you come from a Java background and haven’t touched C++ in a while (or ever), this section will help you:

- Understand the C++ project layout and how it compares to a typical Java project
- Learn the CMake build workflow (analogy to Maven/Gradle)
- Pick up the core C++ language features used in this codebase

### 1. Project Layout Comparison

```text
Java: C++ (this project):
src/main/java/... include/sud.hpp, include/option_pricing.hpp
src/main/resources/... src/main.cpp
pom.xml / build.gradle CMakeLists.txt (root & src)
```

• **Headers (`.hpp`)** declare interfaces and templates (like Java interfaces or class declarations).
• **Sources (`.cpp`)** contain implementations (like Java classes with method bodies).
• **`CMakeLists.txt`** serves as the build spec (similar to `pom.xml` or `build.gradle`).

### 2. CMake vs. Maven/Gradle

| Step | Java (Maven) | C++ (CMake) |
|:-----------------------|:--------------------------|:-------------------------------------|
| Configure/dependencies | `mvn compile` | `cmake ..` |
| Compile & link | `mvn package` | `cmake --build .` |
| Run tests | `mvn test` | (CTest or manual test executable) |
| Execute application | `java -jar target/app.jar`| `./option_pricing` |

### 3. Essential C++ Constructs

1. **Templates** (generic code):
```cpp
template<typename T>
using OptionData = std::tuple<T,T,T,T>;
```

2. **Type aliases** (`using`):
```cpp
using ComputedData = std::tuple<V,V,V>;
```

3. **Tuples** (multiple return values):
```cpp
return std::make_tuple(price, delta, gamma);
```

4. **Lambdas & callbacks** (`std::function`):
```cpp
IAlgorithm<double> bs = [](auto od, double S) { /* ... */ };
```

5. **Inline/free functions** for small utilities:
```cpp
inline double N(double x) { return 0.5*(1-std::erf(-x/√2)); }
```

6. **Thread safety** with `std::mutex` + `std::lock_guard` (vs Java `synchronized`):
```cpp
static std::mutex m;
std::lock_guard<std::mutex> lock(m);
```

### 4. IDE & Tooling

- **CLion**: IntelliJ-like C++ IDE—open the root folder, and it imports the CMake project automatically.
- **Command-line**: requires a C++17 compiler (`g++`/`clang++`) and CMake installed in your PATH.
- **.gitignore**: filters build artifacts, IDE settings, and temporary files so your repo stays clean.
For a detailed introduction, see [docs/JavaGuide.md](docs/JavaGuide.md).

With these points in mind, you’ll feel right at home extending and navigating this C++ project using patterns and workflows you already know from Java.
## License

Happy coding!
This project is licensed under the MIT License. Pricing formulas are provided for educational purposes only and should not be used in production without proper validation.
3 changes: 3 additions & 0 deletions SECURITY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Security Policy

Please report any security vulnerabilities by opening an issue or contacting the maintainers privately. We will respond as soon as possible.
73 changes: 73 additions & 0 deletions docs/JavaGuide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
## Getting Started Guide for Java Developers

If you come from a Java background and haven’t touched C++ in a while (or ever), this section will help you:

- Understand the C++ project layout and how it compares to a typical Java project
- Learn the CMake build workflow (analogy to Maven/Gradle)
- Pick up the core C++ language features used in this codebase

### 1. Project Layout Comparison

```text
Java: C++ (this project):
src/main/java/... include/sud.hpp, include/option_pricing.hpp
src/main/resources/... src/main.cpp
pom.xml / build.gradle CMakeLists.txt (root & src)
```

• **Headers (`.hpp`)** declare interfaces and templates (like Java interfaces or class declarations).
• **Sources (`.cpp`)** contain implementations (like Java classes with method bodies).
• **`CMakeLists.txt`** serves as the build spec (similar to `pom.xml` or `build.gradle`).

### 2. CMake vs. Maven/Gradle

| Step | Java (Maven) | C++ (CMake) |
|:-----------------------|:--------------------------|:-------------------------------------|
| Configure/dependencies | `mvn compile` | `cmake ..` |
| Compile & link | `mvn package` | `cmake --build .` |
| Run tests | `mvn test` | (CTest or manual test executable) |
| Execute application | `java -jar target/app.jar`| `./option_pricing` |

### 3. Essential C++ Constructs

1. **Templates** (generic code):
```cpp
template<typename T>
using OptionData = std::tuple<T,T,T,T>;
```

2. **Type aliases** (`using`):
```cpp
using ComputedData = std::tuple<V,V,V>;
```

3. **Tuples** (multiple return values):
```cpp
return std::make_tuple(price, delta, gamma);
```

4. **Lambdas & callbacks** (`std::function`):
```cpp
IAlgorithm<double> bs = [](auto od, double S) { /* ... */ };
```

5. **Inline/free functions** for small utilities:
```cpp
inline double N(double x) { return 0.5*(1-std::erf(-x/√2)); }
```

6. **Thread safety** with `std::mutex` + `std::lock_guard` (vs Java `synchronized`):
```cpp
static std::mutex m;
std::lock_guard<std::mutex> lock(m);
```

### 4. IDE & Tooling

- **CLion**: IntelliJ-like C++ IDE—open the root folder, and it imports the CMake project automatically.
- **Command-line**: requires a C++17 compiler (`g++`/`clang++`) and CMake installed in your PATH.
- **.gitignore**: filters build artifacts, IDE settings, and temporary files so your repo stays clean.

With these points in mind, you’ll feel right at home extending and navigating this C++ project using patterns and workflows you already know from Java.

Happy coding!
14 changes: 10 additions & 4 deletions include/option_pricing.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,21 @@

#include "sud.hpp"

namespace op {

// Standard normal density function
inline double n(double x)
{
const double A = 1.0 / std::sqrt(2.0 * 3.14159265358979323846);
constexpr double PI = 3.14159265358979323846;
const double A = 1.0 / std::sqrt(2.0 * PI);
return A * std::exp(-x * x * 0.5);
}

// Cumulative normal distribution using error function
static inline auto cndN = [](double x) {
inline double cndN(double x)
{
return 0.5 * (1.0 - std::erf(-x / std::sqrt(2.0)));
};
}

inline double N(double x)
{
Expand Down Expand Up @@ -155,4 +159,6 @@ class ProcessingII
{
return PutValues(optData, S);
}
};
};

} // namespace op
7 changes: 6 additions & 1 deletion include/sud.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <tuple>
#include <functional>

namespace op {

template <typename T>
using OptionData = std::tuple<T, T, T, T>;

Expand Down Expand Up @@ -36,4 +38,7 @@ class SUD : private Source<T>, private Sink<T>
SendData(t2);
end();
}
};
};

} // namespace op

8 changes: 4 additions & 4 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
file(GLOB SOURCES *.cpp)

add_executable(option_pricing ${SOURCES})
add_executable(option_pricing
main.cpp
)

# Place the executable in the top-level build directory for ease of use
set_target_properties(option_pricing PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}
)

target_include_directories(option_pricing PUBLIC ${PROJECT_SOURCE_DIR}/include)
target_include_directories(option_pricing PUBLIC ${PROJECT_SOURCE_DIR}/include)
10 changes: 5 additions & 5 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ int main() {
using value_type = double;

// Instantiate call and put processing policies
Processing<value_type> converter;
ProcessingII<value_type> converter2;
op::Processing<value_type> converter;
op::ProcessingII<value_type> converter2;

// Pricing calls
SUD<value_type, Input, Output> callPricer(converter);
op::SUD<value_type, op::Input, op::Output> callPricer(converter);
value_type S = 60.0;
callPricer.run(S);

// Pricing puts
SUD<value_type, Input, Output> putPricer(converter2);
op::SUD<value_type, op::Input, op::Output> putPricer(converter2);
value_type S2 = 60.0;
putPricer.run(S2);

return 0;
}
}
5 changes: 2 additions & 3 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
file(GLOB TEST_SOURCES *.cpp)
add_executable(test_option_pricing test_option_pricing.cpp)

add_executable(test_option_pricing ${TEST_SOURCES})
target_include_directories(test_option_pricing PRIVATE ${PROJECT_SOURCE_DIR}/include)

# Register with CTest
add_test(NAME option_pricing_unit_tests COMMAND test_option_pricing)
add_test(NAME option_pricing_unit_tests COMMAND test_option_pricing)
12 changes: 6 additions & 6 deletions tests/test_option_pricing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ int main()
{
// Test standard normal PDF at 0: n(0) = 1/sqrt(2*pi)
const double PI = 3.14159265358979323846;
double pdf0 = n(0.0);
double pdf0 = op::n(0.0);
assert(std::abs(pdf0 - (1.0 / std::sqrt(2.0 * PI))) < 1e-12);

// Test cumulative normal CDF at 0: N(0) = 0.5
double cdf0 = N(0.0);
double cdf0 = op::N(0.0);
assert(std::abs(cdf0 - 0.5) < 1e-12);

// Prepare option data: K=100, T=1, r=0, v=0.2
OptionData<double> data(100.0, 1.0, 0.0, 0.2);
op::OptionData<double> data(100.0, 1.0, 0.0, 0.2);

// Test call values: price>0, 0<delta<1, gamma>0
auto call = CallValues(data, 100.0);
auto call = op::CallValues(data, 100.0);
double price = std::get<0>(call);
double delta = std::get<1>(call);
double gamma = std::get<2>(call);
Expand All @@ -28,7 +28,7 @@ int main()
assert(gamma > 0.0);

// Test put values: price>0, delta<0, gamma>0
auto put = PutValues(data, 100.0);
auto put = op::PutValues(data, 100.0);
double price2 = std::get<0>(put);
double delta2 = std::get<1>(put);
double gamma2 = std::get<2>(put);
Expand All @@ -38,4 +38,4 @@ int main()

std::cout << "All unit tests passed." << std::endl;
return 0;
}
}