public AssocMatrix(AssocMatrix <AI1, AI2, V> mat2) { mat = new NumericMatrix <V>(mat2.mat); r = new AssocArray <AI1, int>(mat2.r); c = new AssocArray <AI2, int>(mat2.c); keyRows = new Set <AI1>(mat2.keyRows); keyColumns = new Set <AI2>(mat2.keyColumns); }
public static void Main() { AssocArray <int, double> assArr = new AssocArray <int, double> (); assArr[0] = 2.0; assArr[100] = 3.0; assArr[10] = 43.0; assArr[1000] = 34.0; Console.WriteLine(assArr[100]); foreach (KeyValuePair <int, double> kvp in assArr) { Console.WriteLine("{0}, {1}", kvp.Key, kvp.Value); } Set <string> RowNames = new Set <string>(); RowNames.Insert("A1"); RowNames.Insert("A2"); RowNames.Insert("A3"); RowNames.Insert("A4"); RowNames.Insert("B1"); Set <string> ColNames = new Set <string>(); ColNames.Insert("C1"); ColNames.Insert("C2"); ColNames.Insert("C3"); ColNames.Insert("C4"); ColNames.Insert("C5"); //double defaultValue = 10.0; // Contents of associative matrix (numeric values) NumericMatrix <double> mat1 = new NumericMatrix <double>(RowNames.Size(), ColNames.Size()); mat1.initCells(3.0); AssocMatrix <string, string, double> myMat = new AssocMatrix <string, string, double>(RowNames, ColNames, mat1); Factory fs = new DoubleFactory(); object b; b = fs.create(); Set <double> s1 = (Set <double>)b; Console.WriteLine(s1.Size()); }
public static void Example5() { // 1. Create the date schedule data DateTime startDate = new Date(2009, 8, 3).DateValue; DateTime endDate = new Date(2014, 10, 3).DateValue; bool adjusted = true; int paymentPerYear = 2; bool arrears = false; int fixingDays = -2; bool shortPeriod = true; // 2. My date scheduled. DateSchedule myDL_1 = new DateSchedule(startDate, endDate, paymentPerYear, shortPeriod, adjusted, arrears, fixingDays); // 3. Init a NumericMatrix<double> Class from my dates. NumericMatrix <double> myDates = (NumericMatrix <double>) myDL_1.GetLongScheduleSerial(); // 4. Create an associative matrix AssocMatrix with "header" label // for columns and "n_lines" for rows 4A. Label for columns. Set <string> header = new Set <string>(); header.Insert("FixingDate"); header.Insert("StartDate"); header.Insert("EndDate"); header.Insert("PaymentDate"); // 4B. Label for rows Set <string> n_line = new Set <string>(); for (int i = 0; i < myDates.MaxRowIndex + 1; i++) { n_line.Insert("# " + (i + 1)); } // 5. Creating AssocMatrix. AssocMatrix <string, string, double> OutMatrix = new AssocMatrix <string, string, double>(n_line, header, myDates); // 6. Print associative matrices in Excel, to "My Date // List" sheet, the output in Excel serial number format. ExcelMechanisms exl = new ExcelMechanisms(); exl.printAssocMatrixInExcel <string, string, double> (OutMatrix, "My Date List"); }
// Print a two-dimensional associative array (typically, one time level); // Recall: row, column and value parameters public void printAssocMatrixInExcel <R, C, T>(AssocMatrix <R, C, T> matrix, string SheetName) { List <string> rowlabels = new List <string>(); List <string> columnlabels = new List <string>(); /*foreach (KeyValuePair<Key, Value> kvp in str) * { * Console.WriteLine("{0}, {1}", kvp.Key, kvp.Value); * } */ // Create the row and column annotations foreach (KeyValuePair <R, int> value in matrix.r) { rowlabels.Add(value.Key.ToString()); } foreach (KeyValuePair <C, int> value in matrix.c) { columnlabels.Add(value.Key.ToString()); } excel.AddMatrix(SheetName, matrix.mat, rowlabels, columnlabels); }
// Class ListedSTFutOption and AssocMatrix public static void Example4() { // Stir (Short Term Interest Rate ) Option example. // (Please check details on www.euronext.com) Example is based on Black Formula // I create my option STFutOption opx1 = new STFutOption(1, 97.935, 98.25, 175, 0.4756); // option deltas Console.WriteLine("{0}, {1}", opx1.Price(), opx1.Delta()); opx1.SwitchCallPut(); Console.WriteLine("{0}, {1}", opx1.Price(), opx1.Delta()); // Call and put checking deltas and call put parity double S = 97.935; double K = 98.25; double DaysToExpiry = 175; STFutOption Call = new STFutOption(1, S, K, 175, 0.4756); STFutOption Put = new STFutOption(-1, S, K, 175, 0.4756); Console.WriteLine("{0},{1},{2}", Call.Delta(), Put.Delta(), Call.Delta() - Put.Delta()); // Call/Put delta are reversed Console.WriteLine("{0}", Call.Price() - Put.Price() - S + K); // Call/Put parity // I will crate a matrix showing deltas of a option, for different underlying price (rows) and passing the time (columns) // header Row: different value of underlying. the center is the current one // header column: passing the time, less days to maturity // Time passing - column int d_columns = 20; // how many days from and including today double shift_c = 1.0; // interval in days between each columns // Changing value of underlying int d_rows = 15; // how many values plus or minus the value double shift_h = 0.10; // interval in underlying NumericMatrix <double> deltaMatrix = new NumericMatrix <double>(d_rows * 2 + 1, d_columns); // Underlying value double d_r = -d_rows; Set <double> underlying = new Set <double>(); for (int i = 0; i < deltaMatrix.Rows; i++) { underlying.Insert(S + d_r * shift_h); d_r++; } // Days to maturity double d_c = 0; Set <double> days = new Set <double>(); for (int i = 0; i < deltaMatrix.Columns; i++) { days.Insert(DaysToExpiry + d_c * shift_c); d_c--; } // Populate DeltaMatrix int my_r = 1; int my_c = 1; foreach (double valueS in underlying) { Call.new_S = valueS; foreach (double valueDays in days) { Call.new_DaysToExpiry = valueDays; deltaMatrix[my_r, my_c] = Call.Delta(); my_c++; } my_r++; my_c = 1; } // Creating AssocMatrix AssocMatrix <double, double, double> OutMatrix = new AssocMatrix <double, double, double>(underlying, days, deltaMatrix); // Print associative matrices in Excel, to "StirDeltas" sheet ExcelMechanisms exl = new ExcelMechanisms(); exl.printAssocMatrixInExcel <double, double, double>(OutMatrix, "StirDeltas"); }
public static void Main() { AssocArray <int, double> assArr = new AssocArray <int, double> (); assArr[0] = 2.0; assArr[100] = 3.0; assArr[10] = 43.0; assArr[1000] = 34.0; Console.WriteLine(assArr[100]); foreach (KeyValuePair <int, double> kvp in assArr) { Console.WriteLine("{0}, {1}", kvp.Key, kvp.Value); } Set <string> names = new Set <string>(); names.Insert("A1"); names.Insert("A2"); names.Insert("A3"); names.Insert("A4"); names.Insert("B1"); double defaultValue = 10.0; AssocArray <string, double> myAssocArray = new AssocArray <string, double>(names, defaultValue); myAssocArray.print(); myAssocArray["A4"] = 99.99; myAssocArray.print(); // Test other functions AssocArray <string, double> myAssocArray2 = new AssocArray <string, double> (myAssocArray); myAssocArray2.print(); AssocMatrix <string, string, double> myMat = new AssocMatrix <string, string, double>(); myMat.print(); // Create an associative matrix with 2 sets and a numeric matrix NumericMatrix <double> mat1 = new NumericMatrix <double>(names.Size(), names.Size()); AssocMatrix <string, string, double> myMat2 = new AssocMatrix <string, string, double>(names, names, mat1); myMat2.print(); // Now work with ranges in this associative matrix SpreadSheetVertex <long, string> ul = new SpreadSheetVertex <long, string> (); // Upper left ul.first = 1; ul.second = "B"; SpreadSheetVertex <long, string> lr = new SpreadSheetVertex <long, string> (); // Lower right lr.first = 3; lr.second = "D"; SpreadSheetRange <long, string> myRange = new SpreadSheetRange <long, string>(); myRange.upperLeft = ul; myRange.lowerRight = lr; // myMat22.modify(myRange, Modifier1); //print (*(myMat2.Data())); // print(myMat2.extract(myRange)); SpreadSheetVertex <string, string> ul2 = new SpreadSheetVertex <string, string>(); // Upper left ul2.first = "A1"; ul2.second = "A1"; SpreadSheetVertex <string, string> lr2 = new SpreadSheetVertex <string, string>(); // Upper left lr2.first = "A4"; lr2.second = "A4"; SpreadSheetRange <string, string> myRange2 = new SpreadSheetRange <string, string>(); myRange2.upperLeft = ul2; myRange2.lowerRight = lr2; // Now work with Dates; 101 example, later more extensive, eg. I/O witb Excel Set <DateTime> dates = new Set <DateTime>(); dates.Insert(DateTime.Now); dates.Insert(new DateTime(2009, 3, 17)); NumericMatrix <double> payments = new NumericMatrix <double>(dates.Size(), dates.Size()); AssocMatrix <DateTime, DateTime, double> Sheet = new AssocMatrix <DateTime, DateTime, double>(dates, dates, payments); }