我有一个生成价格格子的函数和一个使用该格子为相关选项定价的函数。该generate_lattice
函数为数组动态分配空间,并返回指向该数组的指针。这个指针就是传递给price_from_tree
. 这是代码:
#include <iostream>
#include <math.h>
using std::cout;
using std::endl;
using std::max;
using std::nothrow;
double* generate_lattice(double asset_price, double u, double d, int periods)
{
int nodes = (periods * (periods + 1)) / 2; //Number of nodes = sum(1, # of periods)
double * lattice;
lattice = new (nothrow) double[nodes];
double up, down;
int price_index = 0;
for (int period = 0; period < periods + 1; period++) {
for (int exp = 0; exp < period + 1; exp++) {
up = pow(1 + u, exp);
down = pow(1 - d, period - exp);
lattice[price_index] = asset_price * up * down;
price_index++;
}
}
return lattice;
}
double price_from_tree(double* lattice, int periods, double strike, double risk_free, double period_len_years, bool euro = true, bool call = true)
{
int num_prices = ((periods + 1) * (periods + 2)) / 2;
double asset_max = lattice[num_prices - 1];
double asset_min = lattice[num_prices - (periods + 1)];
double asset_t0 = lattice[0];
double u = pow(asset_max / asset_t0, pow(periods, -1));
double d = pow(asset_min / asset_t0, pow(periods, -1));
double p = (exp(risk_free * period_len_years) - d) / (u - d);
double p_minus1 = 1 - p;
int start_node;
if (euro == true) { start_node = num_prices - periods - 1; }
else { start_node = 0; }
int sign;
if (call == true) { sign = 1; }
else { sign = -1; }
for (int node = start_node; node < num_prices; node++) {
lattice[node] = max(sign * (lattice[node] - strike), 0.0);
}
int price_index = num_prices - 1;
double pv_mult = exp(-risk_free * period_len_years);
double down_payoff, up_payoff, prev_payoff;
for (int period = periods + 1; period > 0; period--) {
for (int node = 0; node < period - 1; node++) {
down_payoff = lattice[price_index - (node + 1)];
up_payoff = lattice[price_index - node];
prev_payoff = pv_mult * (p * up_payoff + p_minus1 * down_payoff);
if (euro == false) {
prev_payoff = max(lattice[price_index - (node + 1) - (period - 1)], prev_payoff);
}
lattice[price_index - (node + 1) - (period - 1)] = prev_payoff;
}
price_index -= period;
}
return lattice[0];
}
int main(int argc, char** argv)
{
double* lattice = generate_lattice(100, 0.10, 0.10, 2);
double option1 = price_from_tree(lattice, 2, 105, 0.05, 1, true, true);
cout<<"option 1: "<<option1<<endl;
double* lattice2 = generate_lattice(100, 0.10, 0.10, 2);
return 0;
}
当我运行代码时,我得到这个输出:
选项 1: 8.28214 测试: malloc.c:3096: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2]) )) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size)
= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1)) ) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' 失败。中止
------------------ (program exited with code: 134)
我可以在错误 134 上找到的所有描述都是The job is killed with an abort signal.
按照代码返回正确的值price_from_tree
在我尝试过的每种情况下,但是如果我包含对它的多次调用,generate_lattice
则会失败并显示所述错误。我的price_from_tree
函数是否存在导致内存混乱的问题?在这种情况下我最好使用向量吗?