From 906c2dee383a7eb72632bfbdf61916a576a5460d Mon Sep 17 00:00:00 2001 From: Bertrand Date: Sun, 21 Feb 2016 16:35:30 +0000 Subject: création de la struct f_mu MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.cpp | 29 +++++++++++++++------ src/opti.cpp | 20 +++++---------- src/option.cpp | 61 +++++---------------------------------------- src/stratified_sampling.cpp | 2 ++ src/stratified_sampling.hpp | 33 +++++++++++++++++++++--- 5 files changed, 66 insertions(+), 79 deletions(-) (limited to 'src') diff --git a/src/main.cpp b/src/main.cpp index f6e998a..561b8c2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,6 +6,7 @@ #include "stratified_sampling.hpp" #include #include +#include "opti.hpp" using namespace std; //--génération quantiles-- @@ -43,16 +44,30 @@ x = 1.64*S.estimator().second; }; + void exemple2 (){ + std::vector mu(16); + mu = argmax(0.05, 1.0, 50, 0.1, 45, 16); + double norm_mu = 0; + std::vector u(16); + for(int i=0; i<16; i++) { + norm_mu += mu[i]*mu[i]; + u[i] = mu[i]/norm_mu; + } + vector q = quantile_norm(100, 1); + vector p(100, 0.01); + asian_option A(0.05, 1.0, 50, 0.1, 45, 16); + f_mu G(mu,A); + multi_gaussian_truncated MG(q[50],q[51], u); + for(int i=0; i<10; i++){ + std::cout< u {sqrt(0.2), sqrt(0.2), sqrt(0.2), sqrt(0.2), sqrt(0.2)}; - multi_gaussian_truncated G(0, 2, u); - std::vector r(5); - r = G(); - for (int i=0; i<5; i++){ - std::cout< -#include -#include -#include -#include +#include "opti.hpp" double pos (double x){ return x>0?x:0; @@ -30,17 +26,17 @@ double f (const std::vector &X, std::vector &grad, void *params) -int main() { +std::vector argmax(double r, double T, double S0, double V, double K, int d){ - double params[5] = {0.05, 1, 50, 0.1, 45}; + double params[5] = {r, T, S0, V, K}; - nlopt::opt opt(nlopt::LN_COBYLA, 16); + nlopt::opt opt(nlopt::LN_COBYLA, d); opt.set_max_objective(f, ¶ms); opt.set_xtol_rel(1e-4); - std::vector x(16,0); + std::vector x(d,0); std::vector g(0); @@ -48,12 +44,8 @@ int main() { double maxf; nlopt::result result = opt.optimize(x, maxf); - for(int i=0; i<16; i++){ - std::cout< +#include "option.hpp" #include -#include "rqmc.hpp" -#include "p_adic.o" - -double frac_part(double x){ - return x - floor(x); -} double pos (double x){ return x>0?x:0; } -struct asian_option : public std::unary_function, double> -{ - asian_option(double r, double T, double S0, double V, int d, double K) - : r(r), T(T), S0(S0), V(V), d(d), K(K) {}; - - double operator()(std::vector X) const { - std::vector S(d); - S[0]= S0*exp((r-V*V/2)*(T/d)+V*sqrt(T/d)*X[0]); - for(int i=1;i -struct quasi_option : public generator -{ - quasi_option(int n, int d, Fct payoff) : n(n), d(d), payoff(payoff), G(d) {}; - - typename Fct::result_type operator()() { - double sum =0; - for(int i=0; i G; - }; +double frac_part(double x){ + return x - floor(x); +} int main(){ init_alea(1); @@ -73,9 +24,9 @@ int main(){ } std::vector result2(3); - result = monte_carlo(100, quasi_option (N, d, A)); + result2 = monte_carlo(100, quasi_option (N, d, A)); for(int i =0; i<3; i++){ - std::cout< mean_var( std::vector r){ std::pair p; for(auto &x: r){ @@ -12,3 +13,4 @@ std::pair mean_var( std::vector r){ return p; } + diff --git a/src/stratified_sampling.hpp b/src/stratified_sampling.hpp index cebee8d..7573ef2 100644 --- a/src/stratified_sampling.hpp +++ b/src/stratified_sampling.hpp @@ -5,6 +5,7 @@ #include "rtnorm.hpp" #include "var_alea.hpp" #include +#include "option.hpp" using namespace std; @@ -45,14 +46,14 @@ struct multi_gaussian_truncated : public var_alea > double v = V(); double Z = gsl_cdf_gaussian_Pinv(v,1); std::vector Y(d); - double scal = 0; for(int i=0; i X(d); + double scal = 0; for(int j=0; j X(d); for(int i=0; i estimator() const; private: - void update(int N); + void update(int N); vector p; vector X; vector M; @@ -187,3 +188,29 @@ pair stratified_sampling::estimator() const { } return {est_mean, est_std}; } + +struct f_mu : public std::unary_function, double> +{ + f_mu(std::vector mu, asian_option A) : mu(mu), A(A){ + double norm_mu = 0; + for(int i=0; i<16; i++) { + norm_mu += mu[i]*mu[i]; + } + }; + + double operator()(std::vector X) { + std::vector Y(X.size()); + double scal = 0; + for (unsigned int i=0; i mu; + asian_option A; + double norm_mu; + }; + -- cgit v1.2.3-70-g09d2