American-transaction costs 1.0.0.0
American option pricer under proportional transaction costs
|
00001 /* 00002 American option pricer under proportional transaction costs 00003 Copyright (C) 2011 Alet Roux alet.roux@york.ac.uk 00004 00005 This program is free software: you can redistribute it and/or modify 00006 it under the terms of the GNU General Public License as published by 00007 the Free Software Foundation, either version 3 of the License, or 00008 (at your option) any later version. 00009 00010 This program is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 GNU General Public License for more details. 00014 00015 You should have received a copy of the GNU General Public License 00016 along with this program. If not, see <http://www.gnu.org/licenses/>. 00017 00018 */ 00019 00020 00021 #ifndef PIECEWISELINEAR_H 00022 #define PIECEWISELINEAR_H 00023 00029 #include <vector> 00030 00031 #include "line.h" 00032 00034 typedef std::vector<Line> line_array; 00035 00036 //#include <iostream> 00037 00039 #define PIECEWISELINEAR_CHECK_MAXIMUM 1 00040 00042 #define PIECEWISELINEAR_CHECK_MINIMUM 2 00043 00045 00049 class PiecewiseLinear 00050 { 00051 public: 00053 PiecewiseLinear (const Line &line = Line (NAN, NAN)) 00054 { 00055 _lines.push_back (line); 00056 check(); 00057 } 00058 00060 PiecewiseLinear (const line_array &lines) 00061 : _lines (lines) 00062 { 00063 check(); 00064 } 00065 00067 Line operator[] (const size_t k) const 00068 { 00069 return _lines[k]; 00070 } 00071 00073 coefficient operator() (const coefficient x) const 00074 { 00075 return _lines[piece (x) ] (x); 00076 } 00077 00081 size_t piece (const coefficient x) const 00082 { 00083 size_t k = 0; 00084 while (greaterequal (x,intersection (k+1))) 00085 k++; 00086 return k; 00087 } 00088 00090 PiecewiseLinear& operator*= (const coefficient value) 00091 { 00092 for (size_t k = 0; k < size(); k++) 00093 _lines[k] *= value; 00094 return *this; 00095 } 00096 00098 PiecewiseLinear& operator= (const line_array &lines) 00099 { 00100 _lines = lines; 00101 check(); 00102 return *this; 00103 } 00104 00106 int operator>= (const PiecewiseLinear& function) 00107 { 00108 if (function.slope (0) < slope (0)) 00109 return 0; 00110 00111 if (function.slope (function.size()-1) > slope (size()-1)) 00112 return 0; 00113 00114 for (size_t k = 1; k < size(); k++) 00115 if (lessthan (operator() (intersection (k)),function (intersection (k)))) 00116 return 0; 00117 00118 for (size_t k = 1; k < function.size(); k++) 00119 if (lessthan (operator() (function.intersection (k)),function (function.intersection (k)))) 00120 return 0; 00121 00122 return 1; 00123 } 00124 00126 size_t size() const 00127 { 00128 return _lines.size(); 00129 } 00130 00133 inline coefficient intersection (const size_t k) const 00134 { 00135 return _intersections[k]; 00136 } 00137 00139 coefficient slope (const size_t k) const 00140 { 00141 return _lines[k].slope(); 00142 } 00143 00145 coefficient intercept (const size_t k) const 00146 { 00147 return _lines[k].intercept(); 00148 } 00149 00154 void shift (const coefficient x, const coefficient y) 00155 { 00156 for (size_t k = 0; k < size(); k++) 00157 _lines[k].shift (x, y); 00158 00159 for (size_t k = 1; k < size(); k++) 00160 _intersections[k] = _intersections[k] + x; 00161 } 00162 00164 void negate() 00165 { 00166 for (size_t k = 0; k < size(); k++) 00167 _lines[k].negate(); 00168 } 00169 00174 void restrict_slope (const coefficient lowest, const coefficient highest); 00175 00177 int convex() const 00178 { 00179 for (size_t k = 1; k < size(); k++) 00180 if (lessthan (slope (k),slope (k-1))) 00181 return 0; 00182 return 1; 00183 } 00184 00185 protected: 00190 void check (const int objective = PIECEWISELINEAR_CHECK_MAXIMUM); 00191 00192 private: 00193 line_array _lines; 00194 00195 std::vector<coefficient> _intersections; 00196 }; 00197 00199 std::ostream& operator<< (std::ostream& output, const PiecewiseLinear& line); 00200 00202 #define PIECEWISELINEAR_CROSSING_LEFT -1 00203 00205 #define PIECEWISELINEAR_CROSSING_RIGHT 1 00206 00208 coefficient crossing (const Line &line, const PiecewiseLinear &function, const coefficient x = -INFINITY, const int direction = PIECEWISELINEAR_CROSSING_RIGHT); 00209 00215 PiecewiseLinear maximum (PiecewiseLinear &first, PiecewiseLinear &second); 00216 00222 inline PiecewiseLinear minimum (PiecewiseLinear &first, PiecewiseLinear &second) 00223 { 00224 first.negate(); 00225 second.negate(); 00226 00227 PiecewiseLinear minimum = maximum (first, second); 00228 00229 first.negate(); 00230 second.negate(); 00231 minimum.negate(); 00232 00233 return minimum; 00234 } 00235 00237 void print_convex_conjugate (const PiecewiseLinear& line); 00238 00243 void untangle_maximum (const PiecewiseLinear& f, const std::vector<PiecewiseLinear>& g, const coefficient y, coefficient& w, std::vector<coefficient>& p, std::vector<coefficient>& x); 00244 00245 #endif // PIECEWISELINEAR_H