C++ 尝试使用 max 和 accumulate 函数
C++ trying to use max and accumulate function
我是 C++ 新手,这是我尝试编写的第一个程序。在下面的代码中,我想模拟一个期权的价格并计算它的价值。我收到 accumulate 函数的错误。
我已经尝试了 std::max
和 std::accumulate
,但效果不佳。
#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;
}
错误:
> 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.
编辑:固定最大值函数。它使用 0.0 而不是 0
阅读 std::accumulate
上的 C++ 标准库文档可以解决您的问题。但由于您是该语言的新手,而且 STL 对于初学者来说有点难以解读,因此如何阅读文档。
template< class InputIt, class T >
T accumulate( InputIt first, InputIt last, T init );
std::accumulate
是一个泛型函数,因此它是在泛型类型 T
上模板化的。在你的情况下,T = double
。它需要两个输入迭代器 first
和 last
,以及一个类型为 T = double
的初始值 init
。因此,下面是一个关于如何累积 std::vector<double>
.
的示例
std::vector<double> v = { 1., 2., 3. };
double result = std::accumulate(v.begin(), v.end(), 0.);
注意 vector::begin
和 vector::end
return 迭代器 分别指向容器的开头和结尾。
使用迭代器替换对 accumulate
的调用并提供初始值。
我是 C++ 新手,这是我尝试编写的第一个程序。在下面的代码中,我想模拟一个期权的价格并计算它的价值。我收到 accumulate 函数的错误。
我已经尝试了 std::max
和 std::accumulate
,但效果不佳。
#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;
}
错误:
> 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.
编辑:固定最大值函数。它使用 0.0 而不是 0
阅读 std::accumulate
上的 C++ 标准库文档可以解决您的问题。但由于您是该语言的新手,而且 STL 对于初学者来说有点难以解读,因此如何阅读文档。
template< class InputIt, class T >
T accumulate( InputIt first, InputIt last, T init );
std::accumulate
是一个泛型函数,因此它是在泛型类型 T
上模板化的。在你的情况下,T = double
。它需要两个输入迭代器 first
和 last
,以及一个类型为 T = double
的初始值 init
。因此,下面是一个关于如何累积 std::vector<double>
.
std::vector<double> v = { 1., 2., 3. };
double result = std::accumulate(v.begin(), v.end(), 0.);
注意 vector::begin
和 vector::end
return 迭代器 分别指向容器的开头和结尾。
使用迭代器替换对 accumulate
的调用并提供初始值。