我有一个函数想要返回一个向量,在我的函数内部设置了一个int计数器(调味),每个循环后该计数器将增加1。
调试函数时,我发现计数器未初始化,相应地,前29个计算(季节
请看我的代码,谢谢!

vector<float>
RichardRollPrepayment::ComputingCPR(
  float maturity,
  float settledTime,
  float frequencyPreYear,
  float couponRate,
  vector<float> mortgageRate
)
{
  vector<float> cprVector(ceil((maturity-settledTime) * frequencyPreYear));
  float seasonality[] = {
    .94, .76, .73, .96, .98, .92, .99, 1.1, 1.18, 1.21, 1.23, .97
  };
  int seasoning = int(settledTime) * 12;

  for (int i = 0; i < cprVector.size(); i++) {

    if (seasoning < 30) {
      cprVector[i] =
      (.2406 - .1389 * atan(5.952*(1.089 - couponRate/mortgageRate[i])))
        * (seasoning/30) * seasonality[seasoning%12];

      seasoning++;
    }

    else {
      cprVector[i] =
      (.2406 - .1389 * atan(5.952*(1.089 - couponRate/mortgageRate[i])))
        * 1 * seasonality[seasoning%12];

      seasoning++;
    }
  }

  return cprVector;
}

最佳答案

seasoning小于30时,您将与seasoning/30乘以0,因为seasoning是整数。

将其替换为seasoning / 30.0f

10-06 08:47