-1

I am new to C++ and this is my first program that I am trying to write. In the below code I want to simulate a price of an option and calculate its value. I am getting an error for the accumulate function.

I have already tried std::max and std::accumulate but they don't work as well.

#include <iostream>
#include <algorithm>
#include <cmath>
#include<random>
#include<numeric>
#include<vector>
using namespace std;

double mc(int S, int K, float r, float vol, int T, int sim, int N){
mt19937 rng;
normal_distribution<>ND(0,1);
ND(rng);
std::vector<double> s(sim);
double dt = T/N;
for(int i =0;i<sim;i++)
{
    std::vector<double> p(N);
    p[0]=S;
    for(int k = 0;k<N;k++)
    {
        double phi = ND(rng);
        p[i+1] = p[i]*(1+r*dt+vol*phi*sqrt(dt));

    }
    s[i] = max(p[N-1]-K,0);

}
        float payoff = (accumulate(s)/sim)*exp(-r*T);
        return payoff;
}

int main(){
    cout << mc(100,100,0.05,0.2,1,100,100) << endl;
    return 0;
}

Errors :

> test1.cpp:26:21: error: no matching function for call to 'accumulate'
>     float payoff = (accumulate(s)/sim)*exp(-r*T);
>                     ^~~~~~~~~~ /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/numeric:158:1:
> note: candidate function template not viable: requires 3 arguments,
> but 1 was provided accumulate(_InputIterator __first, _InputIterator
> __last, _Tp __init) ^ /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/numeric:168:1:
> note: candidate function template not viable: requires 4 arguments,
> but 1 was provided accumulate(_InputIterator __first, _InputIterator
> __last, _Tp __init, _BinaryOperation __binary_op) ^ 2 errors generated.

EDIT: fixed max function. it uses 0.0 instead of 0

bitmask
  • 32,434
  • 14
  • 99
  • 159
Varun Yadav
  • 43
  • 2
  • 11
  • Unrelated to your (current) problems, but `p[i+1]` will go out of bounds. – Some programmer dude May 04 '19 at 14:21
  • And what is the purpose of that inner loop? The only thing that will vary is the random number `phi`. More specifically, `p[i]` will *always* be the same value, and you will overwrite `p[i + 1]` each iteration, without using it. – Some programmer dude May 04 '19 at 14:23
  • As for your problem, please see e.g. [this `std::max`](https://en.cppreference.com/w/cpp/algorithm/max) and [this `std::accumulate`](https://en.cppreference.com/w/cpp/algorithm/accumulate) references. And of course ***read*** the error messages, they show you how to call these functions! – Some programmer dude May 04 '19 at 14:25
  • @Someprogrammerdude thanks for `p[i+1]` you are right. the inner loop is a single path simulation and the outer is the number of simulations. I have fixed the max function but still having issues with accumulate. – Varun Yadav May 04 '19 at 14:27
  • `std::accumulate` requires two iterators (begin and end), initial value and optionally a function object to be applied. You can't just dump in a vector there. See the examples of usage in the link provided by Some programmer dude. – Yksisarvinen May 04 '19 at 14:33
  • I tried `std::accumulate(s[0],s[sim]);` that didn't work as well – Varun Yadav May 04 '19 at 14:37
  • Don't guess the syntax. Look it up. Both syntax and examples of usage, starting from most basic to more complicated ones, is given to you freely in the [cppreference for `std::accumulate`](https://en.cppreference.com/w/cpp/algorithm/accumulate#Example). Your attempt tries to pass two `double` values, not two iterators. And you also need initial value as the argument. – Yksisarvinen May 04 '19 at 14:39
  • @Yksisarvinen I gave up and used a for loop instead. – Varun Yadav May 04 '19 at 14:51
  • Who are you `using namespace std;` if you are going to `std::` ? – Podo May 04 '19 at 18:35

1 Answers1

1

Reading the C++ standard library documentation on std::accumulate would solve your problem. But since you're new to the language and the STL is a bit hard to decipher for beginners, here's how to read the documentation.

template< class InputIt, class T >
T accumulate( InputIt first, InputIt last, T init );

std::accumulate is a generic function, so it's templated on a generic type, T. In your case, T = double. It takes two input iterators, first and last, and an initial value init, of type T = double. So, below is an example on how to accumulate a std::vector<double>.

std::vector<double> v = { 1., 2., 3. };
double result = std::accumulate(v.begin(), v.end(), 0.);

Note that vector::begin and vector::end return iterators to the beginning and end, respectively, of the container.

Replace your call to accumulate using iterators and supplying an initial value.

Richard
  • 338
  • 2
  • 11