-
Notifications
You must be signed in to change notification settings - Fork 3
Custom non-convex optimizer (PSGD) #43
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,3 +2,6 @@ build | |
| cmake-build-debug | ||
| venv | ||
| .idea | ||
| *.so | ||
| *.pyc | ||
| __pycache__/ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| #ifndef FINMATH_OPTIMIZATION_LINALG_HELPERS_H | ||
| #define FINMATH_OPTIMIZATION_LINALG_HELPERS_H | ||
|
|
||
| #include <vector> | ||
| #include <cmath> | ||
| #include <numeric> | ||
| #include <random> | ||
| #include <stdexcept> | ||
|
|
||
| namespace finmath | ||
| { | ||
| namespace optimization | ||
| { | ||
| namespace linalg | ||
| { | ||
|
|
||
| // Calculate L2 norm (Euclidean norm) | ||
| double norm(const std::vector<double> &v); | ||
|
|
||
| // In-place vector addition: a += b | ||
| void add_vectors(std::vector<double> &a, const std::vector<double> &b); | ||
|
|
||
| // In-place vector subtraction: a -= b | ||
| void subtract_vectors(std::vector<double> &a, const std::vector<double> &b); | ||
|
|
||
| // In-place scalar multiplication: v *= scalar | ||
| void scale_vector(std::vector<double> &v, double scalar); | ||
|
|
||
| // In-place addition with scaling: a += scalar * b | ||
| void add_scaled_vector(std::vector<double> &a, const std::vector<double> &b, double scalar); | ||
|
|
||
| // In-place subtraction with scaling: a -= scalar * b | ||
| void subtract_scaled_vector(std::vector<double> &a, const std::vector<double> &b, double scalar); | ||
|
|
||
| // Clip vector v in-place if its norm exceeds max_norm | ||
| void clip_vector_norm(std::vector<double> &v, double max_norm); | ||
|
|
||
| // Sample a vector uniformly from a ball of given radius and dimension | ||
| std::vector<double> sample_uniform_ball(double radius, size_t dimension, std::mt19937 &rng_engine); | ||
|
|
||
| } // namespace linalg | ||
| } // namespace optimization | ||
| } // namespace finmath | ||
|
|
||
| #endif // FINMATH_OPTIMIZATION_LINALG_HELPERS_H |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| #ifndef FINMATH_OPTIMIZATION_PSGD_H | ||
| #define FINMATH_OPTIMIZATION_PSGD_H | ||
|
|
||
| #include <vector> | ||
| #include <functional> | ||
| #include <stdexcept> // For potential exceptions | ||
|
|
||
| namespace finmath | ||
| { | ||
| namespace optimization | ||
| { | ||
|
|
||
| /** | ||
| * @brief Implements the Enhanced Perturbed Stochastic Gradient Descent (PSGD-C) algorithm. | ||
| * | ||
| * Based on the algorithm described in the user-provided LaTeX source and Python implementation. | ||
| * Designed for non-convex optimization, incorporating EMA smoothing, gradient/parameter clipping, | ||
| * and noise injection to escape saddle points. | ||
| * | ||
| * @param stochastic_grad Function providing a stochastic gradient estimate for a given point x. | ||
| * Signature: std::vector<double>(const std::vector<double>& x) | ||
| * @param objective_f Function providing the objective function value f(x). Needed for threshold calculation. | ||
| * Signature: double(const std::vector<double>& x) | ||
| * @param x0 Initial starting point (vector). | ||
| * @param ell Smoothness parameter (Lipschitz constant of the gradient). | ||
| * @param rho Hessian Lipschitz parameter. | ||
| * @param eps Target accuracy for the norm of the smoothed gradient (termination criterion). | ||
| * @param sigma Standard deviation estimate of the noise in the stochastic gradient samples (||grad_f_i(x) - grad_f(x)||). | ||
| * @param delta Confidence parameter (probability of failure). | ||
| * @param batch_size Mini-batch size used by stochastic_grad (influences g_thresh). | ||
| * @param step_size_coeff Coefficient 'c' to calculate step size eta = c / ell. | ||
| * @param ema_beta Decay factor for the exponential moving average of the gradient (typically 0.8-0.95). | ||
| * @param max_iters Maximum number of iterations to perform. | ||
| * @param grad_clip_norm Maximum L2 norm for the raw stochastic gradient (g_max). Set <= 0 to disable. | ||
| * @param param_clip_norm Maximum L2 norm for the parameter vector x (x_max). Set <= 0 to disable. | ||
| * @return The final optimized parameter vector x. | ||
| * | ||
| * @throws std::invalid_argument if input parameters are inconsistent (e.g., ell <= 0, rho <= 0, eps <= 0, sigma < 0, 0 < delta < 1, batch_size <= 0, c <= 0, 0 <= ema_beta < 1). | ||
|
Comment on lines
+29
to
+38
|
||
| */ | ||
| std::vector<double> perturbed_sgd( | ||
| const std::function<std::vector<double>(const std::vector<double> &)> &stochastic_grad, | ||
| const std::function<double(const std::vector<double> &)> &objective_f, | ||
| const std::vector<double> &x0, | ||
| // Problem params | ||
| double ell, | ||
| double rho, | ||
| double eps = 1e-3, | ||
| double sigma = 0.1, | ||
| double delta = 0.1, | ||
| // Algo params | ||
| int batch_size = 32, | ||
| double step_size_coeff = 0.5, | ||
| double ema_beta = 0.9, | ||
| int max_iters = 100000, | ||
| double grad_clip_norm = 10.0, // Default g_max (or disable if <= 0) | ||
| double param_clip_norm = 100.0 // Default x_max (or disable if <= 0) | ||
| ); | ||
|
|
||
| } // namespace optimization | ||
| } // namespace finmath | ||
|
|
||
| #endif // FINMATH_OPTIMIZATION_PSGD_H | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,21 @@ | ||
| #ifndef RSI_H | ||
| #define RSI_H | ||
|
|
||
| #include<vector> | ||
| #include <vector> | ||
| #include <pybind11/numpy.h> | ||
|
|
||
| //function to compute the average gain over a window | ||
| double compute_avg_gain(const std::vector<double>& price_changes, size_t window_size); | ||
| namespace py = pybind11; | ||
|
|
||
| // function to compute the average gain over a window | ||
| double compute_avg_gain(const std::vector<double> &price_changes, size_t window_size); | ||
|
|
||
| // Function to compute the average loss over a window | ||
| double compute_avg_loss(const std::vector<double>& price_changes, size_t window_size); | ||
| double compute_avg_loss(const std::vector<double> &price_changes, size_t window_size); | ||
|
|
||
| // Function to compute the RSI from a time series of prices | ||
| std::vector<double> compute_smoothed_rsi(const std::vector<double>& prices, size_t window_size); | ||
| std::vector<double> compute_smoothed_rsi(const std::vector<double> &prices, size_t window_size); | ||
|
|
||
| // NumPy overload | ||
| std::vector<double> compute_smoothed_rsi_np(py::array_t<double> prices_arr, size_t window_size); | ||
|
|
||
| #endif // RSI_H |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Large blocks of commented-out code (lines 39-52) should be removed rather than left in the codebase. If these test executables are temporarily disabled, a comment should explain why and when they'll be re-enabled. Otherwise, remove the commented code to improve maintainability.