1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
| struct matrix { double value[12][5]; };
struct matrix add_matrix(struct matrix X, struct matrix Y) { struct matrix M = {{0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.}}; for (int i = 0; i < 12; i++) { for (int j = 0; j < 5; j++) { M.value[i][j] = X.value[i][j] + Y.value[i][j]; } } return M; }
py::array_t<double> calc_greeks_monitor_detail( const py::array_t<bool> &is_call, const py::array_t<double> &S, const py::array_t<double> &X, const py::array_t<double> &T, const py::array_t<double> &b, const py::array_t<double> &r, const py::array_t<double> &sigma, const py::array_t<int> &unit, const py::array_t<int> &pos, const py::array_t<int> &month ) { py::buffer_info buf_is_call = is_call.request(); py::buffer_info buf_spot = S.request(); py::buffer_info buf_strike = X.request(); py::buffer_info buf_time = T.request(); py::buffer_info buf_carry_cost = b.request(); py::buffer_info buf_risk_free = r.request(); py::buffer_info buf_sigma = sigma.request(); py::buffer_info buf_unit = unit.request(); py::buffer_info buf_pos = pos.request(); py::buffer_info buf_month = month.request();
bool *ptr_is_call = static_cast<bool *>(buf_is_call.ptr); auto *ptr_spot = static_cast<double *>(buf_spot.ptr); auto *ptr_strike = static_cast<double *>(buf_strike.ptr); auto *ptr_time = static_cast<double *>(buf_time.ptr); auto *ptr_carry_cost = static_cast<double *>(buf_carry_cost.ptr); auto *ptr_risk_free = static_cast<double *>(buf_risk_free.ptr); auto *ptr_sigma = static_cast<double *>(buf_sigma.ptr); auto *ptr_unit = static_cast<int *>(buf_unit.ptr); auto *ptr_pos = static_cast<int *>(buf_pos.ptr); auto *ptr_month = static_cast<int *>(buf_month.ptr);
std::vector<ssize_t> shape = {13, 5}; auto result = py::array_t<double>(65); result.resize({shape[0], shape[1]}); py::buffer_info buffer_result = result.request(); auto *ptr_result = static_cast<double *>(buffer_result.ptr);
#pragma omp declare reduction(p_add_matrix: struct matrix: \ omp_out=add_matrix(omp_out, omp_in)) initializer( \ omp_priv={{ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0. }} ) struct matrix M = {{0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.}}; { #pragma omp parallel for reduction(p_add_matrix: M) for (int idx = 0; idx < buf_spot.shape[0]; idx++) { const int month_num = ptr_month[idx] - 1; const int unit_ = ptr_unit[idx]; const int pos_ = ptr_pos[idx];
const double d = flow::model::bjerk02::calc_delta( ptr_is_call[idx], ptr_spot[idx], ptr_strike[idx], ptr_time[idx], ptr_carry_cost[idx], ptr_risk_free[idx], ptr_sigma[idx]); const double g = flow::model::bjerk02::calc_gamma( ptr_is_call[idx], ptr_spot[idx], ptr_strike[idx], ptr_time[idx], ptr_carry_cost[idx], ptr_risk_free[idx], ptr_sigma[idx]); const double t = flow::model::bjerk02::calc_theta( ptr_is_call[idx], ptr_spot[idx], ptr_strike[idx], ptr_time[idx], ptr_carry_cost[idx], ptr_risk_free[idx], ptr_sigma[idx]); const double v = flow::model::bjerk02::calc_vega( ptr_is_call[idx], ptr_spot[idx], ptr_strike[idx], ptr_time[idx], ptr_carry_cost[idx], ptr_risk_free[idx], ptr_sigma[idx]);
M.value[month_num][0] += nan_to_num(ptr_spot[idx] * d * unit_ * pos_); M.value[month_num][1] += nan_to_num(d * unit_ * pos_); M.value[month_num][2] += nan_to_num(g * unit_ * pos_); M.value[month_num][3] += nan_to_num(t * unit_ * pos_); M.value[month_num][4] += nan_to_num(v * unit_ * pos_); } }
ptr_result[12 * shape[1] + 0] = 0.; ptr_result[12 * shape[1] + 1] = 0.; ptr_result[12 * shape[1] + 2] = 0.; ptr_result[12 * shape[1] + 3] = 0.; ptr_result[12 * shape[1] + 4] = 0.; for (int idx = 0; idx < 12; idx++) { ptr_result[idx * shape[1] + 0] = M.value[idx][0]; ptr_result[idx * shape[1] + 1] = M.value[idx][1]; ptr_result[idx * shape[1] + 2] = M.value[idx][2]; ptr_result[idx * shape[1] + 3] = M.value[idx][3]; ptr_result[idx * shape[1] + 4] = M.value[idx][4];
ptr_result[12 * shape[1] + 0] += nan_to_num(M.value[idx][0]); ptr_result[12 * shape[1] + 1] += nan_to_num(M.value[idx][1]); ptr_result[12 * shape[1] + 2] += nan_to_num(M.value[idx][2]); ptr_result[12 * shape[1] + 3] += nan_to_num(M.value[idx][3]); ptr_result[12 * shape[1] + 4] += nan_to_num(M.value[idx][4]); } return result; }
|