增加列数增加时填充的列数

时间:2015-11-18 05:17:09

标签: c++ loops matrix nested-loops

我正在编写一个程序来填充矩阵(我使用dlib库但与问题无关。在下面我的目标是第1-19行(行索引0-18),以填充一个额外的列在我的公式中。例如,第1行填充第一列,第2行填充前两列。每行的第一列根据需要预设为初始值。我可以对嵌套for循环做什么通过注释我的lmm()函数表示得到我想要的输出?

#include <random>
#include <iostream>
#include <sstream>
#include <string>
#include <fstream>
#include <vector>
#include <cmath>
#include <limits>
#include <cstdlib>
#include <chrono>
#include <iterator>
#include <algorithm>
#include <boost/accumulators/statistics/stats.hpp>
#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics/mean.hpp>
#include <boost/accumulators/statistics/kurtosis.hpp>
#include <boost/accumulators/statistics/variance.hpp>
#include <boost/accumulators/statistics/skewness.hpp>
#include <dlib/optimization.h>
#include <dlib/matrix.h>
#include "mleFunctor.h"
#include "mleDerFunctor.h"



using namespace boost::accumulators;
using namespace dlib;





//Generate Gaussian via Box-Muller from Mark Joshi's C++ Design Patterns and Derivatives Pricing
double GetOneGaussianByBoxMuller()
{
    double result;

    double x;
    double y;

    double sizeSquared;
    do
    {
        x = 2.0*std::rand() / static_cast<double>(RAND_MAX) - 1;
        y = 2.0*std::rand() / static_cast<double>(RAND_MAX) - 1;
        sizeSquared = x*x + y*y;
    } while (sizeSquared >= 1.0);

    result = x*sqrt(-2 * log(sizeSquared) / sizeSquared);

    return result;
}

double libSum(matrix<double,20,20> v, matrix<double, 20, 20> lib, int r,int c , double d, int index,std::vector<double> W)
{
    double sum = 0.0;
    for (auto k = index + 1; k < lib.nr()-1; ++k)
    {
        sum += ((d*v(k,c-1)*lib(k,c-1))/(1+d*lib(k,c-1)))*v(k,c-1) * lib(r, c-1)*(W[c] - W[c-1]);
    }

    return sum;
}

void lmm()
{
    double dt = .25;
    std::vector<double> W(20);
    std::vector<double> T;
    matrix<double, 20, 20> L;
    W[0] = 0;
    for (auto c = 1; c < W.size(); ++c)
    {
        W[c] = W[c - 1] + sqrt(dt)*GetOneGaussianByBoxMuller();
    }
    for (auto i = 0; i < 20; ++i)
    {
        T.push_back(i*.25);
    }

    set_all_elements(L, 0);
    set_colm(L, 0) = .003641; //3M Libor Rate on November 16,2015
    matrix<double,20,20> vol;
    set_all_elements(vol,.15);


    //Loop that should fill in one more column each (ie 0 indexed row has one column filled in, 
    //row index 1 should have 2 columns filled in etc
    for (auto c = 1; c < L.nc(); ++c)
    {
        for (auto r = 1; r < c; ++r)
        {
            L(r, c) = L(r, c-1) + libSum(vol, L,r,c, .25, c,W) + vol(r,c-1) * L(r, c-1 )*(W[c] - W[c-1]);
        }
    }
    std::ofstream outfile("LMMFlatVol.csv");
    outfile << L << std :: endl;


}

int main()
{
    lmm();

    return 0;
}

截至目前,我的输出只是预设的第一列,其余的矩阵零在我初始化时。

1 个答案:

答案 0 :(得分:0)

您的主要问题似乎是您的循环条件。请注意,内部 for 循环应从第一个活动行开始。例如,在第三列(c == 2)中,第一次计算值是第三行(r == 2)。之后,直到矩阵底部的所有行都有一个计算值。

您之前的逻辑甚至没有计算第二列的任何值(c == 1),因为r设置为1而(r

for (auto c = 1; c < L.nc(); ++c)
{
    for (auto r = c; r < L.nc(); ++r)
    {
        L(r, c) = L(r, c-1) + libSum(vol, L,r,c, .25, c,W) + vol(r,c-1) * L(r, c-1 )*(W[c] - W[c-1]);
    }
}