aboutsummaryrefslogtreecommitdiffstats
path: root/src/option.hpp
blob: 387638c9a16a62eb74a43cd71f5edfb1aa39a283 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <vector>
#include <algorithm>

double pos (double x);

struct asian_option : public std::unary_function<std::vector<double>, double>
{
    asian_option(double r, double T, double S0, double V, double K, bool call=true)
        : r(r), T(T), S0(S0), V(V), K(K), call(call) {};

    double operator()(const std::vector<double> &X) const {
        int d = X.size();
        std::vector<double> S(d);
        S[0]= S0*exp((r-V*V/2)*(T/d)+V*sqrt(T/d)*X[0]);
        for(int i=1;i<d;i++){
            S[i]=S[i-1]*exp((r-V*V/2)*(T/d)+V*sqrt(T/d)*X[i]);
        }
        double temp = std::accumulate(S.begin(), S.end(), 0.)/d;
        if (call) {
            return  exp(-r*T)*pos(temp-K);
            } 
        else {
            return exp(-r*T)*pos(K-temp);
            }
        };

    private:
        double r;
        double T;
        double S0;
        double V;
        double K;
        bool call;
};