从在线浮动的这个特定矩阵类中传递一个向量

计算科学 matlab 算法 C++
2021-12-24 17:47:18

我在这里使用这个矩阵类帮助我转换这个最小二乘算法,该算法为在 C++ 中用 Matlab 编写的美式期权定价。我的问题很简单。我想将一个向量传递给一个函数,但它必须与这个矩阵类一起使用。出于某种原因,我收到一条错误消息:

  cannot convert 'QSMatrix<double>' to 'double' for argument '2' to 
'QSMatrix<double> randn_vec2(int, double)'

我的代码中有很多注释,所以应该很容易理解。我正在使用上面链接的矩阵类,所以我将发布我的主文件以完成。非常感谢任何意见或建议。

 #include <iostream>
#include <cmath>
#include <limits>
#include <algorithm>
#include "matrix.h"

using namespace std;


double LaguerreExplicit(int R, double x); // Generates the (weighted) laguerre value
double payoff_Call(double S, double K); // Pay off of a call option
double LSM(int T, double r, double sigma , double K, double S0, int N, int M, int R);
double generateGaussianNoise(double mu, double sigma); // Generates Normally distributed random numbers
// Notation:
// T        expiration time
// r        riskless interest rate
// sigma    volatility
// K        strike price
// S0       initial asset price
// N        number of time steps
// M        number of paths
// R        number of basis functions
QSMatrix<double> randn_vec(int n, int M);
QSMatrix<double> range(double min, double max, double N); // Creates a vector of evenly spaced numbers
QSMatrix<double> rand_vec(int n, int k); // k = 1 to make this into a vector
QSMatrix<double> randn_vec2(int n, int M); // this is to replace the compand [z;z] in Matlab

int main(){
    int M = 4;
    QSMatrix<double> z = randn_vec2(M/2,1);
    z.print();

    QSMatrix<double> B = range(0,1,6);
    //B.print();

    LSM(1, .06, .25 , 10.5, 12.5, 2, 4, 4);



      return 0;
}


double payoff_Call(double S, double K){
    double payoff;
    if((S - K) > 0)
    {
        payoff = S - K;
    }else
    {
        payoff = 0.0;
    }
    return payoff;
}

double LaguerreExplicit(int R, double x){
    double value;
    if(R==0)
    {
        value = 1;
    }
    else if(R==1)
    {
        value = 0.5*(pow(x,2) - 4.0*x + 2);
    }
    else if(R==3)
    {
        value = (1.0/6.0)*(-1*pow(x,3) + 9*pow(x,2) - 18*x + 6);
    }
    else if(R==4)
    {
        value = (1.0/24.0)*(pow(x,4) - 16*pow(x,3) + 72*pow(x,2) - 96*x + 24);
    }
    else if(R==5)
    {
        value = (1.0/120.0)*(-1*pow(x,5) + 25*pow(x,4) - 200*pow(x,3) + 600*pow(x,2) - 600*x + 120);
    }
    else if (R==6)
    {
        value = (1.0/720.0)*(pow(x,6) - 36*pow(x,5) + 450*pow(x,4) - 2400*pow(x,3) + 5400*pow(x,2) - 4320*x + 720);
    }
    else{
        cout << "Error!, R is out of range" << endl;
        value  = 0;
    }
    value = exp(-0.5*x)*value; // Weighted used in Longstaff-Scwartz
    return value;
}

double generateGaussianNoise(double mu, double sigma)
{
    const double epsilon = std::numeric_limits<double>::min();
    const double two_pi = 2.0*M_PI;

    static double z0, z1;
    static bool generate;
    generate = !generate;

    if (!generate)
       return z1 * sigma + mu;

    double u1, u2;
    do
     {
       u1 = rand() * (1.0 / RAND_MAX);
       u2 = rand() * (1.0 / RAND_MAX);
     }
    while ( u1 <= epsilon );

    z0 = sqrt(-2.0 * log(u1)) * cos(two_pi * u2);
    z1 = sqrt(-2.0 * log(u1)) * sin(two_pi * u2);
    return z0 * sigma + mu;
}


QSMatrix<double> range(double min, double max, double N){
    QSMatrix<double> mat(N+1,1,0);
    double delta = (max-min)/N;
    for(int i = 0; i < mat.get_rows();i++){
        for(int j = 0; j < mat.get_cols();j++){
            mat(i,j) = min + i*delta;
        }
    }
    return mat;
}

QSMatrix<double> randn_vec(int n, int M){
    QSMatrix<double> mat(n,1,0);
    for(int i = 0; i < mat.get_rows();i++){
        for(int j = 0; j < mat.get_cols();j++){
            mat(i,j) = generateGaussianNoise(M/2,1);
        }
    }
    return mat;
}

QSMatrix<double> randn_vec2(int n, int M){
    QSMatrix<double> mat = randn_vec(n,M);
    QSMatrix<double> mat1(2*n,1,0);
    for(int i = 0; i < mat1.get_rows();i++){
            for(int j = 0; j < mat1.get_cols();j++){
                mat1(i,j) = mat(0,j);
            }
        }
        return mat1;
}

QSMatrix<double> rand_vec(int n, int k){
    QSMatrix<double> mat(n,k,0);
    for(int i = 0; i < mat.get_rows(); i++){
        for(int j = 0; j < mat.get_cols(); j++){
            mat(i,j) = 0.0;
        }
    }
    return mat;
}



double LSM(int T, double r, double sigma , double K, double S0, int N, int M, int R){
    double dt = T/N;                                // Time steps
    QSMatrix<double> t = range(0,T,dt);             // Time vector
    //QSMatrix<double> z = randn_vec(M/2,1);
    //QSMatrix<double> temp = rand_vec(4,1);
    //QSMatrix<double> w = temp + (r-pow(sigma,2))*T + sigma*sqrt(T);
    //z.print();
    //cout << " " << endl;
    //w.print();





    return 0;
}

请忽略上面的最后一部分,我正在重新编辑这个问题:

好的,所以我决定不将向量传递给函数,而是创建一个调用前一个函数 randn_vec() 的新函数。请把注意力转向这段代码:

QSMatrix<double> randn_vec2(int n, int M){
    QSMatrix<double> mat = randn_vec(n,M);
    QSMatrix<double> mat1(2*n,1,0);
    for(int i = 0; i < mat1.get_rows();i++){
            for(int j = 0; j < mat1.get_cols();j++){
                mat1(i,j) = mat(0,j);
            }
        }
        return mat1;
}

我在这里要做的是创建一个基本上要复制 Matlab 函数的函数[z;z]其中 z = randn(2,1) Matlab 中的 randn 函数生成一个正态分布随机数的数组或向量。例如 当你在 Matlab 中编写时,它会产生:

z=(0.31881.3077)
[z;z]
[z;z]=(0.31881.30770.31881.3077)

现在在后面的代码块中,我指的是我正在努力实现的目标,但我似乎无法正确理解它。这是我新更新的问题

1个回答
QSMatrix<double> randn_vec2(int n,double vec){

这是罪魁祸首。你想让 vec 成为一个向量,所以让它这样

QSMatrix<double> randn_vec2(int n,QSMatrix<double> vec){

但是代码有太多其他错误。例如,您错过了. 中条件中的循环变量for(int j = 0; 2*vec.length();j++){您需要进行彻底的审查,以确保所有内容都按照您的预期编写。