首页 > 解决方案 > C++:变量“DiscreteGBM::S0”未初始化。始终初始化成员变量(type.6)

问题描述

我有一条错误消息 C++:变量 'DiscreteGBM::S0' 未初始化。始终初始化成员变量(type.6)。此消息会重复几次,并使用不同成员变量的名称。

有人可以教我如何初始化吗?头文件不能更改。只允许更改 cpp 文件。但是对于 cpp 文件,不能更改 main 函数。

第一个代码用于头文件。这是不允许更改的

class DiscreteGBM
{
    protected: 
       double r;              // risk free interest rate
       double S0;            // stock price at time 0
       double sigma;         // volatility
       double delta;         // length of time steps
       int numberSteps;      // total number of time steps; 
                             // start time is 0, end time is numberSteps*delta
    public:
        DiscreteGBM() {}
        void SetRiskFreeRate(double r1);            // set r to r1
        void SetInitialStockPrice(double s);        // set S0 to s
        void SetVolatility(double s);               // set sigma to s
        void SetTimeStepSize(double d);             // set delta to d
        void SetNumberSteps(int n);                 // set numberSteps to n
        vector<double> PricePath() const;           // compute price path S_0,...,S_T (where T=delta*n)
                                                    // according to formula (1) in the instructions,
                                                    // and return the vector (S_0,...,S_T)
};


// class for simulation of European call and put options
class EuropeanOption : public DiscreteGBM
{
    protected:
        bool callPut;    // callPut =0 if object is call option, callPut=1 if it's a put option
        double strike;   // strike price of option
    public:
        EuropeanOption() {}
        void SetCallPut(bool c);                     // set callPut to c
        void SetStrike(double s);                    // set strike to s
        double Payoff(double S_T) const;             // return payoff of option according to
                                                     // formulas (2) and (3) of the instructions;
                                                     // S_T is the stock price at maturity
        double BlackScholesPrice() const;            // return Black Scholes price of the option
                                                     // according to formulas (4) and (6) of the instructions

        double MonteCarloPrice(int numberScenarios=10000) const; 
                                                     // execute algorithm Monte Carlo pricing given 
                                                     // in instructions      
};

CPP 文件如下

#include "Functions.h"
#include "PricingSimulation.h"


void DiscreteGBM::SetRiskFreeRate(double r1) {
    r = r1;
}

void DiscreteGBM::SetInitialStockPrice(double s) {
    S0 = s;
}

void DiscreteGBM::SetVolatility(double s) {
    sigma = s;
}

void DiscreteGBM::SetTimeStepSize(double d) {
    delta = d;
}

void DiscreteGBM::SetNumberSteps(int n) {
    numberSteps = n;
}

vector<double> DiscreteGBM::PricePath() const {
    vector<double> stock;
    vector<double> rnorm;
    rnorm = randn(numberSteps);
    stock[0] = S0;
    for (int i = 1; i <= numberSteps; i++) {
        stock[i] = stock[i - 1] * exp((r - 0.5 * sigma * sigma) * delta + sigma * sqrt(delta) * rnorm[i]);
    }
    return stock;
}

void EuropeanOption::SetCallPut(bool c) {
    callPut = c;
}

void EuropeanOption::SetStrike(double s) {
    strike = s;
}

double EuropeanOption::Payoff(double S_T) const {
    double value;
    if (callPut == 0)
        if ((S_T - strike) > 0)
            value = S_T - strike;
        else
            value = 0;
    else
        if ((strike - S_T) > 0)
            value = strike - S_T;
        else
            value = 0;
    return value;
}

double EuropeanOption::BlackScholesPrice() const {
    double value2, d1, d2;
    d1 = (1 / (sigma * sqrt(numberSteps * delta)) * (log(S0 / strike) + (r + sigma * sigma / 2) * numberSteps * delta));
    d2 = d1 - sigma * sqrt(numberSteps * delta);
    if (callPut == 0)
        value2 = normcdf(d1) * S0 - normcdf(d2) * strike * exp(-r * numberSteps * delta);
    else
        value2 = -normcdf(-d1) * S0 + normcdf(-d2) * strike * exp(-r * numberSteps * delta);
    return value2;
}

double EuropeanOption::MonteCarloPrice(int numberScenarios = 10000) const {
    int N = numberScenarios;
    double sum;
    double T = numberSteps * delta;
    vector <double> A;
    double O;
    for (int i = 1; i <= N; i++) {
        A = PricePath();
        O = A[numberSteps];
        sum += O;
    }
    double average = sum / N;
    return exp(-r * T) * average;
}

int main()
{
    SetSeed();
    EuropeanOption Test;
    for (double r = -0.1; r <= 0.1; r += 0.05)
        for (double sigma = 0.1; sigma <= 1; sigma += 0.3)
            for (int c = 0; c <= 1; c++)
            {
                Test.SetRiskFreeRate(r);
                Test.SetInitialStockPrice(100);
                Test.SetVolatility(sigma);
                Test.SetNumberSteps(100);
                Test.SetTimeStepSize(0.1);
                Test.SetStrike(130);
                Test.SetCallPut(c);
                cout << "r = " << r << ", sigma = " << sigma;
                cout << ", c = " << c << ": ";
                cout << Test.BlackScholesPrice() << " , ";
                cout << Test.MonteCarloPrice() << endl;
            }

    system("pause");
}

标签: c++

解决方案


推荐阅读