first commit
This commit is contained in:
commit
5a31fc8f33
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
/.DS_Store
|
||||||
|
/.ipynb_checkpoints
|
BIN
Exam1/.DS_Store
vendored
Normal file
BIN
Exam1/.DS_Store
vendored
Normal file
Binary file not shown.
302
Exam1/Data/Data_Forwardpremia.csv
Normal file
302
Exam1/Data/Data_Forwardpremia.csv
Normal file
|
@ -0,0 +1,302 @@
|
||||||
|
some,random,information,that,we,do,not,need,or,what
|
||||||
|
,AUD,CAD,EUR,JPY,NZD,NOK,SEK,CHF,GBP
|
||||||
|
1995-01-31,0.14383,0.15598,-0.08519,-0.29138,0.19805,-0.06425,0.12908,-0.17894,0.019
|
||||||
|
1995-02-28,0.14739,0.13631,-0.09617,-0.33185,0.24677,-0.10999,0.16885,-0.21906,0.03157
|
||||||
|
1995-03-31,0.11713,0.16416,-0.12398,-0.40484,0.22202,-0.10589,0.19229,-0.23089,0.01847
|
||||||
|
1995-04-28,0.13086,0.14698,-0.13713,-0.39946,0.22825,-0.04812,0.21572,-0.24473,0.04975
|
||||||
|
1995-05-31,0.12199,0.13135,-0.12744,-0.40864,0.23887,-0.01273,0.22039,-0.23173,0.01891
|
||||||
|
1995-06-30,0.13481,0.08733,-0.13747,-0.43143,0.23384,-0.06168,0.24986,-0.27829,0.05032
|
||||||
|
1995-07-31,0.15517,0.07299,-0.12283,-0.47136,0.26248,-0.0489,0.28551,-0.29596,0.08139
|
||||||
|
1995-08-31,0.12776,0.03722,-0.12961,-0.42537,0.2859,-0.07487,0.26267,-0.24097,0.0709
|
||||||
|
1995-09-29,0.14344,0.0372,-0.16118,-0.51115,0.26971,-0.04462,0.25048,-0.2865,0.06958
|
||||||
|
1995-10-31,0.15218,0.0448,-0.14916,-0.47546,0.19125,-0.06907,0.25862,-0.3351,0.06958
|
||||||
|
1995-11-30,0.12631,-0.00736,-0.15913,-0.4777,0.18919,-0.03612,0.24828,-0.30683,0.05882
|
||||||
|
1995-12-29,0.15612,-0.0,-0.16763,-0.45138,0.25454,-0.02846,0.26637,-0.33044,0.07083
|
||||||
|
1996-01-31,0.14126,-0.00727,-0.1615,-0.41695,0.24166,0.02609,0.24336,-0.30592,0.07935
|
||||||
|
1996-02-29,0.16031,-0.0,-0.16341,-0.39538,0.30235,-0.02813,0.21012,-0.31773,0.07183
|
||||||
|
1996-03-29,0.17189,-0.03669,-0.16949,-0.40756,0.28566,-0.05927,0.13472,-0.30316,0.04587
|
||||||
|
1996-04-30,0.18061,-0.05878,-0.1765,-0.43536,0.37698,-0.06544,0.11637,-0.30655,0.0532
|
||||||
|
1996-05-31,0.17552,-0.05839,-0.17067,-0.42262,0.35933,-0.06128,0.08019,-0.22409,0.05157
|
||||||
|
1996-06-28,0.173,-0.05861,-0.17117,-0.54985,0.37621,-0.05856,0.03925,-0.25643,0.02575
|
||||||
|
1996-07-31,0.17003,-0.08007,-0.18359,-0.33311,0.37165,-0.06128,0.00757,-0.25084,0.03214
|
||||||
|
1996-08-30,0.12637,-0.1098,-0.18946,-0.72472,0.35884,-0.03746,-0.01057,-0.2754,0.02561
|
||||||
|
1996-09-30,0.11858,-0.12489,-0.20323,-0.46233,0.39073,-0.06151,-0.04376,-0.36718,0.03836
|
||||||
|
1996-10-31,0.11888,-0.17167,-0.1852,-0.40094,0.3177,-0.06116,-0.05329,-0.30164,0.06146
|
||||||
|
1996-11-29,0.08137,-0.23029,-0.18875,-0.66059,0.28435,-0.11222,-0.10133,-0.29204,0.04759
|
||||||
|
1996-12-31,0.03982,-0.20457,-0.20006,-0.43724,0.17677,-0.15751,-0.12959,-0.319,0.05839
|
||||||
|
1997-01-31,0.01891,-0.1819,-0.17479,-0.3879,0.13806,-0.16203,-0.08785,-0.29202,0.05618
|
||||||
|
1997-02-28,0.03591,-0.20212,-0.18261,-0.42971,0.18085,-0.17526,-0.10341,-0.31371,0.05395
|
||||||
|
1997-03-31,0.03518,-0.20651,-0.207,-0.42778,0.14691,-0.21241,-0.13216,-0.31843,0.0335
|
||||||
|
1997-04-30,0.02135,-0.21989,-0.21273,-0.43828,0.09088,-0.20243,-0.1167,-0.33993,0.05363
|
||||||
|
1997-05-30,-0.02379,-0.21046,-0.21129,-0.41707,0.11879,-0.19066,-0.1094,-0.39237,0.06237
|
||||||
|
1997-06-30,-0.03406,-0.2097,-0.23441,-0.47302,0.09004,-0.20305,-0.12488,-0.38692,0.09017
|
||||||
|
1997-07-31,-0.05592,-0.18901,-0.17709,-0.43278,0.23459,-0.15693,-0.1024,-0.34847,0.10998
|
||||||
|
1997-08-29,-0.07155,-0.18021,-0.20352,-0.42195,0.19885,-0.16784,-0.10896,-0.36452,0.12401
|
||||||
|
1997-09-30,-0.08216,-0.19182,-0.21709,-0.46742,0.17826,-0.18562,-0.11943,-0.37459,0.12884
|
||||||
|
1997-10-31,-0.06229,-0.14915,-0.17504,-0.42954,0.17971,-0.17085,-0.10361,-0.32637,0.12949
|
||||||
|
1997-11-28,-0.09698,-0.18422,-0.1901,-0.55629,0.0957,-0.20949,-0.12895,-0.39145,0.13364
|
||||||
|
1997-12-31,-0.07727,-0.10837,-0.18258,-0.45865,0.27073,-0.186,-0.08763,-0.37779,0.14902
|
||||||
|
1998-01-30,-0.0522,-0.0479,-0.16151,-0.39312,0.27902,-0.15517,-0.07094,-0.34758,0.14261
|
||||||
|
1998-02-27,-0.06683,-0.0731,-0.18476,-0.43592,0.26112,-0.15867,-0.09497,-0.40967,0.15499
|
||||||
|
1998-03-31,-0.06549,-0.07386,-0.18347,-0.46488,0.35149,-0.15686,-0.10074,-0.37262,0.15957
|
||||||
|
1998-04-30,-0.05383,-0.06574,-0.17457,-0.46464,0.26849,-0.14827,-0.09243,-0.35599,0.15023
|
||||||
|
1998-05-29,-0.06079,-0.06325,-0.17628,-0.42706,0.21502,-0.09497,-0.09262,-0.32516,0.14483
|
||||||
|
1998-06-30,-0.05532,-0.07074,-0.19021,-0.46185,0.20441,-0.07757,-0.12359,-0.31167,0.16796
|
||||||
|
1998-07-31,-0.05978,-0.05574,-0.1874,-0.44113,0.11719,-0.02715,-0.11793,-0.32323,0.16518
|
||||||
|
1998-08-31,-0.04999,0.02237,-0.18105,-0.43262,0.0343,0.20972,-0.10453,-0.33937,0.16197
|
||||||
|
1998-09-30,-0.04739,0.00983,-0.16287,-0.43623,-0.03795,0.22267,-0.09795,-0.34807,0.16844
|
||||||
|
1998-10-30,-0.04888,0.00194,-0.14441,-0.42612,-0.0793,0.22965,-0.09983,-0.34323,0.16255
|
||||||
|
1998-11-30,-0.05882,-0.05166,-0.17166,-0.50567,-0.18365,0.25756,-0.19619,-0.35276,0.117
|
||||||
|
1998-12-31,-0.04215,-0.00651,-0.15322,-0.41309,-0.05109,0.29161,-0.13456,-0.33182,0.09922
|
||||||
|
1999-01-29,-0.02136,0.0086,-0.13829,-0.34891,-0.10396,0.22541,-0.12243,-0.29071,0.0749
|
||||||
|
1999-02-26,-0.02895,0.0106,-0.15334,-0.40495,-0.03815,0.25925,-0.15599,-0.35395,0.05682
|
||||||
|
1999-03-31,-0.01288,-0.00859,-0.16961,-0.44429,-0.0375,0.17452,-0.16005,-0.3106,0.03098
|
||||||
|
1999-04-30,-0.01876,-0.00893,-0.19926,-0.41093,-0.03037,0.17002,-0.15885,-0.33422,0.03168
|
||||||
|
1999-05-31,-0.01578,-0.01896,-0.19534,-0.40354,-0.03181,0.15365,-0.15339,-0.3276,0.02807
|
||||||
|
1999-06-30,-0.03164,-0.04257,-0.22487,-0.44132,-0.05848,0.1022,-0.18483,-0.34925,-0.0222
|
||||||
|
1999-07-30,-0.04364,-0.03999,-0.21967,-0.44231,-0.05467,0.10332,-0.18004,-0.36033,-0.00679
|
||||||
|
1999-08-31,-0.06121,-0.05229,-0.2444,-0.46943,-0.06574,0.06942,-0.21393,-0.39481,-0.03731
|
||||||
|
1999-09-30,-0.04971,-0.06331,-0.24204,-0.45893,-0.06195,0.02579,-0.19475,-0.35984,-0.00729
|
||||||
|
1999-10-29,-0.04664,-0.05985,-0.21249,-0.44272,-0.06912,0.02932,-0.18589,-0.34844,-0.00366
|
||||||
|
1999-11-30,-0.11925,-0.14606,-0.26136,-0.55002,-0.12353,-0.04467,-0.23908,-0.39363,-0.05649
|
||||||
|
1999-12-31,-0.05989,-0.06893,-0.22934,-0.51719,-0.05751,-0.01182,-0.2074,-0.38934,-0.01551
|
||||||
|
2000-01-31,-0.05905,-0.06697,-0.20981,-0.4649,-0.0424,-0.01754,-0.17927,-0.30769,0.00123
|
||||||
|
2000-02-29,-0.04213,-0.07515,-0.21732,-0.50878,-0.02883,-0.0149,-0.17296,-0.32719,0.00887
|
||||||
|
2000-03-31,-0.03568,-0.07097,-0.2052,-0.56759,-0.01812,-0.02785,-0.1871,-0.29212,-0.0163
|
||||||
|
2000-04-28,-0.02368,-0.07427,-0.21968,-0.53392,0.00411,-0.00224,-0.20965,-0.2992,-0.02237
|
||||||
|
2000-05-31,-0.05149,-0.07548,-0.20695,-0.56673,-0.01095,-0.03683,-0.23271,-0.32205,-0.05284
|
||||||
|
2000-06-30,-0.05152,-0.08035,-0.20458,-0.59199,-0.01062,-0.01228,-0.24943,-0.31104,-0.06471
|
||||||
|
2000-07-31,-0.05164,-0.07137,-0.2044,-0.6133,-0.0044,0.00169,-0.24454,-0.31564,-0.06074
|
||||||
|
2000-08-31,-0.02479,-0.0719,-0.15786,-0.51426,-0.00934,0.02589,-0.22332,-0.27871,-0.0536
|
||||||
|
2000-09-29,-0.02303,-0.06915,-0.15442,-0.58611,-0.0123,0.05066,-0.23124,-0.28738,-0.05071
|
||||||
|
2000-10-31,-0.03953,-0.06615,-0.14528,-0.55313,-0.01008,0.06073,-0.25131,-0.30133,-0.05921
|
||||||
|
2000-11-30,-0.06104,-0.08332,-0.14877,-0.51634,-0.0195,0.05068,-0.23709,-0.28558,-0.07898
|
||||||
|
2000-12-29,-0.04202,-0.0686,-0.15372,-0.51886,-0.0,0.08331,-0.22228,-0.2855,-0.06291
|
||||||
|
2001-01-31,0.01115,-0.004,-0.05755,-0.39403,0.07231,0.13172,-0.1139,-0.1601,0.02053
|
||||||
|
2001-02-28,0.01518,-0.00849,-0.04232,-0.42145,0.10038,0.16197,-0.11138,-0.15595,0.02704
|
||||||
|
2001-03-30,-0.00936,-0.01334,-0.02757,-0.46453,0.10141,0.18317,-0.08871,-0.14321,0.03588
|
||||||
|
2001-04-30,0.02832,0.02473,0.04033,-0.39744,0.13592,0.28106,-0.03408,-0.11592,0.0839
|
||||||
|
2001-05-31,0.0743,0.04591,0.04463,-0.34312,0.15427,0.25271,0.00186,-0.06301,0.09296
|
||||||
|
2001-06-29,0.09112,0.05468,0.05668,-0.3259,0.15439,0.29413,0.03444,-0.05064,0.10387
|
||||||
|
2001-07-31,0.09922,0.04448,0.07021,-0.33672,0.18012,0.30866,0.05409,-0.04343,0.12427
|
||||||
|
2001-08-31,0.09753,0.03612,0.06036,-0.2958,0.18094,0.28493,0.06075,-0.02157,0.09657
|
||||||
|
2001-09-28,0.1587,0.06771,0.09448,-0.2248,0.22148,0.38372,0.10166,-0.03155,0.16752
|
||||||
|
2001-10-31,0.17402,0.04285,0.11736,-0.19217,0.24248,0.40676,0.1339,-0.01225,0.16172
|
||||||
|
2001-11-30,0.1839,0.02163,0.1153,-0.176,0.23591,0.42171,0.15198,0.00243,0.16001
|
||||||
|
2001-12-31,0.20886,0.02255,0.12421,-0.15578,0.25489,0.41334,0.18001,-0.00964,0.18775
|
||||||
|
2002-01-31,0.18302,0.01824,0.11789,-0.14063,0.23104,0.36402,0.15636,-0.02045,0.15863
|
||||||
|
2002-02-28,0.19352,0.0187,0.12729,-0.15779,0.2549,0.40208,0.17426,-0.01115,0.17903
|
||||||
|
2002-03-29,0.20691,0.02507,0.12482,-0.15049,0.26614,0.39382,0.18712,-0.03033,0.17783
|
||||||
|
2002-04-30,0.21567,0.04331,0.12892,-0.16096,0.33127,0.41077,0.21388,-0.0284,0.19163
|
||||||
|
2002-05-31,0.24009,0.06412,0.1327,-0.15725,0.30736,0.40916,0.21591,-0.05554,0.18539
|
||||||
|
2002-06-28,0.26507,0.07164,0.13423,-0.15681,0.34029,0.43746,0.21955,-0.05646,0.17729
|
||||||
|
2002-07-31,0.26056,0.08581,0.13876,-0.16045,0.35465,0.46619,0.22166,-0.08979,0.17299
|
||||||
|
2002-08-30,0.24193,0.09483,0.12728,-0.15109,0.32988,0.43252,0.20592,-0.09267,0.17599
|
||||||
|
2002-09-30,0.26424,0.0857,0.13684,-0.16936,0.36313,0.4846,0.2316,-0.10255,0.18904
|
||||||
|
2002-10-31,0.25721,0.09449,0.13257,-0.13927,0.34201,0.44679,0.21968,-0.08471,0.1817
|
||||||
|
2002-11-29,0.28333,0.11866,0.14789,-0.14539,0.37553,0.49104,0.23083,-0.06136,0.21617
|
||||||
|
2002-12-31,0.29327,0.12146,0.13297,-0.11467,0.39842,0.44216,0.21782,-0.06728,0.22138
|
||||||
|
2003-01-31,0.2696,0.11419,0.11738,-0.10427,0.35337,0.3599,0.19429,-0.05705,0.20341
|
||||||
|
2003-02-28,0.28296,0.13818,0.114,-0.11341,0.37981,0.36221,0.20897,-0.06197,0.20082
|
||||||
|
2003-03-31,0.28392,0.15217,0.10818,-0.10631,0.37062,0.3604,0.19335,-0.08513,0.19124
|
||||||
|
2003-04-30,0.29464,0.17968,0.11178,-0.11452,0.37435,0.32814,0.19603,-0.08925,0.18851
|
||||||
|
2003-05-30,0.28455,0.16516,0.08896,-0.11377,0.36092,0.30435,0.16207,-0.0891,0.19123
|
||||||
|
2003-06-30,0.33701,0.20072,0.09625,-0.11775,0.37649,0.27116,0.17474,-0.07829,0.22812
|
||||||
|
2003-07-31,0.31217,0.17524,0.09031,-0.09794,0.34041,0.21305,0.15026,-0.07359,0.19553
|
||||||
|
2003-08-29,0.30331,0.14552,0.08473,-0.09629,0.32456,0.15862,0.14879,-0.07428,0.20378
|
||||||
|
2003-09-30,0.33322,0.14736,0.09106,-0.10255,0.35419,0.14658,0.15437,-0.0803,0.22054
|
||||||
|
2003-10-31,0.31629,0.14253,0.08674,-0.09082,0.33419,0.13777,0.14475,-0.07566,0.21769
|
||||||
|
2003-11-28,0.35288,0.14836,0.0876,-0.11484,0.38886,0.14279,0.14481,-0.08358,0.22469
|
||||||
|
2003-12-31,0.36779,0.13997,0.08445,-0.09616,0.37998,0.10817,0.15623,-0.07523,0.23433
|
||||||
|
2004-01-30,0.3572,0.12094,0.08317,-0.08866,0.3422,0.0841,0.12608,-0.06984,0.23045
|
||||||
|
2004-02-27,0.37218,0.10652,0.08318,-0.09459,0.37268,0.06674,0.12955,-0.07246,0.25844
|
||||||
|
2004-03-31,0.37196,0.09811,0.08165,-0.1031,0.36416,0.06989,0.09874,-0.0766,0.26861
|
||||||
|
2004-04-30,0.37527,0.08649,0.08516,-0.09501,0.38443,0.07009,0.08923,-0.07793,0.29027
|
||||||
|
2004-05-31,0.35925,0.08277,0.08113,-0.09441,0.38588,0.06852,0.08541,-0.07496,0.27527
|
||||||
|
2004-06-30,0.35844,0.06114,0.06281,-0.12261,0.38496,0.04515,0.06609,-0.08228,0.27056
|
||||||
|
2004-07-30,0.36424,0.04814,0.05108,-0.12955,0.39843,0.03698,0.05029,-0.09462,0.27644
|
||||||
|
2004-08-31,0.33321,0.04022,0.03852,-0.14709,0.40622,0.01983,0.03926,-0.10968,0.2761
|
||||||
|
2004-09-30,0.29886,0.04503,0.02222,-0.15527,0.39733,0.00564,0.01978,-0.10817,0.24788
|
||||||
|
2004-10-29,0.27452,0.05082,0.00845,-0.16569,0.38225,-0.00939,0.0045,-0.10909,0.2273
|
||||||
|
2004-11-30,0.2771,0.03363,-0.01019,-0.21113,0.39579,-0.03301,-0.01873,-0.14599,0.22363
|
||||||
|
2004-12-31,0.26628,0.02003,-0.02085,-0.21767,0.37312,-0.04294,-0.02649,-0.14433,0.21744
|
||||||
|
2005-01-31,0.2138,-0.0,-0.036,-0.19883,0.31266,-0.06266,-0.04424,-0.14651,0.16821
|
||||||
|
2005-02-28,0.2562,-0.00569,-0.05431,-0.2454,0.3627,-0.08014,-0.06304,-0.17805,0.18816
|
||||||
|
2005-03-31,0.24001,-0.02149,-0.06315,-0.25238,0.33298,-0.0762,-0.0669,-0.17298,0.15889
|
||||||
|
2005-04-29,0.21205,-0.04463,-0.0832,-0.26389,0.32288,-0.10461,-0.09071,-0.19844,0.1551
|
||||||
|
2005-05-31,0.22438,-0.05264,-0.09285,-0.28526,0.34322,-0.10709,-0.09945,-0.21549,0.1499
|
||||||
|
2005-06-30,0.19147,-0.06697,-0.10219,-0.2825,0.30515,-0.10697,-0.15315,-0.22428,0.11723
|
||||||
|
2005-07-29,0.18731,-0.08324,-0.11931,-0.30064,0.29606,-0.12666,-0.16706,-0.26257,0.08808
|
||||||
|
2005-08-31,0.16235,-0.08673,-0.13403,-0.31498,0.27479,-0.1369,-0.18281,-0.25221,0.06786
|
||||||
|
2005-09-30,0.15147,-0.08447,-0.1462,-0.3292,0.27005,-0.14602,-0.19464,-0.26951,0.05654
|
||||||
|
2005-10-31,0.11729,-0.08566,-0.16058,-0.33862,0.25896,-0.1464,-0.2064,-0.27689,0.03446
|
||||||
|
2005-11-30,0.10834,-0.09418,-0.16711,-0.39357,0.29205,-0.16961,-0.23732,-0.29786,0.02081
|
||||||
|
2005-12-30,0.10422,-0.09164,-0.16901,-0.38581,0.28526,-0.1688,-0.23096,-0.31538,0.0134
|
||||||
|
2006-01-31,0.07378,-0.07875,-0.16777,-0.35145,0.2353,-0.17605,-0.2098,-0.28297,-0.00506
|
||||||
|
2006-02-28,0.0778,-0.07296,-0.1806,-0.40592,0.23593,-0.20187,-0.22986,-0.32149,-0.0137
|
||||||
|
2006-03-31,0.05663,-0.0849,-0.18083,-0.4491,0.21948,-0.17737,-0.22852,-0.30655,-0.02882
|
||||||
|
2006-04-28,0.05078,-0.08214,-0.21857,-0.42832,0.20466,-0.2251,-0.27347,-0.3552,-0.04345
|
||||||
|
2006-05-31,0.05372,-0.07905,-0.19853,-0.42925,0.19211,-0.18828,-0.24935,-0.32646,-0.04595
|
||||||
|
2006-06-30,0.04514,-0.09626,-0.22253,-0.4677,0.18045,-0.23039,-0.2758,-0.35819,-0.06864
|
||||||
|
2006-07-31,0.05604,-0.09649,-0.22144,-0.47493,0.18832,-0.23555,-0.29058,-0.36612,-0.07228
|
||||||
|
2006-08-31,0.0596,-0.08925,-0.18539,-0.41387,0.17432,-0.19067,-0.23073,-0.31275,-0.04205
|
||||||
|
2006-09-29,0.06765,-0.08788,-0.17567,-0.46676,0.18703,-0.18272,-0.23388,-0.3059,-0.04068
|
||||||
|
2006-10-31,0.07858,-0.08661,-0.17178,-0.43936,0.18687,-0.17337,-0.22431,-0.32049,-0.02726
|
||||||
|
2006-11-30,0.07742,-0.09462,-0.15057,-0.42318,0.18713,-0.15485,-0.20617,-0.29675,-0.01779
|
||||||
|
2006-12-29,0.08682,-0.09113,-0.15316,-0.41612,0.19022,-0.14755,-0.20729,-0.29042,-0.02095
|
||||||
|
2007-01-31,0.07289,-0.08051,-0.13031,-0.38126,0.16163,-0.10975,-0.1672,-0.24955,0.00204
|
||||||
|
2007-02-28,0.07999,-0.09386,-0.13517,-0.39679,0.19435,-0.10961,-0.1706,-0.27306,0.00204
|
||||||
|
2007-03-30,0.08727,-0.08759,-0.1185,-0.43941,0.2016,-0.08654,-0.16747,-0.25672,0.00612
|
||||||
|
2007-04-30,0.08315,-0.09234,-0.12848,-0.42725,0.23987,-0.09908,-0.17053,-0.27551,0.0145
|
||||||
|
2007-05-31,0.07731,-0.09545,-0.11152,-0.40248,0.21995,-0.0764,-0.16853,-0.25329,0.02174
|
||||||
|
2007-06-29,0.07956,-0.07335,-0.10363,-0.40269,0.24235,-0.0586,-0.15003,-0.23526,0.03888
|
||||||
|
2007-07-31,0.10025,-0.0787,-0.11484,-0.42917,0.28392,-0.05738,-0.15997,-0.24627,0.04381
|
||||||
|
2007-08-31,0.08114,-0.06241,-0.13183,-0.42362,0.24401,-0.08867,-0.16666,-0.27017,0.05505
|
||||||
|
2007-09-28,0.13102,-0.03019,-0.07711,-0.37238,0.28229,0.02305,-0.10037,-0.23911,0.06481
|
||||||
|
2007-10-31,0.16924,-0.00737,-0.04809,-0.35575,0.31774,0.05062,-0.05048,-0.21679,0.09584
|
||||||
|
2007-11-30,0.11921,-0.04002,-0.06907,-0.38732,0.29915,0.02362,-0.06577,-0.24031,0.09342
|
||||||
|
2007-12-31,0.17296,-0.03952,-0.05832,-0.34343,0.36582,0.08431,-0.03188,-0.20247,0.08896
|
||||||
|
2008-01-31,0.30641,0.07932,0.0787,-0.20713,0.42598,0.18655,0.08997,-0.05811,0.17369
|
||||||
|
2008-02-29,0.37399,0.0541,0.09,-0.1973,0.47625,0.2206,0.12521,-0.04312,0.2023
|
||||||
|
2008-03-31,0.37522,0.06138,0.10284,-0.19307,0.50113,0.25667,0.14947,-0.02022,0.21608
|
||||||
|
2008-04-30,0.3962,0.0278,0.12251,-0.19922,0.5208,0.29428,0.15054,-0.04514,0.20773
|
||||||
|
2008-05-30,0.39359,0.04628,0.13737,-0.16449,0.54166,0.28535,0.16223,-0.02202,0.22543
|
||||||
|
2008-06-30,0.4463,0.04336,0.16179,-0.17722,0.55204,0.33184,0.1749,-0.04026,0.2445
|
||||||
|
2008-07-31,0.41376,0.04289,0.16101,-0.17548,0.49978,0.29393,0.17896,-0.02863,0.22945
|
||||||
|
2008-08-29,0.36957,0.04339,0.1572,-0.16323,0.45879,0.29022,0.19134,-0.03278,0.21845
|
||||||
|
2008-09-30,0.08871,-0.17133,-0.33092,-0.51884,0.37351,0.19128,-0.09714,-0.31264,-0.26613
|
||||||
|
2008-10-31,0.30194,0.03947,0.13347,-0.11017,0.37406,0.24346,0.09387,-0.03853,0.19329
|
||||||
|
2008-11-28,0.14412,0.00564,0.03308,-0.16561,0.35278,0.21213,0.02518,-0.20851,-0.01303
|
||||||
|
2008-12-31,0.3466,0.05183,0.13778,-0.063,0.49747,0.23736,0.06055,-0.03007,0.08698
|
||||||
|
2009-01-30,0.21954,0.03871,0.0452,-0.04444,0.26663,0.15903,0.06219,-0.03188,0.04024
|
||||||
|
2009-02-27,0.22111,0.00315,0.02208,-0.05315,0.21787,0.13366,0.01801,-0.05409,0.01193
|
||||||
|
2009-03-31,0.22708,-0.00715,0.00543,-0.04283,0.22452,0.14385,0.02358,-0.06686,-0.00767
|
||||||
|
2009-04-30,0.21145,-0.01347,0.01084,-0.04625,0.19361,0.1237,-0.00012,-0.04303,0.00405
|
||||||
|
2009-05-29,0.21908,-0.02008,0.02533,-0.03561,0.17816,0.09009,0.01095,-0.03282,0.00992
|
||||||
|
2009-06-30,0.24255,-0.01808,-0.0,-0.03845,0.18077,0.08355,0.01007,-0.03771,0.00243
|
||||||
|
2009-07-31,0.24519,-0.0111,-0.00652,-0.02752,0.19461,0.07957,-0.01469,-0.03253,0.00724
|
||||||
|
2009-08-31,0.23491,-0.00273,-0.00367,-0.02092,0.18245,0.08707,-0.01084,-0.02271,0.00614
|
||||||
|
2009-09-30,0.24346,-0.00466,0.00224,-0.01987,0.21034,0.0913,-0.02188,-0.0212,0.01313
|
||||||
|
2009-10-30,0.28056,-0.00093,0.00981,-0.01424,0.20771,0.11173,-0.01333,-0.0205,0.0182
|
||||||
|
2009-11-30,0.31653,0.00095,0.00998,-0.01624,0.23516,0.12899,-0.01202,-0.02291,0.0195
|
||||||
|
2009-12-31,0.31375,-0.00286,0.0066,-0.016,0.2339,0.1351,-0.01653,-0.02225,0.01734
|
||||||
|
2010-01-29,0.29454,0.00469,0.00497,-0.00916,0.19452,0.12819,-0.00341,-0.01613,0.02122
|
||||||
|
2010-02-26,0.35987,0.00473,0.00558,-0.01439,0.23513,0.16195,-0.00548,-0.02144,0.02431
|
||||||
|
2010-03-31,0.32729,0.00099,-0.00277,-0.01658,0.20033,0.13654,-0.00653,-0.0228,0.01714
|
||||||
|
2010-04-30,0.3502,-0.00889,-0.00952,-0.02765,0.20005,0.14266,-0.01173,-0.04082,0.01437
|
||||||
|
2010-05-31,0.33923,0.00762,-0.02949,-0.03976,0.19764,0.13761,-0.03143,-0.08313,-0.00482
|
||||||
|
2010-06-30,0.36742,0.01882,-0.01628,-0.04859,0.21254,0.15949,0.00244,-0.04359,0.00201
|
||||||
|
2010-07-30,0.39737,0.03592,0.002,-0.02492,0.23912,0.16419,0.0166,-0.03346,0.01469
|
||||||
|
2010-08-31,0.3899,0.04688,0.0026,-0.03286,0.24869,0.16892,0.04775,-0.02564,0.02147
|
||||||
|
2010-09-30,0.37594,0.06821,0.02443,-0.02681,0.24501,0.16362,0.08183,-0.02866,0.02221
|
||||||
|
2010-10-29,0.35642,0.0668,0.03481,-0.02296,0.24219,0.1544,0.0896,-0.02135,0.02189
|
||||||
|
2010-11-30,0.40622,0.06326,0.01265,-0.04894,0.2692,0.1482,0.09792,-0.05319,0.02055
|
||||||
|
2010-12-31,0.40789,0.06338,0.00343,-0.03428,0.25506,0.15128,0.10154,-0.03756,0.02427
|
||||||
|
2011-01-31,0.34887,0.05697,0.03574,-0.01732,0.22018,0.13962,0.1116,-0.02661,0.02123
|
||||||
|
2011-02-28,0.40354,0.0636,0.04378,-0.01854,0.22783,0.17377,0.15959,-0.02905,0.02644
|
||||||
|
2011-03-31,0.37116,0.06577,0.05658,-0.01664,0.19422,0.15498,0.14352,-0.0153,0.03369
|
||||||
|
2011-04-29,0.39074,0.06953,0.08416,-0.01306,0.20704,0.17945,0.1661,-0.01267,0.04138
|
||||||
|
2011-05-31,0.41913,0.08254,0.07421,-0.01045,0.22149,0.20071,0.16388,-0.01524,0.04011
|
||||||
|
2011-06-30,0.39172,0.07768,0.0904,-0.01485,0.20969,0.19216,0.17639,-0.01544,0.038
|
||||||
|
2011-07-29,0.42918,0.06601,0.06684,-0.04223,0.20965,0.18349,0.16189,-0.0533,0.0329
|
||||||
|
2011-08-31,0.40563,0.07463,0.03901,-0.02418,0.22038,0.17278,0.17142,-0.08808,0.0344
|
||||||
|
2011-09-30,0.41384,0.07578,0.02607,-0.04255,0.2174,0.15114,0.16061,-0.05616,0.03082
|
||||||
|
2011-10-31,0.36596,0.06944,0.03209,-0.05399,0.21449,0.14805,0.16287,-0.0448,0.02974
|
||||||
|
2011-11-30,0.37278,0.07865,-0.03304,-0.0911,0.23307,0.14655,0.15661,-0.09343,0.02925
|
||||||
|
2011-12-30,0.37326,0.06774,-0.02124,-0.04744,0.22587,0.10484,0.16878,-0.0492,0.02896
|
||||||
|
2012-01-31,0.32247,0.06679,-0.00669,-0.03004,0.19137,0.11516,0.13956,-0.0348,0.02282
|
||||||
|
2012-02-29,0.35355,0.07104,-0.013,-0.02396,0.21251,0.13324,0.12204,-0.03331,0.02129
|
||||||
|
2012-03-30,0.3387,0.05902,-0.01566,-0.02856,0.204,0.11547,0.1223,-0.0343,0.0194
|
||||||
|
2012-04-30,0.35247,0.06577,-0.01083,-0.0278,0.22515,0.12265,0.12783,-0.03636,0.02156
|
||||||
|
2012-05-31,0.28604,0.06949,-0.01201,-0.03022,0.19983,0.11789,0.12068,-0.05559,0.01559
|
||||||
|
2012-06-29,0.29541,0.06766,-0.02531,-0.04436,0.2055,0.11097,0.11482,-0.07398,0.00701
|
||||||
|
2012-07-31,0.30415,0.06882,-0.03653,-0.03251,0.21702,0.12112,0.13351,-0.07487,0.00191
|
||||||
|
2012-08-31,0.27741,0.06179,-0.03094,-0.02272,0.1981,0.11112,0.1212,-0.042,0.01133
|
||||||
|
2012-09-28,0.29126,0.06502,-0.02895,-0.02544,0.20124,0.11348,0.09055,-0.05322,0.00929
|
||||||
|
2012-10-31,0.23803,0.06201,-0.0265,-0.02439,0.19969,0.11098,0.09068,-0.05049,0.00993
|
||||||
|
2012-11-30,0.24051,0.0624,-0.03459,-0.0388,0.20248,0.11241,0.08962,-0.04643,0.00312
|
||||||
|
2012-12-31,0.2376,0.06526,-0.02764,-0.02533,0.20141,0.11547,0.07453,-0.06338,0.00861
|
||||||
|
2013-01-31,0.21395,0.06005,-0.01805,-0.01818,0.18462,0.11209,0.06017,-0.02526,0.01577
|
||||||
|
2013-02-28,0.23903,0.06807,-0.0234,-0.0195,0.20525,0.11493,0.06871,-0.03108,0.01779
|
||||||
|
2013-03-29,0.22216,0.06396,-0.02232,-0.01956,0.20068,0.11806,0.06657,-0.03908,0.01647
|
||||||
|
2013-04-30,0.24111,0.07553,-0.02022,-0.01498,0.2229,0.11857,0.0681,-0.02692,0.02121
|
||||||
|
2013-05-31,0.22218,0.07061,-0.01723,-0.01218,0.20839,0.11327,0.07279,-0.0344,0.02111
|
||||||
|
2013-06-28,0.22821,0.07296,-0.01196,-0.01157,0.22036,0.10672,0.07154,-0.02325,0.0211
|
||||||
|
2013-07-31,0.21967,0.07876,-0.01154,-0.01627,0.22011,0.11829,0.07173,-0.0226,0.02243
|
||||||
|
2013-08-30,0.19791,0.07297,-0.01214,-0.01344,0.20317,0.11106,0.06792,-0.0236,0.02263
|
||||||
|
2013-09-30,0.21546,0.07585,-0.00692,-0.01906,0.22236,0.12197,0.07672,-0.02323,0.02532
|
||||||
|
2013-10-31,0.19584,0.07764,-0.00348,-0.01467,0.20317,0.10906,0.0726,-0.02761,0.02179
|
||||||
|
2013-11-29,0.19945,0.07742,0.00348,-0.03,0.21459,0.11537,0.06714,-0.03099,0.02198
|
||||||
|
2013-12-31,0.2027,0.07621,0.00352,-0.01541,0.22381,0.11663,0.05308,-0.02362,0.01932
|
||||||
|
2014-01-31,0.18754,0.06545,-0.00276,-0.01373,0.19308,0.10265,0.04597,-0.02319,0.02069
|
||||||
|
2014-02-28,0.20658,0.07315,0.00424,-0.01578,0.22519,0.11544,0.05182,-0.02044,0.02268
|
||||||
|
2014-03-31,0.20392,0.06974,0.00775,-0.01661,0.22959,0.11568,0.04703,-0.02491,0.0222
|
||||||
|
2014-04-30,0.21207,0.07469,0.00922,-0.0188,0.26341,0.12366,0.0563,-0.02272,0.02369
|
||||||
|
2014-05-30,0.20734,0.07273,0.00488,-0.01818,0.28423,0.11829,0.05642,-0.02797,0.02206
|
||||||
|
2014-06-30,0.244,0.08258,-0.0119,-0.02498,0.2974,0.12117,0.03963,-0.0282,0.02515
|
||||||
|
2014-07-31,0.21111,0.07529,-0.00889,-0.0178,0.30089,0.10855,0.01347,-0.0198,0.02488
|
||||||
|
2014-08-29,0.20671,0.06912,-0.01751,-0.02311,0.28831,0.10436,0.00861,-0.02621,0.02288
|
||||||
|
2014-09-30,0.22853,0.07871,-0.02002,-0.02215,0.31739,0.11079,0.01332,-0.02407,0.02714
|
||||||
|
2014-10-31,0.20615,0.07359,-0.01922,-0.0223,0.28966,0.10319,-0.00649,-0.02182,0.02438
|
||||||
|
2014-11-28,0.21617,0.07364,-0.01785,-0.06279,0.33176,0.10913,-0.00712,-0.04464,0.0166
|
||||||
|
2014-12-31,0.22186,0.06646,-0.03094,-0.02611,0.33449,0.08706,-0.01048,-0.05134,0.02373
|
||||||
|
2015-01-30,0.18012,0.04179,-0.02366,-0.02793,0.27594,0.08228,-0.02357,-0.14684,0.01798
|
||||||
|
2015-02-27,0.19804,0.04644,-0.03843,-0.0456,0.33621,0.09209,-0.03322,-0.13722,0.0233
|
||||||
|
2015-03-31,0.18548,0.03631,-0.04009,-0.04779,0.30985,0.09153,-0.04558,-0.12981,0.02088
|
||||||
|
2015-04-30,0.17061,0.0421,-0.03954,-0.02816,0.30123,0.08914,-0.05942,-0.10679,0.02148
|
||||||
|
2015-05-29,0.15867,0.04886,-0.03869,-0.04264,0.27865,0.08384,-0.0569,-0.10606,0.02097
|
||||||
|
2015-06-30,0.17781,0.04324,-0.03989,-0.03719,0.27985,0.07369,-0.06305,-0.11777,0.02099
|
||||||
|
2015-07-31,0.18441,0.02075,-0.04012,-0.02898,0.25624,0.06837,-0.05226,-0.09681,0.02179
|
||||||
|
2015-08-31,0.15412,0.00827,-0.04355,-0.052,0.22607,0.05997,-0.06767,-0.09518,0.01626
|
||||||
|
2015-09-30,0.16796,0.01492,-0.04681,-0.03633,0.22693,0.04958,-0.06739,-0.09932,0.01452
|
||||||
|
2015-10-30,0.15396,0.01911,-0.03785,-0.02636,0.22342,0.0508,-0.06515,-0.09134,0.01748
|
||||||
|
2015-11-30,0.16993,0.00375,-0.1113,-0.12922,0.23246,0.0433,-0.13574,-0.26886,-0.01727
|
||||||
|
2015-12-31,0.15191,-0.0072,-0.07557,-0.06744,0.21056,0.03197,-0.0909,-0.13296,-0.00882
|
||||||
|
2016-01-29,0.13725,0.00071,-0.06918,-0.05114,0.1748,0.03162,-0.08622,-0.11327,-0.00282
|
||||||
|
2016-02-29,0.15044,-0.0,-0.09671,-0.11213,0.19584,0.02214,-0.12571,-0.16278,-0.0122
|
||||||
|
2016-03-31,0.13623,-0.00387,-0.08685,-0.09498,0.16,0.0133,-0.10552,-0.13375,-0.00904
|
||||||
|
2016-04-29,0.12687,-0.0,-0.09022,-0.07012,0.16603,0.01453,-0.10788,-0.1305,-0.00819
|
||||||
|
2016-05-31,0.1193,0.00229,-0.1139,-0.11801,0.17012,0.00789,-0.13585,-0.15502,-0.02336
|
||||||
|
2016-06-30,0.11156,-0.01078,-0.11139,-0.09606,0.15879,0.00335,-0.13417,-0.20036,-0.02244
|
||||||
|
2016-07-29,0.10427,-0.02071,-0.11728,-0.10358,0.14431,-0.00831,-0.14542,-0.17606,-0.04744
|
||||||
|
2016-08-31,0.07787,-0.01601,-0.13334,-0.14232,0.13379,-0.00683,-0.15357,-0.18744,-0.07785
|
||||||
|
2016-09-30,0.06968,-0.02511,-0.14663,-0.12796,0.12659,-0.01026,-0.15606,-0.18379,-0.06618
|
||||||
|
2016-10-31,0.07705,-0.01865,-0.12001,-0.09778,0.11469,-0.00376,-0.13333,-0.14977,-0.05732
|
||||||
|
2016-11-30,0.08026,-0.0357,-0.18349,-0.20557,0.11293,-0.01833,-0.2172,-0.25065,-0.10879
|
||||||
|
2016-12-30,0.08229,-0.03804,-0.16462,-0.17961,0.10905,-0.02928,-0.20495,-0.2305,-0.08737
|
||||||
|
2017-01-31,0.0671,-0.02685,-0.10006,-0.07544,0.08733,-0.02115,-0.11927,-0.13896,-0.04927
|
||||||
|
2017-02-28,0.07417,-0.02265,-0.14898,-0.13917,0.0996,-0.02537,-0.16891,-0.19091,-0.08354
|
||||||
|
2017-03-31,0.0544,-0.04275,-0.13407,-0.12212,0.07585,-0.03144,-0.14078,-0.17299,-0.07115
|
||||||
|
2017-04-28,0.05537,-0.04243,-0.15546,-0.11364,0.07869,-0.03946,-0.16457,-0.20313,-0.08885
|
||||||
|
2017-05-31,0.04831,-0.05925,-0.16457,-0.14081,0.06483,-0.04976,-0.18161,-0.20899,-0.09988
|
||||||
|
2017-06-30,0.03959,-0.06162,-0.16751,-0.13742,0.05738,-0.06699,-0.18322,-0.20592,-0.10388
|
||||||
|
2017-07-31,0.04094,-0.04799,-0.17316,-0.14364,0.06004,-0.07508,-0.18833,-0.20231,-0.11144
|
||||||
|
2017-08-31,0.03267,-0.03271,-0.16,-0.1423,0.05726,-0.06763,-0.17018,-0.19791,-0.10549
|
||||||
|
2017-09-29,0.03817,-0.02159,-0.16455,-0.15212,0.06227,-0.06861,-0.17093,-0.20484,-0.10132
|
||||||
|
2017-10-31,0.0309,-0.02483,-0.16572,-0.13095,0.06279,-0.07311,-0.16976,-0.20386,-0.08807
|
||||||
|
2017-11-30,0.01652,-0.0559,-0.2246,-0.1916,0.04373,-0.11913,-0.28129,-0.28964,-0.1351
|
||||||
|
2017-12-29,-0.00385,-0.0471,-0.18867,-0.15102,0.04078,-0.09394,-0.19573,-0.22704,-0.10639
|
||||||
|
2018-01-31,0.01165,-0.03261,-0.17531,-0.14852,0.03514,-0.08341,-0.18338,-0.20891,-0.10262
|
||||||
|
2018-02-28,-0.00118,-0.06167,-0.24975,-0.22846,0.03047,-0.11049,-0.25329,-0.29141,-0.15447
|
||||||
|
2018-03-30,5.0e-5,-0.05586,-0.21339,-0.18776,0.00832,-0.09099,-0.21109,-0.24361,-0.12752
|
||||||
|
2018-04-30,-0.0071,-0.0632,-0.22821,-0.20684,0.01277,-0.10466,-0.23291,-0.25895,-0.1451
|
||||||
|
2018-05-31,-0.0181,-0.09023,-0.245,-0.21773,-0.0,-0.12437,-0.24862,-0.28114,-0.16144
|
||||||
|
2018-06-29,-0.01091,-0.05171,-0.22351,-0.2056,-0.00148,-0.12301,-0.2283,-0.25512,-0.14003
|
||||||
|
2018-07-31,-0.01456,-0.05762,-0.23898,-0.21266,0.00441,-0.13289,-0.24499,-0.26398,-0.13408
|
||||||
|
2018-08-31,0.00113,-0.0583,-0.21976,-0.21008,0.00301,-0.11965,-0.23275,-0.25262,-0.12149
|
||||||
|
2018-09-28,-0.02243,-0.06733,-0.23902,-0.22361,-0.01961,-0.12497,-0.25152,-0.27474,-0.1364
|
||||||
|
2018-10-31,-0.03542,-0.05332,-0.23781,-0.21839,-0.03215,-0.12489,-0.24692,-0.26977,-0.13218
|
||||||
|
2018-11-30,-0.05283,-0.09409,-0.31658,-0.3315,-0.04949,-0.15844,-0.33855,-0.36691,-0.19731
|
||||||
|
2018-12-31,-0.06724,-0.08204,-0.26512,-0.24521,-0.05814,-0.14585,-0.25772,-0.29359,-0.1577
|
||||||
|
2019-01-31,-0.04871,-0.06474,-0.23554,-0.2169,-0.06486,-0.13234,-0.22053,-0.26342,-0.14281
|
||||||
|
2019-02-28,-0.05275,-0.07596,-0.25709,-0.24788,-0.06454,-0.13969,-0.24524,-0.29556,-0.16001
|
||||||
|
2019-03-29,-0.05761,-0.07563,-0.25292,-0.27159,-0.05717,-0.1177,-0.22429,-0.28356,-0.15107
|
||||||
|
2019-04-30,-0.07844,-0.08106,-0.26566,-0.26135,-0.05996,-0.12671,-0.2533,-0.30075,-0.16938
|
||||||
|
2019-05-31,-0.0965,-0.07918,-0.26471,-0.25222,-0.06901,-0.11876,-0.23827,-0.30169,-0.16172
|
||||||
|
2019-06-28,-0.10918,-0.07273,-0.25536,-0.23985,-0.07144,-0.10491,-0.23599,-0.29068,-0.15545
|
||||||
|
2019-07-31,-0.1105,-0.06246,-0.2525,-0.22877,-0.06816,-0.09364,-0.23712,-0.28323,-0.15261
|
||||||
|
2019-08-30,-0.09238,-0.04893,-0.23902,-0.22296,-0.08717,-0.07701,-0.20323,-0.27731,-0.13375
|
||||||
|
2019-09-30,-0.11606,-0.04835,-0.24556,-0.22092,-0.07493,-0.05791,-0.21534,-0.27114,-0.133
|
||||||
|
2019-10-31,-0.08161,-0.00609,-0.19128,-0.17128,-0.05456,-0.01318,-0.16101,-0.22016,-0.09269
|
||||||
|
2019-11-29,-0.09119,-0.03614,-0.25118,-0.28421,-0.04984,-0.04453,-0.22235,-0.29161,-0.14446
|
||||||
|
2019-12-31,-0.07637,-0.0162,-0.1913,-0.17194,-0.04446,-0.014,-0.15127,-0.21606,-0.08979
|
|
301
Exam1/Data/Data_Returns.csv
Normal file
301
Exam1/Data/Data_Returns.csv
Normal file
|
@ -0,0 +1,301 @@
|
||||||
|
,AUD,CAD,EUR,JPY,NZD,NOK,SEK,CHF,GBP
|
||||||
|
1995-01-31,-2.23468,-0.45413,1.35587,-0.50166,0.0064,0.85443,-0.45672,1.49242,0.81721
|
||||||
|
1995-02-28,-2.50018,1.33534,4.73052,2.8885,-0.74121,3.58752,2.48313,4.07651,0.3357
|
||||||
|
1995-03-31,-0.53472,-0.36431,6.05685,11.12778,3.44512,5.046,-0.37411,9.20703,2.57641
|
||||||
|
1995-04-28,-0.56744,3.12569,-1.16841,2.67762,3.03784,-1.61174,1.674,-1.80707,-0.96053
|
||||||
|
1995-05-31,-1.17039,-0.56955,-2.03071,-1.06974,-0.93005,-0.79586,-0.77358,-2.04029,-1.31865
|
||||||
|
1995-06-30,-0.99404,-0.16017,2.05336,-0.62511,0.92977,1.93428,1.15052,1.06817,0.24587
|
||||||
|
1995-07-31,4.28899,0.3797,-0.26717,-4.33994,0.98389,0.36177,3.23001,-0.19122,0.54112
|
||||||
|
1995-08-31,1.8499,2.04006,-5.70591,-10.1616,-3.18844,-4.36579,-2.94247,-4.79708,-2.78717
|
||||||
|
1995-09-29,0.59686,-0.00744,2.58385,-1.71126,1.52164,2.10627,5.49438,4.20459,1.96647
|
||||||
|
1995-10-31,0.93664,0.39585,1.19952,-3.62347,0.48188,0.74187,4.71846,1.25891,0.0696
|
||||||
|
1995-11-30,-2.21561,-1.35503,-2.74456,-0.00491,-0.88808,-2.29688,1.55864,-3.65926,-3.15743
|
||||||
|
1995-12-29,0.19344,-0.41804,0.78861,-1.90315,0.26794,0.67515,-0.94485,1.70154,1.5625
|
||||||
|
1996-01-31,0.15624,-0.84358,-3.819,-3.89434,3.10505,-2.9442,-4.07007,-5.21753,-2.54445
|
||||||
|
1996-02-29,2.83444,0.16755,1.02048,1.26937,0.36337,1.82358,2.99067,0.80975,1.33624
|
||||||
|
1996-03-29,2.51799,0.70428,-0.59608,-2.05236,1.52568,-0.24636,1.33613,0.4036,-0.29396
|
||||||
|
1996-04-30,0.6838,0.0955,-3.74241,1.69467,1.11248,-2.49182,-1.47384,-4.49456,-1.40928
|
||||||
|
1996-05-31,1.78072,-0.72966,0.24267,-3.34137,-0.71996,0.60801,0.91963,-1.05524,3.21958
|
||||||
|
1996-06-28,-1.26712,0.32227,0.1184,-1.81411,1.26096,0.52376,1.71222,-0.11204,0.15475
|
||||||
|
1996-07-31,-1.54703,-0.71309,3.10462,1.93911,0.9929,1.90371,0.25117,4.07516,0.19963
|
||||||
|
1996-08-30,2.34727,0.4609,-0.676,-2.12952,0.63602,-0.71624,-0.17369,-0.45004,0.4243
|
||||||
|
1996-09-30,0.21357,0.24227,-3.30081,-3.33632,1.48909,-1.5128,-0.13124,-4.66098,0.17929
|
||||||
|
1996-10-31,0.37272,1.45425,0.69385,-2.26453,1.54151,1.91268,0.86923,-0.88769,4.07954
|
||||||
|
1996-11-29,2.7676,-0.67522,-1.78165,-0.57502,0.99665,-0.69943,-2.2118,-3.46865,3.36878
|
||||||
|
1996-12-31,-2.07089,-1.86118,-1.03797,-2.23758,-0.30432,-0.08103,-2.39814,-3.76268,1.92777
|
||||||
|
1997-01-31,-4.26465,1.42423,-5.47719,-5.07002,-2.52867,-1.20422,-5.09438,-5.43845,-6.4015
|
||||||
|
1997-02-28,1.84346,-1.55461,-3.16076,0.19886,0.6035,-3.91179,-3.64026,-3.79322,1.86688
|
||||||
|
1997-03-31,1.03057,-1.25661,1.00216,-2.93797,0.61545,1.95365,-0.57541,2.06719,0.69789
|
||||||
|
1997-04-30,-0.24671,-1.36295,-3.85459,-2.85911,-0.04036,-7.53988,-4.10108,-2.53699,-1.14837
|
||||||
|
1997-05-30,-2.58757,1.1041,1.31449,8.43131,-0.31319,-0.10262,1.39942,3.79586,0.87992
|
||||||
|
1997-06-30,-1.61344,-0.2247,-2.32985,1.41134,-1.75103,-3.01401,-0.1636,-3.34316,1.79329
|
||||||
|
1997-07-31,-0.5787,-0.39719,-5.35061,-3.80912,-4.20709,-4.13397,-3.01586,-3.95675,-1.52153
|
||||||
|
1997-08-29,-1.48034,-0.6417,1.85257,-1.99626,-1.21759,2.0842,1.32149,1.53224,-0.84979
|
||||||
|
1997-09-30,-1.47423,0.23141,1.71781,-0.78398,0.32444,4.93865,3.42829,1.81781,-0.27102
|
||||||
|
1997-10-31,-2.73746,-2.04755,2.39789,-0.08564,-2.37261,1.10348,1.19251,3.61429,3.94094
|
||||||
|
1997-11-28,-2.98639,-1.16614,-2.59709,-6.1669,-0.94431,-2.82474,-3.16734,-2.37427,0.60129
|
||||||
|
1997-12-31,-4.87402,-0.71414,-2.08832,-2.39031,-5.76265,-2.65382,-2.81741,-2.63746,-2.21093
|
||||||
|
1998-01-30,5.10884,-2.20618,-1.8151,2.07413,1.01359,-2.89518,-2.22435,-1.5248,-0.48086
|
||||||
|
1998-02-27,-0.65266,2.66301,0.5648,-0.02177,0.57082,-0.03501,1.19717,0.19624,0.84708
|
||||||
|
1998-03-31,-2.72325,-0.00281,-2.05202,-5.67995,-5.51464,-0.8827,0.05379,-4.12594,1.86137
|
||||||
|
1998-04-30,-1.74677,-0.64667,2.87044,0.45902,0.86032,2.06822,3.20011,1.26208,0.00718
|
||||||
|
1998-05-29,-3.98278,-1.75533,0.4807,-5.08606,-3.39402,-1.13179,-1.26741,1.08008,-2.32346
|
||||||
|
1998-06-30,-1.19392,-1.12335,-1.40388,-0.584,-2.83575,-1.93328,-1.89658,-2.83492,2.46632
|
||||||
|
1998-07-31,-1.99053,-2.50381,1.29147,-4.31812,-1.10802,1.55157,0.40401,1.44784,-1.78604
|
||||||
|
1998-08-31,-5.82208,-3.69134,0.66742,1.80926,-3.12683,-3.95646,-2.03566,2.7235,2.52564
|
||||||
|
1998-09-30,3.44423,2.51278,5.31949,3.27689,1.00297,6.40241,3.13166,4.12483,1.65152
|
||||||
|
1998-10-30,5.01506,-1.33484,0.75353,16.30267,5.73409,0.62668,0.12447,1.86752,-1.29027
|
||||||
|
1998-11-30,1.00394,1.14613,-2.18772,-5.60161,-0.40014,-1.00944,-3.73744,-3.01401,-1.27577
|
||||||
|
1998-12-31,-2.51578,-0.47461,1.33886,8.42199,-0.06999,-1.77517,-0.1135,0.95377,0.91468
|
||||||
|
1999-01-29,2.38687,1.6681,-3.43456,-3.39267,1.85973,1.43432,3.71296,-3.54752,-1.16118
|
||||||
|
1999-02-26,-1.18555,0.08811,-3.47309,-2.33881,-2.75716,-4.62704,-5.07124,-2.34076,-2.4105
|
||||||
|
1999-03-31,1.67259,-0.20756,-1.80117,-0.21912,1.71657,2.69447,-0.28943,-2.44038,0.82506
|
||||||
|
1999-04-30,4.93297,3.87077,-2.17288,-1.22314,4.89222,-0.56765,-2.39151,-3.08637,-0.23237
|
||||||
|
1999-05-31,-2.12901,-1.39026,-1.62517,-2.39706,-4.55008,-1.28773,-2.48897,-0.5427,-0.39079
|
||||||
|
1999-06-30,2.01134,-0.24863,-1.30549,0.21852,-0.85505,0.52271,1.13856,-2.17998,-1.65648
|
||||||
|
1999-07-30,-1.37666,-1.41467,3.54802,5.00327,0.01697,1.19486,3.49146,3.76289,2.74959
|
||||||
|
1999-08-31,-2.47062,0.52942,-1.52346,4.20767,-2.5613,-0.66199,-0.89297,-1.81308,-0.7475
|
||||||
|
1999-09-30,2.36467,1.49166,0.57606,2.49977,-0.1817,1.29571,0.52879,0.56844,2.38103
|
||||||
|
1999-10-29,-2.4026,-0.15841,-1.54441,1.55471,-2.03592,-1.14856,-0.68158,-1.86979,-0.36856
|
||||||
|
1999-11-30,-0.29151,-0.20908,-4.3992,1.67735,0.62185,-2.66204,-3.27138,-4.55717,-2.94096
|
||||||
|
1999-12-31,2.81221,1.35396,-0.71593,-0.71369,2.18878,0.25755,-0.72925,-1.00269,1.13579
|
||||||
|
2000-01-31,-2.59293,0.09663,-2.61468,-4.87459,-5.09774,-2.82061,-3.04998,-3.00037,0.55213
|
||||||
|
2000-02-29,-3.75923,-0.21158,-1.80113,-3.00988,-1.98018,-1.43589,0.12262,-1.77715,-2.60237
|
||||||
|
2000-03-31,-1.2055,-0.13018,-0.8273,6.56413,2.25635,-0.61558,1.29456,0.01504,1.06366
|
||||||
|
2000-04-28,-3.90609,-2.08747,-4.96599,-5.63544,-2.13169,-5.70191,-3.67642,-3.64138,-1.93098
|
||||||
|
2000-05-31,-2.25037,-1.14175,1.55002,-0.16299,-6.0636,-0.16738,-0.87374,1.20118,-4.49812
|
||||||
|
2000-06-30,5.18765,1.00358,3.1484,1.19229,3.07608,4.80597,2.52024,3.90197,1.22769
|
||||||
|
2000-07-31,-3.61723,-0.35604,-3.53278,-4.01487,-3.51492,-3.50946,-4.60087,-2.8788,-1.13412
|
||||||
|
2000-08-31,-0.25605,0.68082,-4.2461,2.12866,-5.74925,-2.38859,-3.04986,-4.51968,-2.91013
|
||||||
|
2000-09-29,-6.29559,-2.05769,-0.90101,-1.82029,-5.07659,-0.01818,-2.25838,0.54096,1.56416
|
||||||
|
2000-10-31,-4.34479,-1.55438,-4.09621,-1.61324,-2.37363,-2.33858,-4.08433,-4.15216,-1.83877
|
||||||
|
2000-11-30,1.42548,-0.69035,2.55252,-1.97454,3.34056,0.36507,-0.34051,3.27745,-2.43374
|
||||||
|
2000-12-29,5.62416,2.23835,7.6878,-3.49869,7.85317,5.1993,6.03837,6.80963,5.29581
|
||||||
|
2001-01-31,-1.21228,-0.0353,-1.09833,-2.30124,0.0452,-0.14312,-1.2218,-1.71046,-2.25056
|
||||||
|
2001-02-28,-4.5404,-1.91795,-1.17119,-1.25095,-3.11497,-1.06037,-3.221,-2.00191,-1.26985
|
||||||
|
2001-03-30,-6.83415,-2.78991,-3.9096,-6.79724,-5.52818,-1.64231,-4.78172,-3.11182,-1.40166
|
||||||
|
2001-04-30,4.59648,2.46599,0.26245,0.97478,2.03173,0.13496,0.38312,-0.65537,0.67974
|
||||||
|
2001-05-31,-0.72729,-0.58991,-4.3558,3.5558,-0.73833,-2.42727,-4.63298,-3.40635,-0.62955
|
||||||
|
2001-06-29,0.31249,1.91816,-0.08094,-5.05693,-1.512,0.58163,-1.09229,-0.25758,-0.91103
|
||||||
|
2001-07-31,-0.07623,-0.65232,3.41371,-0.45706,2.47209,2.46326,2.46779,4.00544,1.44211
|
||||||
|
2001-08-31,3.79736,-1.35604,3.89049,4.58821,6.46592,3.38175,1.75369,3.44797,1.90181
|
||||||
|
2001-09-28,-6.08359,-1.83832,0.32129,-0.39998,-6.74217,0.25255,-1.98868,3.23167,1.42507
|
||||||
|
2001-10-31,2.41057,-0.36745,-1.00714,-2.89408,1.72456,0.24996,0.41327,-1.03558,-0.88193
|
||||||
|
2001-11-30,3.25092,0.99128,-0.47381,-0.7716,0.97111,-0.16904,-0.33086,-0.85621,-1.78381
|
||||||
|
2001-12-31,-1.39692,-1.51976,-0.44068,-6.22349,0.3567,0.01394,2.01102,-0.8107,2.22157
|
||||||
|
2002-01-31,-0.80919,0.40624,-3.19424,-2.18202,0.15891,-1.07042,-1.23496,-3.02354,-2.71362
|
||||||
|
2002-02-28,2.05391,-0.87317,0.59617,-0.17784,1.26735,2.54077,1.84205,0.4195,0.24025
|
||||||
|
2002-03-29,3.59956,0.58919,0.98488,0.81412,5.00203,1.301,1.19664,1.3325,0.85914
|
||||||
|
2002-04-30,0.81615,1.66274,3.41857,3.06519,1.92925,5.48008,0.99657,3.77693,2.52063
|
||||||
|
2002-05-31,5.64735,2.77356,3.8118,3.29439,7.39429,5.34428,5.73268,3.37248,0.59401
|
||||||
|
2002-06-28,-0.58865,0.53126,5.85134,3.37894,1.71089,7.2072,6.19607,5.21602,4.36923
|
||||||
|
2002-07-31,-2.7906,-3.93297,-0.59351,-0.07331,-3.12984,-1.06844,-3.13397,0.37519,2.66833
|
||||||
|
2002-08-30,1.47764,1.64487,0.16898,0.84595,0.07705,1.61701,1.51693,-1.34137,-0.80477
|
||||||
|
2002-09-30,-1.12392,-1.55844,0.89637,-2.75505,0.63082,2.08142,1.44783,1.77233,1.84046
|
||||||
|
2002-10-31,2.41658,1.40913,0.34481,-0.79667,4.04451,0.13716,1.4363,-0.30551,-0.33001
|
||||||
|
2002-11-29,1.41051,0.02426,0.58082,-0.14325,2.96364,1.99904,1.29228,-0.56964,-0.35924
|
||||||
|
2002-12-31,0.58932,-0.71781,5.65028,3.09008,5.24516,6.23214,4.28139,7.21704,3.68791
|
||||||
|
2003-01-31,4.40792,3.27239,2.4303,-1.17225,4.46816,0.15401,1.38747,1.03612,2.32578
|
||||||
|
2003-02-28,3.79984,2.92321,0.5202,1.34489,2.62178,-2.68314,1.41674,0.79794,-3.97839
|
||||||
|
2003-03-31,-0.13662,1.42153,1.36246,-0.41238,-0.06132,-0.90399,0.56434,0.27452,0.55409
|
||||||
|
2003-04-30,3.81201,2.68995,2.38459,-0.67583,1.31317,4.07414,3.85907,-0.44238,1.30702
|
||||||
|
2003-05-30,4.47991,4.65157,5.50604,-0.40258,3.60514,4.90392,5.50453,4.04161,2.70241
|
||||||
|
2003-06-30,3.20966,1.2379,-2.27637,-0.50468,1.7696,-7.00155,-2.80077,-3.93193,0.91303
|
||||||
|
2003-07-31,-3.03711,-3.32114,-1.89106,-0.50711,-0.60866,-0.08352,-2.35856,-1.42098,-2.36517
|
||||||
|
2003-08-29,0.18112,1.69917,-2.37054,3.21135,-0.10905,-3.15695,-1.62895,-2.0392,-1.40621
|
||||||
|
2003-09-30,4.89108,2.94342,6.16333,4.34382,3.25435,6.40811,8.25103,5.97531,5.24982
|
||||||
|
2003-10-31,5.18244,2.53329,-0.0844,1.51499,3.81854,-0.08201,-0.68582,-1.18396,2.36527
|
||||||
|
2003-11-28,2.3015,1.54089,3.19983,0.2878,4.34357,3.78697,3.38865,3.21717,1.57034
|
||||||
|
2003-12-31,4.49289,0.74134,5.31923,2.07549,3.1468,2.71089,5.16657,4.44067,4.3217
|
||||||
|
2004-01-30,1.3819,-2.73151,-1.42535,1.14974,2.4906,-5.02267,-2.76267,-1.95017,1.91998
|
||||||
|
2004-02-27,1.74146,-0.72592,0.09592,-3.20685,2.61054,-0.30329,-0.31635,-0.8291,2.18825
|
||||||
|
2004-03-31,-0.6785,2.20126,-1.01096,4.92305,-2.51849,2.59691,-1.15961,0.17997,-0.71096
|
||||||
|
2004-04-30,-5.05304,-4.34176,-2.37198,-5.82411,-5.70304,0.15452,-1.18726,-2.36927,-3.25001
|
||||||
|
2004-05-31,-0.81877,0.86765,1.95361,-0.24863,0.84256,2.28808,2.56222,3.28364,3.68961
|
||||||
|
2004-06-30,-2.03048,1.86314,-0.28615,1.19663,1.45703,-3.11044,-0.94691,0.08464,-0.81546
|
||||||
|
2004-07-30,0.89268,0.94582,-0.98122,-2.21946,0.18021,-0.95579,-1.77185,-2.18705,0.54462
|
||||||
|
2004-08-31,1.20228,0.91378,0.99295,1.42667,3.94785,1.38878,2.19572,0.79571,-0.79849
|
||||||
|
2004-09-30,2.91866,4.15472,2.23655,-0.57279,3.42163,2.64532,3.23549,1.4383,0.86734
|
||||||
|
2004-10-29,3.42957,3.8016,2.4422,3.62934,1.62366,5.31974,2.36871,3.82038,1.50853
|
||||||
|
2004-11-30,4.16124,2.63404,4.49035,3.13167,5.12994,4.41939,5.7097,5.46972,4.55983
|
||||||
|
2004-12-31,1.31239,-0.72612,2.26214,0.10559,1.21633,1.02499,1.24281,-0.07563,0.66431
|
||||||
|
2005-01-31,-0.87935,-3.41406,-4.1189,-1.16746,-1.17564,-4.7094,-4.90951,-4.46277,-1.54667
|
||||||
|
2005-02-28,2.65763,0.93964,1.78894,-0.99295,2.85924,2.53989,2.34272,2.48359,2.27146
|
||||||
|
2005-03-31,-2.30555,1.59359,-2.13968,-2.7399,-1.87321,-2.01698,-3.17753,-3.01703,-1.68984
|
||||||
|
2005-04-29,1.17843,-3.61449,-0.73265,1.69223,3.02851,0.25407,-1.2114,-0.05124,1.23503
|
||||||
|
2005-05-31,-2.73268,0.01914,-4.43245,-2.91023,-3.2121,-1.90556,-3.86533,-4.57108,-4.42541
|
||||||
|
2005-06-30,0.80779,2.34242,-2.04085,-3.01597,-1.14792,-2.07272,-5.27292,-3.02709,-1.50399
|
||||||
|
2005-07-29,-0.09959,-0.16069,0.22668,-1.42522,-1.49637,0.81211,0.50012,-0.53846,-1.66169
|
||||||
|
2005-08-31,-0.98488,3.09991,1.1306,0.56103,1.63222,1.27177,1.99238,1.85387,2.24553
|
||||||
|
2005-09-30,1.82894,2.28072,-2.09777,-2.25384,0.41999,-2.21158,-1.93375,-2.63487,-1.57066
|
||||||
|
2005-10-31,-2.01238,-1.68963,-0.80042,-2.95305,1.20313,0.22045,-3.12834,-0.41205,0.1216
|
||||||
|
2005-11-30,-0.81989,0.85491,-1.7282,-3.045,0.7034,-3.59441,-1.62858,-2.06291,-2.2174
|
||||||
|
2005-12-30,-0.78527,-0.05992,-0.1158,0.97856,-2.83207,-0.73413,1.2697,-0.54706,-0.76819
|
||||||
|
2006-01-31,3.22988,2.07994,2.77217,0.43608,0.87467,1.5363,4.26967,2.55249,3.5438
|
||||||
|
2006-02-28,-1.72409,0.39105,-1.98116,0.71599,-3.13707,-1.5256,-4.03006,-2.60512,-1.47347
|
||||||
|
2006-03-31,-3.93872,-2.527,1.31729,-2.23256,-7.45564,2.62599,1.36482,0.04515,-0.96718
|
||||||
|
2006-04-28,6.46963,4.02124,3.90159,2.85578,4.26163,6.0497,5.28189,4.75176,4.76067
|
||||||
|
2006-05-31,-0.62176,1.69672,1.74755,1.42886,0.17335,1.52043,2.07712,2.00329,2.90689
|
||||||
|
2006-06-30,-1.3654,-1.08084,-0.64854,-2.34895,-3.82706,-2.61498,-0.07871,-1.13959,-1.2077
|
||||||
|
2006-07-31,3.18862,-1.70051,-0.41759,-0.57098,1.23143,0.66366,-0.50608,-0.8714,0.87691
|
||||||
|
2006-08-31,-0.28922,1.74904,0.08246,-2.89227,6.36344,-2.41023,-0.46868,-0.49475,1.80636
|
||||||
|
2006-09-29,-2.2203,-0.61758,-1.2228,-1.07094,-0.07805,-3.46483,-1.52991,-1.98174,-1.83925
|
||||||
|
2006-10-31,3.76907,-0.51586,0.58342,0.30746,2.74214,-0.52286,1.24671,0.50408,2.06239
|
||||||
|
2006-11-30,2.09388,-1.97198,3.67469,0.85288,2.44651,6.0184,5.31946,3.49067,3.10729
|
||||||
|
2006-12-29,-0.07936,-1.95755,-0.66882,-3.34724,3.17983,-1.23556,-0.23075,-2.14631,-0.52099
|
||||||
|
2007-01-31,-1.64131,-1.50457,-1.60587,-1.90129,-2.33887,-0.73493,-1.99094,-2.45552,-0.00817
|
||||||
|
2007-02-28,1.79839,0.58846,1.51893,1.70038,2.07814,1.84238,-0.86424,1.92352,0.1221
|
||||||
|
2007-03-30,2.60818,1.54293,0.63978,-0.05014,2.27582,0.52948,-0.16405,-0.24001,0.08369
|
||||||
|
2007-04-30,3.12232,4.29715,2.40109,-1.62981,4.11878,2.54685,4.41256,0.90909,1.96917
|
||||||
|
2007-05-31,-0.48342,3.27659,-1.52671,-2.31458,-0.5154,-1.56872,-3.118,-1.80097,-1.07324
|
||||||
|
2007-06-29,2.58824,0.40705,0.25412,-1.75659,5.00455,2.11958,0.63597,-0.26518,1.45019
|
||||||
|
2007-07-31,1.07092,-0.44767,1.24576,3.30383,-0.23096,1.37894,1.61801,1.60545,1.32032
|
||||||
|
2007-08-31,-4.95475,0.85645,-0.51859,2.27762,-8.4868,-0.04878,-2.33338,-0.6505,-0.69468
|
||||||
|
2007-09-28,8.86461,6.38925,4.18179,0.35648,7.95211,7.27908,6.2966,3.15357,1.06204
|
||||||
|
2007-10-31,4.86174,4.59044,1.65255,-0.60502,2.08104,0.59633,1.59501,0.54873,2.02944
|
||||||
|
2007-11-30,-4.4074,-4.989,1.40718,3.56151,-0.00783,-2.71998,-0.47758,2.43479,-0.92562
|
||||||
|
2007-12-31,-0.59915,1.2412,-0.46496,-1.09099,0.679,2.14297,-1.2223,-0.51318,-3.09751
|
||||||
|
2008-01-31,1.80941,-2.1464,1.21276,4.70915,2.17284,-0.12626,0.96636,4.18257,-0.04173
|
||||||
|
2008-02-29,5.14144,3.02226,2.61109,1.92114,2.79847,4.50918,3.81141,3.83886,0.23431
|
||||||
|
2008-03-31,-2.04644,-4.49491,4.47298,4.3852,-1.48981,2.87342,4.17676,5.49012,0.11687
|
||||||
|
2008-04-30,3.62053,1.9442,-1.64617,-4.94402,-0.08565,-0.29726,-0.91811,-5.05977,-0.13413
|
||||||
|
2008-05-30,1.58053,1.37667,-0.06039,-1.19642,0.70865,0.44607,-0.03447,-0.35128,-0.01214
|
||||||
|
2008-06-30,1.04698,-2.00976,1.52495,-0.57875,-2.27524,0.46,-0.01081,2.54467,0.93318
|
||||||
|
2008-07-31,-1.55291,-1.02969,-0.81289,-2.09807,-3.27119,-0.44262,-0.42666,-2.84447,-0.22113
|
||||||
|
2008-08-29,-8.05653,-3.19479,-5.48609,-0.58005,-3.43857,-4.81789,-5.60247,-4.61146,-7.71165
|
||||||
|
2008-09-30,-8.06855,-0.27182,-4.43926,2.05623,-4.61705,-8.25026,-7.71718,-2.07447,-2.06157
|
||||||
|
2008-10-31,-16.28485,-12.70469,-10.02658,7.3875,-12.65536,-12.01064,-10.62897,-4.30632,-9.59322
|
||||||
|
2008-11-28,-0.99916,-2.00371,0.20177,3.14631,-5.35033,-3.68829,-3.67115,-3.87354,-4.84798
|
||||||
|
2008-12-31,7.21747,0.5484,9.58104,4.8951,7.00945,0.2832,2.44298,13.88359,-6.31414
|
||||||
|
2009-01-30,-8.47542,-0.37988,-7.67986,0.87173,-12.81169,1.55382,-4.61004,-8.32859,0.36199
|
||||||
|
2009-02-27,0.68415,-2.2477,-0.84808,-8.25274,-0.94927,-1.82108,-7.7323,-0.40086,-1.09429
|
||||||
|
2009-03-31,9.00114,0.85363,4.56113,-0.98916,14.20482,4.56273,8.83263,2.40271,0.57326
|
||||||
|
2009-04-30,5.98097,5.88839,-0.19106,0.34834,-0.13521,2.81869,2.66621,-0.23355,3.37226
|
||||||
|
2009-05-29,9.18129,8.40847,6.83264,3.02185,12.83474,4.67099,6.25696,6.75231,8.82813
|
||||||
|
2009-06-30,1.22054,-5.66324,-0.87855,-1.10275,1.32799,-2.1771,-2.15311,-1.97232,2.1372
|
||||||
|
2009-07-31,3.08074,7.40357,1.07281,1.31615,1.81247,4.64644,6.35979,1.01185,0.67039
|
||||||
|
2009-08-31,1.65901,-1.5725,1.21506,2.59128,4.36483,2.61501,2.58612,1.80253,-1.68483
|
||||||
|
2009-09-30,4.9301,2.32181,1.85344,3.58587,5.68298,3.5629,1.52985,1.82721,-1.86231
|
||||||
|
2009-10-30,2.63535,-0.36669,0.94527,-1.10781,0.1413,1.92527,-0.87382,1.2868,3.07728
|
||||||
|
2009-11-30,1.58286,2.17595,1.76403,5.06368,-0.85251,0.44851,0.89867,1.97062,-0.42171
|
||||||
|
2009-12-31,-1.43601,0.56374,-4.42504,-7.48052,1.97318,-1.56955,-2.12518,-2.87594,-1.58035
|
||||||
|
2010-01-29,-0.85394,-1.61239,-3.12049,2.67509,-2.88587,-1.84846,-2.75806,-1.95238,-0.75376
|
||||||
|
2010-02-26,1.0411,0.81366,-1.8093,2.00496,-0.7575,-0.15803,3.21984,-1.75895,-4.97235
|
||||||
|
2010-03-31,2.86903,4.18412,-0.84541,-4.90988,1.85026,-0.264,-1.18438,1.90205,-0.33705
|
||||||
|
2010-04-30,1.74826,0.16883,-1.73629,-0.6228,3.16609,0.65554,-0.71835,-2.38312,0.92375
|
||||||
|
2010-05-31,-9.57952,-3.48692,-7.72257,3.23942,-7.61086,-8.81289,-7.42373,-6.73302,-5.06263
|
||||||
|
2010-06-30,1.0477,-1.24406,-0.20792,2.83513,2.28777,-0.14101,0.5717,7.05151,2.96486
|
||||||
|
2010-07-30,7.56929,3.19433,6.34405,2.06209,5.56114,7.24861,7.6412,3.02318,4.68093
|
||||||
|
2010-08-31,-1.28832,-3.37882,-2.4446,3.17781,-3.05416,-3.34258,-1.8907,3.14596,-1.85009
|
||||||
|
2010-09-30,9.21206,3.99669,7.42287,0.4817,5.25751,7.65155,9.55747,3.7658,2.55309
|
||||||
|
2010-10-29,1.61382,0.87952,1.82987,3.68418,4.23169,-0.39582,0.46773,-0.69446,1.48212
|
||||||
|
2010-11-30,-1.80832,-0.85873,-6.30703,-3.86593,-2.49033,-5.02246,-4.57711,-1.34544,-2.57124
|
||||||
|
2010-12-31,7.31632,3.43179,3.0688,3.23531,5.29011,6.99888,4.70991,6.87373,0.55362
|
||||||
|
2011-01-31,-2.337,-0.59685,2.19753,-1.04088,-0.80338,0.78515,4.65658,-0.84074,2.33059
|
||||||
|
2011-02-28,2.49017,2.6956,0.7846,-0.01732,-2.57374,3.28146,1.70037,1.07053,1.56674
|
||||||
|
2011-03-31,1.97019,0.24878,2.78769,-1.16456,1.74205,1.41438,0.49949,1.56638,-1.42183
|
||||||
|
2011-04-29,6.21332,2.58629,4.6029,2.12743,6.06618,5.75066,4.81236,5.32673,4.09009
|
||||||
|
2011-05-31,-2.2506,-1.99618,-3.02021,-0.13613,2.08742,-2.72343,-2.34662,1.81029,-1.27814
|
||||||
|
2011-06-30,0.89488,0.47145,0.93109,0.58384,0.6238,0.76023,-1.86068,1.2791,-2.42146
|
||||||
|
2011-07-29,3.00395,1.22622,-0.79938,4.60942,6.02182,-0.37398,0.21466,6.80919,2.28434
|
||||||
|
2011-08-31,-2.08413,-2.35926,0.26207,0.89893,-2.06085,1.063,-0.09772,-2.31508,-0.77455
|
||||||
|
2011-09-30,-8.87926,-6.09088,-6.77574,-0.81537,-10.29432,-8.73361,-7.76477,-11.29142,-4.29382
|
||||||
|
2011-10-31,9.61747,4.9927,3.98517,-1.18986,6.47141,6.62677,6.38932,4.24782,3.64601
|
||||||
|
2011-11-30,-2.81789,-2.24342,-3.4586,0.3902,-4.05499,-4.26412,-3.96299,-4.36631,-2.52972
|
||||||
|
2011-12-30,0.16801,-0.06875,-3.59938,0.80493,0.56853,-3.19859,-1.47083,-2.75906,-1.16005
|
||||||
|
2012-01-31,4.04859,1.60443,0.81855,0.85705,6.17468,1.98204,0.84413,1.63549,1.5705
|
||||||
|
2012-02-29,1.99594,1.88537,2.18179,-5.8227,2.22836,5.52562,3.69862,2.04585,1.25246
|
||||||
|
2012-03-30,-3.77909,-1.38076,-0.46231,-1.67009,-2.6099,-2.43494,-0.77795,-0.3651,0.04007
|
||||||
|
2012-04-30,0.83423,1.22375,-0.62462,3.03903,0.04524,-0.30916,-1.1881,-0.45282,1.65327
|
||||||
|
2012-05-31,-6.50275,-4.55686,-6.59123,1.79533,-7.95164,-6.16274,-7.41115,-6.63031,-5.19557
|
||||||
|
2012-06-29,5.98589,1.67443,2.62198,-1.75296,7.18096,2.94073,5.46448,2.61067,1.91954
|
||||||
|
2012-07-31,2.96621,1.77991,-2.97777,2.11858,1.21556,-1.06482,1.78491,-3.03962,-0.09819
|
||||||
|
2012-08-31,-1.49807,1.63551,2.31173,-0.28785,-0.82583,3.96875,2.66656,2.32207,1.37736
|
||||||
|
2012-09-28,0.9038,0.35673,2.03388,0.61981,3.6031,1.29507,1.1146,1.31943,1.67677
|
||||||
|
2012-10-31,0.00726,-1.49175,0.69301,-2.68959,-0.83592,0.38446,-1.09805,0.86985,-0.22296
|
||||||
|
2012-11-30,0.84836,0.68153,0.34247,-3.10354,0.02316,0.92241,-0.25951,0.4674,-0.51152
|
||||||
|
2012-12-31,-0.24561,-0.17375,1.33333,-4.65738,0.73383,1.87898,2.44749,1.16021,1.42893
|
||||||
|
2013-01-31,0.70764,-0.25029,2.92803,-5.27821,2.06598,1.77607,2.31334,0.46897,-2.45546
|
||||||
|
2013-02-28,-1.63867,-2.78197,-3.70375,-1.10188,-1.15641,-4.21396,-1.21899,-2.45714,-4.24237
|
||||||
|
2013-03-29,2.07606,1.2599,-1.80219,-1.89108,1.28112,-1.78688,-0.80083,-1.48265,0.05074
|
||||||
|
2013-04-30,-0.20863,1.06875,2.64981,-3.49901,2.57437,1.43025,0.51258,1.89784,2.51614
|
||||||
|
2013-05-31,-7.41753,-2.60655,-1.71951,-3.54504,-6.83926,-2.0656,-2.54912,-3.22009,-2.56865
|
||||||
|
2013-06-28,-4.31247,-1.95933,0.28179,1.63861,-2.94569,-3.50357,-1.44619,1.38667,0.06069
|
||||||
|
2013-07-31,-1.73012,2.6915,2.13992,0.9847,3.28964,3.35675,3.34124,1.80595,-0.02176
|
||||||
|
2013-08-30,-0.54037,-2.46848,-0.70923,0.22831,-2.6001,-3.385,-1.46969,-0.37109,2.05778
|
||||||
|
2013-09-30,5.19096,2.70162,2.64872,-0.02364,7.8914,2.01289,3.41829,3.08875,4.71048
|
||||||
|
2013-10-31,1.49221,-1.35878,0.4275,-0.00376,-0.39709,1.24148,-0.66481,-0.16675,-0.76529
|
||||||
|
2013-11-29,-3.30537,-1.42061,0.14619,-4.17076,-1.1706,-2.64407,-0.92106,0.18259,1.97977
|
||||||
|
2013-12-31,-1.90764,-0.28047,1.2083,-2.63613,1.03112,0.92569,1.83321,1.57643,1.1333
|
||||||
|
2014-01-31,-2.27649,-4.6316,-2.13397,3.04339,-1.51727,-3.4313,-1.83603,-1.823,-0.75366
|
||||||
|
2014-02-28,2.75203,0.78865,2.41941,-0.0921,4.09569,5.09448,2.24115,2.79746,1.99256
|
||||||
|
2014-03-31,3.79042,0.36331,-0.20718,-0.90897,3.50565,0.18156,-1.1503,-0.30905,-0.49658
|
||||||
|
2014-04-30,0.1733,0.64422,0.6104,0.80069,-0.76927,0.63599,-0.46494,0.30439,1.30611
|
||||||
|
2014-05-30,0.66059,1.1438,-1.58161,0.37918,-0.90332,-0.16791,-2.38229,-1.53252,-0.63975
|
||||||
|
2014-06-30,1.62475,2.01756,0.34442,0.41607,3.40857,-2.53785,-0.02812,0.77808,1.96007
|
||||||
|
2014-07-31,-1.26167,-2.0778,-2.2863,-1.51242,-2.77221,-2.45012,-3.15597,-2.50192,-1.23551
|
||||||
|
2014-08-29,0.81612,0.43512,-1.56447,-1.02835,-1.0754,2.06712,-0.92336,-0.73484,-1.60734
|
||||||
|
2014-09-30,-6.24545,-2.86788,-4.11174,-5.31838,-6.61803,-3.7227,-3.28159,-4.17478,-2.36212
|
||||||
|
2014-10-31,0.67809,-0.80004,-0.83982,-2.17145,0.11841,-4.75678,-2.57951,-0.74601,-1.28709
|
||||||
|
2014-11-28,-2.69922,-1.04977,-0.51947,-5.56515,1.20544,-3.53819,-0.56612,-0.13077,-2.08883
|
||||||
|
2014-12-31,-3.91076,-1.48155,-2.94995,-1.07135,-0.10225,-6.45611,-4.9421,-3.07754,-0.41768
|
||||||
|
2015-01-30,-4.62789,-8.59103,-6.7736,2.04206,-6.82807,-3.17941,-5.37954,7.94413,-3.65519
|
||||||
|
2015-02-27,0.6932,1.58812,-0.62568,-1.76737,4.55236,1.65958,-0.84225,-3.09072,2.91483
|
||||||
|
2015-03-31,-2.20488,-1.3636,-4.28434,-0.36231,-0.57146,-5.23408,-3.31712,-2.53269,-3.91835
|
||||||
|
2015-04-30,3.43775,4.59938,4.28944,0.1652,1.84194,7.04638,3.23064,3.5372,3.54133
|
||||||
|
2015-05-29,-2.79094,-2.92329,-2.19681,-3.59297,-6.25906,-3.27635,-2.32398,-0.78974,-0.68145
|
||||||
|
2015-06-30,0.57122,0.03284,1.59,1.37057,-4.68876,-0.80407,2.9116,0.83463,3.08528
|
||||||
|
2015-07-31,-4.28516,-3.994,-0.88123,-1.27164,-1.49909,-3.14374,-3.30165,-2.87691,-0.75491
|
||||||
|
2015-08-31,-3.28079,-2.20068,1.37726,2.20663,-4.42952,-2.99376,1.00963,-0.71144,-1.42037
|
||||||
|
2015-09-30,-0.79274,-0.75259,-0.42291,1.13305,1.24719,-1.63564,1.10241,-1.12271,-1.49569
|
||||||
|
2015-10-30,1.76553,2.48787,-1.08554,-0.79014,6.00862,0.85596,-1.2902,-0.97591,1.97223
|
||||||
|
2015-11-30,1.71829,-1.73094,-4.42638,-2.13887,-2.39079,-2.48675,-2.70241,-4.19281,-2.51468
|
||||||
|
2015-12-31,0.58123,-4.13217,2.74043,2.34906,4.13434,-1.87079,3.30546,2.41658,-2.10287
|
||||||
|
2016-01-29,-2.63192,-1.29686,-0.46804,-0.70301,-5.29305,1.44976,-1.77272,-2.43791,-3.76722
|
||||||
|
2016-02-29,1.11751,3.88749,0.34721,7.20097,2.08711,0.1458,-0.24846,2.76004,-1.75815
|
||||||
|
2016-03-31,7.86007,4.72767,4.7783,0.31452,5.5071,5.42095,5.81507,3.83543,3.12318
|
||||||
|
2016-04-29,-0.65769,3.25722,0.41929,4.94724,0.87422,2.73212,0.9436,-0.22222,1.91105
|
||||||
|
2016-05-31,-4.94978,-4.24279,-2.88812,-3.59317,-3.08556,-3.70231,-4.06369,-3.71656,-0.64982
|
||||||
|
2016-06-30,2.90222,0.72611,-0.32093,7.97758,5.44862,-0.04947,-1.54366,1.89489,-8.17672
|
||||||
|
2016-07-29,2.18781,-0.421,0.54545,0.00634,1.42461,-0.64906,-0.84445,0.60013,-0.70301
|
||||||
|
2016-08-31,-1.01483,-0.63031,-0.51821,-1.02103,0.7415,0.97229,-0.64017,-1.81161,-1.40251
|
||||||
|
2016-09-30,1.90332,-0.18337,0.76766,1.99763,0.36851,4.35707,-0.05439,1.1667,-0.89417
|
||||||
|
2016-10-31,-0.4922,-1.96456,-2.60061,-3.75839,-1.49804,-3.1506,-5.18162,-2.15189,-6.0745
|
||||||
|
2016-11-30,-2.82856,-0.34211,-3.3459,-7.86581,-0.80887,-3.07771,-2.41031,-3.06582,2.2757
|
||||||
|
2016-12-30,-1.91727,0.22893,-0.75123,-2.50697,-1.5113,-1.11656,1.40204,-0.0246,-1.20807
|
||||||
|
2017-01-31,4.92587,2.82186,2.29313,3.42056,5.26316,4.58728,3.71099,2.77823,1.72794
|
||||||
|
2017-02-28,1.34715,-1.58934,-1.77442,0.54082,-1.27089,-1.57155,-3.05421,-1.61965,-1.13766
|
||||||
|
2017-03-31,-0.70613,-0.71233,0.50365,0.26869,-3.24969,-2.7206,0.70282,-0.14087,0.40227
|
||||||
|
2017-04-28,-1.91436,-2.50274,1.67868,-0.15789,-1.71362,0.22997,0.68821,0.37872,3.38917
|
||||||
|
2017-05-31,-0.40009,1.18605,3.10536,0.68581,3.46059,1.67078,1.90478,2.6748,-0.30504
|
||||||
|
2017-06-30,3.08479,3.9424,1.25904,-1.71823,3.22999,0.49614,2.66416,0.82285,0.51925
|
||||||
|
2017-07-31,4.12223,3.78633,3.19981,1.54821,2.45566,5.94376,4.08033,-0.95041,1.38814
|
||||||
|
2017-08-31,-0.61038,-0.30309,0.66442,0.25114,-4.41746,1.48615,1.4638,0.19771,-2.36926
|
||||||
|
2017-09-29,-1.03804,0.21509,-0.72413,-2.36441,0.99331,-2.33295,-2.67729,-0.87846,4.01113
|
||||||
|
2017-10-31,-2.24681,-3.00365,-1.6213,-1.08783,-5.17683,-2.77515,-2.66836,-3.12801,-1.12136
|
||||||
|
2017-11-30,-0.9582,0.03726,2.18375,1.40861,0.23809,-1.37314,0.28378,1.30557,1.84558
|
||||||
|
2017-12-29,3.04466,2.76388,0.48319,-0.85149,3.68103,1.22907,1.50278,0.47306,-0.2014
|
||||||
|
2018-01-31,3.52664,2.07931,3.5465,3.04613,4.106,6.45324,4.03578,4.5923,5.01111
|
||||||
|
2018-02-28,-3.75456,-4.28454,-2.26677,2.14423,-2.37754,-2.75386,-5.37554,-1.84011,-3.20756
|
||||||
|
2018-03-30,-1.5668,-0.67404,0.5848,0.10484,-0.08725,0.34951,-1.31657,-1.59774,1.65365
|
||||||
|
2018-04-30,-1.59356,0.51328,-1.96998,-2.99246,-2.32068,-2.08823,-4.41336,-3.49833,-1.93936
|
||||||
|
2018-05-31,0.23355,-1.23805,-3.60717,0.50983,-0.40596,-2.19773,-1.23464,0.43129,-3.53065
|
||||||
|
2018-06-29,-2.37062,-1.4763,-0.22088,-2.1273,-3.50602,0.26968,-1.5228,-1.26995,-0.94163
|
||||||
|
2018-07-31,0.60652,0.9808,-0.01137,-1.2529,0.55239,-0.08494,1.58253,0.04242,-0.78285
|
||||||
|
2018-08-31,-2.73453,-0.20706,-0.7936,0.73698,-2.53683,-2.70511,-4.06504,2.09803,-1.04757
|
||||||
|
2018-09-28,0.05084,0.81928,-0.39137,-2.58194,-0.07988,2.57144,2.4964,-1.24283,0.20902
|
||||||
|
2018-10-31,-2.06865,-1.63868,-2.68343,0.42205,-1.52018,-3.23405,-2.94145,-3.15936,-2.15036
|
||||||
|
2018-11-30,2.99921,-1.2452,-0.30971,-0.82889,5.15761,-2.42687,0.17646,0.38425,-0.27668
|
||||||
|
2018-12-31,-3.6466,-2.77859,0.64527,3.1529,-2.42855,-0.80285,2.30649,1.00325,-0.37703
|
||||||
|
2019-01-31,3.50259,3.91228,0.10971,0.5663,3.37735,2.64771,-2.25726,-0.92632,3.12321
|
||||||
|
2019-02-28,-2.47366,-0.34548,-0.99328,-2.44862,-1.80823,-1.56838,-2.2033,-0.67356,0.96939
|
||||||
|
2019-03-29,-0.20642,-1.49332,-1.64825,0.32471,0.0088,-0.89565,-0.72461,-0.26509,-2.19031
|
||||||
|
2019-04-30,-0.97251,-0.76271,-0.44122,-0.88907,-2.26996,-0.53229,-2.65368,-2.54367,-0.10498
|
||||||
|
2019-05-31,-1.64094,-0.57623,-0.83355,2.31112,-2.3153,-1.33885,-0.36198,1.00313,-3.48123
|
||||||
|
2019-06-28,1.18719,3.37325,1.92495,0.52116,2.96643,2.54878,2.33181,2.85333,0.81271
|
||||||
|
2019-07-31,-1.88636,-0.55972,-2.48086,-1.00677,-1.80471,-3.02015,-3.51105,-1.80101,-3.9406
|
||||||
|
2019-08-30,-2.35826,-1.23946,-1.34174,2.05558,-4.5045,-3.64717,-2.41027,-0.2274,-0.69068
|
||||||
|
2019-09-30,0.01139,0.31344,-1.2369,-2.00453,-0.66535,0.14627,-0.46033,-1.04698,1.05127
|
||||||
|
2019-10-31,2.0315,0.68551,2.08494,-0.24837,2.19595,-1.09474,1.83407,0.78034,4.86734
|
||||||
|
2019-11-29,-1.9038,-1.05251,-1.36145,-1.45192,0.03896,-0.32151,0.48735,-1.47796,-0.13125
|
||||||
|
2019-12-31,3.83199,2.39213,1.5525,0.48236,5.04967,4.75914,2.02798,2.90081,2.26808
|
|
316
Exam1/Exam1.ipynb
Normal file
316
Exam1/Exam1.ipynb
Normal file
File diff suppressed because one or more lines are too long
BIN
Exam1/Plot1_3_structure.pdf
Normal file
BIN
Exam1/Plot1_3_structure.pdf
Normal file
Binary file not shown.
59
Exam1/jlFiles/printTeXTable.jl
Normal file
59
Exam1/jlFiles/printTeXTable.jl
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
#------------------------------------------------------------------------------
|
||||||
|
"""
|
||||||
|
printTeXTable(fh::IO,x;colNames=[],rowNames=[],width=12,prec=2) or
|
||||||
|
printTeXTable(x;colNames=[],rowNames=[],width=12,prec=2) or
|
||||||
|
|
||||||
|
Create LaTeX table from numeric matrix x. Print to file or screen and returns a string
|
||||||
|
with the table contents.
|
||||||
|
|
||||||
|
# Input
|
||||||
|
-`fh::IOStream`:
|
||||||
|
-`x::`:
|
||||||
|
-`colNames::`:
|
||||||
|
-`rowNames::`:
|
||||||
|
-`width::`:
|
||||||
|
-`prec::`:
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
|
function printTeXTable(fh::IO,x;colNames=[],rowNames=[],width=12,prec=2)
|
||||||
|
|
||||||
|
(m,n) = (size(x,1),size(x,2))
|
||||||
|
|
||||||
|
isempty(rowNames) && (rowNames = [string("r",i) for i = 1:m]) #create row names "r1"
|
||||||
|
isempty(colNames) && (colNames = [string("c",i) for i = 1:n]) #create column names "c1"
|
||||||
|
|
||||||
|
str = """
|
||||||
|
\\begin{table}
|
||||||
|
\\begin{tabular}{l"""
|
||||||
|
str = string(str,"r"^n,"}\n"," ")
|
||||||
|
|
||||||
|
str = string(str," & ") #empty cell above rowNames[1]
|
||||||
|
for i = 1:n-1 #column names
|
||||||
|
str = string(str,colNames[i]," & ")
|
||||||
|
end
|
||||||
|
str = string(str,colNames[n]," \\\\ \\hline \n")
|
||||||
|
|
||||||
|
for i = 1:m #loop over rows
|
||||||
|
str = string(str," ",rowNames[i]," & ") #row name
|
||||||
|
for j = 1:n-1
|
||||||
|
str = string(str,fmtNumPs(x[i,j],width,prec,"right"), " & ") #x[i,1:end-1]
|
||||||
|
end
|
||||||
|
str = string(str,fmtNumPs(x[i,n],width,prec,"right")," \\\\ \n") #x[i,end]
|
||||||
|
end
|
||||||
|
|
||||||
|
endstr = """
|
||||||
|
\\hline
|
||||||
|
\\end{tabular}
|
||||||
|
\\end{table}"""
|
||||||
|
str = string(str,endstr,"\n")
|
||||||
|
|
||||||
|
print(fh,str) #print
|
||||||
|
|
||||||
|
return str
|
||||||
|
|
||||||
|
end
|
||||||
|
#when fh is not supplied: printing to screen
|
||||||
|
printTeXTable(x;colNames=[],rowNames=[],width=12,prec=2) =
|
||||||
|
printTeXTable(stdout::IO,x;colNames,rowNames,width,prec)
|
||||||
|
#------------------------------------------------------------------------------
|
246
Exam1/jlFiles/printmat.jl
Normal file
246
Exam1/jlFiles/printmat.jl
Normal file
|
@ -0,0 +1,246 @@
|
||||||
|
#------------------------------------------------------------------------------
|
||||||
|
"""
|
||||||
|
printmat([fh::IO],x...;colNames=[],rowNames=[],
|
||||||
|
width=10,prec=3,NoPrinting=false,StringFmt="",cell00="")
|
||||||
|
|
||||||
|
Print all elements of a matrix (or several) with predefined formatting. It can also handle
|
||||||
|
OffsetArrays. StringFmt = "csv" prints using a csv format.
|
||||||
|
|
||||||
|
# Input
|
||||||
|
- `fh::IO`: (optional) file handle. If not supplied, prints to screen
|
||||||
|
- `x::Array(s)`: (of numbers, dates, strings, ...) to print
|
||||||
|
- `colNames::Array`: of strings with column headers
|
||||||
|
- `rowNames::Array`: of strings with row labels
|
||||||
|
- `width::Int`: (keyword) scalar, minimum width of printed cells
|
||||||
|
- `prec::Int`: (keyword) scalar, precision of printed cells
|
||||||
|
- `NoPrinting::Bool`: (keyword) bool, true: no printing, just return formatted string [false]
|
||||||
|
- `StringFmt::String`: (keyword) string, "", "csv"
|
||||||
|
- `cell00::String`: (keyword) string, for row 0, column 0
|
||||||
|
|
||||||
|
# Output
|
||||||
|
- str (if NoPrinting) string, (otherwise nothing)
|
||||||
|
|
||||||
|
# Examples
|
||||||
|
```
|
||||||
|
x = [11 12;21 22]
|
||||||
|
printmat(x)
|
||||||
|
```
|
||||||
|
```
|
||||||
|
x = [1 "ab"; Date(2018,10,7) 3.14]
|
||||||
|
printmat(x,width=20,colNames=["col 1","col 2"])
|
||||||
|
```
|
||||||
|
```
|
||||||
|
printmat([11,12],[21,22])
|
||||||
|
```
|
||||||
|
Can also call as
|
||||||
|
```
|
||||||
|
opt = Dict(:rowNames=>["1";"4"],:width=>10,:prec=>3,:NoPrinting=>false,:StringFmt=>"")
|
||||||
|
printmat(x;colNames=["a","b"],opt...) #notice ; and ...
|
||||||
|
```
|
||||||
|
(not all keywords are needed)
|
||||||
|
|
||||||
|
# Requires
|
||||||
|
- fmtNumPs
|
||||||
|
|
||||||
|
# Notice
|
||||||
|
- The prefixN and suffixN could potentially be made function inputs. This would allow
|
||||||
|
a fairly flexible way to format tables.
|
||||||
|
|
||||||
|
|
||||||
|
Paul.Soderlind@unisg.ch
|
||||||
|
|
||||||
|
"""
|
||||||
|
function printmat(fh::IO,x...;colNames=[],rowNames=[],
|
||||||
|
width=10,prec=3,NoPrinting=false,StringFmt="",cell00="")
|
||||||
|
|
||||||
|
isempty(x) && return nothing #do nothing is isempty(x)
|
||||||
|
|
||||||
|
typeTestQ = any(!=(eltype(x[1])),[eltype(z) for z in x]) #test if eltype(x[i]) differs
|
||||||
|
if typeTestQ #create matrix from tuple created by x...
|
||||||
|
x = hcat(Matrix{Any}(hcat(x[1])),x[2:end]...) #preserving types of x[i]
|
||||||
|
else
|
||||||
|
x = hcat(x...)
|
||||||
|
end
|
||||||
|
|
||||||
|
(m,n) = (size(x,1),size(x,2))
|
||||||
|
|
||||||
|
(length(rowNames) == 1 < m) && (rowNames = [string(rowNames[1],i) for i = 1:m]) #"ri"
|
||||||
|
(length(colNames) == 1 < n) && (colNames = [string(colNames[1],i) for i = 1:n]) #"ci"
|
||||||
|
|
||||||
|
if StringFmt == "csv"
|
||||||
|
(prefixN,suffixN) = (fill("",n),vcat(fill(",",n-1),"")) #prefix and suffix for column 1:n
|
||||||
|
(prefixC0,suffixC0) = ("",",") #prefix and suffix for column 0
|
||||||
|
else
|
||||||
|
(prefixN,suffixN) = (fill("",n),fill("",n))
|
||||||
|
(prefixC0,suffixC0) = ("","")
|
||||||
|
end
|
||||||
|
|
||||||
|
if length(rowNames) == 0 #width of column 0 (cell00 and rowNames)
|
||||||
|
col0Width = 0
|
||||||
|
else
|
||||||
|
col0Width = maximum(length,vcat(cell00,rowNames)) + length(prefixC0) + length(suffixC0)
|
||||||
|
end
|
||||||
|
|
||||||
|
colWidth = [width + length(prefixN[j]) + length(suffixN[j]) for j=1:n] #widths of column 1:n
|
||||||
|
|
||||||
|
iob = IOBuffer()
|
||||||
|
|
||||||
|
if !isempty(colNames) #print (cell00,colNames), if any
|
||||||
|
!isempty(cell00) ? txt0 = string(prefixC0,cell00,suffixC0) : txt0 = ""
|
||||||
|
print(iob,rpad(txt0,col0Width))
|
||||||
|
for j = 1:n #loop over columns
|
||||||
|
print(iob,lpad(string(prefixN[j],colNames[j],suffixN[j]),colWidth[j]))
|
||||||
|
end
|
||||||
|
print(iob,"\n")
|
||||||
|
end
|
||||||
|
#print rowNames and x
|
||||||
|
(i0,j0) = (1 - first(axes(x,1)),1 - first(axes(x,2))) #i+i0,j+j0 give traditional indices
|
||||||
|
for i in axes(x,1) #loop over rows
|
||||||
|
!isempty(rowNames) && print(iob,rpad(string(prefixC0,rowNames[i+i0],suffixC0),col0Width))
|
||||||
|
for j in axes(x,2) #loop over columns
|
||||||
|
print(iob,fmtNumPs(x[i,j],width,prec,"right",prefix=prefixN[j+j0],suffix=suffixN[j+j0]))
|
||||||
|
end
|
||||||
|
print(iob,"\n")
|
||||||
|
end
|
||||||
|
str = String(take!(iob))
|
||||||
|
|
||||||
|
if NoPrinting #no printing, just return str
|
||||||
|
return str
|
||||||
|
else #print, return nothing
|
||||||
|
print(fh,str,"\n")
|
||||||
|
return nothing
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
#when fh is not supplied: printing to screen
|
||||||
|
printmat(x...;colNames=[],rowNames=[],width=10,prec=3,NoPrinting=false,StringFmt="",cell00="") =
|
||||||
|
printmat(stdout::IO,x...;colNames,rowNames,width,prec,NoPrinting,StringFmt,cell00)
|
||||||
|
#------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
#------------------------------------------------------------------------------
|
||||||
|
"""
|
||||||
|
printlnPs([fh::IO],z...;width=10,prec=3)
|
||||||
|
|
||||||
|
Subsitute for println, with predefined formatting.
|
||||||
|
|
||||||
|
|
||||||
|
# Input
|
||||||
|
- `fh::IO`: (optional) file handle. If not supplied, prints to screen
|
||||||
|
- `z::String`: string, numbers and arrays to print
|
||||||
|
|
||||||
|
Paul.Soderlind@unisg.ch
|
||||||
|
|
||||||
|
"""
|
||||||
|
function printlnPs(fh::IO,z...;width=10,prec=3)
|
||||||
|
|
||||||
|
for x in z #loop over inputs in z...
|
||||||
|
if isa(x,AbstractArray)
|
||||||
|
iob = IOBuffer()
|
||||||
|
for i = 1:length(x)
|
||||||
|
print(iob,fmtNumPs(x[i],width,prec,"right"))
|
||||||
|
end
|
||||||
|
print(fh,String(take!(iob)))
|
||||||
|
else
|
||||||
|
print(fh,fmtNumPs(x,width,prec,"right"))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
print(fh,"\n")
|
||||||
|
|
||||||
|
end
|
||||||
|
#when fh is not supplied: printing to screen
|
||||||
|
printlnPs(z...;width=10,prec=3) = printlnPs(stdout::IO,z...;width,prec)
|
||||||
|
#------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
#------------------------------------------------------------------------------
|
||||||
|
"""
|
||||||
|
fmtNumPs(z,width=10,prec=2,justify="right";prefix="",suffix="")
|
||||||
|
|
||||||
|
Create a formatted string of a float (eg, "%10.4f"), nothing (""),
|
||||||
|
while other values are passed through. Strings are right (or left) justified
|
||||||
|
and can optionally be given prefix and suffix (eg, ",")
|
||||||
|
|
||||||
|
# Notice
|
||||||
|
- With prec > 0 and isa(z,Integer), then the string is padded with 1+prec spaces
|
||||||
|
to align with the printing of floats with the same prec.
|
||||||
|
|
||||||
|
# Requires
|
||||||
|
- Printf (for 1.6-), fmtNumPsC (for < 1.6)
|
||||||
|
|
||||||
|
"""
|
||||||
|
function fmtNumPs(z,width=10,prec=2,justify="right";prefix="",suffix="")
|
||||||
|
|
||||||
|
isa(z,Bool) && (z = convert(Int,z)) #Bool -> Int
|
||||||
|
|
||||||
|
if isa(z,AbstractFloat) #example: 101.0234, prec=3
|
||||||
|
if VERSION < v"1.6-"
|
||||||
|
fmt = "%$(width).$(prec)f"
|
||||||
|
zRound = round(z,digits=prec)
|
||||||
|
strLR = fmtNumPsC(fmt,zRound) #C fallback solution
|
||||||
|
else
|
||||||
|
fmt = Printf.Format("%$(width).$(prec)f")
|
||||||
|
strLR = Printf.format(fmt,z)
|
||||||
|
end
|
||||||
|
elseif isa(z,Nothing)
|
||||||
|
strLR = ""
|
||||||
|
elseif isa(z,Integer) && prec > 0 #integer followed by (1+prec spaces)
|
||||||
|
strLR = string(z," "^(1+prec))
|
||||||
|
else #Int, String, Date, Missing, etc
|
||||||
|
strLR = string(z)
|
||||||
|
end
|
||||||
|
|
||||||
|
strLR = string(prefix,strLR,suffix)
|
||||||
|
|
||||||
|
if justify == "left" #justification
|
||||||
|
strLR = rpad(strLR,width+length(prefix)+length(suffix))
|
||||||
|
else
|
||||||
|
strLR = lpad(strLR,width+length(prefix)+length(suffix))
|
||||||
|
end
|
||||||
|
|
||||||
|
return strLR
|
||||||
|
|
||||||
|
end
|
||||||
|
#------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
#------------------------------------------------------------------------------
|
||||||
|
"""
|
||||||
|
fmtNumPsC(fmt,z)
|
||||||
|
|
||||||
|
c fallback solution for formatting of floating point number. Used if VERSION < v"1.6-"
|
||||||
|
"""
|
||||||
|
function fmtNumPsC(fmt,z) #c fallback solution
|
||||||
|
if ismissing(z) || isnan(z) || isinf(z) #asprintf does not work for these cases
|
||||||
|
str = string(z)
|
||||||
|
else
|
||||||
|
strp = Ref{Ptr{Cchar}}(0)
|
||||||
|
len = ccall(:asprintf,Cint,(Ptr{Ptr{Cchar}},Cstring,Cdouble...),strp,fmt,z)
|
||||||
|
str = unsafe_string(strp[],len)
|
||||||
|
Libc.free(strp[])
|
||||||
|
end
|
||||||
|
return str
|
||||||
|
end
|
||||||
|
#------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
#------------------------------------------------------------------------------
|
||||||
|
function printblue(x...)
|
||||||
|
foreach(z->printstyled(z,color=:blue,bold=true),x)
|
||||||
|
print("\n")
|
||||||
|
end
|
||||||
|
function printred(x...)
|
||||||
|
foreach(z->printstyled(z,color=:red,bold=true),x)
|
||||||
|
print("\n")
|
||||||
|
end
|
||||||
|
function printmagenta(x...)
|
||||||
|
foreach(z->printstyled(z,color=:magenta,bold=true),x)
|
||||||
|
print("\n")
|
||||||
|
end
|
||||||
|
function printyellow(x...)
|
||||||
|
foreach(z->printstyled(z,color=:yellow,bold=true),x)
|
||||||
|
print("\n")
|
||||||
|
end
|
||||||
|
#------------------------------------------------------------------------------
|
||||||
|
|
17
JuliaTutorial-master/.gitattributes
vendored
Normal file
17
JuliaTutorial-master/.gitattributes
vendored
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
# Auto detect text files and perform LF normalization
|
||||||
|
* text=auto
|
||||||
|
|
||||||
|
# Custom for Visual Studio
|
||||||
|
*.cs diff=csharp
|
||||||
|
|
||||||
|
# Standard to msysgit
|
||||||
|
*.doc diff=astextplain
|
||||||
|
*.DOC diff=astextplain
|
||||||
|
*.docx diff=astextplain
|
||||||
|
*.DOCX diff=astextplain
|
||||||
|
*.dot diff=astextplain
|
||||||
|
*.DOT diff=astextplain
|
||||||
|
*.pdf diff=astextplain
|
||||||
|
*.PDF diff=astextplain
|
||||||
|
*.rtf diff=astextplain
|
||||||
|
*.RTF diff=astextplain
|
47
JuliaTutorial-master/.gitignore
vendored
Normal file
47
JuliaTutorial-master/.gitignore
vendored
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
# Windows image file caches
|
||||||
|
Thumbs.db
|
||||||
|
ehthumbs.db
|
||||||
|
|
||||||
|
# Folder config file
|
||||||
|
Desktop.ini
|
||||||
|
|
||||||
|
# Recycle Bin used on file shares
|
||||||
|
$RECYCLE.BIN/
|
||||||
|
|
||||||
|
# Windows Installer files
|
||||||
|
*.cab
|
||||||
|
*.msi
|
||||||
|
*.msm
|
||||||
|
*.msp
|
||||||
|
|
||||||
|
# Windows shortcuts
|
||||||
|
*.lnk
|
||||||
|
|
||||||
|
# =========================
|
||||||
|
# Operating System Files
|
||||||
|
# =========================
|
||||||
|
|
||||||
|
# OSX
|
||||||
|
# =========================
|
||||||
|
|
||||||
|
.DS_Store
|
||||||
|
.AppleDouble
|
||||||
|
.LSOverride
|
||||||
|
|
||||||
|
# Thumbnails
|
||||||
|
._*
|
||||||
|
|
||||||
|
# Files that might appear in the root of a volume
|
||||||
|
.DocumentRevisions-V100
|
||||||
|
.fseventsd
|
||||||
|
.Spotlight-V100
|
||||||
|
.TemporaryItems
|
||||||
|
.Trashes
|
||||||
|
.VolumeIcon.icns
|
||||||
|
|
||||||
|
# Directories potentially created on remote AFP share
|
||||||
|
.AppleDB
|
||||||
|
.AppleDesktop
|
||||||
|
Network Trash Folder
|
||||||
|
Temporary Items
|
||||||
|
.apdisk
|
3
JuliaTutorial-master/Data/CsvFile.csv
Normal file
3
JuliaTutorial-master/Data/CsvFile.csv
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
X,Y,Z
|
||||||
|
1.1,1.2,1.3
|
||||||
|
2.1,2.2,2.3
|
|
16
JuliaTutorial-master/Data/CsvFileWithDates.csv
Normal file
16
JuliaTutorial-master/Data/CsvFileWithDates.csv
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
,GSPC,DTB3
|
||||||
|
02/01/1979,96.730003,
|
||||||
|
03/01/1979, ,9.31
|
||||||
|
04/01/1979,98.580002,9.31
|
||||||
|
05/01/1979,99.129997,9.34
|
||||||
|
08/01/1979,98.800003,9.36
|
||||||
|
09/01/1979,99.330002,9.27
|
||||||
|
10/01/1979,98.769997,9.25
|
||||||
|
11/01/1979,99.099998,9.28
|
||||||
|
12/01/1979,99.93,9.35
|
||||||
|
15/01/1979,100.690002,9.44
|
||||||
|
16/01/1979,99.459999,9.44
|
||||||
|
17/01/1979,99.480003,9.44
|
||||||
|
18/01/1979,99.720001,9.46
|
||||||
|
19/01/1979,99.75,9.4
|
||||||
|
22/01/1979,99.900002,9.34
|
|
6
JuliaTutorial-master/Data/FileWithText.txt
Normal file
6
JuliaTutorial-master/Data/FileWithText.txt
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
Dogs are nicer
|
||||||
|
than cats.
|
||||||
|
|
||||||
|
This
|
||||||
|
is a
|
||||||
|
fairly short file.
|
BIN
JuliaTutorial-master/Data/H5File.h5
Normal file
BIN
JuliaTutorial-master/Data/H5File.h5
Normal file
Binary file not shown.
BIN
JuliaTutorial-master/Data/JldFile.jld
Normal file
BIN
JuliaTutorial-master/Data/JldFile.jld
Normal file
Binary file not shown.
BIN
JuliaTutorial-master/Data/JldFile.jld2
Normal file
BIN
JuliaTutorial-master/Data/JldFile.jld2
Normal file
Binary file not shown.
BIN
JuliaTutorial-master/Data/MatFile.mat
Normal file
BIN
JuliaTutorial-master/Data/MatFile.mat
Normal file
Binary file not shown.
389
JuliaTutorial-master/Data/MyData.csv
Normal file
389
JuliaTutorial-master/Data/MyData.csv
Normal file
|
@ -0,0 +1,389 @@
|
||||||
|
date,Mkt_RF,RF,SmallGrowth
|
||||||
|
197901,4.18,0.77,10.96
|
||||||
|
197902,-3.41,0.73,-2.09
|
||||||
|
197903,5.75,0.81,11.71
|
||||||
|
197904,0.05,0.8,3.27
|
||||||
|
197905,-2.18,0.82,-1.89
|
||||||
|
197906,3.88,0.81,7.4
|
||||||
|
197907,0.73,0.77,1.77
|
||||||
|
197908,5.7,0.77,9.32
|
||||||
|
197909,-0.69,0.83,-0.04
|
||||||
|
197910,-8.14,0.87,-10.56
|
||||||
|
197911,5.37,0.99,9.25
|
||||||
|
197912,1.87,0.95,12.14
|
||||||
|
198001,5.76,0.8,15.28
|
||||||
|
198002,-0.79,0.89,1.77
|
||||||
|
198003,-13.23,1.21,-21.65
|
||||||
|
198004,3.97,1.26,5.24
|
||||||
|
198005,5.2,0.81,6.93
|
||||||
|
198006,3.16,0.61,4.77
|
||||||
|
198007,6.41,0.53,11.3
|
||||||
|
198008,1.72,0.64,11.74
|
||||||
|
198009,2.2,0.75,8.51
|
||||||
|
198010,1.06,0.95,8.42
|
||||||
|
198011,9.54,0.96,14.84
|
||||||
|
198012,-4.75,1.31,-4.95
|
||||||
|
198101,-5.05,1.04,-2.1
|
||||||
|
198102,0.48,1.07,-3.01
|
||||||
|
198103,3.41,1.21,6.89
|
||||||
|
198104,-2.21,1.08,3.04
|
||||||
|
198105,0.21,1.15,3.83
|
||||||
|
198106,-2.37,1.35,-4.98
|
||||||
|
198107,-1.55,1.24,-4.08
|
||||||
|
198108,-6.91,1.28,-11.91
|
||||||
|
198109,-7.62,1.24,-13.64
|
||||||
|
198110,4.81,1.21,9.93
|
||||||
|
198111,3.51,1.07,-2.94
|
||||||
|
198112,-3.68,0.87,-4.44
|
||||||
|
198201,-3.42,0.8,-2.05
|
||||||
|
198202,-6.03,0.92,-8.04
|
||||||
|
198203,-1.99,0.98,-2.25
|
||||||
|
198204,3.2,1.13,6.35
|
||||||
|
198205,-3.88,1.06,-3.22
|
||||||
|
198206,-3.35,0.96,-6.33
|
||||||
|
198207,-3.1,1.05,-3.22
|
||||||
|
198208,11.14,0.76,3.55
|
||||||
|
198209,1.17,0.51,1.04
|
||||||
|
198210,11.27,0.59,16.05
|
||||||
|
198211,4.56,0.63,8.65
|
||||||
|
198212,0.78,0.67,2.9
|
||||||
|
198301,3.5,0.69,20.39
|
||||||
|
198302,2.4,0.62,3.1
|
||||||
|
198303,2.84,0.63,3.63
|
||||||
|
198304,6.71,0.71,7.86
|
||||||
|
198305,0.63,0.69,14.91
|
||||||
|
198306,3.11,0.67,5.41
|
||||||
|
198307,-3.9,0.74,-4.87
|
||||||
|
198308,-0.41,0.76,-6.13
|
||||||
|
198309,0.85,0.76,-3.79
|
||||||
|
198310,-3.56,0.76,-10.18
|
||||||
|
198311,2.26,0.7,2.79
|
||||||
|
198312,-1.78,0.73,-5.86
|
||||||
|
198401,-2.06,0.76,0.34
|
||||||
|
198402,-4.62,0.71,-7.31
|
||||||
|
198403,0.61,0.73,-1.55
|
||||||
|
198404,-0.56,0.81,-4.57
|
||||||
|
198405,-6.01,0.78,-6.38
|
||||||
|
198406,1.59,0.75,0.68
|
||||||
|
198407,-2.88,0.82,-7.62
|
||||||
|
198408,10.44,0.83,9.25
|
||||||
|
198409,-0.82,0.86,-2.21
|
||||||
|
198410,-1.01,1,-3.99
|
||||||
|
198411,-1.8,0.73,-6.55
|
||||||
|
198412,1.73,0.64,-0.84
|
||||||
|
198501,7.92,0.65,15.82
|
||||||
|
198502,1.11,0.58,6.58
|
||||||
|
198503,-0.79,0.62,-1.59
|
||||||
|
198504,-0.94,0.72,-2.76
|
||||||
|
198505,4.92,0.66,0.92
|
||||||
|
198506,1.16,0.55,-1.22
|
||||||
|
198507,-0.65,0.62,1.43
|
||||||
|
198508,-1.03,0.55,-1.45
|
||||||
|
198509,-4.58,0.6,-8.36
|
||||||
|
198510,3.79,0.65,-0.37
|
||||||
|
198511,6.31,0.61,3.64
|
||||||
|
198512,3.66,0.65,1.59
|
||||||
|
198601,0.42,0.56,5.7
|
||||||
|
198602,6.72,0.53,5.85
|
||||||
|
198603,4.79,0.6,3.88
|
||||||
|
198604,-1.31,0.52,1.36
|
||||||
|
198605,4.59,0.49,2.51
|
||||||
|
198606,0.9,0.52,0.47
|
||||||
|
198607,-6.49,0.52,-9.62
|
||||||
|
198608,6.16,0.46,-0.72
|
||||||
|
198609,-8.35,0.45,-8.18
|
||||||
|
198610,4.47,0.46,1.88
|
||||||
|
198611,1.12,0.39,-2.68
|
||||||
|
198612,-3.13,0.49,-6.11
|
||||||
|
198701,12.43,0.42,11.38
|
||||||
|
198702,4.36,0.43,10.95
|
||||||
|
198703,1.9,0.47,3.73
|
||||||
|
198704,-2.14,0.44,-2.64
|
||||||
|
198705,0.13,0.38,0.12
|
||||||
|
198706,3.89,0.48,1.5
|
||||||
|
198707,3.96,0.46,1.77
|
||||||
|
198708,3.24,0.47,-0.11
|
||||||
|
198709,-2.53,0.45,-2.48
|
||||||
|
198710,-23.14,0.6,-32.71
|
||||||
|
198711,-7.58,0.35,-7.88
|
||||||
|
198712,6.64,0.39,-0.16
|
||||||
|
198801,4.2,0.29,10.53
|
||||||
|
198802,4.71,0.46,5.53
|
||||||
|
198803,-2.1,0.44,4.32
|
||||||
|
198804,0.64,0.46,0.74
|
||||||
|
198805,-0.47,0.51,-3.35
|
||||||
|
198806,4.66,0.49,4.37
|
||||||
|
198807,-1.24,0.51,0.14
|
||||||
|
198808,-3.39,0.59,-3.39
|
||||||
|
198809,3.1,0.62,1.52
|
||||||
|
198810,1.15,0.61,-4.14
|
||||||
|
198811,-2.21,0.57,-6.02
|
||||||
|
198812,1.48,0.63,0.7
|
||||||
|
198901,6.06,0.55,6.76
|
||||||
|
198902,-2.25,0.61,0.4
|
||||||
|
198903,1.48,0.67,0.05
|
||||||
|
198904,4.15,0.67,1.77
|
||||||
|
198905,3.14,0.79,1.6
|
||||||
|
198906,-1.2,0.71,-3.23
|
||||||
|
198907,7.01,0.7,0.88
|
||||||
|
198908,1.47,0.74,1.18
|
||||||
|
198909,-0.8,0.65,1.01
|
||||||
|
198910,-3.61,0.68,-3.53
|
||||||
|
198911,1.09,0.69,-2.45
|
||||||
|
198912,1.22,0.61,-2.84
|
||||||
|
199001,-7.58,0.57,-5.16
|
||||||
|
199002,0.92,0.57,1.55
|
||||||
|
199003,1.77,0.64,3.45
|
||||||
|
199004,-3.52,0.69,-0.26
|
||||||
|
199005,8.21,0.68,6.31
|
||||||
|
199006,-1.05,0.63,2.15
|
||||||
|
199007,-1.62,0.68,-2.87
|
||||||
|
199008,-9.85,0.66,-15.37
|
||||||
|
199009,-5.98,0.6,-9.26
|
||||||
|
199010,-1.93,0.68,-7.11
|
||||||
|
199011,6,0.57,1.37
|
||||||
|
199012,2.35,0.6,-2.15
|
||||||
|
199101,4.39,0.52,8.26
|
||||||
|
199102,7.1,0.48,17.89
|
||||||
|
199103,2.45,0.44,10.37
|
||||||
|
199104,-0.2,0.53,6.44
|
||||||
|
199105,3.6,0.47,2.27
|
||||||
|
199106,-4.82,0.42,-4.93
|
||||||
|
199107,4.19,0.49,2.54
|
||||||
|
199108,2.22,0.46,3.28
|
||||||
|
199109,-1.56,0.46,2.62
|
||||||
|
199110,1.36,0.42,5.27
|
||||||
|
199111,-4.12,0.39,-0.02
|
||||||
|
199112,10.3,0.38,1.35
|
||||||
|
199201,-0.46,0.34,24.13
|
||||||
|
199202,1.06,0.28,5.13
|
||||||
|
199203,-2.71,0.34,-2.41
|
||||||
|
199204,1.02,0.32,-8.38
|
||||||
|
199205,0.36,0.28,-1.84
|
||||||
|
199206,-2.25,0.32,-8.98
|
||||||
|
199207,3.68,0.31,0.05
|
||||||
|
199208,-2.34,0.26,-4.14
|
||||||
|
199209,0.98,0.26,0.11
|
||||||
|
199210,0.87,0.23,0.99
|
||||||
|
199211,3.79,0.23,8.56
|
||||||
|
199212,1.5,0.28,2.63
|
||||||
|
199301,1.03,0.23,7.67
|
||||||
|
199302,0.32,0.22,-4.83
|
||||||
|
199303,2.26,0.25,1.16
|
||||||
|
199304,-2.78,0.24,-2.73
|
||||||
|
199305,2.74,0.22,4.79
|
||||||
|
199306,0.29,0.25,1.71
|
||||||
|
199307,-0.32,0.24,-1.04
|
||||||
|
199308,3.7,0.25,5.02
|
||||||
|
199309,-0.2,0.26,2.27
|
||||||
|
199310,1.59,0.22,4.86
|
||||||
|
199311,-2.01,0.25,-3.79
|
||||||
|
199312,1.72,0.23,-1.01
|
||||||
|
199401,2.9,0.25,4.7
|
||||||
|
199402,-2.63,0.21,-2.93
|
||||||
|
199403,-4.85,0.27,-7.07
|
||||||
|
199404,0.68,0.27,-5.05
|
||||||
|
199405,0.62,0.32,-2.12
|
||||||
|
199406,-3.1,0.31,-6.03
|
||||||
|
199407,2.78,0.28,0.26
|
||||||
|
199408,3.89,0.37,1.93
|
||||||
|
199409,-2.21,0.37,1.23
|
||||||
|
199410,1.07,0.38,-1.82
|
||||||
|
199411,-4.09,0.37,-5.21
|
||||||
|
199412,0.82,0.44,-6.13
|
||||||
|
199501,1.62,0.42,5.08
|
||||||
|
199502,3.56,0.4,1.72
|
||||||
|
199503,2.24,0.46,1.14
|
||||||
|
199504,2.06,0.44,0.97
|
||||||
|
199505,2.86,0.54,1.16
|
||||||
|
199506,2.65,0.47,8.19
|
||||||
|
199507,3.63,0.45,6.12
|
||||||
|
199508,0.46,0.47,5.21
|
||||||
|
199509,3.21,0.43,3.62
|
||||||
|
199510,-1.6,0.47,-6.47
|
||||||
|
199511,3.85,0.42,-0.83
|
||||||
|
199512,1.03,0.49,-1.04
|
||||||
|
199601,2.38,0.43,4.46
|
||||||
|
199602,1.24,0.39,4.21
|
||||||
|
199603,0.7,0.39,4
|
||||||
|
199604,2.09,0.46,6.18
|
||||||
|
199605,2.26,0.42,10
|
||||||
|
199606,-1.23,0.4,-6.21
|
||||||
|
199607,-5.83,0.45,-14.21
|
||||||
|
199608,2.84,0.41,3.57
|
||||||
|
199609,4.86,0.44,0.89
|
||||||
|
199610,0.95,0.42,-7.58
|
||||||
|
199611,6.15,0.41,-1.55
|
||||||
|
199612,-1.6,0.46,-4.86
|
||||||
|
199701,4.9,0.45,11.33
|
||||||
|
199702,-0.5,0.39,-4.96
|
||||||
|
199703,-4.92,0.43,-9.53
|
||||||
|
199704,3.81,0.43,-5.34
|
||||||
|
199705,6.67,0.49,7.18
|
||||||
|
199706,4.04,0.37,2.07
|
||||||
|
199707,7.22,0.43,1.93
|
||||||
|
199708,-4.04,0.41,5.64
|
||||||
|
199709,5.41,0.44,10.41
|
||||||
|
199710,-3.86,0.42,-3.69
|
||||||
|
199711,2.65,0.39,-6.1
|
||||||
|
199712,1.3,0.48,-10.85
|
||||||
|
199801,0.02,0.43,3.87
|
||||||
|
199802,6.94,0.39,4.12
|
||||||
|
199803,4.74,0.39,4.6
|
||||||
|
199804,0.66,0.43,2.95
|
||||||
|
199805,-2.97,0.4,-5.74
|
||||||
|
199806,2.78,0.41,-5.16
|
||||||
|
199807,-2.74,0.4,-6.56
|
||||||
|
199808,-16.21,0.43,-25.96
|
||||||
|
199809,5.92,0.46,3.98
|
||||||
|
199810,7.12,0.32,3.3
|
||||||
|
199811,5.89,0.31,14.11
|
||||||
|
199812,5.93,0.38,0.16
|
||||||
|
199901,3.5,0.35,15.62
|
||||||
|
199902,-4.16,0.35,-5.7
|
||||||
|
199903,3.36,0.43,-4.13
|
||||||
|
199904,4.54,0.37,6.33
|
||||||
|
199905,-2.41,0.34,3.42
|
||||||
|
199906,4.68,0.4,3.84
|
||||||
|
199907,-3.45,0.38,1.82
|
||||||
|
199908,-1.39,0.39,-4.63
|
||||||
|
199909,-2.67,0.39,0.38
|
||||||
|
199910,5.82,0.39,-1.21
|
||||||
|
199911,3.32,0.36,25.55
|
||||||
|
199912,7.94,0.44,19.41
|
||||||
|
200001,-4.37,0.41,17.77
|
||||||
|
200002,2.75,0.43,31.04
|
||||||
|
200003,4.88,0.47,-5.17
|
||||||
|
200004,-6.41,0.46,-23.16
|
||||||
|
200005,-4.4,0.5,-13.89
|
||||||
|
200006,4.76,0.4,15.12
|
||||||
|
200007,-2.19,0.48,-8.29
|
||||||
|
200008,7.09,0.5,7
|
||||||
|
200009,-5.62,0.51,-10.64
|
||||||
|
200010,-3.02,0.56,-12.85
|
||||||
|
200011,-10.76,0.51,-24.64
|
||||||
|
200012,1.54,0.5,-15.92
|
||||||
|
200101,3.41,0.54,56.97
|
||||||
|
200102,-10.32,0.39,-13.15
|
||||||
|
200103,-7.47,0.44,-13.66
|
||||||
|
200104,7.99,0.39,11.53
|
||||||
|
200105,0.74,0.32,11.42
|
||||||
|
200106,-2.03,0.28,-1.22
|
||||||
|
200107,-2.13,0.3,-8.02
|
||||||
|
200108,-6.21,0.31,-7.89
|
||||||
|
200109,-9.43,0.28,-17.49
|
||||||
|
200110,2.56,0.22,13.33
|
||||||
|
200111,7.71,0.17,6.9
|
||||||
|
200112,1.64,0.15,11.46
|
||||||
|
200201,-1.74,0.14,-1.89
|
||||||
|
200202,-2.3,0.13,-9.83
|
||||||
|
200203,4.34,0.13,5.53
|
||||||
|
200204,-5.11,0.15,-8.88
|
||||||
|
200205,-1.19,0.14,-8.85
|
||||||
|
200206,-7.16,0.13,-12.09
|
||||||
|
200207,-8.26,0.15,-15.27
|
||||||
|
200208,0.66,0.14,-0.26
|
||||||
|
200209,-10.14,0.14,-11.59
|
||||||
|
200210,7.35,0.14,8.14
|
||||||
|
200211,6.01,0.12,18.54
|
||||||
|
200212,-5.44,0.11,-10.71
|
||||||
|
200301,-2.44,0.1,2.69
|
||||||
|
200302,-1.63,0.09,-5.17
|
||||||
|
200303,0.93,0.1,2.85
|
||||||
|
200304,8.18,0.1,13.77
|
||||||
|
200305,6.26,0.09,26.36
|
||||||
|
200306,1.53,0.1,9.48
|
||||||
|
200307,2.24,0.07,8.67
|
||||||
|
200308,2.43,0.07,4.24
|
||||||
|
200309,-0.99,0.08,6.57
|
||||||
|
200310,5.96,0.07,6.42
|
||||||
|
200311,1.59,0.07,1.97
|
||||||
|
200312,4.47,0.08,1.61
|
||||||
|
200401,2.23,0.07,10.88
|
||||||
|
200402,1.49,0.06,-0.35
|
||||||
|
200403,-1.16,0.09,-1.77
|
||||||
|
200404,-2.5,0.08,-3.38
|
||||||
|
200405,1.35,0.06,-3.35
|
||||||
|
200406,2.08,0.08,-0.19
|
||||||
|
200407,-3.87,0.1,-12.07
|
||||||
|
200408,0.16,0.11,-2.4
|
||||||
|
200409,1.94,0.11,4.41
|
||||||
|
200410,1.67,0.11,1.77
|
||||||
|
200411,4.67,0.15,10.92
|
||||||
|
200412,3.36,0.16,9.33
|
||||||
|
200501,-2.82,0.16,-5.86
|
||||||
|
200502,2.11,0.16,-0.91
|
||||||
|
200503,-1.9,0.21,-6.84
|
||||||
|
200504,-2.73,0.21,-7.42
|
||||||
|
200505,3.56,0.24,6.6
|
||||||
|
200506,0.92,0.23,2.59
|
||||||
|
200507,4.09,0.24,8.25
|
||||||
|
200508,-0.89,0.3,-2.82
|
||||||
|
200509,0.77,0.29,0.97
|
||||||
|
200510,-2.35,0.27,-5.12
|
||||||
|
200511,3.73,0.31,2.87
|
||||||
|
200512,0.03,0.32,1.32
|
||||||
|
200601,3.65,0.35,11.12
|
||||||
|
200602,-0.5,0.34,1.07
|
||||||
|
200603,1.54,0.37,4.43
|
||||||
|
200604,0.94,0.36,-1.39
|
||||||
|
200605,-3.53,0.43,-8.87
|
||||||
|
200606,-0.44,0.4,-1.87
|
||||||
|
200607,-0.59,0.4,-5.61
|
||||||
|
200608,2.09,0.42,3.23
|
||||||
|
200609,1.53,0.41,-1.32
|
||||||
|
200610,3.3,0.41,6.61
|
||||||
|
200611,1.95,0.42,2.06
|
||||||
|
200612,0.68,0.4,0.58
|
||||||
|
200701,1.5,0.44,3.4
|
||||||
|
200702,-1.78,0.38,0.49
|
||||||
|
200703,0.87,0.43,0.4
|
||||||
|
200704,3.55,0.44,3.91
|
||||||
|
200705,3.48,0.41,0.23
|
||||||
|
200706,-1.87,0.4,-0.37
|
||||||
|
200707,-3.57,0.4,-5.09
|
||||||
|
200708,0.75,0.42,-1.83
|
||||||
|
200709,3.77,0.32,2.26
|
||||||
|
200710,2.26,0.32,3.21
|
||||||
|
200711,-5.27,0.34,-10.34
|
||||||
|
200712,-0.7,0.27,-2.42
|
||||||
|
200801,-6.44,0.21,-7.22
|
||||||
|
200802,-2.33,0.13,-3.91
|
||||||
|
200803,-1.21,0.17,-4.41
|
||||||
|
200804,4.94,0.18,1.59
|
||||||
|
200805,2.21,0.18,4.83
|
||||||
|
200806,-8.03,0.17,-7.84
|
||||||
|
200807,-1.47,0.15,1.6
|
||||||
|
200808,0.99,0.13,-0.35
|
||||||
|
200809,-9.97,0.15,-15.3
|
||||||
|
200810,-18.55,0.08,-23.95
|
||||||
|
200811,-8.56,0.03,-14.79
|
||||||
|
200812,2.06,0.09,3.55
|
||||||
|
200901,-7.75,0,2.39
|
||||||
|
200902,-10.12,0.01,-10.44
|
||||||
|
200903,8.75,0.02,14.23
|
||||||
|
200904,11.04,0.01,21.72
|
||||||
|
200905,6.73,0,9.4
|
||||||
|
200906,-0.28,0.01,9.87
|
||||||
|
200907,8.23,0.01,8.54
|
||||||
|
200908,3.18,0.01,6.06
|
||||||
|
200909,4.52,0.01,7.69
|
||||||
|
200910,-2.84,0,-9.31
|
||||||
|
200911,5.74,0,-0.77
|
||||||
|
200912,2.91,0.01,6.06
|
||||||
|
201001,-3.71,0,-2.72
|
||||||
|
201002,3.53,0,3.13
|
||||||
|
201003,6.44,0.01,5.68
|
||||||
|
201004,2.02,0,8.44
|
||||||
|
201005,-8,0.01,-9.64
|
||||||
|
201006,-5.21,0.01,-7.77
|
||||||
|
201007,7.1,0.01,5.54
|
||||||
|
201008,-4.4,0.01,-7.37
|
||||||
|
201009,9.24,0.01,12.01
|
||||||
|
201010,3.88,0.01,5.69
|
||||||
|
201011,0.56,0.01,0.47
|
||||||
|
201012,6.77,0.01,11.28
|
||||||
|
201101,2.01,0.01,-0.38
|
||||||
|
201102,3.85,0.01,1.43
|
||||||
|
201103,0.28,0.01,1.92
|
||||||
|
201104,2.82,0,1.03
|
|
23
JuliaTutorial-master/Data/My_C_Stuff.c
Normal file
23
JuliaTutorial-master/Data/My_C_Stuff.c
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
// calculate the inner (dot) product of vectors Y and Y, returns the result (Sxy)
|
||||||
|
double c_dot(size_t n, double *Y, double *X) {
|
||||||
|
double Sxy = 0.0;
|
||||||
|
for (size_t i = 0; i < n; ++i) {
|
||||||
|
Sxy += X[i]*Y[i];
|
||||||
|
}
|
||||||
|
return Sxy;
|
||||||
|
}
|
||||||
|
|
||||||
|
// calculate a simple regression, Y = a + b*X + u, puts (a,b) in vector ab, returns nothing
|
||||||
|
void c_ols(size_t n, double *Y, double *X, double *ab) {
|
||||||
|
double Sx = 0.0, Sy = 0.0, Sxx = 0.0, Sxy = 0.0;
|
||||||
|
for (size_t i = 0; i < n; ++i) {
|
||||||
|
Sx += X[i];
|
||||||
|
Sy += Y[i];
|
||||||
|
Sxx += X[i]*X[i];
|
||||||
|
Sxy += X[i]*Y[i];
|
||||||
|
}
|
||||||
|
ab[1] = (Sxy-Sx*Sy/n)/(Sxx-Sx*Sx/n); //slope
|
||||||
|
ab[0] = (Sy - ab[1]*Sx)/n; //intercept
|
||||||
|
}
|
BIN
JuliaTutorial-master/Data/XlsFile.xlsx
Normal file
BIN
JuliaTutorial-master/Data/XlsFile.xlsx
Normal file
Binary file not shown.
8
JuliaTutorial-master/LICENSE.txt
Normal file
8
JuliaTutorial-master/LICENSE.txt
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
The MIT License (MIT)
|
||||||
|
Copyright (c) 2016 Paul Söderlind
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
21
JuliaTutorial-master/Readme.md
Normal file
21
JuliaTutorial-master/Readme.md
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
# Introduction
|
||||||
|
|
||||||
|
This repository contains my Julia tutorials (aimed at students in finance and economics).
|
||||||
|
|
||||||
|
|
||||||
|
# Instructions
|
||||||
|
|
||||||
|
1. Most files are jupyter notebooks. Click one of them to see it online. If GitHub fails to render the notebook, then use [nbviewer](https://nbviewer.jupyter.org/). Instructions: try to open the notebook at GitHub, copy the link and paste it in the address field of nbviewer.
|
||||||
|
|
||||||
|
2. To download this repository, use the Download (as zip) in the Github menu. Otherwise, clone it.
|
||||||
|
|
||||||
|
|
||||||
|
# On the Files
|
||||||
|
|
||||||
|
1. Tutorial_ChapterNumber_Topic.ipynb are (relatively) short notebooks organised around different topics.
|
||||||
|
|
||||||
|
2. The folder jlFiles contains some .jl files with functions used in some of the notebooks.
|
||||||
|
|
||||||
|
3. The folder Data contains some data sets used in the notebooks, while the folder Results is for output.
|
||||||
|
|
||||||
|
4. The current version is tested on Julia 1.6.
|
0
JuliaTutorial-master/Results/EmptyFile.txt
Normal file
0
JuliaTutorial-master/Results/EmptyFile.txt
Normal file
1072
JuliaTutorial-master/Tutorial_01_Basics.ipynb
Normal file
1072
JuliaTutorial-master/Tutorial_01_Basics.ipynb
Normal file
File diff suppressed because one or more lines are too long
546
JuliaTutorial-master/Tutorial_02_Stats.ipynb
Normal file
546
JuliaTutorial-master/Tutorial_02_Stats.ipynb
Normal file
File diff suppressed because one or more lines are too long
712
JuliaTutorial-master/Tutorial_03_Functions.ipynb
Normal file
712
JuliaTutorial-master/Tutorial_03_Functions.ipynb
Normal file
|
@ -0,0 +1,712 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"# Functions\n",
|
||||||
|
"\n",
|
||||||
|
"This notebook shows how to define functions with one (or several) inputs and outputs. It also shows how to use to dot (.) syntax to apply a function to each element of an array."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Load Packages and Extra Functions"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"printyellow (generic function with 1 method)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"using Printf\n",
|
||||||
|
"\n",
|
||||||
|
"include(\"jlFiles/printmat.jl\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Functions with One Output"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## The Basic Approach\n",
|
||||||
|
"\n",
|
||||||
|
"The basic approach to define a function with the name `fn` is\n",
|
||||||
|
"```\n",
|
||||||
|
"function fn(x,b)\n",
|
||||||
|
" ...(some code)\n",
|
||||||
|
" return y\n",
|
||||||
|
"end\n",
|
||||||
|
"``` \n",
|
||||||
|
"\n",
|
||||||
|
"Once you have defined a function, you can use it (call on it) by, for instance, `y1 = fn(2,1)`. This will generate a `y1` variable (not a `y` variable) in the workspace. Inside the function, `x` is then 2 and `b` is 1. Clearly, if `x1=2 and b1=1`, you get the same result by calling as `y1 = fn(x1,b1)`."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"result from fn1(2,1): 0.310\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"function fn1(x,b) #x and b are the inputs\n",
|
||||||
|
" c = 0.5 #c is only \"seen\" inside the function\n",
|
||||||
|
" y = b*(x-1.1)^2 - c\n",
|
||||||
|
" return y #this is the output\n",
|
||||||
|
"end\n",
|
||||||
|
"\n",
|
||||||
|
"y1 = fn1(2,1)\n",
|
||||||
|
"printlnPs(\"result from fn1(2,1): \",y1)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Default Values for the Inputs\n",
|
||||||
|
"\n",
|
||||||
|
"You can change the first line of the function to specify default values as \n",
|
||||||
|
"```\n",
|
||||||
|
"function fn(x,b=1)\n",
|
||||||
|
"```\n",
|
||||||
|
"\n",
|
||||||
|
"In this case you can call on the function as `fn(x)` and the value of `b` will default to 1. (Clearly, inputs with default values must be towards the end of the list of inputs.)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 3,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"result from fn2(2,1) and fn2(2): 0.310 0.310\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"function fn2(x,b=1) #b=1 is the default in case we call as fn2(x)\n",
|
||||||
|
" y = b*(x-1.1)^2 - 0.5\n",
|
||||||
|
" return y\n",
|
||||||
|
"end\n",
|
||||||
|
"\n",
|
||||||
|
"printlnPs(\"result from fn2(2,1) and fn2(2): \",fn2(2,1),fn2(2))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Elementwise Evaluation\n",
|
||||||
|
"\n",
|
||||||
|
"To apply the function to each element of arrays `x` and `b`, use the dot syntax (broadcasting): \n",
|
||||||
|
"```\n",
|
||||||
|
"y = fn.(x,b)\n",
|
||||||
|
"```\n",
|
||||||
|
"This calculates `fn(x[i],b[i])` for each pair `(x[i],b[i])`.\n",
|
||||||
|
"\n",
|
||||||
|
"Instead, with `fn.(x,2)`, you calculate `fn(x[i],2)` for each element `x[i]`."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"\n",
|
||||||
|
"result from fn2.(x1,2): \n",
|
||||||
|
" -0.480\n",
|
||||||
|
" -0.180\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"result from fn2.(x1,b1): \n",
|
||||||
|
" -0.480\n",
|
||||||
|
" -0.180\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"x1 = [1,1.5]\n",
|
||||||
|
"b1 = [2,2]\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"\\nresult from fn2.(x1,2): \")\n",
|
||||||
|
"printmat(fn2.(x1,2))\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"\\nresult from fn2.(x1,b1): \")\n",
|
||||||
|
"printmat(fn2.(x1,b1))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Elementwise Evaluation over *Some* Inputs (extra)\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"To apply the function to each element of the array `x`, but keep `b` fixed, use : \n",
|
||||||
|
"```\n",
|
||||||
|
"y = fn.(x,Ref(b))\n",
|
||||||
|
"```\n",
|
||||||
|
"\n",
|
||||||
|
"For instance, `fn301.(x1,Ref(b1))` in the next cell will create the 2-element vector\n",
|
||||||
|
"```\n",
|
||||||
|
"1 + (10+20+30)\n",
|
||||||
|
"1.5 + (10+20+30)\n",
|
||||||
|
"```"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"result from fn301.(x1,Ref(b1)):\n",
|
||||||
|
" 61.000\n",
|
||||||
|
" 61.500\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"function fn301(x,b)\n",
|
||||||
|
" y = x + sum(b)\n",
|
||||||
|
" return y\n",
|
||||||
|
"end\n",
|
||||||
|
"\n",
|
||||||
|
"x1 = [1,1.5]\n",
|
||||||
|
"b1 = [10,20,30]\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"result from fn301.(x1,Ref(b1)):\")\n",
|
||||||
|
"printmat(fn301.(x1,Ref(b1)))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Short Form (extra)\n",
|
||||||
|
"\n",
|
||||||
|
"We can also use short forms of a function as in the cell below. \n",
|
||||||
|
"\n",
|
||||||
|
"The first version (`fn3`) is just a single expression. It could span several lines. The second version (`fn3b`) is a sequence of expressions (a \"compound expression\") separated by semicolons (`;`). The last expression is the function output."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 6,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"result from fn3(1.5,1) and fn3b(1.5,1): -0.340 -0.340\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"fn3(x,b) = b*(x-1.1)^2 - 0.5 #short form of a function\n",
|
||||||
|
"\n",
|
||||||
|
"fn3b(x,b) = (c = 0.5;b*(x-1.1)^2 - c) #this works too. Notice the ;\n",
|
||||||
|
"\n",
|
||||||
|
"printlnPs(\"result from fn3(1.5,1) and fn3b(1.5,1): \",fn3(1.5,1),fn3b(1.5,1))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Explicit Names of the Inputs: Keyword Arguments\n",
|
||||||
|
"\n",
|
||||||
|
"You can also define functions that take *keyword arguments* like in \n",
|
||||||
|
"```\n",
|
||||||
|
"fn(x;b,c)\n",
|
||||||
|
"``` \n",
|
||||||
|
"Notice the semi-colon (`;`). You can also specify default values as `fn(x;b=1,c=0.5)`\n",
|
||||||
|
"\n",
|
||||||
|
"In this case, you *call* on the function by `fn(x,c=3,b=2)` or just `fn(x)` if you want to use the default values. This helps remembering/interpreting what the arguments represent. When calling on the function, you can pass the keyword arguments in any order and you can use comma (`,`) instead of semi-colon (`;`).\n",
|
||||||
|
"\n",
|
||||||
|
"(Extra) You can also use a `Dict()` to supply the keyword arguments as in\n",
|
||||||
|
"```\n",
|
||||||
|
"opt = Dict(:b=>1,:c=>0.5)\n",
|
||||||
|
"fn(x;opt...)\n",
|
||||||
|
"```\n",
|
||||||
|
"Notice that the dictionary must use symbols and that you need to use `;` and `...` in the call. It is also possible to mix traditional keywords with a dictionary as in `opt = Dict(:c=>0.5); fn(x;b=1,opt...)`"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 7,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"result from fn4(1,c=3,b=2): -2.980\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"function fn4(x;b=1,c=0.5)\n",
|
||||||
|
" y = b*(x-1.1)^2 - c\n",
|
||||||
|
" return y\n",
|
||||||
|
"end\n",
|
||||||
|
"\n",
|
||||||
|
"printlnPs(\"result from fn4(1,c=3,b=2): \",fn4(1,c=3,b=2))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Variable Number of Inputs (extra)\n",
|
||||||
|
"\n",
|
||||||
|
"When *defining a function*, we can use the `x...` syntax (also called \"slurping\") on the last non-keyword input to capture a variable number of inputs. Later, when calling on this function, several inputs will be combined into a vector `x`.\n",
|
||||||
|
"\n",
|
||||||
|
"For instance, `fn5(-9,1,2,b=10)` in the next cell will create the 2-element vector\n",
|
||||||
|
"```\n",
|
||||||
|
"-9 + 1*10\n",
|
||||||
|
"-9 + 2*10\n",
|
||||||
|
"```"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 8,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"result from fn5(-9,1,2,b=10): \n",
|
||||||
|
" 1.000\n",
|
||||||
|
" 11.000\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"function fn5(a,x...;b=1) #a is traditional input, x... captures several inputs; b=1 is a keyword input\n",
|
||||||
|
" n = length(x)\n",
|
||||||
|
" y = zeros(n)\n",
|
||||||
|
" for i = 1:n\n",
|
||||||
|
" y[i] = a + x[i]*b\n",
|
||||||
|
" end\n",
|
||||||
|
" return y\n",
|
||||||
|
"end\n",
|
||||||
|
"\n",
|
||||||
|
"y = fn5(-9,1,2,b=10)\n",
|
||||||
|
"println(\"result from fn5(-9,1,2,b=10): \")\n",
|
||||||
|
"printmat(y)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Conversely, you can also use the `x...` syntax (also called \"splatting\") when *calling on a function* that requires several inputs.\n",
|
||||||
|
"\n",
|
||||||
|
"In the next cell, when running `fn5b([1,2]...)`, `x1` will be 1 and `x2` will be 2."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 9,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"1 and then also 2\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"function fn5b(x1,x2)\n",
|
||||||
|
" println(x1,\" and then also \",x2)\n",
|
||||||
|
" return nothing\n",
|
||||||
|
"end\n",
|
||||||
|
"\n",
|
||||||
|
"fn5b([1,2]...)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Functions with Several Outputs"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Several Outputs 1: Basic Approach\n",
|
||||||
|
"\n",
|
||||||
|
"A function can produce a \"tuple\" like `(y1,y2,y3)` as output.\n",
|
||||||
|
"\n",
|
||||||
|
"In case you only want the first two outputs, call as `(y1,y2,) = fn(x)`.\n",
|
||||||
|
"\n",
|
||||||
|
"Instead, if you only want the second and third outputs, call as `(_,y2,y3) = fn(x)`\n",
|
||||||
|
"\n",
|
||||||
|
"You can also extract the second output as `y2 = fn(x)[2]`\n",
|
||||||
|
"\n",
|
||||||
|
"If `Y1` is an already existing array, then `(Y1[3],y2,) = fn(x)` will change element 3 of `Y1` (and also assign a value to `y2`)."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 10,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"The first 2 outputs from fn11(1,2): -0.480 2 \n",
|
||||||
|
"\n",
|
||||||
|
"The result from calling fn11(1,2)[2]: 2 \n",
|
||||||
|
"\n",
|
||||||
|
"Y1 array after calling fn11(1,2): 0.000 0.000 -0.480 0.000 0.000\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"function fn11(x,b=1)\n",
|
||||||
|
" y1 = b*(x-1.1)^2 - 0.5\n",
|
||||||
|
" y2 = b*x\n",
|
||||||
|
" y3 = 3\n",
|
||||||
|
" return y1, y2, y3\n",
|
||||||
|
"end\n",
|
||||||
|
"\n",
|
||||||
|
"(y1,y2,) = fn11(1,2)\n",
|
||||||
|
"printlnPs(\"The first 2 outputs from fn11(1,2): \",y1,y2)\n",
|
||||||
|
"\n",
|
||||||
|
"y2 = fn11(1,2)[2] #grab the second output\n",
|
||||||
|
"printlnPs(\"\\nThe result from calling fn11(1,2)[2]: \",y2)\n",
|
||||||
|
"\n",
|
||||||
|
"Y1 = zeros(5) #create an array\n",
|
||||||
|
"(Y1[3],y2,) = fn11(1,2) #put result in existing array\n",
|
||||||
|
"printlnPs(\"\\nY1 array after calling fn11(1,2): \",Y1)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Several Outputs 2: Named Tuples and Dictionaries (extra)\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"Instead of returning several values, it might be easier to combine them into either a \"named tuple\" or a dictionary and then export that.\n",
|
||||||
|
"\n",
|
||||||
|
"You could end the function with \n",
|
||||||
|
"\n",
|
||||||
|
"```\n",
|
||||||
|
" y = (a=y1,b=y2,c=y3) #named tuple\n",
|
||||||
|
" return y\n",
|
||||||
|
"end\n",
|
||||||
|
"```\n",
|
||||||
|
"\n",
|
||||||
|
"or \n",
|
||||||
|
"\n",
|
||||||
|
"```\n",
|
||||||
|
" y = Dict(:a=>y1,:b=>y2,:c=>y3) #dictionary\n",
|
||||||
|
" return y\n",
|
||||||
|
"end\n",
|
||||||
|
"```"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Several Outputs 3: Elementwise Evaluation (extra)\n",
|
||||||
|
"\n",
|
||||||
|
"...can be tricky, because you get an array (same dimension as the input) of tuples instead of a tuple of arrays (which is probably what you want).\n",
|
||||||
|
"\n",
|
||||||
|
"One way around this is to reshuffle the output to get a tuple of arrays.\n",
|
||||||
|
"\n",
|
||||||
|
"Alternatively, you could loop over the function calls or write the function in such a way that it directly handles array inputs (without the dot). The latter is done in `fn14()`."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 11,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"y, a 2-element vector of tuples (3 elements in each):\n",
|
||||||
|
"(-0.48, 2.0, 3)\n",
|
||||||
|
"(-0.18000000000000016, 3.0, 3)\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"the vectors y1, y2 and y3:\n",
|
||||||
|
" -0.480 2.000 3.000\n",
|
||||||
|
" -0.180 3.000 3.000\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"y = fn11.([1,1.5],2)\n",
|
||||||
|
"println(\"y, a 2-element vector of tuples (3 elements in each):\")\n",
|
||||||
|
"printmat(y)\n",
|
||||||
|
"\n",
|
||||||
|
"(y1,y2,y3) = ntuple(i->getindex.(y,i),3) #split up into 3 vectors\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"\\nthe vectors y1, y2 and y3:\")\n",
|
||||||
|
"printmat([y1 y2 y3])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 12,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"result from fn14(x1,2): \n",
|
||||||
|
" -0.480 2.000\n",
|
||||||
|
" -0.180 3.000\n",
|
||||||
|
"\n",
|
||||||
|
" 3 \n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"function fn14(x,b=1) #x can be an array\n",
|
||||||
|
" y1 = b*(x.-1.1).^2 .- 0.5\n",
|
||||||
|
" y2 = b*x\n",
|
||||||
|
" y3 = 3\n",
|
||||||
|
" return y1, y2, y3\n",
|
||||||
|
"end\n",
|
||||||
|
"\n",
|
||||||
|
"(y1,y2,y3) = fn14(x1,2) #function written to handle arrays\n",
|
||||||
|
"println(\"result from fn14(x1,2): \")\n",
|
||||||
|
"printmat([y1 y2])\n",
|
||||||
|
"printmat(y3)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"# Documenting Your Function \n",
|
||||||
|
"\n",
|
||||||
|
"To use Julia's help function (`? FunctionName`), put the documentation in triple quotes, just above the function definition. (No empty lines between the last triple quote and the start of the function.) The cell below illustrates a simple case."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 13,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"fn101"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 13,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"\"\"\"\n",
|
||||||
|
" fn101(x,b=1)\n",
|
||||||
|
"\n",
|
||||||
|
"Calculate `b*(x-1.1)^2 - 0.5`.\n",
|
||||||
|
"\n",
|
||||||
|
"# Arguments\n",
|
||||||
|
"- `x::Number`: an important number\n",
|
||||||
|
"- `b::Number`: another number\n",
|
||||||
|
"\n",
|
||||||
|
"\"\"\"\n",
|
||||||
|
"function fn101(x,b=1)\n",
|
||||||
|
" y = b*(x-1.1)^2 - 0.5\n",
|
||||||
|
" return y\n",
|
||||||
|
"end"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 14,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"search: \u001b[0m\u001b[1mf\u001b[22m\u001b[0m\u001b[1mn\u001b[22m\u001b[0m\u001b[1m1\u001b[22m\u001b[0m\u001b[1m0\u001b[22m\u001b[0m\u001b[1m1\u001b[22m\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/latex": [
|
||||||
|
"\\begin{verbatim}\n",
|
||||||
|
"fn101(x,b=1)\n",
|
||||||
|
"\\end{verbatim}\n",
|
||||||
|
"Calculate \\texttt{b*(x-1.1)\\^{}2 - 0.5}.\n",
|
||||||
|
"\n",
|
||||||
|
"\\section{Arguments}\n",
|
||||||
|
"\\begin{itemize}\n",
|
||||||
|
"\\item \\texttt{x::Number}: an important number\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"\\item \\texttt{b::Number}: another number\n",
|
||||||
|
"\n",
|
||||||
|
"\\end{itemize}\n"
|
||||||
|
],
|
||||||
|
"text/markdown": [
|
||||||
|
"```\n",
|
||||||
|
"fn101(x,b=1)\n",
|
||||||
|
"```\n",
|
||||||
|
"\n",
|
||||||
|
"Calculate `b*(x-1.1)^2 - 0.5`.\n",
|
||||||
|
"\n",
|
||||||
|
"# Arguments\n",
|
||||||
|
"\n",
|
||||||
|
" * `x::Number`: an important number\n",
|
||||||
|
" * `b::Number`: another number\n"
|
||||||
|
],
|
||||||
|
"text/plain": [
|
||||||
|
"\u001b[36m fn101(x,b=1)\u001b[39m\n",
|
||||||
|
"\n",
|
||||||
|
" Calculate \u001b[36mb*(x-1.1)^2 - 0.5\u001b[39m.\n",
|
||||||
|
"\n",
|
||||||
|
"\u001b[1m Arguments\u001b[22m\n",
|
||||||
|
"\u001b[1m ≡≡≡≡≡≡≡≡≡≡≡\u001b[22m\n",
|
||||||
|
"\n",
|
||||||
|
" • \u001b[36mx::Number\u001b[39m: an important number\n",
|
||||||
|
"\n",
|
||||||
|
" • \u001b[36mb::Number\u001b[39m: another number"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 14,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"? fn101"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Sending a Function to a Function\n",
|
||||||
|
"\n",
|
||||||
|
"You can use a function name (for instance, `cos`) as an input to another function."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 15,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"result from fn201(cos,3.145,10): 9.000\n",
|
||||||
|
"\n",
|
||||||
|
"result from fn201(z->mod(z,2),3.145,10): 11.145\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"function fn201(f,a,b) #f is a function, could also write f::Function\n",
|
||||||
|
" y = f(a) + b\n",
|
||||||
|
" return y\n",
|
||||||
|
"end\n",
|
||||||
|
"\n",
|
||||||
|
"#here f is the cos function\n",
|
||||||
|
"printlnPs(\"result from fn201(cos,3.145,10): \",fn201(cos,3.145,10)) \n",
|
||||||
|
"\n",
|
||||||
|
"#here f is z->mod(z,2), an anonymous function\n",
|
||||||
|
"printlnPs(\"\\nresult from fn201(z->mod(z,2),3.145,10):\",fn201(z->mod(z,2),3.145,10))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"@webio": {
|
||||||
|
"lastCommId": null,
|
||||||
|
"lastKernelId": null
|
||||||
|
},
|
||||||
|
"anaconda-cloud": {},
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Julia 1.6.1",
|
||||||
|
"language": "julia",
|
||||||
|
"name": "julia-1.6"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"file_extension": ".jl",
|
||||||
|
"mimetype": "application/julia",
|
||||||
|
"name": "julia",
|
||||||
|
"version": "1.6.1"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 1
|
||||||
|
}
|
812
JuliaTutorial-master/Tutorial_03b_DataContainers.ipynb
Normal file
812
JuliaTutorial-master/Tutorial_03b_DataContainers.ipynb
Normal file
|
@ -0,0 +1,812 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"# Data Containers\n",
|
||||||
|
"\n",
|
||||||
|
"This notebook shows how to combine data into different types of \"containers\" (arrays, dictionaries, tuples, ...) inside your program."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Load Packages and Extra Functions"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {
|
||||||
|
"scrolled": true
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"printyellow (generic function with 1 method)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"using Printf\n",
|
||||||
|
"\n",
|
||||||
|
"include(\"jlFiles/printmat.jl\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Arrays\n",
|
||||||
|
"\n",
|
||||||
|
"are used everywhere in finance and statistics/econometrics."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Vectors, Matrices and High-dimensional Arrays\n",
|
||||||
|
"\n",
|
||||||
|
"can be created in many ways: the code below demonstrates just a few of them. See the tutorial on Arrays for (many) more details.\n",
|
||||||
|
"\n",
|
||||||
|
"To access an array element, just do `A[2]` or similarly. Also, you can change an array element as in `B[2,1] = -999.`\n",
|
||||||
|
"\n",
|
||||||
|
"Notice that `D = [A B]` creates an independent copy, so later changing `B` does not affect `D`. However, if we define `E = B`, then a change of `B` will affect both itself and `E`.\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"metadata": {
|
||||||
|
"scrolled": true
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
" 100 \n",
|
||||||
|
" 101 \n",
|
||||||
|
"\n",
|
||||||
|
" 1 2 \n",
|
||||||
|
" 0 10 \n",
|
||||||
|
"\n",
|
||||||
|
" 100 1 2 \n",
|
||||||
|
" 101 0 10 \n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"A = [100,101] #a vector\n",
|
||||||
|
"printmat(A) #or display(A)\n",
|
||||||
|
"\n",
|
||||||
|
"B = [1 2; #a matrix\n",
|
||||||
|
" 0 10]\n",
|
||||||
|
"printmat(B)\n",
|
||||||
|
"\n",
|
||||||
|
"D = [A B] #a 2x3 matrix\n",
|
||||||
|
"printmat(D)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 3,
|
||||||
|
"metadata": {
|
||||||
|
"scrolled": true
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"A[2] is 101\n",
|
||||||
|
"\n",
|
||||||
|
"B is now\n",
|
||||||
|
" 1 2 \n",
|
||||||
|
" -999 10 \n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"D is not affected\n",
|
||||||
|
" 100 1 2 \n",
|
||||||
|
" 101 0 10 \n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"println(\"A[2] is \",A[2]) #access an element\n",
|
||||||
|
"\n",
|
||||||
|
"B[2,1] = -999 #change an element\n",
|
||||||
|
"println(\"\\nB is now\")\n",
|
||||||
|
"printmat(B)\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"\\nD is not affected\")\n",
|
||||||
|
"printmat(D) #D is not changed when B is"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"4×3×2 Array{Float64, 3}:\n",
|
||||||
|
"[:, :, 1] =\n",
|
||||||
|
" 0.0822035 0.727781 0.616084\n",
|
||||||
|
" 0.700477 0.148329 0.0486485\n",
|
||||||
|
" 0.497247 0.0274626 0.233258\n",
|
||||||
|
" 0.908952 0.0319028 0.63753\n",
|
||||||
|
"\n",
|
||||||
|
"[:, :, 2] =\n",
|
||||||
|
" 0.991477 0.725542 0.505041\n",
|
||||||
|
" 0.999608 0.801481 0.636564\n",
|
||||||
|
" 0.841383 0.160885 0.691288\n",
|
||||||
|
" 0.473578 0.14428 0.724725"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "display_data"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"C = rand(4,3,2) #a 4x3x3 array\n",
|
||||||
|
"display(C)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Arrays of Arrays (or other types)\n",
|
||||||
|
"\n",
|
||||||
|
"You can store very different things (a mixture of numbers, matrices, strings) in an array. For instance, if `a` is a vector, `str` is a string and `C` is a matrix, then `x = [a,str,C]` puts them into a vector.\n",
|
||||||
|
"\n",
|
||||||
|
"If you later change elements of the matrix `C` then it will affect `x` (discussed at the end of the notebook). "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
|
"metadata": {
|
||||||
|
"scrolled": true
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"1:10"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "display_data"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"\"Hazel\""
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "display_data"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"2×2 Matrix{Int64}:\n",
|
||||||
|
" 11 12\n",
|
||||||
|
" 21 22"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "display_data"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"a = 1:10\n",
|
||||||
|
"str = \"Hazel\"\n",
|
||||||
|
"C = [11 12;21 22]\n",
|
||||||
|
"x = [a,str,C] #element 1 of x is a\n",
|
||||||
|
"\n",
|
||||||
|
"foreach(display,x) #loops over the elements of x"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Tuples and Named Tuples\n",
|
||||||
|
"\n",
|
||||||
|
"are very useful for collecting very different types of data (a number, a string, and a couple of vectors, say). \n",
|
||||||
|
"\n",
|
||||||
|
"Once created, you cannot change tuples (they are immutable). (Exception: *changing elements of an array* that belongs to the tuple will affect the tuple too.)\n",
|
||||||
|
"\n",
|
||||||
|
"Tuples are often used as inputs or outputs of functions.\n",
|
||||||
|
"\n",
|
||||||
|
"The next few cells show how to create (named) tuples, how to extract parts of them and what happens when you to try to change them."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 6,
|
||||||
|
"metadata": {
|
||||||
|
"scrolled": true
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"(1:10, \"Hazel\", [11 12; 21 22])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "display_data"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"(a = 1:10, str = \"Hazel\", C = [11 12; 21 22])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "display_data"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"(a = 1:10, str = \"Hazel\", C = [11 12; 21 22])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "display_data"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"a = 1:10 #how to create tuples and named tuples\n",
|
||||||
|
"str = \"Hazel\"\n",
|
||||||
|
"C = [11 12;21 22]\n",
|
||||||
|
"\n",
|
||||||
|
"t = (a,str,C) #a tuple, or tuple(a,str,C)\n",
|
||||||
|
"display(t)\n",
|
||||||
|
"\n",
|
||||||
|
"nt = (a=a,str=str,C=C) #a named tuple, (a2=a,str2=str,C2=C) would also work\n",
|
||||||
|
"display(nt)\n",
|
||||||
|
"\n",
|
||||||
|
"nt_ = (;a,str,C) #also a named tuple (Julia 1.5+), names are given by variables\n",
|
||||||
|
"display(nt_)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 7,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"a2 and str2 are: 1:10 Hazel \n",
|
||||||
|
"\n",
|
||||||
|
"t[3] is [11 12; 21 22]\n",
|
||||||
|
"\n",
|
||||||
|
"nt.C is [11 12; 21 22]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"(a2,str2,C2) = t #extract the tuple into variables (\"destructuring\")\n",
|
||||||
|
"println(\"a2 and str2 are: $a2 $str2 \\n\")\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"t[3] is \",t[3],\"\\n\") #can index into (tuple) t\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"nt.C is \",nt.C) #we can use nt.C as a name (nt is a named tuple)\n",
|
||||||
|
"\n",
|
||||||
|
"#(a3,str3...) = t #in Julia 1.6, str3 will be a tuple "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 8,
|
||||||
|
"metadata": {
|
||||||
|
"scrolled": true
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"t[1] is 1:10\n",
|
||||||
|
"\n",
|
||||||
|
"nt.C is [11 12; 21 22]\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"println(\"t[1] is \",t[1],\"\\n\")\n",
|
||||||
|
"#t[1] = -999 #cannot change the tuple, uncomment to get an error\n",
|
||||||
|
"#t[4] = 34 #cannot add elements, uncomment to get an error\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"nt.C is \",nt.C,\"\\n\")\n",
|
||||||
|
"#nt.a = -999 #cannot change the tuple\n",
|
||||||
|
"#n.D = 34 #cannot add elements"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Create a Tuple Dynamically (extra)\n",
|
||||||
|
"\n",
|
||||||
|
"when the values and (perhaps also the names) are created dynamically in the program.\n",
|
||||||
|
"\n",
|
||||||
|
"Suppose `values` and `names` in the next cell may differ in length from one run of the program to the next. Using `tuple(values...)` and `NamedTuple{names}(values)` allows you to still create tuples/named tuples."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 9,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"(1:10, \"Hazel\", [11 12; 21 22])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "display_data"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"(a = 1:10, b = \"Hazel\", c = [11 12; 21 22])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "display_data"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"values = [a,str,C]\n",
|
||||||
|
"\n",
|
||||||
|
"t2 = tuple(values...) #or (values...,)\n",
|
||||||
|
"display(t2)\n",
|
||||||
|
"\n",
|
||||||
|
"names = (:a, :b, :c)\n",
|
||||||
|
"nt2 = NamedTuple{names}(values) #or (;zip(names,values)...)\n",
|
||||||
|
"display(nt2)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Dictionaries\n",
|
||||||
|
"\n",
|
||||||
|
"is a flexible way to collect different types of data. Dictionaries can (in contrast to tuples) be changed. Also, changing elements of an array that belongs to the dictionary will affect the dictionary too.\n",
|
||||||
|
"\n",
|
||||||
|
"A dictionary is organised as (key,value) pairs, where the key is the name of the element. You can loop over the elements (see below) and also change/add elements in a loop."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 10,
|
||||||
|
"metadata": {
|
||||||
|
"scrolled": true
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"Dict{Symbol, Any} with 4 entries:\n",
|
||||||
|
" :a => -999\n",
|
||||||
|
" :verse2 => \"Stardust\"\n",
|
||||||
|
" :str => \"Hazel\"\n",
|
||||||
|
" :C => [11 12; 21 22]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "display_data"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"D[:C] is [11 12; 21 22]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"a = 1:10\n",
|
||||||
|
"str = \"Hazel\"\n",
|
||||||
|
"C = [11 12;21 22]\n",
|
||||||
|
"\n",
|
||||||
|
"D = Dict(:a=>a,:str=>str,:C=>C) #dictionary, \"a\" instead of :a works too\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"D[:C] is \",D[:C])\n",
|
||||||
|
"\n",
|
||||||
|
"D[:a] = -999 #can change an element\n",
|
||||||
|
"\n",
|
||||||
|
"D[:verse2] = \"Stardust\" #can add an element\n",
|
||||||
|
"\n",
|
||||||
|
"display(D)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 11,
|
||||||
|
"metadata": {
|
||||||
|
"scrolled": true
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"a: -999\n",
|
||||||
|
"verse2: Stardust\n",
|
||||||
|
"str: Hazel\n",
|
||||||
|
"C: [11 12; 21 22]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"for (key,value) in D #loop over a dictionary\n",
|
||||||
|
" println(\"$key: $value\")\n",
|
||||||
|
"end"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## From a Dict to a NamedTuple and Back Again (extra)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 12,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"(a = -999, verse2 = \"Stardust\", str = \"Hazel\", C = [11 12; 21 22])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "display_data"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"Dict{Symbol, Any} with 4 entries:\n",
|
||||||
|
" :a => -999\n",
|
||||||
|
" :verse2 => \"Stardust\"\n",
|
||||||
|
" :str => \"Hazel\"\n",
|
||||||
|
" :C => [11 12; 21 22]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "display_data"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"nt = (;D...) #create a named tuple from a dict\n",
|
||||||
|
"display(nt)\n",
|
||||||
|
"\n",
|
||||||
|
"D2 = Dict(pairs(nt)) #create a dict from a named tuple\n",
|
||||||
|
"display(D2)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## A Potential Pitfall in Adding to a Dict (extra)\n",
|
||||||
|
"\n",
|
||||||
|
"If you have created a dict with only numbers by \n",
|
||||||
|
"```\n",
|
||||||
|
"D = Dict(:aa=>1)\n",
|
||||||
|
"``` \n",
|
||||||
|
"then you cannot add eg. a string by `D[:cc] = \"hello\"` since `D` is only set up to accept variables of the type `Int`. "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 13,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"Dict{Any, Any} with 2 entries:\n",
|
||||||
|
" :aa => 1\n",
|
||||||
|
" :cc => \"hello\""
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "display_data"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"D = Dict(:aa=>1)\n",
|
||||||
|
"#D[:cc] = \"hello\" #error since D only accepts Int\n",
|
||||||
|
"\n",
|
||||||
|
"D = Dict{Any,Any}(:aa=>1) #this works\n",
|
||||||
|
"D[:cc] = \"hello\"\n",
|
||||||
|
"display(D)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Create a Dictionary Dynamically (extra)\n",
|
||||||
|
"\n",
|
||||||
|
"See below for examples.\n",
|
||||||
|
"\n",
|
||||||
|
"Remark: if you have the names as an array of strings (`names = [\"a\",\"b\",\"c\"]`), but want symbol names (`:a` etc), then use `Symbol.(names)`."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 14,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"Dict{Symbol, Any} with 3 entries:\n",
|
||||||
|
" :a => 1:10\n",
|
||||||
|
" :b => \"Hazel\"\n",
|
||||||
|
" :c => [11 12; 21 22]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "display_data"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"names = (:a, :b, :c) #or [\"a\",\"b\",\"c\"]\n",
|
||||||
|
"values = [a,str,C]\n",
|
||||||
|
"\n",
|
||||||
|
"D = Dict(zip(names,values))\n",
|
||||||
|
"display(D)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 15,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"Dict{Any, Any} with 3 entries:\n",
|
||||||
|
" :a => 1:10\n",
|
||||||
|
" :b => \"Hazel\"\n",
|
||||||
|
" :c => [11 12; 21 22]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "display_data"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"D = Dict() #empty dictionary\n",
|
||||||
|
"for i = 1:length(values) #loop\n",
|
||||||
|
" D[names[i]] = values[i] #add this to the dictionary\n",
|
||||||
|
"end\n",
|
||||||
|
"display(D)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Your Own Tailor Made Data Type\n",
|
||||||
|
"\n",
|
||||||
|
"It is sometime conventient to define your own `struct` as a container. The `struct` command creates an immutable type (you cannot change it, except for elements of arrays that belong to it). There is also a `mutable struct` approach."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 16,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"x1: MyType(1:10, \"Hazel\", [11 12; 21 22])\n",
|
||||||
|
"x1.s: Hazel\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"a = 1:10\n",
|
||||||
|
"str = \"Hazel\"\n",
|
||||||
|
"C = [11 12;21 22]\n",
|
||||||
|
"\n",
|
||||||
|
"struct MyType #change to `mutable struct` to be able to change it later\n",
|
||||||
|
" x #can be anything\n",
|
||||||
|
" s::String #has to be a String\n",
|
||||||
|
" z::Array #has to be an Array\n",
|
||||||
|
"end\n",
|
||||||
|
"\n",
|
||||||
|
"x1 = MyType(a,str,C) #has to specify all arguments\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"x1: \",x1)\n",
|
||||||
|
"println(\"x1.s: \",x1.s)\n",
|
||||||
|
"\n",
|
||||||
|
"#x1 = MyType(1:10,10,[1;2]) #error since 10 is not a string\n",
|
||||||
|
"#x1.x = 3 #error since we cannot change"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## A Potential Pitfall in Using Arrays in Structures (extra)\n",
|
||||||
|
"\n",
|
||||||
|
"It is also possible to specify array types (for instance, `z::Array{Float64}` instead of just `z::Array`). This has the effect of converting (if possible) an input array to Float64. While this might have its uses, it also comes with a potential drawback: the conversion breaks the link between the input array and the array inside `MyType`."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### DataFrames and Other Things\n",
|
||||||
|
"\n",
|
||||||
|
"See [DataFrames.jl](https://juliadata.github.io/DataFrames.jl/stable/) for how to work with DataFrames."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# A Potential Pitfall when Using an Array in another Data Container (extra)\n",
|
||||||
|
"\n",
|
||||||
|
"Suppose you create an array of arrays (or a tuple or a dictionary) called `y`, and that the array `C` is one of the elements.\n",
|
||||||
|
"\n",
|
||||||
|
"If you later change *elements* of `C` then it will affect `y` as well (and vice versa). This happens with *arrays*, since they are designed to conserve memory space. For instance, even if `C` is a very large array (several GB, say), creating `y=[\"hello\",C]` will require very little additional memory space.\n",
|
||||||
|
"\n",
|
||||||
|
"If you want an independent copy, use `copy(C)`, for instance, `y=[\"hello\",copy(C)]`.\n",
|
||||||
|
"\n",
|
||||||
|
"In contrast, if you change the shape of `C` then it will *not* affect `y` (but you don't save any memory)."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 17,
|
||||||
|
"metadata": {
|
||||||
|
"scrolled": true
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"3-element Vector{Any}:\n",
|
||||||
|
" 1:10\n",
|
||||||
|
" \"Hazel\"\n",
|
||||||
|
" [-999 12; 21 22]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "display_data"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"(1:10, \"Hazel\", [-999 12; 21 22])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "display_data"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"Dict{Symbol, Any} with 3 entries:\n",
|
||||||
|
" :a => 1:10\n",
|
||||||
|
" :str => \"Hazel\"\n",
|
||||||
|
" :C => [-999 12; 21 22]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "display_data"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"MyType(1:10, \"Hazel\", [-999 12; 21 22])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "display_data"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"a = 1:10\n",
|
||||||
|
"str = \"Hazel\"\n",
|
||||||
|
"C = [11 12;21 22]\n",
|
||||||
|
"\n",
|
||||||
|
"x = [a,str,C]\n",
|
||||||
|
"t = (a,str,C)\n",
|
||||||
|
"d = Dict(:a=>a,:str=>str,:C=>C)\n",
|
||||||
|
"e = MyType(a,str,C)\n",
|
||||||
|
"\n",
|
||||||
|
"C[1,1] = -999 #changing an element of C affects x,t,d,e\n",
|
||||||
|
"\n",
|
||||||
|
"display(x)\n",
|
||||||
|
"display(t)\n",
|
||||||
|
"display(d)\n",
|
||||||
|
"display(e)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 18,
|
||||||
|
"metadata": {
|
||||||
|
"scrolled": true
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"(1:10, \"Hazel\", [-999 12; 21 22])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "display_data"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"C = 0 #changing the shape of C does not affect x,t,d\n",
|
||||||
|
"display(t)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"@webio": {
|
||||||
|
"lastCommId": null,
|
||||||
|
"lastKernelId": null
|
||||||
|
},
|
||||||
|
"anaconda-cloud": {},
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Julia 1.6.0",
|
||||||
|
"language": "julia",
|
||||||
|
"name": "julia-1.6"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"file_extension": ".jl",
|
||||||
|
"mimetype": "application/julia",
|
||||||
|
"name": "julia",
|
||||||
|
"version": "1.6.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 1
|
||||||
|
}
|
608
JuliaTutorial-master/Tutorial_04_Plots.ipynb
Normal file
608
JuliaTutorial-master/Tutorial_04_Plots.ipynb
Normal file
File diff suppressed because one or more lines are too long
1642
JuliaTutorial-master/Tutorial_05_Finance.ipynb
Normal file
1642
JuliaTutorial-master/Tutorial_05_Finance.ipynb
Normal file
File diff suppressed because one or more lines are too long
722
JuliaTutorial-master/Tutorial_06a_Arrays.ipynb
Normal file
722
JuliaTutorial-master/Tutorial_06a_Arrays.ipynb
Normal file
|
@ -0,0 +1,722 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"# Arrays\n",
|
||||||
|
"\n",
|
||||||
|
"This notebook illustrates how to create and reshuffle arrays. Other notebooks focus on matrix algebra and other functions applied to matrices."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Load Packages and Extra Functions"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"printyellow (generic function with 1 method)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"using Printf, DelimitedFiles\n",
|
||||||
|
"\n",
|
||||||
|
"include(\"jlFiles/printmat.jl\") #a function for prettier matrix printing"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Scalars, Vectors and Multi-dimensional Arrays\n",
|
||||||
|
"\n",
|
||||||
|
"*are different things*, even if they happen to \"look\" similar. For instance, a 1x1 array is not a scalar and an nx1 array is not a vector. This is discussed in some detail further down. \n",
|
||||||
|
"\n",
|
||||||
|
"However, we first present some common features of all arrays (vectors or multi-dimensional arrays)."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Creating Arrays\n",
|
||||||
|
"\n",
|
||||||
|
"The typical ways of getting an array are \n",
|
||||||
|
"\n",
|
||||||
|
"* hard coding the contents\n",
|
||||||
|
"* reading in data from a file\n",
|
||||||
|
"* as a result from computations\n",
|
||||||
|
"* allocating the array and then changing the elements\n",
|
||||||
|
"* (often not so smart) growing the array by adding rows (or columns,..)\n",
|
||||||
|
"* by list comprehension\n",
|
||||||
|
"\n",
|
||||||
|
"The next few cells give simple examples."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"## 1. Hard Coding the Contents or Reading from a File"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"A matrix that we typed in:\n",
|
||||||
|
" 11 12 \n",
|
||||||
|
" 21 22 \n",
|
||||||
|
"\n",
|
||||||
|
"First four lines of x from csv file:\n",
|
||||||
|
"197901.000 4.180 0.770 10.960\n",
|
||||||
|
"197902.000 -3.410 0.730 -2.090\n",
|
||||||
|
"197903.000 5.750 0.810 11.710\n",
|
||||||
|
"197904.000 0.050 0.800 3.270\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"z = [11 12; #typing in your matrix\n",
|
||||||
|
" 21 22]\n",
|
||||||
|
"println(\"A matrix that we typed in:\")\n",
|
||||||
|
"printmat(z)\n",
|
||||||
|
"\n",
|
||||||
|
"x = readdlm(\"Data/MyData.csv\",',',skipstart=1) #read matrix from file\n",
|
||||||
|
"println(\"First four lines of x from csv file:\")\n",
|
||||||
|
"printmat(x[1:4,:])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## 2a. Allocating an Array and Then Changing the Elements: Fill\n",
|
||||||
|
"\n",
|
||||||
|
"An easy way to create an array is to use the `fill()` function.\n",
|
||||||
|
"\n",
|
||||||
|
"```\n",
|
||||||
|
"A = fill(0,(10,2)) #10x2, integers (0)\n",
|
||||||
|
"B = fill(0.0,10) #vector with 10 elements, floats (0.0)\n",
|
||||||
|
"C = fill(NaN,(10,2)) #10x2, floats (NaN)\n",
|
||||||
|
"D = fill(\"\",3) #vector with 3 elements, strings (\"\")\n",
|
||||||
|
"E = fill(Date(1),3) #vector with 3 elements, dates (0001-01-01) \n",
|
||||||
|
"```"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 3,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"so far, x is filled with 0.0. For instance, x[1,1] is 0.0\n",
|
||||||
|
"\n",
|
||||||
|
"x after some computations\n",
|
||||||
|
" 1.000 0.500\n",
|
||||||
|
" 2.000 1.000\n",
|
||||||
|
" 3.000 1.500\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"x = fill(0.0,(3,2)) #creates a 3x2 matrix filled with 0.0\n",
|
||||||
|
"println(\"so far, x is filled with 0.0. For instance, x[1,1] is $(x[1,1])\")\n",
|
||||||
|
"\n",
|
||||||
|
"for i = 1:size(x,1), j = 1:size(x,2)\n",
|
||||||
|
" x[i,j] = i/j\n",
|
||||||
|
"end\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"\\nx after some computations\")\n",
|
||||||
|
"printmat(x)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## 2b. Allocating an Array and Then Changing the Elements: A More General Approach (extra)\n",
|
||||||
|
"\n",
|
||||||
|
"You can also create an array by \n",
|
||||||
|
"\n",
|
||||||
|
"```\n",
|
||||||
|
"A = Array{Int}(undef,10,2) #10x2, integers\n",
|
||||||
|
"F = Array{Any}(undef,3) #vector with 3 elements, can include anything\n",
|
||||||
|
"```\n",
|
||||||
|
"\n",
|
||||||
|
"The ```undef``` signals that the matrix is yet not initialized. This is more cumbersome than `fill()`, but sometimes more flexible."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"[1, 2, 3, 4]\n",
|
||||||
|
"Sultans of Swing\n",
|
||||||
|
" 1978 \n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"F = Array{Any}(undef,3)\n",
|
||||||
|
"F[1] = [1;2;3;4] #F[1] contains a vector\n",
|
||||||
|
"F[2] = \"Sultans of Swing\" #F[2] a string\n",
|
||||||
|
"F[3] = 1978 #F[3] an integer\n",
|
||||||
|
"\n",
|
||||||
|
"printmat(F)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## 3. Growing an Array\n",
|
||||||
|
"\n",
|
||||||
|
"Growing a matrix is done by `[A;B]` and/or `[A B]` (or by `vcat`, `hcat` and `cat`). This is somewhat slow, so do not use it for appending to a matrix in a long loop. Instead, pre-allocate the matrix and then fill it (see above).\n",
|
||||||
|
"\n",
|
||||||
|
"However, growing a *vector* is not that slow. It can be done by \n",
|
||||||
|
"```\n",
|
||||||
|
"push!(old vector,new element 1,new element 2)\n",
|
||||||
|
"```\n",
|
||||||
|
"\n",
|
||||||
|
"If you instead want to append all elements of a vector, then do\n",
|
||||||
|
"```\n",
|
||||||
|
"append!(old vector,vector to append) #in Julia 1.6, you can append several vectors\n",
|
||||||
|
"```"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"\n",
|
||||||
|
"stacking A and B vertically\n",
|
||||||
|
" 11 12 \n",
|
||||||
|
" 21 22 \n",
|
||||||
|
" 1 2 \n",
|
||||||
|
" 0 10 \n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"stacking A and B horizontally\n",
|
||||||
|
" 11 12 1 2 \n",
|
||||||
|
" 21 22 0 10 \n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"A = [11 12;\n",
|
||||||
|
" 21 22]\n",
|
||||||
|
"B = [1 2;\n",
|
||||||
|
" 0 10]\n",
|
||||||
|
"\n",
|
||||||
|
"z = [A;B] #same as vcat(A,B)\n",
|
||||||
|
"println(\"\\n\",\"stacking A and B vertically\")\n",
|
||||||
|
"printmat(z)\n",
|
||||||
|
"\n",
|
||||||
|
"z2 = [A B] #same as hcat(A,B)\n",
|
||||||
|
"println(\"\\n\",\"stacking A and B horizontally\")\n",
|
||||||
|
"printmat(z2) "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 6,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"a vector with 3 elements:\n",
|
||||||
|
" 12.000\n",
|
||||||
|
" 102.000\n",
|
||||||
|
" 1002.000\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"B = Float64[] #empty vector, to include floats\n",
|
||||||
|
"for i = 1:3\n",
|
||||||
|
" x_i = 2.0 + 10^i\n",
|
||||||
|
" push!(B,x_i) #adding an element at the end\n",
|
||||||
|
"end \n",
|
||||||
|
"println(\"a vector with 3 elements:\")\n",
|
||||||
|
"printmat(B)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## 4. List Comprehension and map (extra)\n",
|
||||||
|
"\n",
|
||||||
|
"List comprehension sounds fancy, but it is just a simple way to create arrays from repeated calculations. Similar to a \"for loop.\"\n",
|
||||||
|
"\n",
|
||||||
|
"You can achieve the same thing with ```map``` (for instance, by ```map(i->collect(1:i),1:3)```)."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 7,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"A[1] is vector with 1 element, A[2] a vector with 2 elements,...\n",
|
||||||
|
" [1]\n",
|
||||||
|
" [1, 2]\n",
|
||||||
|
" [1, 2, 3]\n",
|
||||||
|
"\n",
|
||||||
|
" [1]\n",
|
||||||
|
" [1, 2]\n",
|
||||||
|
" [1, 2, 3]\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"A = [collect(1:i) for i=1:3] #this creates a vector of vectors\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"A[1] is vector with 1 element, A[2] a vector with 2 elements,...\")\n",
|
||||||
|
"printmat(A)\n",
|
||||||
|
"\n",
|
||||||
|
"B = map(i->collect(1:i),1:3)\n",
|
||||||
|
"printmat(B)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Using Parts of a Matrix 1\n",
|
||||||
|
"\n",
|
||||||
|
"The most common way to use parts of an array is by indexing. For instance, to use the second column of `A`, do `A[:,2]`.\n",
|
||||||
|
"\n",
|
||||||
|
"Notice that `A[1,:]` gives a (column) vector (yes, it does), while `A[1:1,:]` gives a 1xk matrix. (It looks like a row vector, but is really a matrix with just one row.)\n",
|
||||||
|
"\n",
|
||||||
|
"Also notice that `z = A[1,:]` creates an independent copy, so changing `z` will *not* change `A`."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 8,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"A:\n",
|
||||||
|
" 11 12 \n",
|
||||||
|
" 21 22 \n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"second column of A:\n",
|
||||||
|
" 12 \n",
|
||||||
|
" 22 \n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"first row of A (as a vector): \n",
|
||||||
|
" 11 \n",
|
||||||
|
" 12 \n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"first row of A: \n",
|
||||||
|
" 11 12 \n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"A = [11 12;\n",
|
||||||
|
" 21 22]\n",
|
||||||
|
"println(\"A:\")\n",
|
||||||
|
"printmat(A)\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"\\nsecond column of A:\")\n",
|
||||||
|
"printmat(A[:,2])\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"\\n\",\"first row of A (as a vector): \")\n",
|
||||||
|
"printmat(A[1,:]) #notice 1 makes it a vector\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"\\n\",\"first row of A: \")\n",
|
||||||
|
"printmat(A[1:1,:]) #use 1:1 to keep it as a 1x2 matrix"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Using Parts of a Matrix 2 (extra)\n",
|
||||||
|
"\n",
|
||||||
|
"In case you do not need an independent copy, then `y = view(A,1,:)` creates a *view* of the first row of `A`. This saves memory and is sometimes faster. Notice, however, that changing `y` by `y .= [1,2]` will now change the first row of `A`. Notice that the dot `.` is needed. \n",
|
||||||
|
"\n",
|
||||||
|
"A shortcut to loop over all rows of `A` is `for i in eachrow(A)`. There is also `eachcol()`.\n",
|
||||||
|
"\n",
|
||||||
|
"To make a *copy or a view?* If you need to save memory: a view. Instead, if you need speed: try both. (Copies are often quicker when you need to do lots of computations on the matrix, for instance, in a linear regression.)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 9,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"\n",
|
||||||
|
"view of first row of A (although it prints like a column vector): \n",
|
||||||
|
" 11 \n",
|
||||||
|
" 12 \n",
|
||||||
|
"\n",
|
||||||
|
"A after changing y\n",
|
||||||
|
" 1 2 \n",
|
||||||
|
" 21 22 \n",
|
||||||
|
"\n",
|
||||||
|
"another row: \n",
|
||||||
|
" 1 \n",
|
||||||
|
" 2 \n",
|
||||||
|
"\n",
|
||||||
|
"another row: \n",
|
||||||
|
" 21 \n",
|
||||||
|
" 22 \n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"println(\"\\n\",\"view of first row of A (although it prints like a column vector): \")\n",
|
||||||
|
"y = view(A,1,:)\n",
|
||||||
|
"printmat(y)\n",
|
||||||
|
"\n",
|
||||||
|
"y .= [1,2] #changing y and thus the first row of A\n",
|
||||||
|
"println(\"A after changing y\")\n",
|
||||||
|
"printmat(A)\n",
|
||||||
|
"\n",
|
||||||
|
"for i in eachrow(A) #looping over all rows\n",
|
||||||
|
" println(\"another row: \")\n",
|
||||||
|
" printmat(i)\n",
|
||||||
|
"end"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Splitting up an Array (extra)\n",
|
||||||
|
"\n",
|
||||||
|
"Sometimes you want to assign separate names to the columns (or rows) of a matrix. The next cell shows an example."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 10,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"A simple way...which works well when you want to create a few variables\n",
|
||||||
|
" 2 \n",
|
||||||
|
" 22 \n",
|
||||||
|
"\n",
|
||||||
|
"Another, prettier way\n",
|
||||||
|
" 2 \n",
|
||||||
|
" 22 \n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"println(\"A simple way...which works well when you want to create a few variables\")\n",
|
||||||
|
"x1 = A[:,1]\n",
|
||||||
|
"x2 = A[:,2]\n",
|
||||||
|
"printmat(x2)\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"Another, prettier way\")\n",
|
||||||
|
"(z1,z2) = [A[:,i] for i = 1:2]\n",
|
||||||
|
"printmat(z2)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Arrays vs. Vectors vs. Scalars\n",
|
||||||
|
"\n",
|
||||||
|
"Matrices, vectors and scalars are different things, even if they contain the same number of elements. In particular,\n",
|
||||||
|
"\n",
|
||||||
|
"(a) an nx1 matrix is not the same thing as an n-vector\n",
|
||||||
|
"\n",
|
||||||
|
"(b) a 1x1 matrix or a 1-element vector are not the same thing as a scalar.\n",
|
||||||
|
"\n",
|
||||||
|
"As you will see further on, vectors are often more convenient than nx1 matrices.\n",
|
||||||
|
"\n",
|
||||||
|
"To convert a 1-element vector or 1x1 matrix `C` to a scalar, just do `myScalar = C[]`."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 11,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"The sizes of matrix A and vector B: (3, 1) (3,)\n",
|
||||||
|
"\n",
|
||||||
|
"Testing if A==B: false\n",
|
||||||
|
"\n",
|
||||||
|
"The nx1 matrix A and n-element vector B can often be used together, for instance, as in A+B, whose size is (3, 1)\n",
|
||||||
|
" 2.000\n",
|
||||||
|
" 2.000\n",
|
||||||
|
" 2.000\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"A = ones(3,1) #this is a 3x1 matrix\n",
|
||||||
|
"B = ones(3) #a vector with 3 elements\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"The sizes of matrix A and vector B: $(size(A)) $(size(B))\")\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"\\nTesting if A==B: \",isequal(A,B))\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"\\nThe nx1 matrix A and n-element vector B can often be used together, for instance, as in A+B, whose size is \",size(A+B))\n",
|
||||||
|
"printmat(A+B)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 12,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"\n",
|
||||||
|
"c/C would give an error since C is a (1x1) matrix\n",
|
||||||
|
"\n",
|
||||||
|
"Instead, do c/C[]: 1.0\n",
|
||||||
|
"\n",
|
||||||
|
"After conversion of C, do c/C: 1.0\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"C = ones(1,1) #a 1x1 matrix\n",
|
||||||
|
"c = 1 #a scalar\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"\\nc/C would give an error since C is a (1x1) matrix\")\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"\\nInstead, do c/C[]: \",c/C[])\n",
|
||||||
|
"\n",
|
||||||
|
"if length(C) == 1 && !isa(C,Number)\n",
|
||||||
|
" C = C[]\n",
|
||||||
|
"end\n",
|
||||||
|
"println(\"\\nAfter conversion of C, do c/C: \",c/C)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Vectors: x'x and x'A*x Create Scalars (if x is a vector)\n",
|
||||||
|
"\n",
|
||||||
|
"If `x` is a vector and `A` a matrix, then `x'x` and `x'A*x` are scalars. This is what a linear algebra text book would teach you, so vectors are very useful.\n",
|
||||||
|
"\n",
|
||||||
|
"This is *not* true if `x` is a matrix of size nx1. In that case the result is a 1x1 matrix. \n",
|
||||||
|
"\n",
|
||||||
|
"Recommendation: use vectors (instead of nx1 matrices) when you can."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 13,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"\n",
|
||||||
|
"x'x and x'A*x when x is a 2 element vector: 5 165\n",
|
||||||
|
"\n",
|
||||||
|
"x'x and x'A*x when x is a 2x1 array: [5] [165]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"x = [1;2] #this is a vector\n",
|
||||||
|
"A = [11 12;\n",
|
||||||
|
" 21 22]\n",
|
||||||
|
"println(\"\\nx'x and x'A*x when x is a 2 element vector: \",x'x,\" \",x'A*x)\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"x = zeros(Int,2,1) #this is a 2x1 matrix (array)\n",
|
||||||
|
"x[1] = 1\n",
|
||||||
|
"x[2] = 2\n",
|
||||||
|
"println(\"\\nx'x and x'A*x when x is a 2x1 array: \",x'x,\" \",x'A*x)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# An Array of Arrays (extra)\n",
|
||||||
|
"\n",
|
||||||
|
"If `x1` and `x2` are two arrays, then `y=[x1,x2]` is a vector (of arrays) where `y[1] = x1` and `y[2] = x2`. (If you instead want to stack `x1` and `x2` into a single matrix, use `[x1 x2]`, `[x1;x2]` or one of the `cat` functions discussed above.)\n",
|
||||||
|
"\n",
|
||||||
|
"In this case `y[1]` is actually a view of `x1` so changing elements of one changes the other."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 14,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"(2,)\n",
|
||||||
|
" 1.000 1.000\n",
|
||||||
|
" 1.000 1.000\n",
|
||||||
|
" 1.000 1.000\n",
|
||||||
|
"\n",
|
||||||
|
" 1 \n",
|
||||||
|
" 2 \n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"x1 = ones(3,2)\n",
|
||||||
|
"x2 = [1;2]\n",
|
||||||
|
"y = [x1,x2] #a vector of arrays\n",
|
||||||
|
"\n",
|
||||||
|
"println(size(y))\n",
|
||||||
|
"printmat(y[1])\n",
|
||||||
|
"printmat(y[2])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Arrays are Different...\n",
|
||||||
|
"\n",
|
||||||
|
"Vectors and matrices (arrays) can take lots of memory space, so **Julia is designed to avoid unnecessary copies of arrays**. In short, notice the following\n",
|
||||||
|
"\n",
|
||||||
|
"* ```B = A``` creates two names of the *same* array (changing one changes the other)\n",
|
||||||
|
"* ```B = reshape(A,n,m)```, ```B = vec(A)```, and ```B = A'``` and create *another view* of the same array (changing one changes the other)\n",
|
||||||
|
"* When an you input an array to a function, then this array is shared between the function and the calling program (scope). Changing *elements* of the array (inside the function) will then change the array outside the function. The next cell provides some details.\n",
|
||||||
|
"\n",
|
||||||
|
"If you do not like this behaviour, then use `copy(A)` to create an independent copy of the array."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 15,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"original x: 1.000 2.000\n",
|
||||||
|
"x after calling f1(x): 0.500 2.000\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"function f1(A)\n",
|
||||||
|
" A[1] = A[1]/2 #changing ELEMENTS of A, affects outside value\n",
|
||||||
|
" #A = A/2 #this would NOT affect the outside value\n",
|
||||||
|
" return A\n",
|
||||||
|
"end\n",
|
||||||
|
"\n",
|
||||||
|
"x = [1.0 2.0]\n",
|
||||||
|
"printlnPs(\"original x: \",x)\n",
|
||||||
|
"\n",
|
||||||
|
"y1 = f1(x)\n",
|
||||||
|
"printlnPs(\"x after calling f1(x): \",x)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"@webio": {
|
||||||
|
"lastCommId": null,
|
||||||
|
"lastKernelId": null
|
||||||
|
},
|
||||||
|
"anaconda-cloud": {},
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Julia 1.6.1",
|
||||||
|
"language": "julia",
|
||||||
|
"name": "julia-1.6"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"file_extension": ".jl",
|
||||||
|
"mimetype": "application/julia",
|
||||||
|
"name": "julia",
|
||||||
|
"version": "1.6.1"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 1
|
||||||
|
}
|
518
JuliaTutorial-master/Tutorial_06b_MatrixAlgebra.ipynb
Normal file
518
JuliaTutorial-master/Tutorial_06b_MatrixAlgebra.ipynb
Normal file
|
@ -0,0 +1,518 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Basic Matrix Algebra\n",
|
||||||
|
"\n",
|
||||||
|
"This notebook presents some basic linear algebra in Julia."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"printyellow (generic function with 1 method)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"using Printf, LinearAlgebra\n",
|
||||||
|
"\n",
|
||||||
|
"include(\"jlFiles/printmat.jl\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Adding and Multiplying: A Matrix and a Scalar"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"With a matrix $A$ and a scalar $c$, do\n",
|
||||||
|
"\n",
|
||||||
|
"1. `A*c` (textbook: $Ac$) to multiply each element of $A$ by $c$\n",
|
||||||
|
"\n",
|
||||||
|
"2. `A .+ c` (textbook: $A+cJ$, where $J$ is a matrix of ones) to add $c$ to each element of $A$, and similarly `A .- c` ($A-c$)\n",
|
||||||
|
"\n",
|
||||||
|
"Watch out when the number comes first: `2.+A` is not allowed since it is ambiguous. However, `2.0.+A` and `2 .+ A` both work."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"A:\n",
|
||||||
|
" 1 3 \n",
|
||||||
|
" 3 4 \n",
|
||||||
|
"\n",
|
||||||
|
"c:\n",
|
||||||
|
" 10 \n",
|
||||||
|
"\n",
|
||||||
|
"A*c:\n",
|
||||||
|
" 10 30 \n",
|
||||||
|
" 30 40 \n",
|
||||||
|
"\n",
|
||||||
|
"A .+ c:\n",
|
||||||
|
" 11 13 \n",
|
||||||
|
" 13 14 \n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"A = [1 3;3 4]\n",
|
||||||
|
"c = 10\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"A:\")\n",
|
||||||
|
"printmat(A)\n",
|
||||||
|
"println(\"c:\")\n",
|
||||||
|
"printmat(c)\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"A*c:\")\n",
|
||||||
|
"printmat(A*c)\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"A .+ c:\")\n",
|
||||||
|
"printmat(A .+ c) #notice the dot in .+"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Adding and Multiplying Two Matrices"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"With two matrices of the same dimensions ($A$ and $B$), do\n",
|
||||||
|
"\n",
|
||||||
|
"`A+B` (textbook: $A+B$) to add them (element by element), and similarly `A-B` (textbook: $A-B$).\n",
|
||||||
|
"\n",
|
||||||
|
"Multiplying matrices ($A$ and $B$) of conformable dimensions\n",
|
||||||
|
"\n",
|
||||||
|
"`A*B` (textbook: $AB$)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 3,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"A:\n",
|
||||||
|
" 1 3 \n",
|
||||||
|
" 3 4 \n",
|
||||||
|
"\n",
|
||||||
|
"B:\n",
|
||||||
|
" 1 2 \n",
|
||||||
|
" 3 -2 \n",
|
||||||
|
"\n",
|
||||||
|
"A+B:\n",
|
||||||
|
" 2 5 \n",
|
||||||
|
" 6 2 \n",
|
||||||
|
"\n",
|
||||||
|
"A*B:\n",
|
||||||
|
" 10 -4 \n",
|
||||||
|
" 15 -2 \n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"A = [1 3;3 4] #A and B are 2x2 matrices\n",
|
||||||
|
"B = [1 2;3 -2]\n",
|
||||||
|
"println(\"A:\")\n",
|
||||||
|
"printmat(A)\n",
|
||||||
|
"println(\"B:\")\n",
|
||||||
|
"printmat(B)\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"A+B:\")\n",
|
||||||
|
"printmat(A+B)\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"A*B:\")\n",
|
||||||
|
"printmat(A*B)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Transpose\n",
|
||||||
|
"\n",
|
||||||
|
"You can transpose a numerical matrix `A` by `A'`. \n",
|
||||||
|
"\n",
|
||||||
|
"Notice that (in Julia) `A` and `B=A'` share the same elements (changing one changes the other). If you want an independent copy, use `B=copy(A')`.\n",
|
||||||
|
"\n",
|
||||||
|
"For an array of other elements (for instance, strings), use `permutedims(A)` to swap the dimensions."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"A: \n",
|
||||||
|
" 1 2 3 \n",
|
||||||
|
" 4 5 6 \n",
|
||||||
|
"\n",
|
||||||
|
"A': \n",
|
||||||
|
" 1 4 \n",
|
||||||
|
" 2 5 \n",
|
||||||
|
" 3 6 \n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"A = [1 2 3;4 5 6]\n",
|
||||||
|
"println(\"A: \")\n",
|
||||||
|
"printmat(A)\n",
|
||||||
|
"println(\"A': \")\n",
|
||||||
|
"printmat(A')"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Matrix Inverse"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"A matrix inverse of an $nxn$ matrix $A$:\n",
|
||||||
|
"\n",
|
||||||
|
"`inv(A)` or `A^(-1)` (textbook: $A^{-1}$)\n",
|
||||||
|
"\n",
|
||||||
|
"The inverse is such that $AA^{-1}=I$ and $A^{-1}A=I$"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"A:\n",
|
||||||
|
" 1 3 \n",
|
||||||
|
" 3 4 \n",
|
||||||
|
"\n",
|
||||||
|
"inv(A):\n",
|
||||||
|
" -0.800 0.600\n",
|
||||||
|
" 0.600 -0.200\n",
|
||||||
|
"\n",
|
||||||
|
"inv(A)*A:\n",
|
||||||
|
" 1.000 -0.000\n",
|
||||||
|
" 0.000 1.000\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"A = [1 3;3 4]\n",
|
||||||
|
"println(\"A:\")\n",
|
||||||
|
"printmat(A)\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"inv(A):\")\n",
|
||||||
|
"printmat(inv(A))\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"inv(A)*A:\")\n",
|
||||||
|
"printmat(inv(A)*A)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## The Identity Matrix\n",
|
||||||
|
"\n",
|
||||||
|
"The identity matrix $I_n$ can often be represented by `I` and then Julia will compare with the surrounding code to create the right dimension. For instance, if `A` is a square matrix, then `I+A` works.\n",
|
||||||
|
"\n",
|
||||||
|
"If you still need to specify the dimension, then `1I(3)` or `Matrix(1I(3))` will create an $I_3$ matrix. (The former is an explicitly diagonal matrix, will the second is a full matrix.) In Julia 1.6 in will be possible to do `1I[1:3,1:3]`."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 6,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"I + A\n",
|
||||||
|
" 2 3 \n",
|
||||||
|
" 3 5 \n",
|
||||||
|
"\n",
|
||||||
|
"Matrix(1I(3))\n",
|
||||||
|
" 1 0 0 \n",
|
||||||
|
" 0 1 0 \n",
|
||||||
|
" 0 0 1 \n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"println(\"I + A\")\n",
|
||||||
|
"printmat(I + A)\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"Matrix(1I(3))\")\n",
|
||||||
|
"printmat(Matrix(1I(3)))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Vectors: Inner and Outer Products\n",
|
||||||
|
"\n",
|
||||||
|
"There are several different ways to think about a vector in mathematics: as a $K \\times 1$ matrix (a column vector), a $1 \\times K$ matrix (a row vector) or just a flat $K-$vector. Julia uses flat vectors but they are mostly interchangable with column vectors. \n",
|
||||||
|
"\n",
|
||||||
|
"The inner product of two (column) vectors with $k$ elements is calculated as `x'z` or `dot(x,y)` (textbook: $x'z$ or $x \\cdot z$) to get a scalar. (You can also use or `x⋅y` where the dot is obtained by `\\cdot + TAB`, but that is sometimes hard to distinguish from or `x.y`) \n",
|
||||||
|
"\n",
|
||||||
|
"In contrast, the outer of two (column) vectors with $k$ elements is calculated\n",
|
||||||
|
"(textbook: $xz'$) to get a $k\\times k$ matrix."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 7,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"x and z\n",
|
||||||
|
" 10 2 \n",
|
||||||
|
" 11 5 \n",
|
||||||
|
"\n",
|
||||||
|
"x'z: \n",
|
||||||
|
" 75 \n",
|
||||||
|
"x*z':\n",
|
||||||
|
" 20 50 \n",
|
||||||
|
" 22 55 \n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"x = [10,11] #[10;11] gives the same\n",
|
||||||
|
"z = [2,5]\n",
|
||||||
|
"println(\"x and z\")\n",
|
||||||
|
"printmat([x z])\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"x'z: \")\n",
|
||||||
|
"printlnPs(x'z) #dot(x,z) gives the same\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"x*z':\")\n",
|
||||||
|
"printmat(x*z')"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Vectors: Quadratic Forms\n",
|
||||||
|
"\n",
|
||||||
|
"A quadratic form ($A$ is an $n \\times n$ matrix and x is an $n$ vector): `x'A*x` (textbook: $x'Ax$) to get a scalar. \n",
|
||||||
|
"\n",
|
||||||
|
"(In Julia 1.5+ there is also the form `dot(x,A,x)`)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 8,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"x:\n",
|
||||||
|
" 10 \n",
|
||||||
|
" 11 \n",
|
||||||
|
"\n",
|
||||||
|
"A:\n",
|
||||||
|
" 1 3 \n",
|
||||||
|
" 3 4 \n",
|
||||||
|
"\n",
|
||||||
|
"x'A*x: \n",
|
||||||
|
" 1244 \n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"A = [1 3;3 4]\n",
|
||||||
|
"x = [10,11]\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"x:\")\n",
|
||||||
|
"printmat(x)\n",
|
||||||
|
"println(\"A:\")\n",
|
||||||
|
"printmat(A)\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"x'A*x: \")\n",
|
||||||
|
"printlnPs(x'A*x)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Vectors: Extracting Vectors from Matrices\n",
|
||||||
|
"\n",
|
||||||
|
"Notice that `A[1,:]` and `A[:,1]` both give flat vectors. In case you want a row vector use `A[1:1,:]`."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 9,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"2-element Vector{Int64}:\n",
|
||||||
|
" 1\n",
|
||||||
|
" 3"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 9,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"A[1,:]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## OLS Notation\n",
|
||||||
|
"\n",
|
||||||
|
"$X'X$ or $\\sum\\nolimits_{t=1}^{T}x_{t}x_{t}^{\\prime}$?\n",
|
||||||
|
"\n",
|
||||||
|
"Let $x_t$ be a (column) vector with values of $K$ regressors for observation $t$. Then $x_{t}x_{t}^{\\prime}$ is the outer product (a $K\\times K$ matrix) and $\\sum\\nolimits_{t=1}^{T}x_{t}x_{t}^{\\prime}$ is just the sum (of each element) across the $T$ observations.\n",
|
||||||
|
"\n",
|
||||||
|
"We can calculate the same thing by (a) letting $X$ be a $T\\times K$ matrix with $x_{t}^{\\prime}$ in row $t$ and (b) then do $X'X$."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 10,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"X\n",
|
||||||
|
" 1.000 -1.000\n",
|
||||||
|
" 1.000 0.000\n",
|
||||||
|
" 1.000 1.000\n",
|
||||||
|
"\n",
|
||||||
|
"sum of outer products, three versions\n",
|
||||||
|
" 3.000 0.000\n",
|
||||||
|
" 0.000 2.000\n",
|
||||||
|
"\n",
|
||||||
|
" 3.000 0.000\n",
|
||||||
|
" 0.000 2.000\n",
|
||||||
|
"\n",
|
||||||
|
" 3.000 0.000\n",
|
||||||
|
" 0.000 2.000\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"x₁ = [1,-1] #a (column) vector\n",
|
||||||
|
"x₂ = [1,0]\n",
|
||||||
|
"x₃ = [1,1.0]\n",
|
||||||
|
"\n",
|
||||||
|
"X = [x₁';x₂';x₃']\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"X\")\n",
|
||||||
|
"printmat(X)\n",
|
||||||
|
"\n",
|
||||||
|
"(T,K) = (size(X,1),size(X,2))\n",
|
||||||
|
"\n",
|
||||||
|
"Sxx1 = x₁*x₁' + x₂*x₂' + x₃*x₃' #just to illustrate\n",
|
||||||
|
"\n",
|
||||||
|
"Sxx2 = zeros(K,K)\n",
|
||||||
|
"for t = 1:T\n",
|
||||||
|
" #global Sxx2 #only needed in script\n",
|
||||||
|
" Sxx2 = Sxx2 + X[t,:]*X[t,:]' #X[t,:] becomes a flat vector\n",
|
||||||
|
"end\n",
|
||||||
|
"\n",
|
||||||
|
"Sxx3 = X'X\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"sum of outer products, three versions\")\n",
|
||||||
|
"printmat(Sxx1)\n",
|
||||||
|
"printmat(Sxx2)\n",
|
||||||
|
"printmat(Sxx3)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"@webio": {
|
||||||
|
"lastCommId": null,
|
||||||
|
"lastKernelId": null
|
||||||
|
},
|
||||||
|
"anaconda-cloud": {},
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Julia 1.6.0",
|
||||||
|
"language": "julia",
|
||||||
|
"name": "julia-1.6"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"file_extension": ".jl",
|
||||||
|
"mimetype": "application/julia",
|
||||||
|
"name": "julia",
|
||||||
|
"version": "1.6.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 1
|
||||||
|
}
|
305
JuliaTutorial-master/Tutorial_06c_ArrayFunctions.ipynb
Normal file
305
JuliaTutorial-master/Tutorial_06c_ArrayFunctions.ipynb
Normal file
|
@ -0,0 +1,305 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"# Functions of Arrays\n",
|
||||||
|
"\n",
|
||||||
|
"This notebook illustrates how to apply a function to arrays. Another notebook focuses on matrix algebra."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Load Packages and Extra Functions"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"printyellow (generic function with 1 method)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"using Printf\n",
|
||||||
|
"\n",
|
||||||
|
"include(\"jlFiles/printmat.jl\") #a function for prettier matrix printing"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Elementwise Functions of Arrays: Using Dot (.)\n",
|
||||||
|
"\n",
|
||||||
|
"Let `X` be an array, and `a` and `b` be scalars. Then, `y = fn.(X,a,b)` generates an array `y` where `y[i,j] = fn(X[i,j],a,b)`\n",
|
||||||
|
"\n",
|
||||||
|
"(You could achieve the same thing with ```map(xi->fn(xi,a,b),x)```.)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"fn (generic function with 1 method)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 2,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"fn(x,a,b) = a/x + b #x has to be a scalar for this to work"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 3,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
" 110.000 60.000\n",
|
||||||
|
" Inf 20.000\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"X = [1 2;\n",
|
||||||
|
" 0 10]\n",
|
||||||
|
"\n",
|
||||||
|
"printmat(fn.(X,100,10)) #notice the dot."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Looping over Columns or Rows\n",
|
||||||
|
"\n",
|
||||||
|
"Suppose you want to calculate the sum of each row of a matrix. The classical way of doing that is to loop and extract each row as `X[i,:]`\n",
|
||||||
|
"\n",
|
||||||
|
"Notice that instead of `sum(X[:,i])` it may be faster and save memory to do `sum(view(X,i,:))` because this avoids creating a new array of `X[:,i]`.\n",
|
||||||
|
"\n",
|
||||||
|
"Also, there is a direct way to loop over all rows by using `for r in eachrow(X)`. To also get the row number `i`, we use `for (i,row) in enumerate(eachrow(X))`. For looping over columns, use `eachcol()`."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"X:\n",
|
||||||
|
" 1 2 \n",
|
||||||
|
" 0 10 \n",
|
||||||
|
"\n",
|
||||||
|
"sum of each row:\n",
|
||||||
|
" 3.000\n",
|
||||||
|
" 10.000\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"X = [1 2;\n",
|
||||||
|
" 0 10]\n",
|
||||||
|
"println(\"X:\")\n",
|
||||||
|
"printmat(X)\n",
|
||||||
|
"\n",
|
||||||
|
"m = size(X,1)\n",
|
||||||
|
"\n",
|
||||||
|
"z = zeros(m) #to fill with results, could here use zeros(Int,m)\n",
|
||||||
|
"for i = 1:m #loop over rows\n",
|
||||||
|
" z[i] = sum(X[i,:]) #or sum(view(X,i,:)) to save memory\n",
|
||||||
|
"end\n",
|
||||||
|
"println(\"sum of each row:\")\n",
|
||||||
|
"printmat(z)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"sum of each row:\n",
|
||||||
|
" 3.000\n",
|
||||||
|
" 10.000\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"z = zeros(m) \n",
|
||||||
|
"for (i,row) in enumerate(eachrow(X)) #enumerate to get both index and value\n",
|
||||||
|
" z[i] = sum(row)\n",
|
||||||
|
"end\n",
|
||||||
|
"println(\"sum of each row:\")\n",
|
||||||
|
"printmat(z)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Functions with Built-in `dims` Argument\n",
|
||||||
|
"\n",
|
||||||
|
"May functions have a `dims` argument that allows you to avoid looping, for instance, `sum`."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 6,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
" 3 \n",
|
||||||
|
" 10 \n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"printmat(sum(X,dims=2))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Functions with Built-in `function` and `dims` Arguments (extra)\n",
|
||||||
|
"\n",
|
||||||
|
"Several functions allow you to also apply an elementwise function to `X` before doing the rest of the calculations, for instance, `any`, `all`, `sum`, `prod`, `maximum`, and `minimum`."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 7,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"2×1 Matrix{Int64}:\n",
|
||||||
|
" 5\n",
|
||||||
|
" 100"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 7,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"sum(abs2,X,dims=2) #same as sum(abs2.(X),dims=2) but faster"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Apply Your Own Function on Each Column: mapslices (extra)\n",
|
||||||
|
"\n",
|
||||||
|
"...or each row (or some other dimension)\n",
|
||||||
|
"\n",
|
||||||
|
"The `mapslices(fun,x,dims=1)` applies `fun(x[:,i])` to each column of a matrix `x`. This is an alternative to looping over the columns.\n",
|
||||||
|
"\n",
|
||||||
|
"The cell below illustrates this by calling a function which calculates the moving average of `x[t]` and `x[t-1]` for each column of a matrix `X`."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 8,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
" X (with 2 columns) and Y (MA(1) of X):\n",
|
||||||
|
" 1.000 101.000 NaN NaN\n",
|
||||||
|
" 2.000 102.000 1.500 101.500\n",
|
||||||
|
" 3.000 103.000 2.500 102.500\n",
|
||||||
|
" 4.000 104.000 3.500 103.500\n",
|
||||||
|
" 5.000 105.000 4.500 104.500\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"function MovingAvg2(x) #moving average of t and t-1\n",
|
||||||
|
" T = length(x)\n",
|
||||||
|
" y = fill(NaN,T)\n",
|
||||||
|
" for t = 2:T\n",
|
||||||
|
" y[t] = (x[t] + x[t-1])/2\n",
|
||||||
|
" end\n",
|
||||||
|
" return y\n",
|
||||||
|
"end\n",
|
||||||
|
"\n",
|
||||||
|
"X = [1:5 101:105]\n",
|
||||||
|
"Y = mapslices(MovingAvg2,X,dims=1)\n",
|
||||||
|
"println(\" X (with 2 columns) and Y (MA(1) of X):\")\n",
|
||||||
|
"printmat([X Y])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"@webio": {
|
||||||
|
"lastCommId": null,
|
||||||
|
"lastKernelId": null
|
||||||
|
},
|
||||||
|
"anaconda-cloud": {},
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Julia 1.6.1",
|
||||||
|
"language": "julia",
|
||||||
|
"name": "julia-1.6"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"file_extension": ".jl",
|
||||||
|
"mimetype": "application/julia",
|
||||||
|
"name": "julia",
|
||||||
|
"version": "1.6.1"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 1
|
||||||
|
}
|
463
JuliaTutorial-master/Tutorial_07_Types.ipynb
Normal file
463
JuliaTutorial-master/Tutorial_07_Types.ipynb
Normal file
|
@ -0,0 +1,463 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Types\n",
|
||||||
|
"\n",
|
||||||
|
"This notebook provides (a) a brief introduction to types (for instance, integers, bools and strings), (b) how to test types and (c) convert from one type to another."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Load Packages and Extra Functions"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"printyellow (generic function with 1 method)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"using Printf\n",
|
||||||
|
"\n",
|
||||||
|
"include(\"jlFiles/printmat.jl\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Some Important Types\n",
|
||||||
|
"\n",
|
||||||
|
"Julia has many different types of variables: signed integers (like 2 or -5), floating point numbers (2.0 and -5.1), bools (false/true), bitarrays (similar to bools, but with more efficient use of memory), strings (\"hello\"), Dates (2017-04-23) and many more. \n",
|
||||||
|
"\n",
|
||||||
|
"The numerical types also comes with subtypes for different precisions, for instance, Float16, Float32 and Float64. Unless you specify otherwise, code like\n",
|
||||||
|
"```\n",
|
||||||
|
"a = 2\n",
|
||||||
|
"b = 2.0\n",
|
||||||
|
"```\n",
|
||||||
|
"gives an Int64 and a Float64 respectively (at least on the 64 bit version of Julia)."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Integers and Floats"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"a: Int64\n",
|
||||||
|
"2\n",
|
||||||
|
"\n",
|
||||||
|
"b: Float64\n",
|
||||||
|
"2.0\n",
|
||||||
|
"\n",
|
||||||
|
"A: Vector{Int64}\n",
|
||||||
|
" 1 \n",
|
||||||
|
" 2 \n",
|
||||||
|
"\n",
|
||||||
|
"B: Vector{Float64}\n",
|
||||||
|
" 1.000\n",
|
||||||
|
" 2.000\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"a = 2 #integer, Int (Int64 on most machines)\n",
|
||||||
|
"b = 2.0 #floating point, (Float64 on most machines)\n",
|
||||||
|
"A = [1;2]\n",
|
||||||
|
"B = [1.0;2.0]\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"a: \",typeof(a))\n",
|
||||||
|
"println(a)\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"\\nb: \",typeof(b))\n",
|
||||||
|
"println(b)\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"\\nA: \",typeof(A))\n",
|
||||||
|
"printmat(A)\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"B: \",typeof(B))\n",
|
||||||
|
"printmat(B)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Why Use Int When There Are Floats? \n",
|
||||||
|
"\n",
|
||||||
|
"That is, why bother with sometimes using 3 when you could use 3.0 everywhere? Mostly because you cannot use 3.0 everywhere... \n",
|
||||||
|
"\n",
|
||||||
|
"For instance, you cannot pick out element `x[3.0]` from a vector. It has to be `x[3]`."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 3,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"100\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"x = [1,10,100,1000]\n",
|
||||||
|
"\n",
|
||||||
|
"#println(x[3.0]) #uncomment and run. Will give an error\n",
|
||||||
|
"\n",
|
||||||
|
"println(x[3])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Bools and BitArrays"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"c: Bool\n",
|
||||||
|
"true\n",
|
||||||
|
"\n",
|
||||||
|
"C: BitVector\n",
|
||||||
|
" 0 \n",
|
||||||
|
" 1 \n",
|
||||||
|
"\n",
|
||||||
|
"A BitArray is a more economical array version of Bool, but prints as 0/1 by printmat.\n",
|
||||||
|
"Notice that typeof(C[1]) gives: Bool\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"c = 2 > 1.1\n",
|
||||||
|
"println(\"c: \",typeof(c))\n",
|
||||||
|
"println(c)\n",
|
||||||
|
"\n",
|
||||||
|
"C = A .> 1.5\n",
|
||||||
|
"println(\"\\nC: \",typeof(C))\n",
|
||||||
|
"printmat(C)\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"A BitArray is a more economical array version of Bool, but prints as 0/1 by printmat.\\n\",\n",
|
||||||
|
" \"Notice that typeof(C[1]) gives: \",typeof(C[1]))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Char and Strings"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Char\n",
|
||||||
|
"String\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"t = 'a' #Char, just one letter\n",
|
||||||
|
"println(typeof(t))\n",
|
||||||
|
"\n",
|
||||||
|
"txt = \"Dogs are nicer than cats.\" #String, could be a long novel\n",
|
||||||
|
"println(typeof(txt))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Calculations with Mixed Types and Converting Types\n",
|
||||||
|
"\n",
|
||||||
|
"A calculation like \"integer\" + \"float\" works and the type of the result is a float (the more flexible type). Similarly, \"bool\" + \"integer\" will give an integer. \n",
|
||||||
|
"\n",
|
||||||
|
"There are also direct ways of converting a variable from one type to another using the `convert()` function."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Some Calculations with Mixed Types (\"promotion\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 6,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Int + Float64: 3.0\n",
|
||||||
|
"Bool + Int: 3\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"println(\"Int + Float64: \",1+2.0)\n",
|
||||||
|
"println(\"Bool + Int: \",(1 .> 0) + 2)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Converting from Int to Float and Vice Versa"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 7,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"x: Vector{Float64}\n",
|
||||||
|
" 1.100\n",
|
||||||
|
" 10.100\n",
|
||||||
|
" 100.100\n",
|
||||||
|
"\n",
|
||||||
|
"rounding x to Int: Vector{Int64}\n",
|
||||||
|
" 1 \n",
|
||||||
|
" 10 \n",
|
||||||
|
" 100 \n",
|
||||||
|
"\n",
|
||||||
|
"A: Vector{Int64}\n",
|
||||||
|
" 1 \n",
|
||||||
|
" 2 \n",
|
||||||
|
"\n",
|
||||||
|
"after converting A to Float64: Vector{Float64}\n",
|
||||||
|
" 1.000\n",
|
||||||
|
" 2.000\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"x = [1.1;10.1;100.1]\n",
|
||||||
|
"println(\"x: \",typeof(x))\n",
|
||||||
|
"printmat(x)\n",
|
||||||
|
"\n",
|
||||||
|
"B_to_Int = round.(Int,x) #Float64 -> Int by rounding\n",
|
||||||
|
"println(\"rounding x to Int: \",typeof(B_to_Int))\n",
|
||||||
|
"printmat(B_to_Int)\n",
|
||||||
|
"\n",
|
||||||
|
"A = [1;2]\n",
|
||||||
|
"println(\"A: \",typeof(A))\n",
|
||||||
|
"printmat(A)\n",
|
||||||
|
"\n",
|
||||||
|
"A_to_Float64 = convert.(Float64,A) #Int -> Float64\n",
|
||||||
|
"println(\"after converting A to Float64: \",typeof(A_to_Float64))\n",
|
||||||
|
"printmat(A_to_Float64) #Float64.(A) also works"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Converting from Bools and BitArrays to Int and Vice Versa"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 8,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Vector{Int64}\n",
|
||||||
|
" 0 \n",
|
||||||
|
" 1 \n",
|
||||||
|
"\n",
|
||||||
|
"BitVector\n",
|
||||||
|
" 1 \n",
|
||||||
|
" 0 \n",
|
||||||
|
" 1 \n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"C = A .> 1.5\n",
|
||||||
|
"C_to_Int = convert.(Int,C) #BitArray -> Int\n",
|
||||||
|
"println(typeof(C_to_Int)) #Int.(C) also works\n",
|
||||||
|
"printmat(C_to_Int)\n",
|
||||||
|
"\n",
|
||||||
|
"D = [1;0;1]\n",
|
||||||
|
"D_to_Bool = convert.(Bool,D) #Int -> BitArray\n",
|
||||||
|
"println(typeof(D_to_Bool)) #Bool.(D) also works\n",
|
||||||
|
"printmat(D_to_Bool)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## From Bools and BitArrays to Int: A Tricky Case (extra)\n",
|
||||||
|
"\n",
|
||||||
|
"`false` is a \"strong zero\" in the sense that \n",
|
||||||
|
"`false*NaN == 0` and `false*Inf == 0`. \n",
|
||||||
|
"\n",
|
||||||
|
"If you do not want that behaviour in your code, transform `false` to 0 and then multiply."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 9,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"0.0\n",
|
||||||
|
"0.0\n",
|
||||||
|
"NaN\n",
|
||||||
|
"NaN\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"println(false*NaN)\n",
|
||||||
|
"println(false*Inf)\n",
|
||||||
|
"\n",
|
||||||
|
"println(convert(Int,false)*NaN)\n",
|
||||||
|
"println(convert(Int,false)*Inf)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Testing the Type\n",
|
||||||
|
"\n",
|
||||||
|
"The perhaps easiest way to test the type is by using the `isa(variable,Type)` function. The type can be a union of other types (see below for an example). \n",
|
||||||
|
"\n",
|
||||||
|
"Notice that an array has the type `Array` and more specifically, `Array{Float64}` if it is an array with Float64 numbers."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 10,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"1.2 is a Number: true\n",
|
||||||
|
"1.2 is an Int: false\n",
|
||||||
|
"1.2 is an Int or a Float64: true\n",
|
||||||
|
"[1.2, 1.3] is a Float64: false\n",
|
||||||
|
"[1.2, 1.3] is an Array: true\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"x = 1.2\n",
|
||||||
|
"z = [1.2,1.3]\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"$x is a Number: \",isa(x,Number))\n",
|
||||||
|
"println(\"$x is an Int: \",isa(x,Int))\n",
|
||||||
|
"println(\"$x is an Int or a Float64: \",isa(x,Union{Int,Float64}))\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"$z is a Float64: \",isa(z,Float64))\n",
|
||||||
|
"println(\"$z is an Array: \",isa(z,Array))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"# Type Instability (extra)\n",
|
||||||
|
"\n",
|
||||||
|
"Your code will often run faster if your variables do not change type in the computations. For instance, do *not* do something like this\n",
|
||||||
|
"```\n",
|
||||||
|
"x = 0 #instead, do x = 0.0\n",
|
||||||
|
"x = x + 0.1\n",
|
||||||
|
"```\n",
|
||||||
|
"\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"@webio": {
|
||||||
|
"lastCommId": null,
|
||||||
|
"lastKernelId": null
|
||||||
|
},
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Julia 1.6.0",
|
||||||
|
"language": "julia",
|
||||||
|
"name": "julia-1.6"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"file_extension": ".jl",
|
||||||
|
"mimetype": "application/julia",
|
||||||
|
"name": "julia",
|
||||||
|
"version": "1.6.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 2
|
||||||
|
}
|
337
JuliaTutorial-master/Tutorial_08_DaysAndDates.ipynb
Normal file
337
JuliaTutorial-master/Tutorial_08_DaysAndDates.ipynb
Normal file
|
@ -0,0 +1,337 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Days and Dates\n",
|
||||||
|
"\n",
|
||||||
|
"This notebook (a) introduces the Date type; (b) discusses how to convert from one date format (for instance, Excel and Matlab) to Julia dates; (c) and how to do date arithmetics."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Load Packages and Extra Functions"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"printyellow (generic function with 1 method)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"using Printf, Dates\n",
|
||||||
|
"\n",
|
||||||
|
"include(\"jlFiles/printmat.jl\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Building a Calendar"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Dates and day of the week\n",
|
||||||
|
"2014-01-31 5 \n",
|
||||||
|
"2014-02-28 5 \n",
|
||||||
|
"2014-03-31 1 \n",
|
||||||
|
"2014-04-30 3 \n",
|
||||||
|
"2014-05-31 6 \n",
|
||||||
|
"2014-06-30 1 \n",
|
||||||
|
"2014-07-31 4 \n",
|
||||||
|
"2014-08-31 7 \n",
|
||||||
|
"2014-09-30 2 \n",
|
||||||
|
"2014-10-31 5 \n",
|
||||||
|
"2014-11-30 7 \n",
|
||||||
|
"2014-12-31 3 \n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"dNb = Date(2014,1,31):Month(1):Date(2014,12,31) #build a monthly calendar\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"Dates and day of the week\")\n",
|
||||||
|
"printmat([dNb Dates.dayofweek.(dNb)])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Converting from Other Date Formats"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Converting from yyyymmdd\n",
|
||||||
|
"\n",
|
||||||
|
"Background: financial data is often downloaded as CSV files (eg. from finance.yahoo), where the date may look like 20160331. The next cell shows a simple way to create a Julia Date."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 3,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"2016-03-31\n",
|
||||||
|
"2016-04-01\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"csvDate = [20160331;20160401] #two dates\n",
|
||||||
|
"\n",
|
||||||
|
"jlDate = Date.(string.(csvDate),\"yyyymmdd\") #convert to string and then Julia Date\n",
|
||||||
|
"\n",
|
||||||
|
"printmat(jlDate)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Converting from DateTime (Excel) to Date\n",
|
||||||
|
"\n",
|
||||||
|
"Background: importing xls sheets with ExcelReaders gives DateTime, even if the sheets only contain dates (daily data, say)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"\n",
|
||||||
|
"DateTime and then converted to Date:\n",
|
||||||
|
"2016-03-31T00:00:00\n",
|
||||||
|
"2016-04-01T00:00:00\n",
|
||||||
|
"\n",
|
||||||
|
"2016-03-31\n",
|
||||||
|
"2016-04-01\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"xlsDate = [DateTime(2016,3,31);DateTime(2016,4,1)] #to be converted\n",
|
||||||
|
"\n",
|
||||||
|
"jlDate = Date.(xlsDate)\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"\\nDateTime and then converted to Date:\")\n",
|
||||||
|
"printmat(xlsDate)\n",
|
||||||
|
"printmat(jlDate)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Converting from Matlab's datenum to Date\n",
|
||||||
|
"\n",
|
||||||
|
"Background: in Matlab `datenum(2016,3,31)` gives 736420.0. In contrast, in Julia (which follows the ISO 8601 standard), `Dates.value(Date(2016,3,31))` gives 736054, which is 366 less (and it is an integer). A conversion is therefore required."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"\n",
|
||||||
|
"matlab datenum and correct Julia Date:\n",
|
||||||
|
" 736420.000 2016-03-31\n",
|
||||||
|
" 736421.000 2016-04-01\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"dNml = [736420.0;736421.0] #to be converted, 2016-03-31;2016-04-01\n",
|
||||||
|
"\n",
|
||||||
|
"jlDate = round.(Int,dNml) .- 366\n",
|
||||||
|
"jlDate = Date.(rata2datetime.(jlDate))\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"\\nmatlab datenum and correct Julia Date:\")\n",
|
||||||
|
"printmat([dNml jlDate],width=12)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"# Time Arithmetics\n",
|
||||||
|
"\n",
|
||||||
|
"You can add and subtract Dates from each other."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 6,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"difference between two dates: 30 days\n",
|
||||||
|
"as a fraction of the year: 0.082\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"d1 = Date(2016,3,31)\n",
|
||||||
|
"d2 = Date(2016,4,30)\n",
|
||||||
|
"\n",
|
||||||
|
"dif = d2 - d1 #count the number of days between d2 and d1\n",
|
||||||
|
"difRel = Dates.value(dif)/daysinyear(d1) #Dates.value() is the datenumber, needs prefix Dates\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"difference between two dates: \",dif)\n",
|
||||||
|
"printlnPs(\"as a fraction of the year: \",difRel)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 7,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"d1 and one month later: 2016-03-31 2016-04-30\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"d3 = d1 + Month(1) #one month after d1\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"d1 and one month later: \",d1,\" \",d3)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Looking up Day of the Week and More"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 8,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"day of the week of date: 2016-03-31 4\n",
|
||||||
|
"day of the year of date: 2016-03-31 91\n",
|
||||||
|
"\n",
|
||||||
|
"Splitting up a date: 2016 3 31\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"println(\"day of the week of date: \",d1,\" \",dayofweek(d1))\n",
|
||||||
|
"println(\"day of the year of date: \",d1,\" \",dayofyear(d1))\n",
|
||||||
|
"\n",
|
||||||
|
"(y,m,d)= yearmonthday(d1) #splitting up a date\n",
|
||||||
|
"println(\"\\nSplitting up a date: \",y,\" \",m,\" \",d)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Printing a Date\n",
|
||||||
|
"\n",
|
||||||
|
"with your own formatting (see the manual for many other formatting options)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 9,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"31 Mar 2016\n",
|
||||||
|
"31-03-2016\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"println(Dates.format(d1,\"d u yyyy\")) #needs prefix Dates\n",
|
||||||
|
"println(Dates.format(d1,\"dd-mm-yyyy\"))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"@webio": {
|
||||||
|
"lastCommId": null,
|
||||||
|
"lastKernelId": null
|
||||||
|
},
|
||||||
|
"anaconda-cloud": {},
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Julia 1.6.2",
|
||||||
|
"language": "julia",
|
||||||
|
"name": "julia-1.6"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"file_extension": ".jl",
|
||||||
|
"mimetype": "application/julia",
|
||||||
|
"name": "julia",
|
||||||
|
"version": "1.6.2"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 1
|
||||||
|
}
|
616
JuliaTutorial-master/Tutorial_09_LoadSaveData.ipynb
Normal file
616
JuliaTutorial-master/Tutorial_09_LoadSaveData.ipynb
Normal file
|
@ -0,0 +1,616 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Loading and Saving Data\n",
|
||||||
|
"\n",
|
||||||
|
"to/from csv, hdf5, jld2, mat and xlsx files."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Load Packages and Extra Functions\n",
|
||||||
|
"\n",
|
||||||
|
"The packages are loaded in the respective sections below. This allows you to run parts of this notebook without having to install all packages.\n",
|
||||||
|
"\n",
|
||||||
|
"The data files created by this notebook are written to and loaded from the subfolder \"Results\"."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"using Printf, Dates\n",
|
||||||
|
"include(\"jlFiles/printmat.jl\")\n",
|
||||||
|
"\n",
|
||||||
|
"if !isdir(\"Results\")\n",
|
||||||
|
" error(\"create the subfolder Results before running this program\")\n",
|
||||||
|
"end"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Loading a csv File\n",
|
||||||
|
"\n",
|
||||||
|
"The csv (\"comma-separated values\") format provides a simple and robust method for moving data, and it can be read by most software.\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"For instance, for *reading* a data file delimited by comma (,) and where the first line of the file contains variable names, then use the following\n",
|
||||||
|
"```\n",
|
||||||
|
"(x,header) = readdlm(FileName,',',header=true)\n",
|
||||||
|
"```\n",
|
||||||
|
"Alternatively, use\n",
|
||||||
|
"```\n",
|
||||||
|
"x = readdlm(FileName,',',skipstart=1)\n",
|
||||||
|
"```\n",
|
||||||
|
"to disregard the first line.\n",
|
||||||
|
"\n",
|
||||||
|
"Extra arguments control the type of data (Float64, Int, etc), suppression of comment lines and more.\n",
|
||||||
|
"\n",
|
||||||
|
"If you need more powerful write/read routines, try the [CSV package](https://github.com/JuliaData/CSV.jl)."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"the raw contents of Data/CsvFile.csv:\n",
|
||||||
|
"\n",
|
||||||
|
"X,Y,Z\n",
|
||||||
|
"1.1,1.2,1.3\n",
|
||||||
|
"2.1,2.2,2.3\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"println(\"the raw contents of Data/CsvFile.csv:\\n\")\n",
|
||||||
|
"println(read(\"Data/CsvFile.csv\",String))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 3,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"header of csv file:\n",
|
||||||
|
" X Y Z\n",
|
||||||
|
"\n",
|
||||||
|
"x:\n",
|
||||||
|
" 1.100 1.200 1.300\n",
|
||||||
|
" 2.100 2.200 2.300\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"using DelimitedFiles\n",
|
||||||
|
"\n",
|
||||||
|
"(x,header) = readdlm(\"Data/CsvFile.csv\",',',header=true) #read csv file\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"header of csv file:\")\n",
|
||||||
|
"printmat(header)\n",
|
||||||
|
"println(\"x:\")\n",
|
||||||
|
"printmat(x)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Saving a csv File\n",
|
||||||
|
"\n",
|
||||||
|
"To *write* csv data, the simplest approach is to create the matrix you want to save and then run\n",
|
||||||
|
"```\n",
|
||||||
|
"writedlm(FileName,matrix)\n",
|
||||||
|
"```\n",
|
||||||
|
"Alternatively, to write several matrices to the file (without having to first combine them), use\n",
|
||||||
|
"```\n",
|
||||||
|
"fh = open(Filename, \"w\")\n",
|
||||||
|
" writedlm(fh,matrix1,',')\n",
|
||||||
|
" writedlm(fh,matrix2,',')\n",
|
||||||
|
"close(fh)\n",
|
||||||
|
"```"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"\u001b[34m\u001b[1mNewCsvFile.csv has been created in the subfolder Results. Check it out.\u001b[22m\u001b[39m\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"x = [1.1 1.2 1.3;\n",
|
||||||
|
" 2.1 2.2 2.3]\n",
|
||||||
|
"header = [\"X\" \"Y\" \"Z\"]\n",
|
||||||
|
"\n",
|
||||||
|
"xx = [header; x] #to save \n",
|
||||||
|
"writedlm(\"Results/NewCsvFile.csv\",xx,',') #write csv file\n",
|
||||||
|
"\n",
|
||||||
|
"printblue(\"NewCsvFile.csv has been created in the subfolder Results. Check it out.\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Loading csv with Dates and Missing Values (extra)\n",
|
||||||
|
"\n",
|
||||||
|
"The next cells show how to load a csv files with dates (for instance, 15/01/1979) and some missing values. \n",
|
||||||
|
"\n",
|
||||||
|
"The code does the following\n",
|
||||||
|
"\n",
|
||||||
|
"1. reads the csv file\n",
|
||||||
|
"2. converts `x2[:,1]` to Date,\n",
|
||||||
|
"3. finds all elements in `x = x2[:,2:end]` that are not numbers and converts them to `NaN` (use with `Float64`) or `missing` (use when data is not `Float64`)."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"readdlmFix"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 5,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"\"\"\"\n",
|
||||||
|
" readdlmFix(x,Typ=Float64,missVal=NaN)\n",
|
||||||
|
"\n",
|
||||||
|
"Change elements with missing data (' ') to either NaN or missing. \n",
|
||||||
|
"`x` is the input matrix, `Typ` is the type of the output (Float64, Int, etc) and \n",
|
||||||
|
"`missval` is your choice of either `NaN` or `missing`\n",
|
||||||
|
"\"\"\"\n",
|
||||||
|
"function readdlmFix(x,Typ=Float64,missVal=NaN)\n",
|
||||||
|
" y = replace(z->!isa(z,Number) ? missVal : z,x)\n",
|
||||||
|
" ismissing(missVal) && (Typ = Union{Missing,Typ}) #allow missing\n",
|
||||||
|
" y = convert.(Typ,y)\n",
|
||||||
|
" return y\n",
|
||||||
|
"end"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 6,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"dates and data (first 4 obs):\n",
|
||||||
|
"1979-01-02 96.730 \n",
|
||||||
|
"1979-01-03 9.310\n",
|
||||||
|
"1979-01-04 98.580 9.310\n",
|
||||||
|
"1979-01-05 99.130 9.340\n",
|
||||||
|
"\n",
|
||||||
|
"after fix of missing data (first 4 obs):\n",
|
||||||
|
"1979-01-02 96.730 NaN\n",
|
||||||
|
"1979-01-03 NaN 9.310\n",
|
||||||
|
"1979-01-04 98.580 9.310\n",
|
||||||
|
"1979-01-05 99.130 9.340\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"x2 = readdlm(\"Data/CsvFileWithDates.csv\",',',skipstart=1)\n",
|
||||||
|
"dN = Date.(x2[:,1],\"d/m/y\") #to Date, \"d/m/y\" is the date format in the file\n",
|
||||||
|
"x = x2[:,2:end] #the data, but Any[] since missing data\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"dates and data (first 4 obs):\")\n",
|
||||||
|
"printmat([dN[1:4] x[1:4,:]])\n",
|
||||||
|
"\n",
|
||||||
|
"x = readdlmFix(x)\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"after fix of missing data (first 4 obs):\")\n",
|
||||||
|
"printmat([dN[1:4] x[1:4,:]])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Loading and Saving hdf5\n",
|
||||||
|
"\n",
|
||||||
|
"hdf5 files are used in many computer languages. They can store different types of data: integers, floats, strings (but not Julia Dates).\n",
|
||||||
|
"\n",
|
||||||
|
"The basic syntax of the [HDF5 package](https://github.com/JuliaIO/HDF5.jl) is \n",
|
||||||
|
"```\n",
|
||||||
|
"fh = h5open(FileName,\"r\") #open for reading\n",
|
||||||
|
" (x,y) = read(fh,\"x\",\"y\")\n",
|
||||||
|
"close(fh)\n",
|
||||||
|
"\n",
|
||||||
|
"fh = h5open(FileName,\"w\") #open for writing\n",
|
||||||
|
" write(fh,\"x\",x,\"y\",y)\n",
|
||||||
|
"close(fh)\n",
|
||||||
|
"```\n",
|
||||||
|
"\n",
|
||||||
|
"To save dates, save either a matrix `[y m d]` (see eg. `Dates.month(date)`) or a date value (see eg. `Dates.value(date)`). \n",
|
||||||
|
"\n",
|
||||||
|
"The [HDFVIEW](https://support.hdfgroup.org/products/java/hdfview/) program allows you to look at the contents of a hdf5 file. (It is not needed here.)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 7,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"\n",
|
||||||
|
"Variables in h5 file: [\"B\", \"C\", \"x\", \"ymd\"]\n",
|
||||||
|
"\n",
|
||||||
|
"dates and x from h5 file is\n",
|
||||||
|
"2019-05-14 1.100 1.200 1.300\n",
|
||||||
|
"2019-05-15 2.100 2.200 2.300\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"using HDF5\n",
|
||||||
|
"\n",
|
||||||
|
"fh = h5open(\"Data/H5File.h5\",\"r\") #open for reading\n",
|
||||||
|
" println(\"\\nVariables in h5 file: \",keys(fh))\n",
|
||||||
|
" (x,B,ymd) = read(fh,\"x\",\"B\",\"ymd\") #load some of the data\n",
|
||||||
|
"close(fh)\n",
|
||||||
|
"\n",
|
||||||
|
"dN = Date.(ymd[:,1],ymd[:,2],ymd[:,3]) #reconstructing dates\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"\\ndates and x from h5 file is\")\n",
|
||||||
|
"printmat([dN x])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 8,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"NewH5File.h5 has been created in the subfolder Results\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"x = [1.1 1.2 1.3;\n",
|
||||||
|
" 2.1 2.2 2.3]\n",
|
||||||
|
"ymd = [2019 5 14;\n",
|
||||||
|
" 2019 5 15]\n",
|
||||||
|
"B = 1\n",
|
||||||
|
"C = \"Nice cat\"\n",
|
||||||
|
"\n",
|
||||||
|
"fh = h5open(\"Results/NewH5File.h5\",\"w\") #open file for writing\n",
|
||||||
|
" write(fh,\"x\",x,\"ymd\",ymd,\"B\",B,\"C\",C)\n",
|
||||||
|
"close(fh) #close file\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"NewH5File.h5 has been created in the subfolder Results\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Loading and Saving jld2\n",
|
||||||
|
"\n",
|
||||||
|
"jld2 files can store very different types of data: integers, floats, strings, dictionaries, etc. It is a dialect of hdf5, designed to save different Julia objects (including Dates). \n",
|
||||||
|
"\n",
|
||||||
|
"The basic syntax of the [JLD2 package](https://github.com/JuliaIO/JLD2.jl) is \n",
|
||||||
|
"```\n",
|
||||||
|
"(A,B) = load(FileName,\"A\",\"B\") #load some data\n",
|
||||||
|
"xx = load(FileName) #load all data into a Dict()\n",
|
||||||
|
"save(FileName,\"A\",A,\"B\",B) #save data\n",
|
||||||
|
"```\n",
|
||||||
|
"(It also possible to use the same syntax as for HDF5, except that we use ```jldopen``` instead of ```h5open```.)\n",
|
||||||
|
"\n",
|
||||||
|
"The [JLD package](https://github.com/JuliaIO/JLD.jl) is an an older alternative. It uses a similar (often the same) syntax. To use it, change the first line to `using JLD` and then also change `jld2` to `jld` everywhere in the cells below."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 9,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"The variables are: [\"B\", \"C\", \"x\", \"d\"]\n",
|
||||||
|
"\n",
|
||||||
|
"dates and x from jld2 file is\n",
|
||||||
|
"2019-05-14 1.100 1.200 1.300\n",
|
||||||
|
"2019-05-15 2.100 2.200 2.300\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"using FileIO, JLD2\n",
|
||||||
|
"\n",
|
||||||
|
"xx = load(\"Data/JldFile.jld2\") #load entire file\n",
|
||||||
|
"println(\"The variables are: \",keys(xx)) #list contents of the file \n",
|
||||||
|
" \n",
|
||||||
|
"(x,dN) = load(\"Data/JldFile.jld2\",\"x\",\"d\") #read some of the data\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"\\ndates and x from jld2 file is\")\n",
|
||||||
|
"printmat([dN x])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 10,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"NewJldFile.jld2 has been created in the subfolder Results\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"x = [1.1 1.2 1.3;\n",
|
||||||
|
" 2.1 2.2 2.3]\n",
|
||||||
|
"d = [Date(2019,5,14); #Julia dates\n",
|
||||||
|
" Date(2019,5,15)]\n",
|
||||||
|
"B = 1\n",
|
||||||
|
"C = \"Nice cat\"\n",
|
||||||
|
"\n",
|
||||||
|
"save(\"Results/NewJldFile.jld2\",\"x\",x,\"d\",d,\"B\",B,\"C\",C) #write jld2 file\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"NewJldFile.jld2 has been created in the subfolder Results\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Saving and Loading Matlab mat files (extra)\n",
|
||||||
|
"\n",
|
||||||
|
"The [MAT package](https://github.com/JuliaIO/MAT.jl) allows you to load/save (Matlab) mat files (which is a dialect of HDF5)."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 11,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"\n",
|
||||||
|
"Variables in mat file: [\"B\", \"C\", \"dM\", \"x\"]\n",
|
||||||
|
"\n",
|
||||||
|
"dates and x from mat file is\n",
|
||||||
|
"2019-05-14 1.100 1.200 1.300\n",
|
||||||
|
"2019-05-15 2.100 2.200 2.300\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"using MAT\n",
|
||||||
|
"\n",
|
||||||
|
"function DateMLtoDate(dNum) #Matlab datenum to Julia date\n",
|
||||||
|
" dNum = round.(Int,dNum) .- 366\n",
|
||||||
|
" dNTime = Dates.rata2datetime.(dNum)\n",
|
||||||
|
" dN = Date.(dNTime)\n",
|
||||||
|
" return dN\n",
|
||||||
|
"end\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"fh = matopen(\"Data/MatFile.mat\")\n",
|
||||||
|
" println(\"\\nVariables in mat file: \",names(fh))\n",
|
||||||
|
" (x,dM) = read(fh,\"x\",\"dM\")\n",
|
||||||
|
"close(fh) \n",
|
||||||
|
"\n",
|
||||||
|
"d = DateMLtoDate(dM) #Matlab datenum to Julia date\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"\\ndates and x from mat file is\")\n",
|
||||||
|
"printmat([d x])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 12,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"\n",
|
||||||
|
"NewMatFile.mat has been created in the subfolder Results\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"x = [1.1 1.2 1.3;\n",
|
||||||
|
" 2.1 2.2 2.3]\n",
|
||||||
|
"d = [Date(2019,5,14); #Julia dates\n",
|
||||||
|
" Date(2019,5,15)]\n",
|
||||||
|
"B = 1\n",
|
||||||
|
"C = \"Nice cat\"\n",
|
||||||
|
"\n",
|
||||||
|
"dM = Dates.value.(d) .+ 366.0 #Julia Date to Matlab's datenum(), Float64\n",
|
||||||
|
"\n",
|
||||||
|
"fh = matopen(\"Results/NewMatFile.mat\",\"w\")\n",
|
||||||
|
" write(fh,\"x\",x) #write one variable at a time\n",
|
||||||
|
" write(fh,\"B\",B)\n",
|
||||||
|
" write(fh,\"dM\",dM)\n",
|
||||||
|
" write(fh,\"C\",C)\n",
|
||||||
|
"close(fh)\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"\\nNewMatFile.mat has been created in the subfolder Results\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Loading an xls File\n",
|
||||||
|
"\n",
|
||||||
|
"The [XLSX package](https://github.com/felipenoris/XLSX.jl) allows you to read and write xls (and xlsx) files. \n",
|
||||||
|
"\n",
|
||||||
|
"As an alternative, you can use [ExcelReaders](https://github.com/davidanthoff/ExcelReaders.jl), which requires python and python's xlrd libarary. For instance, this would work\n",
|
||||||
|
"```\n",
|
||||||
|
"using ExcelReaders\n",
|
||||||
|
"data1 = readxl(\"Data/XlsFile.xlsx\",\"Data!B2:C11\")\n",
|
||||||
|
"x1 = convert.(Float64,data1) \n",
|
||||||
|
"printmat(x1)\n",
|
||||||
|
"```"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 13,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"part of the xlsx file:\n",
|
||||||
|
" 16.660 -999.990\n",
|
||||||
|
" 16.850 -999.990\n",
|
||||||
|
" 16.930 -999.990\n",
|
||||||
|
" 16.980 -999.990\n",
|
||||||
|
" 17.080 -999.990\n",
|
||||||
|
" 17.030 7.000\n",
|
||||||
|
" 17.090 8.000\n",
|
||||||
|
" 16.760 -999.990\n",
|
||||||
|
" 16.670 -999.990\n",
|
||||||
|
" 16.720 -999.990\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"using XLSX\n",
|
||||||
|
"\n",
|
||||||
|
"data1 = XLSX.readxlsx(\"Data/XlsFile.xlsx\") #reading the entire file\n",
|
||||||
|
"x1 = data1[\"Data!B2:C11\"] #extracting a part of the sheet \"Data\"\n",
|
||||||
|
"x1 = convert.(Float64,x1) #converting from Any to Float64\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"part of the xlsx file:\")\n",
|
||||||
|
"printmat(x1)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Creating Variables from Variable Names (extra)\n",
|
||||||
|
"\n",
|
||||||
|
"Suppose you have \n",
|
||||||
|
"\n",
|
||||||
|
"1. a matrix `x` with data\n",
|
||||||
|
"2. a list of the variable names for each column of `x`, for instance, from `header` in a CSV file or saved as a vector of strings in a hdf5/mat/jld2 file.\n",
|
||||||
|
"\n",
|
||||||
|
"If there are few variables, then you can manually create each of them from the loaded matrix. This becomes tedious when there are many variables.\n",
|
||||||
|
"\n",
|
||||||
|
"However, it is easy to create a Dict() or NamedTuple which can be used to easily refer to the variable names, for instance, `D[:X]` or `N.X` to get variable `X`."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 14,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"x[:,1], X, D[:X] and N.X\n",
|
||||||
|
"\n",
|
||||||
|
" x[:,1] X D[:X] N.X\n",
|
||||||
|
" 1.100 1.100 1.100 1.100\n",
|
||||||
|
" 2.100 2.100 2.100 2.100\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"using DelimitedFiles\n",
|
||||||
|
"(x,header) = readdlm(\"Data/CsvFile.csv\",',',header=true)\n",
|
||||||
|
"n = size(x,2)\n",
|
||||||
|
"\n",
|
||||||
|
"(X,Y,Z) = [x[:,i] for i=1:n] #manually creating X,Y,Z\n",
|
||||||
|
"\n",
|
||||||
|
"D = Dict([(Symbol(header[i]),x[:,i]) for i=1:n]) #Creating D with :X,:Y,:Z\n",
|
||||||
|
"\n",
|
||||||
|
"namesB = tuple(Symbol.(header)...) #a tuple (:X,:Y,:Z)\n",
|
||||||
|
"N = NamedTuple{namesB}([x[:,i] for i=1:n]) #NamedTuple with N.X, N.Y and N.Z\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"x[:,1], X, D[:X] and N.X\\n\")\n",
|
||||||
|
"printmat([x[:,1] X D[:X] N.X],colNames=[\"x[:,1]\",\"X\",\"D[:X]\",\"N.X\"])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"@webio": {
|
||||||
|
"lastCommId": null,
|
||||||
|
"lastKernelId": null
|
||||||
|
},
|
||||||
|
"anaconda-cloud": {},
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Julia 1.6.1",
|
||||||
|
"language": "julia",
|
||||||
|
"name": "julia-1.6"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"file_extension": ".jl",
|
||||||
|
"mimetype": "application/julia",
|
||||||
|
"name": "julia",
|
||||||
|
"version": "1.6.1"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 1
|
||||||
|
}
|
486
JuliaTutorial-master/Tutorial_10_Strings.ipynb
Normal file
486
JuliaTutorial-master/Tutorial_10_Strings.ipynb
Normal file
|
@ -0,0 +1,486 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Strings\n",
|
||||||
|
"\n",
|
||||||
|
"This notebook demonstrates some basic string commands."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Load Packages and Extra Functions"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"printyellow (generic function with 1 method)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"using Printf\n",
|
||||||
|
"\n",
|
||||||
|
"include(\"jlFiles/printmat.jl\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# String Basics\n",
|
||||||
|
"\n",
|
||||||
|
"The next few cells show how to\n",
|
||||||
|
"\n",
|
||||||
|
"1. combine several strings into one string by `string(str1,str2)` or `str1 * str2`.\n",
|
||||||
|
"\n",
|
||||||
|
"2. test if a string contains a specific substring\n",
|
||||||
|
"\n",
|
||||||
|
"3. replace part of a string with something else\n",
|
||||||
|
"\n",
|
||||||
|
"4. split a string into a vector of words (and then to join them back into a string again)\n",
|
||||||
|
"\n",
|
||||||
|
"5. sort a vector of words in alphabetical order"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Hello world!\n",
|
||||||
|
"Where are you?\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"str1 = \"Hello\"\n",
|
||||||
|
"str2 = \"world!\\n\"\n",
|
||||||
|
"str3 = \"Where are you?\"\n",
|
||||||
|
"\n",
|
||||||
|
"str3b = string(str1,\" \",str2,str3) #combine into one string\n",
|
||||||
|
"println(str3b)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 3,
|
||||||
|
"metadata": {
|
||||||
|
"scrolled": true
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Highway 62 Revisited\n",
|
||||||
|
"\u001b[34m\u001b[1mcontains the word Highway\u001b[22m\u001b[39m\n",
|
||||||
|
"\n",
|
||||||
|
"\u001b[34m\u001b[1mNew, better string after a replacement: \u001b[22m\u001b[39m\n",
|
||||||
|
"Highway 61 Revisited\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"str4 = \"Highway 62 Revisited\"\n",
|
||||||
|
"\n",
|
||||||
|
"if occursin(\"Highway\",str4)\n",
|
||||||
|
" println(str4)\n",
|
||||||
|
" printblue(\"contains the word Highway\")\n",
|
||||||
|
"end\n",
|
||||||
|
"\n",
|
||||||
|
"str4 = replace(str4,\"62\" => \"61\")\n",
|
||||||
|
"printblue(\"\\nNew, better string after a replacement: \")\n",
|
||||||
|
"println(str4)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"\u001b[34m\u001b[1msplit a string into a vector of words:\u001b[22m\u001b[39m\n",
|
||||||
|
" Highway\n",
|
||||||
|
" 61\n",
|
||||||
|
" Revisited\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"\u001b[34m\u001b[1mand join the words again into a string:\u001b[22m\u001b[39m\n",
|
||||||
|
"Highway 61 Revisited\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"words = split(str4)\n",
|
||||||
|
"printblue(\"split a string into a vector of words:\")\n",
|
||||||
|
"printmat(words)\n",
|
||||||
|
"\n",
|
||||||
|
"printblue(\"\\nand join the words again into a string:\")\n",
|
||||||
|
"println(join(words,\" \"))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"\u001b[34m\u001b[1msort the words alphabetically:\u001b[22m\u001b[39m\n",
|
||||||
|
" 61\n",
|
||||||
|
" Highway\n",
|
||||||
|
" Revisited\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"printblue(\"sort the words alphabetically:\")\n",
|
||||||
|
"printmat(sort(words,lt=isless))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Reading an Entire File as a String\n",
|
||||||
|
"\n",
|
||||||
|
"The next cell reads a file into one single string. It keeps the formatting (spaces, line breaks etc). "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 6,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"txtFile = \"Data/FileWithText.txt\"\n",
|
||||||
|
"\n",
|
||||||
|
"fh1 = open(txtFile) #open the file, can then refer to it as fh1\n",
|
||||||
|
" str = read(fh1,String) #read as string\n",
|
||||||
|
"close(fh1)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 7,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"String\n",
|
||||||
|
"\n",
|
||||||
|
"Dogs are nicer\n",
|
||||||
|
"than cats.\n",
|
||||||
|
" \n",
|
||||||
|
" This\n",
|
||||||
|
" is a\n",
|
||||||
|
"fairly short file.\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"println(typeof(str),\"\\n\")\n",
|
||||||
|
"\n",
|
||||||
|
"println(str) #Printing the string read from a file"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Reading all Lines of a File into an Array of Strings\n",
|
||||||
|
"\n",
|
||||||
|
"The next cell reads a file into an array of strings: one string per line of the file. The second cell joins the lines into one string."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 8,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Dogs are nicer\n",
|
||||||
|
"than cats.\n",
|
||||||
|
" \n",
|
||||||
|
" This\n",
|
||||||
|
" is a\n",
|
||||||
|
"fairly short file.\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"fh2 = open(txtFile)\n",
|
||||||
|
" lines = readlines(fh2)\n",
|
||||||
|
"close(fh2)\n",
|
||||||
|
"\n",
|
||||||
|
"printmat(lines)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 9,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Dogs are nicer\n",
|
||||||
|
"than cats.\n",
|
||||||
|
" \n",
|
||||||
|
" This\n",
|
||||||
|
" is a\n",
|
||||||
|
"fairly short file.\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"linesJoined = join(lines,\"\\n\") #join the lines of the array,\n",
|
||||||
|
"println(linesJoined) # \"\\n\" to create line breaks"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Strings and Indexing (extra)\n",
|
||||||
|
"\n",
|
||||||
|
"can be tricky when the string contains non-ascii characters.\n",
|
||||||
|
"\n",
|
||||||
|
"Notice that you cannot change a string by indexing. For instance, `str[1] = \"D\"` does not work. However, you can *read* strings by indexing, if you are careful.\n",
|
||||||
|
"\n",
|
||||||
|
"The next cell gives two versions of a string. Try running the subsequent cells for both versions."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 10,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"\"Dx = -0.9x\""
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 10,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"str = \"Dx = -0.9x\"\n",
|
||||||
|
"#str = \"Δx = -0.9x\" #uncomment this and re-run the cells below"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 11,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"D\n",
|
||||||
|
"x\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"println(str[1]) #works\n",
|
||||||
|
"println(str[2]) #might not work, depending on the contents of string"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"If `str[2]` does not work (it does not if the first character is Δ), then that is due to the fact that the first character takes more than one byte to store. Julia has commands to get around this. For instance, see the next cell."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 12,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"x\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
" #this should work in all cases\n",
|
||||||
|
"println(str[nextind(str,1)]) #nextind() gives the starting point of the next character"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Looping over All Characters in a String\n",
|
||||||
|
"\n",
|
||||||
|
"in a way that works even if there are some non-ascii characters."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 13,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"1 D\n",
|
||||||
|
"2 x\n",
|
||||||
|
"3 \n",
|
||||||
|
"4 =\n",
|
||||||
|
"5 \n",
|
||||||
|
"6 -\n",
|
||||||
|
"7 0\n",
|
||||||
|
"8 .\n",
|
||||||
|
"9 9\n",
|
||||||
|
"10 x\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"i = 1\n",
|
||||||
|
"for c in str #alternatively, while i <= lastindex(str)\n",
|
||||||
|
" #global i #only needed in script \n",
|
||||||
|
" println(i,\" \",c)\n",
|
||||||
|
" i = nextind(str,i) #nextind() gives the starting point of the next character\n",
|
||||||
|
"end"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Creating a Long String (extra)\n",
|
||||||
|
"\n",
|
||||||
|
"can be done with `string()`, but it is often quicker to write to an `IOBuffer()`. \n",
|
||||||
|
"\n",
|
||||||
|
"Both approaches are demonstrated below by combining a vector of words into a string. (This is just meant as an illustration since `join(txt,\" \")` would here be a better way to achieve the same thing.)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 14,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"txt = [\"The\",\"highway\",\"is\",\"for\",\"gamblers,\",\"better\",\"use\",\"your\",\"sense\\n\", #a vector of words from a song\n",
|
||||||
|
" \"Take\",\"what\",\"you\",\"have\",\"gathered\",\"from\",\"coincidence\"];"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 15,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
" The highway is for gamblers, better use your sense\n",
|
||||||
|
" Take what you have gathered from coincidence\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"BabyBlue1 = \"\" #an empty string\n",
|
||||||
|
"for i = 1:length(txt)\n",
|
||||||
|
" #global BabyBlue1 #only needed in script\n",
|
||||||
|
" BabyBlue1 = string(BabyBlue1,\" \",txt[i]) #add to the string\n",
|
||||||
|
"end\n",
|
||||||
|
"println(BabyBlue1)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 16,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
" The highway is for gamblers, better use your sense\n",
|
||||||
|
" Take what you have gathered from coincidence\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"iob = IOBuffer() #an IOBuffer\n",
|
||||||
|
"for i = 1:length(txt)\n",
|
||||||
|
" write(iob,\" \",txt[i]) #write to the buffer\n",
|
||||||
|
"end\n",
|
||||||
|
"BabyBlue2 = String(take!(iob)) #convert to a string\n",
|
||||||
|
"println(BabyBlue2)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"@webio": {
|
||||||
|
"lastCommId": null,
|
||||||
|
"lastKernelId": null
|
||||||
|
},
|
||||||
|
"anaconda-cloud": {},
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Julia 1.6.1",
|
||||||
|
"language": "julia",
|
||||||
|
"name": "julia-1.6"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"file_extension": ".jl",
|
||||||
|
"mimetype": "application/julia",
|
||||||
|
"name": "julia",
|
||||||
|
"version": "1.6.1"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 1
|
||||||
|
}
|
410
JuliaTutorial-master/Tutorial_11_Printing.ipynb
Normal file
410
JuliaTutorial-master/Tutorial_11_Printing.ipynb
Normal file
|
@ -0,0 +1,410 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Printing\n",
|
||||||
|
"\n",
|
||||||
|
"This notebook is focused on how to print numbers with formatting (width, number of digits shown, etc)."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Load Packages and Extra Functions"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"printTeXTable (generic function with 2 methods)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"using Printf\n",
|
||||||
|
"\n",
|
||||||
|
"include(\"jlFiles/printmat.jl\")\n",
|
||||||
|
"include(\"jlFiles/printTeXTable.jl\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Basic Printing\n",
|
||||||
|
"\n",
|
||||||
|
"is done with `println()` or `display()`."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"[11.11 12.12; 21.0 22.0]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"x = [11.11 12.12;21 22] #a matrix to print\n",
|
||||||
|
"\n",
|
||||||
|
"println(x)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 3,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"2×2 Matrix{Float64}:\n",
|
||||||
|
" 11.11 12.12\n",
|
||||||
|
" 21.0 22.0"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "display_data"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"display(x) #gives basic formatting"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## `printmat()`\n",
|
||||||
|
"\n",
|
||||||
|
"My simple function `printmat()` allows basic formatting and `printTeXTable()` prints a simple LaTeX table."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
" 11.11 12.12\n",
|
||||||
|
" 21.00 22.00\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"printmat(x,width=7,prec=2)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
" A B\n",
|
||||||
|
"row 1 11.11 12.12\n",
|
||||||
|
"row 2 21.00 22.00\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"colNames = [\"A\" \"B\"]\n",
|
||||||
|
"rowNames = [\"row 1\",\"row 2\"]\n",
|
||||||
|
"printmat(x,colNames=colNames,rowNames=rowNames,width=7,prec=2)\n",
|
||||||
|
"#printmat(x;colNames,rowNames,width=7,prec=2) #this works too, notice the ;"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 6,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"\\begin{table}\n",
|
||||||
|
" \\begin{tabular}{lrr}\n",
|
||||||
|
" & A & B \\\\ \\hline \n",
|
||||||
|
" row 1 & 11.11 & 12.12 \\\\ \n",
|
||||||
|
" row 2 & 21.00 & 22.00 \\\\ \n",
|
||||||
|
" \\hline\n",
|
||||||
|
" \\end{tabular}\n",
|
||||||
|
"\\end{table}\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"str = printTeXTable(x,colNames=colNames,rowNames=rowNames,width=7,prec=2);"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## The PrettyTables.jl Package\n",
|
||||||
|
"\n",
|
||||||
|
"The [PrettyTables.jl](https://github.com/ronisbr/PrettyTables.jl) package provides powerful methods for formatted printing, including printing to LaTeX.\n",
|
||||||
|
"\n",
|
||||||
|
"Try also `tf=tf_borderless`, `tf=tf_ascii_dots` or `tf=tf_unicode` (the default) as a keyword argument to modify the table border/lines.\n",
|
||||||
|
"\n",
|
||||||
|
"To get a witdh of 10 and 3 digits, change to `ft_printf(\"%10.3f\")`."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 7,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
" ------- ------- -------\n",
|
||||||
|
" \u001b[1m \u001b[0m \u001b[1m A \u001b[0m \u001b[1m B \u001b[0m\n",
|
||||||
|
" ------- ------- -------\n",
|
||||||
|
" \u001b[1m row 1 \u001b[0m 11.11 12.12\n",
|
||||||
|
" \u001b[1m row 2 \u001b[0m 21.00 22.00\n",
|
||||||
|
" ------- ------- -------\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"using PrettyTables\n",
|
||||||
|
"\n",
|
||||||
|
"pretty_table(x,colNames,row_names = rowNames,tf=tf_compact,formatters=ft_printf(\"%5.2f\")) "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 8,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"\\begin{table}\n",
|
||||||
|
" \\begin{tabular}{rr}\n",
|
||||||
|
" \\hline\\hline\n",
|
||||||
|
" \\textbf{A} & \\textbf{B} \\\\\\hline\n",
|
||||||
|
" 11.11 & 12.12 \\\\\n",
|
||||||
|
" 21.00 & 22.00 \\\\\\hline\\hline\n",
|
||||||
|
" \\end{tabular}\n",
|
||||||
|
"\\end{table}\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"pretty_table(x,colNames,formatters=ft_printf(\"%5.2f\"),backend=:latex) #LaTeX table"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Printing Output to a Text File\n",
|
||||||
|
"\n",
|
||||||
|
"is simple. You `open()` the file, write to it and then `close()` it. The next cells demonstrate this."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 9,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"NewTxtFile.txt has been created in the subfolder Results. It's still empty.\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"fh = open(\"Results/NewTxtFile.txt\", \"w\") #open the file, \"w\" for writing\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"NewTxtFile.txt has been created in the subfolder Results. It's still empty.\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 10,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"println(fh,\"Dogs are \") #printing to the file, notice the fh \n",
|
||||||
|
"println(fh,\"nicer than cats.\\n\")\n",
|
||||||
|
"\n",
|
||||||
|
"printmat(fh,x,prec=2) #to pretty print the matrix\n",
|
||||||
|
"printmat(fh,x,colNames=colNames,rowNames=rowNames,prec=2)\n",
|
||||||
|
"printTeXTable(fh,x,colNames=colNames,rowNames=rowNames,prec=2)\n",
|
||||||
|
"\n",
|
||||||
|
"pretty_table(fh,x,colNames,row_names=rowNames,formatters=ft_printf(\"%5.2f\"))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 11,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Results/NewTxtFile.txt has been closed. Check it out. The raw contents are:\n",
|
||||||
|
"\n",
|
||||||
|
"Dogs are \n",
|
||||||
|
"nicer than cats.\n",
|
||||||
|
"\n",
|
||||||
|
" 11.11 12.12\n",
|
||||||
|
" 21.00 22.00\n",
|
||||||
|
"\n",
|
||||||
|
" A B\n",
|
||||||
|
"row 1 11.11 12.12\n",
|
||||||
|
"row 2 21.00 22.00\n",
|
||||||
|
"\n",
|
||||||
|
"\\begin{table}\n",
|
||||||
|
" \\begin{tabular}{lrr}\n",
|
||||||
|
" & A & B \\\\ \\hline \n",
|
||||||
|
" row 1 & 11.11 & 12.12 \\\\ \n",
|
||||||
|
" row 2 & 21.00 & 22.00 \\\\ \n",
|
||||||
|
" \\hline\n",
|
||||||
|
" \\end{tabular}\n",
|
||||||
|
"\\end{table}\n",
|
||||||
|
"┌───────┬───────┬───────┐\n",
|
||||||
|
"│ │ A │ B │\n",
|
||||||
|
"├───────┼───────┼───────┤\n",
|
||||||
|
"│ row 1 │ 11.11 │ 12.12 │\n",
|
||||||
|
"│ row 2 │ 21.00 │ 22.00 │\n",
|
||||||
|
"└───────┴───────┴───────┘\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"close(fh) #close the file\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"Results/NewTxtFile.txt has been closed. Check it out. The raw contents are:\\n\")\n",
|
||||||
|
"println(read(\"Results/NewTxtFile.txt\",String))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Formatted Printing (extra)\n",
|
||||||
|
"\n",
|
||||||
|
"using Printf in Julia 1.6+. \n",
|
||||||
|
"\n",
|
||||||
|
"(For earlier Julia versions, you may consider the [Formatting.jl](https://github.com/JuliaIO/Formatting.jl) package.)\n",
|
||||||
|
"\n",
|
||||||
|
"`@printf()` prints to a file or to the screen (and IOBuffers etc), while `@sprintf()` prints to a string. \n",
|
||||||
|
"\n",
|
||||||
|
"To print with dynamic formatting (that is, where the format is decided at run-time), use `Printf.format()`. "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 12,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
" 3.14\n",
|
||||||
|
"my string is just this: 1828 2.71828\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"z = 3.14159 \n",
|
||||||
|
" #the format string is constant\n",
|
||||||
|
"@printf(\"%10.2f\\n\",z) #width 10, 2 digits, floating point\n",
|
||||||
|
"\n",
|
||||||
|
"str = @sprintf(\"%5d %g\",1828,exp(1)) #width 5 and integer, compact floating point\n",
|
||||||
|
"println(\"my string is just this: \",str)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 13,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
" 3.14159\n",
|
||||||
|
" 3.1416\n",
|
||||||
|
" 3.142\n",
|
||||||
|
" 3.142\n",
|
||||||
|
" 3.142\n",
|
||||||
|
" 3.14159\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"for i = 1:6\n",
|
||||||
|
" (width,prec) = (10,rand(0:5)) #the format string is created at run-time, here random\n",
|
||||||
|
" fmt = Printf.Format(\"%$(width).$(prec)f\") \n",
|
||||||
|
" str = Printf.format(fmt,z)\n",
|
||||||
|
" println(str)\n",
|
||||||
|
"end "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"@webio": {
|
||||||
|
"lastCommId": null,
|
||||||
|
"lastKernelId": null
|
||||||
|
},
|
||||||
|
"anaconda-cloud": {},
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Julia 1.6.1",
|
||||||
|
"language": "julia",
|
||||||
|
"name": "julia-1.6"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"file_extension": ".jl",
|
||||||
|
"mimetype": "application/julia",
|
||||||
|
"name": "julia",
|
||||||
|
"version": "1.6.1"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 1
|
||||||
|
}
|
338
JuliaTutorial-master/Tutorial_12_MissingValues.ipynb
Normal file
338
JuliaTutorial-master/Tutorial_12_MissingValues.ipynb
Normal file
|
@ -0,0 +1,338 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Missing Values\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Load Packages and Extra Functions"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"printyellow (generic function with 1 method)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"using Printf\n",
|
||||||
|
"\n",
|
||||||
|
"include(\"jlFiles/printmat.jl\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# NaN\n",
|
||||||
|
"\n",
|
||||||
|
"The `NaN` (Not-a-Number) can be used to indicate that a floating point number (for instance, 2.0) is missing or otherwise strange. For other types of data (for instance, 2), use a ```missing``` (see below) instead.\n",
|
||||||
|
"\n",
|
||||||
|
"Most computations involving NaNs give `NaN` as the result."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"NaN\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"println(2.0 + NaN)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Loading Data\n",
|
||||||
|
"\n",
|
||||||
|
"When your data (loaded from a csv file, say) has special values for missing data points (for instance, -999.99), then you can simply replace those values with `NaN`. This works since `NaN` is a Float64 value, so you can change an existing array of `Float64`s to `NaN`.\n",
|
||||||
|
"\n",
|
||||||
|
"(See the tutorial on loading and saving data for more information.)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 3,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"z: \n",
|
||||||
|
" 1.000 NaN\n",
|
||||||
|
" 2.000 12.000\n",
|
||||||
|
" 3.000 13.000\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"data = [1.0 -999.99;\n",
|
||||||
|
" 2.0 12.0;\n",
|
||||||
|
" 3.0 13.0]\n",
|
||||||
|
"\n",
|
||||||
|
"z = replace(data,-999.99=>NaN) #replace -999.99 by NaN\n",
|
||||||
|
"println(\"z: \")\n",
|
||||||
|
"printmat(z)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## NaNs in a Matrix\n",
|
||||||
|
"\n",
|
||||||
|
"If a matrix contains NaNs, then many calculations (eg. summing all elements) give NaN as the result. \n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"z has some NaNs\n",
|
||||||
|
"\n",
|
||||||
|
"The sum of each column: \n",
|
||||||
|
" 6.000 NaN\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"if any(isnan,z) #check if any NaNs\n",
|
||||||
|
" println(\"z has some NaNs\") #can also do any(isnan.(z))\n",
|
||||||
|
"end\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"\\nThe sum of each column: \")\n",
|
||||||
|
"printmat(sum(z,dims=1))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Getting Rid of NaNs\n",
|
||||||
|
"\n",
|
||||||
|
"It is a common procedure in statistics to throw out all cases with NaNs/missing values. For instance, let `z` be a matrix and `z[t,:]` the data for period $t$ which contains one or more `NaN` values. It is then common (for instance, in linear regressions) to throw out that entire row of the matrix.\n",
|
||||||
|
"\n",
|
||||||
|
"This is a reasonable approach if it can be argued that the fact that the data is missing is random - and not related to the subject of the investigation. It is much less reasonable if, for instance, the returns for all poorly performing mutual funds are listed as \"missing\" - and you want to study what fund characteristics that drive performance.\n",
|
||||||
|
"\n",
|
||||||
|
"The code below shows a simple way of how to through out all rows of a matrix with at least one `NaN`."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"z:\n",
|
||||||
|
" 1.000 NaN\n",
|
||||||
|
" 2.000 12.000\n",
|
||||||
|
" 3.000 13.000\n",
|
||||||
|
"\n",
|
||||||
|
"z2: a new matrix where all rows with any NaNs have been pruned:\n",
|
||||||
|
" 2.000 12.000\n",
|
||||||
|
" 3.000 13.000\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"println(\"z:\")\n",
|
||||||
|
"printmat(z)\n",
|
||||||
|
"\n",
|
||||||
|
"vb = any(isnan,z,dims=2) #indicates rows with NaNs\n",
|
||||||
|
"vc = .!vec(vb) #indicates rows without NaNs\n",
|
||||||
|
"\n",
|
||||||
|
"z2 = z[vc,:] #keep only rows without NaNs\n",
|
||||||
|
"println(\"z2: a new matrix where all rows with any NaNs have been pruned:\")\n",
|
||||||
|
"printmat(z2)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Missings \n",
|
||||||
|
"\n",
|
||||||
|
"can be used to indicate missing values for most types (not just floats).\n",
|
||||||
|
"\n",
|
||||||
|
"Similarly to `NaN`s, computations involving `missing` (for instance, `1+missing`) result in `missing`.\n",
|
||||||
|
"\n",
|
||||||
|
"In contrast to `NaN`s, you cannot just change an element of an existing matrix (of Float64 or Int, say) to `missing.` The [Missings](https://github.com/JuliaData/Missings.jl) package has help routines to handle that."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 6,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"using Missings"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 7,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"z: \n",
|
||||||
|
" 1 missing\n",
|
||||||
|
" 2 12 \n",
|
||||||
|
" 3 13 \n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"data = [1 -999;\n",
|
||||||
|
" 2 12;\n",
|
||||||
|
" 3 13]\n",
|
||||||
|
"z = allowmissing(data) #convert to an array that can include missing\n",
|
||||||
|
"z = replace(data,-999=>missing) #replace -999 by missing\n",
|
||||||
|
"println(\"z: \")\n",
|
||||||
|
"printmat(z)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 8,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"z has some missings\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"if any(ismissing,z) #check if any NaNs\n",
|
||||||
|
" println(\"z has some missings\")\n",
|
||||||
|
"end"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 9,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"z2: a new matrix where all rows with any missings have been pruned:\n",
|
||||||
|
" 2 12 \n",
|
||||||
|
" 3 13 \n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"vd = .!vec(any(ismissing,z,dims=2))\n",
|
||||||
|
"\n",
|
||||||
|
"z2 = z[vd,:] #keep only rows without NaNs\n",
|
||||||
|
"println(\"z2: a new matrix where all rows with any missings have been pruned:\")\n",
|
||||||
|
"printmat(z2)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Once `z2` does not have any `missing` (although it still allows you to) you can typically use it as any other array. However, if you for some reason need to work with a traditional array, then convert `z2` (see below) by using the `disallowmissing` function."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 10,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"The type of z2 is Matrix{Union{Missing, Int64}}\n",
|
||||||
|
"\n",
|
||||||
|
"The type of z3 is Matrix{Int64}\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"println(\"The type of z2 is \", typeof(z2))\n",
|
||||||
|
"\n",
|
||||||
|
"z3 = disallowmissing(z2) #convert to traditional array,\n",
|
||||||
|
" #same as same as convert.(Int,z2)\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"\\nThe type of z3 is \", typeof(z3)) "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"@webio": {
|
||||||
|
"lastCommId": null,
|
||||||
|
"lastKernelId": null
|
||||||
|
},
|
||||||
|
"anaconda-cloud": {},
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Julia 1.6.0",
|
||||||
|
"language": "julia",
|
||||||
|
"name": "julia-1.6"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"file_extension": ".jl",
|
||||||
|
"mimetype": "application/julia",
|
||||||
|
"name": "julia",
|
||||||
|
"version": "1.6.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 1
|
||||||
|
}
|
92
JuliaTutorial-master/Tutorial_13_Downloading.ipynb
Normal file
92
JuliaTutorial-master/Tutorial_13_Downloading.ipynb
Normal file
|
@ -0,0 +1,92 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"# Downloading Files from Internet\n",
|
||||||
|
"\n",
|
||||||
|
"is done by the `download()` command. \n",
|
||||||
|
"\n",
|
||||||
|
"In Julia 1.6+ it is probably better to do\n",
|
||||||
|
"```\n",
|
||||||
|
"Import Downloads\n",
|
||||||
|
"Downloads.download()\n",
|
||||||
|
"```"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"if !isdir(\"Results\")\n",
|
||||||
|
" error(\"create the subfolder Results before running this program\")\n",
|
||||||
|
"end"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Some Data from Kenneth French's Homepage"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"File to download: http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/F-F_Research_Data_Factors_daily_CSV.zip\n",
|
||||||
|
"\n",
|
||||||
|
"check the subfolder Results\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"http = string(\"http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/\",\n",
|
||||||
|
" \"F-F_Research_Data_Factors_daily_CSV.zip\")\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"File to download: \",http)\n",
|
||||||
|
"download(http,\"Results/WhatIJustDownloaded.zip\")\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"\\ncheck the subfolder Results\\n\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"@webio": {
|
||||||
|
"lastCommId": null,
|
||||||
|
"lastKernelId": null
|
||||||
|
},
|
||||||
|
"anaconda-cloud": {},
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Julia 1.6.1",
|
||||||
|
"language": "julia",
|
||||||
|
"name": "julia-1.6"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"file_extension": ".jl",
|
||||||
|
"mimetype": "application/julia",
|
||||||
|
"name": "julia",
|
||||||
|
"version": "1.6.1"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 1
|
||||||
|
}
|
388
JuliaTutorial-master/Tutorial_21_FindingRoots.ipynb
Normal file
388
JuliaTutorial-master/Tutorial_21_FindingRoots.ipynb
Normal file
File diff suppressed because one or more lines are too long
1376
JuliaTutorial-master/Tutorial_22a_Optimization_Optim.ipynb
Normal file
1376
JuliaTutorial-master/Tutorial_22a_Optimization_Optim.ipynb
Normal file
File diff suppressed because one or more lines are too long
2127
JuliaTutorial-master/Tutorial_22b_Optimization_NLopt.ipynb
Normal file
2127
JuliaTutorial-master/Tutorial_22b_Optimization_NLopt.ipynb
Normal file
File diff suppressed because one or more lines are too long
237
JuliaTutorial-master/Tutorial_22c_Optimization_Convex.ipynb
Normal file
237
JuliaTutorial-master/Tutorial_22c_Optimization_Convex.ipynb
Normal file
|
@ -0,0 +1,237 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Optimization\n",
|
||||||
|
"\n",
|
||||||
|
"of linear-quadratic problems.\n",
|
||||||
|
"\n",
|
||||||
|
"Many optimization problems in finance and econometrics involve linear-quadratic objectives/constraints. This notebook illustrates how the package [Convex.jl](https://github.com/jump-dev/Convex.jl) can be used for this. The example is (for pedagogical reasons) the same as in the other notebooks on optimization. Otherwise, the methods illustrated here are well suited for cases when the objective involves the portfolio variance ($ w'\\Sigma w $) or when the estimation problem is based on minimizing the sum of squared residuals ($u'u$).\n",
|
||||||
|
"\n",
|
||||||
|
"The notebook also uses [SCS.jl](https://github.com/jump-dev/SCS.jl) (for the optimization algorithm). To check for convergence, we also need a function from the [MathOptInterface.jl](https://github.com/jump-dev/MathOptInterface.jl) package."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Load Packages and Utility Functions"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"printyellow (generic function with 1 method)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"using Printf, LinearAlgebra, Convex, SCS\n",
|
||||||
|
"import MathOptInterface\n",
|
||||||
|
"const MOI = MathOptInterface\n",
|
||||||
|
"\n",
|
||||||
|
"include(\"jlFiles/printmat.jl\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## A Linear-Quadratic Minimization Problem\n",
|
||||||
|
"\n",
|
||||||
|
"with/without constraints.\n",
|
||||||
|
"\n",
|
||||||
|
"We specify a matrix $Q$ and a vector $c$ and write the loss function as $b'Qb + c'b$ where $b$ are the choice variables. \n",
|
||||||
|
"\n",
|
||||||
|
"We consider several cases below: no restrictions on $b$, bounds on $b$, a linear equality restriction and a non-linear inequality restriction."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"2-element Vector{Int64}:\n",
|
||||||
|
" -4\n",
|
||||||
|
" 24"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 2,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"Q = [1 0; #we want to minimize b'Q*b + c'b\n",
|
||||||
|
" 0 16] #this is the same as minimizing (x-2)^2 + (4y+3)^2 \n",
|
||||||
|
"c = [-4, 24]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Unconstrained Minimization"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 3,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Unconstrained minimization: the solution should be (2,-3/4)\n",
|
||||||
|
" 2.000\n",
|
||||||
|
" -0.750\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"n = length(c)\n",
|
||||||
|
"b = Variable(n) #define the choice variables\n",
|
||||||
|
"L1 = quadform(b,Q) #part 1 of the objective, b'Q*b\n",
|
||||||
|
"L2 = dot(c,b) #part 2, c'b\n",
|
||||||
|
"\n",
|
||||||
|
"problem = minimize(L1+L2)\n",
|
||||||
|
"solve!(problem,()->SCS.Optimizer(verbose=false))\n",
|
||||||
|
"problem.status == MOI.OPTIMAL ? b_sol = evaluate(b) : b_sol = NaN\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"Unconstrained minimization: the solution should be (2,-3/4)\")\n",
|
||||||
|
"printmat(b_sol)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Constrained Minimization"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"with bounds on the solution: the solution should be (2.75,-0.75)\n",
|
||||||
|
" 2.750\n",
|
||||||
|
" -0.750\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"c1 = [2.75 <= b[1],b[2] <= -0.3] #bounds on the solution\n",
|
||||||
|
"\n",
|
||||||
|
"problem = minimize(L1+L2,c1)\n",
|
||||||
|
"solve!(problem,()->SCS.Optimizer(verbose=false))\n",
|
||||||
|
"problem.status == MOI.OPTIMAL ? b_sol = evaluate(b) : b_sol = NaN\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"with bounds on the solution: the solution should be (2.75,-0.75)\")\n",
|
||||||
|
"printmat(b_sol)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"equality constraint: the solution should be (4,-1/2)\n",
|
||||||
|
" 4.000\n",
|
||||||
|
" -0.500\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"c2 = dot([1,2],b) == 3 #equality constraint\n",
|
||||||
|
"\n",
|
||||||
|
"problem = minimize(L1+L2,c2)\n",
|
||||||
|
"solve!(problem,()->SCS.Optimizer(verbose=false))\n",
|
||||||
|
"problem.status == MOI.OPTIMAL ? b_sol = evaluate(b) : b_sol = NaN\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"equality constraint: the solution should be (4,-1/2)\")\n",
|
||||||
|
"printmat(b_sol)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 6,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"non-linear inequality constraint: the solution should be close to (3.1,-0.79)\n",
|
||||||
|
" 3.112\n",
|
||||||
|
" -0.789\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"c3 = b[2] + square(b[1]-4) <= 0 #non-linear inequality constraint\n",
|
||||||
|
"\n",
|
||||||
|
"problem = minimize(L1+L2,c3)\n",
|
||||||
|
"solve!(problem,()->SCS.Optimizer(verbose=false))\n",
|
||||||
|
"problem.status == MOI.OPTIMAL ? b_sol = evaluate(b) : b_sol = NaN\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"non-linear inequality constraint: the solution should be close to (3.1,-0.79)\")\n",
|
||||||
|
"printmat(b_sol)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"@webio": {
|
||||||
|
"lastCommId": null,
|
||||||
|
"lastKernelId": null
|
||||||
|
},
|
||||||
|
"anaconda-cloud": {},
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Julia 1.6.1",
|
||||||
|
"language": "julia",
|
||||||
|
"name": "julia-1.6"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"file_extension": ".jl",
|
||||||
|
"mimetype": "application/julia",
|
||||||
|
"name": "julia",
|
||||||
|
"version": "1.6.1"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 1
|
||||||
|
}
|
701
JuliaTutorial-master/Tutorial_24_Integration.ipynb
Normal file
701
JuliaTutorial-master/Tutorial_24_Integration.ipynb
Normal file
File diff suppressed because one or more lines are too long
737
JuliaTutorial-master/Tutorial_25_Interpolation.ipynb
Normal file
737
JuliaTutorial-master/Tutorial_25_Interpolation.ipynb
Normal file
File diff suppressed because one or more lines are too long
493
JuliaTutorial-master/Tutorial_26_Py_R_C_Call.ipynb
Normal file
493
JuliaTutorial-master/Tutorial_26_Py_R_C_Call.ipynb
Normal file
|
@ -0,0 +1,493 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Running Python, R or C Code from Julia\n",
|
||||||
|
"\n",
|
||||||
|
"This notebook provides a basic introduction to how to run Python, R and C code from Julia."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"printyellow (generic function with 1 method)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"using Printf, DelimitedFiles\n",
|
||||||
|
"include(\"jlFiles/printmat.jl\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Load Data"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"OLS coeffs according to Julia\n",
|
||||||
|
" -0.504\n",
|
||||||
|
" 1.341\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"x = readdlm(\"Data/MyData.csv\",',',skipstart=1) #reading the csv file\n",
|
||||||
|
"\n",
|
||||||
|
"(Rme,Rf,R) = (x[:,2],x[:,3],x[:,4]) #creating variables from columns of x\n",
|
||||||
|
"y = R - Rf #do R .- Rf if R has several columns\n",
|
||||||
|
"\n",
|
||||||
|
"c = ones(length(Rme))\n",
|
||||||
|
"x = [c Rme]\n",
|
||||||
|
"\n",
|
||||||
|
"b = x\\y\n",
|
||||||
|
"println(\"OLS coeffs according to Julia\")\n",
|
||||||
|
"printmat(b)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Python\n",
|
||||||
|
"\n",
|
||||||
|
"using the [PyCall.jl](https://github.com/JuliaPy/PyCall.jl) package."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 3,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"PyObject <module 'statsmodels.api' from 'C:\\\\Miniconda3\\\\lib\\\\site-packages\\\\statsmodels\\\\api.py'>"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 3,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"using PyCall\n",
|
||||||
|
"sm = pyimport(\"statsmodels.api\") #activate this package"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"PyObject <class 'statsmodels.iolib.summary.Summary'>\n",
|
||||||
|
"\"\"\"\n",
|
||||||
|
" OLS Regression Results \n",
|
||||||
|
"==============================================================================\n",
|
||||||
|
"Dep. Variable: y R-squared: 0.519\n",
|
||||||
|
"Model: OLS Adj. R-squared: 0.518\n",
|
||||||
|
"Method: Least Squares F-statistic: 416.2\n",
|
||||||
|
"Date: Fri, 16 Jul 2021 Prob (F-statistic): 2.72e-63\n",
|
||||||
|
"Time: 15:04:44 Log-Likelihood: -1241.7\n",
|
||||||
|
"No. Observations: 388 AIC: 2487.\n",
|
||||||
|
"Df Residuals: 386 BIC: 2495.\n",
|
||||||
|
"Df Model: 1 \n",
|
||||||
|
"Covariance Type: nonrobust \n",
|
||||||
|
"==============================================================================\n",
|
||||||
|
" coef std err t P>|t| [0.025 0.975]\n",
|
||||||
|
"------------------------------------------------------------------------------\n",
|
||||||
|
"const -0.5042 0.305 -1.654 0.099 -1.103 0.095\n",
|
||||||
|
"x1 1.3410 0.066 20.401 0.000 1.212 1.470\n",
|
||||||
|
"==============================================================================\n",
|
||||||
|
"Omnibus: 259.682 Durbin-Watson: 1.870\n",
|
||||||
|
"Prob(Omnibus): 0.000 Jarque-Bera (JB): 5249.944\n",
|
||||||
|
"Skew: 2.482 Prob(JB): 0.00\n",
|
||||||
|
"Kurtosis: 20.323 Cond. No. 4.68\n",
|
||||||
|
"==============================================================================\n",
|
||||||
|
"\n",
|
||||||
|
"Notes:\n",
|
||||||
|
"[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.\n",
|
||||||
|
"\"\"\"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"resultsP = sm.OLS(y, x).fit() #can use Python functions directly\n",
|
||||||
|
"\n",
|
||||||
|
"println(resultsP.summary())"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"[:HC0_se, :HC1_se, :HC2_se, :HC3_se, :_HCCM, :__class__, :__delattr__, :__dict__, :__dir__, :__doc__, :__eq__, :__format__, :__ge__, :__getattribute__, :__gt__, :__hash__, :__init__, :__init_subclass__, :__le__, :__lt__, :__module__, :__ne__, :__new__, :__reduce__, :__reduce_ex__, :__repr__, :__setattr__, :__sizeof__, :__str__, :__subclasshook__, :__weakref__, :_abat_diagonal, :_cache, :_data_attr, :_data_in_cache, :_get_robustcov_results, :_is_nested, :_use_t, :_wexog_singular_values, :aic, :bic, :bse, :centered_tss, :compare_f_test, :compare_lm_test, :compare_lr_test, :condition_number, :conf_int, :conf_int_el, :cov_HC0, :cov_HC1, :cov_HC2, :cov_HC3, :cov_kwds, :cov_params, :cov_type, :df_model, :df_resid, :diagn, :eigenvals, :el_test, :ess, :f_pvalue, :f_test, :fittedvalues, :fvalue, :get_influence, :get_prediction, :get_robustcov_results, :initialize, :k_constant, :llf, :load, :model, :mse_model, :mse_resid, :mse_total, :nobs, :normalized_cov_params, :outlier_test, :params, :predict, :pvalues, :remove_data, :resid, :resid_pearson, :rsquared, :rsquared_adj, :save, :scale, :ssr, :summary, :summary2, :t_test, :t_test_pairwise, :tvalues, :uncentered_tss, :use_t, :wald_test, :wald_test_terms, :wresid]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"println(keys(resultsP)) #print all keys (field names)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 6,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Comparing the estimates in Julia and Python\n",
|
||||||
|
" -0.504 -0.504\n",
|
||||||
|
" 1.341 1.341\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"b_P = resultsP.params #the numerical results are now a Julia vector\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"Comparing the estimates in Julia and Python\")\n",
|
||||||
|
"printmat([b b_P])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 7,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"[-0.5041626 1.34104865]\r\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#we can run blocks of code like this, notice: $x and $y\n",
|
||||||
|
"py\"\"\"\n",
|
||||||
|
" import numpy as np\n",
|
||||||
|
" xx = np.matmul(np.matrix.transpose($x),$x)\n",
|
||||||
|
" xy = np.matmul(np.matrix.transpose($x),$y)\n",
|
||||||
|
" b_p = np.linalg.solve(xx,xy)\n",
|
||||||
|
" #b_p = np.linalg.lstsq($x,$y,rcond=None)\n",
|
||||||
|
" print(b_p)\n",
|
||||||
|
" \"\"\""
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# R\n",
|
||||||
|
"\n",
|
||||||
|
"using the [RCall.jl](https://github.com/JuliaInterop/RCall.jl) package."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 8,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"ENV[\"R_HOME\"]=\"C:/PROGRA~1/R/R-40~1.4\" #path to R, do R.home() in R to see\n",
|
||||||
|
"using RCall #do import Pkg; Pkg.build(\"RCall\") after changing R location "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 9,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"RObject{VecSxp}\n",
|
||||||
|
"\n",
|
||||||
|
"Call:\n",
|
||||||
|
"lm(formula = y ~ x - 1)\n",
|
||||||
|
"\n",
|
||||||
|
"Residuals:\n",
|
||||||
|
" Min 1Q Median 3Q Max \n",
|
||||||
|
"-17.981 -3.131 -0.359 2.281 52.361 \n",
|
||||||
|
"\n",
|
||||||
|
"Coefficients:\n",
|
||||||
|
" Estimate Std. Error t value Pr(>|t|) \n",
|
||||||
|
"x1 -0.50416 0.30483 -1.654 0.099 . \n",
|
||||||
|
"x2 1.34105 0.06573 20.401 <2e-16 ***\n",
|
||||||
|
"---\n",
|
||||||
|
"Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1\n",
|
||||||
|
"\n",
|
||||||
|
"Residual standard error: 5.954 on 386 degrees of freedom\n",
|
||||||
|
"Multiple R-squared: 0.5194,\tAdjusted R-squared: 0.5169 \n",
|
||||||
|
"F-statistic: 208.6 on 2 and 386 DF, p-value: < 2.2e-16\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"[:coefficients, :residuals, :effects, :rank, Symbol(\"fitted.values\"), :assign, :qr, Symbol(\"df.residual\"), :xlevels, :call, :terms, :model]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"@rput x y #send x and y to R\n",
|
||||||
|
"\n",
|
||||||
|
"resultsR = reval(\"summary(mod <- lm(y ~ x-1))\") #run R code and print output\n",
|
||||||
|
"println(resultsR)\n",
|
||||||
|
"\n",
|
||||||
|
"resultsR = reval(\"mod <- lm(y ~ x-1)\") #run R code\n",
|
||||||
|
"\n",
|
||||||
|
"println(names(resultsR)) #print all keys (field names)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 10,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Comparing the estimates in Julia and R\n",
|
||||||
|
" -0.504 -0.504\n",
|
||||||
|
" 1.341 1.341\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"b_R = rcopy(resultsR[:coefficients]) #the numerical results are now a Julia array\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"Comparing the estimates in Julia and R\")\n",
|
||||||
|
"printmat([b b_R[:,1]])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 11,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"RObject{RealSxp}\n",
|
||||||
|
" [,1]\n",
|
||||||
|
"[1,] -0.5041626\n",
|
||||||
|
"[2,] 1.3410486\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 11,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#we can run blocks of code like this\n",
|
||||||
|
"#do @rput x y or use $x and $y in the code below\n",
|
||||||
|
"\n",
|
||||||
|
"#solve x'x*b = x'y\n",
|
||||||
|
"R\"\"\"\n",
|
||||||
|
"xx <- t(x)%*%x\n",
|
||||||
|
"xy <- t(x)%*%y\n",
|
||||||
|
"b_R <- solve(xx,xy)\n",
|
||||||
|
"\"\"\""
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Interactive Usage of R\n",
|
||||||
|
"\n",
|
||||||
|
"If you are using the Julia REPL (\"command prompt\"), then you can work more interactively with R. Unfortunaterly, that does not work in a notebook. The following trivial example illustrates the idea (`>julia` means that you are at the julia prompt and `R>` at the R prompt).\n",
|
||||||
|
"\n",
|
||||||
|
"```\n",
|
||||||
|
"julia> x = 1;y=2\n",
|
||||||
|
"julia> @rput x y\n",
|
||||||
|
"julia> $ #to switch over to R\n",
|
||||||
|
"R> z <- x + y\n",
|
||||||
|
"R> hit ESC #to switch over to Julia\n",
|
||||||
|
"julia> @rget z\n",
|
||||||
|
"```\n",
|
||||||
|
"\n",
|
||||||
|
"At this point you can work with `z` in Julia."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# C\n",
|
||||||
|
"\n",
|
||||||
|
"This section illustrates some simple examples of how to call a C function. The functions are in the file `My_C_Stuff.c` which contains:"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 12,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"#include <stddef.h>\r\n",
|
||||||
|
"\r\n",
|
||||||
|
"// calculate the inner (dot) product of vectors Y and Y, returns the result (Sxy)\r\n",
|
||||||
|
"double c_dot(size_t n, double *Y, double *X) {\r\n",
|
||||||
|
" double Sxy = 0.0;\r\n",
|
||||||
|
" for (size_t i = 0; i < n; ++i) {\r\n",
|
||||||
|
" Sxy += X[i]*Y[i];\r\n",
|
||||||
|
" }\r\n",
|
||||||
|
" return Sxy;\r\n",
|
||||||
|
"}\r\n",
|
||||||
|
"\r\n",
|
||||||
|
"// calculate a simple regression, Y = a + b*X + u, puts (a,b) in vector ab, returns nothing\r\n",
|
||||||
|
"void c_ols(size_t n, double *Y, double *X, double *ab) {\r\n",
|
||||||
|
" double Sx = 0.0, Sy = 0.0, Sxx = 0.0, Sxy = 0.0;\r\n",
|
||||||
|
" for (size_t i = 0; i < n; ++i) {\r\n",
|
||||||
|
" Sx += X[i];\r\n",
|
||||||
|
" Sy += Y[i];\r\n",
|
||||||
|
" Sxx += X[i]*X[i];\r\n",
|
||||||
|
" Sxy += X[i]*Y[i];\r\n",
|
||||||
|
" }\r\n",
|
||||||
|
" ab[1] = (Sxy-Sx*Sy/n)/(Sxx-Sx*Sx/n); //slope\r\n",
|
||||||
|
" ab[0] = (Sy - ab[1]*Sx)/n; //intercept\r\n",
|
||||||
|
"}\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"println(read(\"Data/My_C_Stuff.c\",String))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"To compile to a dynamlic library (dll on windows), I use gcc (for x86_64) from [mingw-64](http://mingw-w64.org)\n",
|
||||||
|
"and run the following in the mingw terminal\n",
|
||||||
|
"```\n",
|
||||||
|
"gcc -shared -fPIC My_C_Stuff.c -o My_C_Stuff.dll\n",
|
||||||
|
"```\n",
|
||||||
|
"\n",
|
||||||
|
"To call the C functions, place the dll file in the current folder and then run the following cells."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 13,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Comparing the estimates in Julia and C\n",
|
||||||
|
" -0.504 -0.504\n",
|
||||||
|
" 1.341 1.341\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"mylibc = \"My_C_Stuff.dll\"\n",
|
||||||
|
"\n",
|
||||||
|
"b_c = zeros(2) #where C will store the regression results\n",
|
||||||
|
"x2 = x[:,2]\n",
|
||||||
|
"@ccall mylibc.c_ols(length(y)::Csize_t, y::Ptr{Float64}, x2::Ptr{Float64}, b_c::Ptr{Float64})::Cvoid\n",
|
||||||
|
"println(\"Comparing the estimates in Julia and C\")\n",
|
||||||
|
"printmat([b b_c])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"In this call `mylibc.c_ols` is the library.function, `length(y)::Csize_t` is the first input and its type (an `Int` indicating the number of elements in `y`), `y::Ptr{Float64}` is the second input and its type (an `Array{Float64}`) and similarly for the remaining inputs. `Cvoid` is the type of the output, which here indicates that the function does not have an output. Rather, the function modifies the vector `b_c` by putting the OLS results there. \n",
|
||||||
|
"\n",
|
||||||
|
"We could potentially wrap this in a Julia function that checks for the right input types and outputs the `b_c` vector.\n",
|
||||||
|
"\n",
|
||||||
|
"In the next example, we instead use a function that outputs a `Float64` number. The function calculates the inner product of two vectors."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 14,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"The inner product of x2 and y in Julia and C: 11071.648 11071.648\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"z = @ccall mylibc.c_dot(length(y)::Csize_t, y::Ptr{Float64}, x2::Ptr{Float64})::Float64\n",
|
||||||
|
"\n",
|
||||||
|
"printlnPs(\"The inner product of x2 and y in Julia and C: \",x2'y,\" \",z)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"@webio": {
|
||||||
|
"lastCommId": null,
|
||||||
|
"lastKernelId": null
|
||||||
|
},
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Julia 1.6.1",
|
||||||
|
"language": "julia",
|
||||||
|
"name": "julia-1.6"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"file_extension": ".jl",
|
||||||
|
"mimetype": "application/julia",
|
||||||
|
"name": "julia",
|
||||||
|
"version": "1.6.2"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 3
|
||||||
|
}
|
21787
JuliaTutorial-master/Tutorial_31_PyPlot.ipynb
Normal file
21787
JuliaTutorial-master/Tutorial_31_PyPlot.ipynb
Normal file
File diff suppressed because one or more lines are too long
642
JuliaTutorial-master/Tutorial_99_TrickyStuff.ipynb
Normal file
642
JuliaTutorial-master/Tutorial_99_TrickyStuff.ipynb
Normal file
|
@ -0,0 +1,642 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Tricky Stuff\n",
|
||||||
|
"\n",
|
||||||
|
"This file highlights some tricky aspects of Julia (from the perspective of a Matlab user)."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Load Packages and Extra Functions"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"printyellow (generic function with 1 method)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"using Printf, LinearAlgebra\n",
|
||||||
|
"\n",
|
||||||
|
"include(\"jlFiles/printmat.jl\") #function for prettier matrix printing"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# An Nx1 Array Is Not a Vector\n",
|
||||||
|
"\n",
|
||||||
|
"and it sometimes matters. \n",
|
||||||
|
"\n",
|
||||||
|
"Julia has both vectors and Nx1 arrays (the latter being a special case of NxM arrays). They can often be used interchangeably, but not always."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"v and v2 look similar:\n",
|
||||||
|
" 1 \n",
|
||||||
|
" 1 \n",
|
||||||
|
"\n",
|
||||||
|
" 1 \n",
|
||||||
|
" 1 \n",
|
||||||
|
"\n",
|
||||||
|
"but they have different sizes: (2,) (2, 1)\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"v = ones(Int,2) #a vector with two elements\n",
|
||||||
|
"v2 = ones(Int,2,1) #a 2x1 matrix (Array)\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"v and v2 look similar:\")\n",
|
||||||
|
"printmat(v)\n",
|
||||||
|
"printmat(v2)\n",
|
||||||
|
"println(\"but they have different sizes: \",size(v),\" \",size(v2))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# X[1,:] Gives a (Flat) Vector\n",
|
||||||
|
"\n",
|
||||||
|
"If `X` is a $T\\times n$ matrix, then `X[1,:]` gives a flat vector, *not* a $1xn$ matrix (or row vector)."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 3,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"size of X[1,:]: (2,)\n",
|
||||||
|
" 11 \n",
|
||||||
|
" 12 \n",
|
||||||
|
"\n",
|
||||||
|
"size of X[1:1,:]: (1, 2)\n",
|
||||||
|
" 11 12 \n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"X = [11 12;21 22]\n",
|
||||||
|
"\n",
|
||||||
|
"x1 = X[1,:] #this gives a flat vector\n",
|
||||||
|
"println(\"size of X[1,:]: \", size(x1))\n",
|
||||||
|
"printmat(x1)\n",
|
||||||
|
"\n",
|
||||||
|
"x1b = X[1:1,:] #this gives a 1x2 matrix\n",
|
||||||
|
"println(\"size of X[1:1,:]: \", size(x1b))\n",
|
||||||
|
"printmat(x1b)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Array .+ Scalar Requires a Dot (.)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
" 2 \n",
|
||||||
|
" 3 \n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"y = [1;2] .+ 1 #do not forget the dot (.)\n",
|
||||||
|
"printmat(y)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Creating Variables in a Loop"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Tor: 0.28366218546322625\n",
|
||||||
|
"Oden: 0.28366218546322625\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"for i = 1:5\n",
|
||||||
|
" global Tor #without this, Tor is not seen outside the loop\n",
|
||||||
|
" Tor = cos(i)\n",
|
||||||
|
"end\n",
|
||||||
|
"println(\"Tor: $Tor\")\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"Oden = Inf\n",
|
||||||
|
"for i = 1:5\n",
|
||||||
|
" #global Oden #only needed in script\n",
|
||||||
|
" Oden = cos(i) #will overwrite an existing value \n",
|
||||||
|
"end\n",
|
||||||
|
"println(\"Oden: $Oden\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Threads.@threads and Variable Scope\n",
|
||||||
|
"\n",
|
||||||
|
"Code like this\n",
|
||||||
|
"```\n",
|
||||||
|
"v = 1:2\n",
|
||||||
|
"Threads.@threads for i = 1:N\n",
|
||||||
|
" v = something \n",
|
||||||
|
" x = SomeFunction(v)\n",
|
||||||
|
"end\n",
|
||||||
|
"```\n",
|
||||||
|
"can create unexpected results since the threads are sharing `v`. This is solved by declaring `v` inside the loop to be `local`. (Clearly, this only happens when you have configured Julia to use several threads.)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 6,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"this should always be zero. Run a few times to check if that is true.\n",
|
||||||
|
"\n",
|
||||||
|
"[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, 2.0, 15.0, 11.0, 16.0, 0.0, 16.0, 16.0, 0.0, 0.0, 6.0, 0.0, 0.0, 19.0, 20.0, 17.0, 0.0, 22.0, 9.0, 0.0, 0.0, 20.0, 23.0, 18.0, 32.0, 22.0, 28.0, 16.0, 28.0, 40.0, 0.0, 20.0, 21.0, 35.0, 30.0, 26.0, 33.0, 27.0, 27.0, 19.0, 47.0, 12.0, 0.0, 35.0, 0.0, 31.0, 51.0, 50.0, 31.0, 29.0, 0.0, 50.0, 47.0, 39.0, 53.0, 49.0, 19.0, 66.0, 59.0, 46.0, 38.0, 50.0, 66.0, 39.0, 67.0, 40.0, 0.0, 69.0, 36.0, 68.0, 73.0, 43.0, 67.0, 77.0, 65.0, 78.0, 47.0, 80.0, 56.0, 32.0, 61.0, 81.0, 70.0, 67.0, 76.0, 88.0, 90.0, 81.0]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"function f2(N)\n",
|
||||||
|
" v = falses(N+1)\n",
|
||||||
|
" x = zeros(Int,N,N)\n",
|
||||||
|
" Threads.@threads for i = 1:N\n",
|
||||||
|
" #local v #uncomment to solve the problem\n",
|
||||||
|
" v = falses(N)\n",
|
||||||
|
" v[i] = true\n",
|
||||||
|
" x[v,i] .= i\n",
|
||||||
|
" end\n",
|
||||||
|
" return x\n",
|
||||||
|
"end\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"this should always be zero. Run a few times to check if that is true.\\n\")\n",
|
||||||
|
"M = 100\n",
|
||||||
|
"dev = zeros(M)\n",
|
||||||
|
"for i = 1:M\n",
|
||||||
|
" dev[i] = maximum(abs,f2(i) - diagm(1:i))\n",
|
||||||
|
"end\n",
|
||||||
|
"println(dev)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# A Heterogeneous Array"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"To create a 'cell array' (a heterogeneous Array), use `[x1,x2,...]`\n",
|
||||||
|
"\n",
|
||||||
|
"Alternatively, you can preallocate as in `B = Array{Any}(undef,3)` and then fill by, for instance, `B[1] = [11 12]`"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 7,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"\n",
|
||||||
|
"The array A: \n",
|
||||||
|
" 11 12 \n",
|
||||||
|
" 21 22 \n",
|
||||||
|
"\n",
|
||||||
|
"A nice dog\n",
|
||||||
|
"\n",
|
||||||
|
" 27 \n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"The array B: \n",
|
||||||
|
" 11 12 \n",
|
||||||
|
" 21 22 \n",
|
||||||
|
"\n",
|
||||||
|
"A nice dog\n",
|
||||||
|
"\n",
|
||||||
|
" 27 \n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"A = [[11 12;21 22],\"A nice dog\",27]\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"\\nThe array A: \")\n",
|
||||||
|
"foreach(i->printmat(A[i]),1:length(A)) #print each element of A\n",
|
||||||
|
"\n",
|
||||||
|
"B = Array{Any}(undef,3)\n",
|
||||||
|
"B[1] = [11 12]\n",
|
||||||
|
"B[2] = \"A bad cat\"\n",
|
||||||
|
"B[3] = pi\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"\\nThe array B: \")\n",
|
||||||
|
"foreach(i->printmat(A[i]),1:length(A))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# An Array of Arrays\n",
|
||||||
|
"\n",
|
||||||
|
"can be initialized by comprehension (see below). (Do *not* use fill. See \"A Reshaped Array\" for why.)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 8,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"x[1]\n",
|
||||||
|
" -99.000 0.000\n",
|
||||||
|
" 0.000 0.000\n",
|
||||||
|
"\n",
|
||||||
|
"x[2]\n",
|
||||||
|
" 0.000 0.000\n",
|
||||||
|
" 0.000 0.000\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"x = [zeros(2,2) for i=1:2] #a vector of two matrices\n",
|
||||||
|
"x[1][1,1] = -99 #change an element of x[1]\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"x[1]\")\n",
|
||||||
|
"printmat(x[1])\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"x[2]\")\n",
|
||||||
|
"printmat(x[2])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Arrays are Different...\n",
|
||||||
|
"\n",
|
||||||
|
"Vectors and matrices (arrays) can take lots of memory space, so **Julia is designed to avoid unnecessary copies of arrays**."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Issue 1. B = A Creates Two Names of the Same Array\n",
|
||||||
|
"\n",
|
||||||
|
"If A is an array, then\n",
|
||||||
|
"```\n",
|
||||||
|
"B = A\n",
|
||||||
|
"```\n",
|
||||||
|
"creates two names of the *same* array. If you later change A, then B is changed automatically. (Similarly, if you change B, then A is changed automatically.) To avoid this, do\n",
|
||||||
|
"```\n",
|
||||||
|
"C = copy(A)\n",
|
||||||
|
"```"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 9,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"old A,B,C (each is a column): \n",
|
||||||
|
" A B C\n",
|
||||||
|
" 1 1 1\n",
|
||||||
|
" 2 2 2\n",
|
||||||
|
"\n",
|
||||||
|
"after changing A[2] to -999, A,B,C are:\n",
|
||||||
|
" A B C\n",
|
||||||
|
" 1 1 1\n",
|
||||||
|
" -999 -999 2\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"\u001b[34m\u001b[1mNotice that B changed, but C did not.\u001b[22m\u001b[39m\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"A = [1,2]\n",
|
||||||
|
"B = A #A and B are the same\n",
|
||||||
|
"C = copy(A) #A and C are not the same\n",
|
||||||
|
"println(\"old A,B,C (each is a column): \")\n",
|
||||||
|
"printmat([A B C],colNames=[\"A\",\"B\",\"C\"],prec=0)\n",
|
||||||
|
"\n",
|
||||||
|
"A[2] = -999\n",
|
||||||
|
"println(\"after changing A[2] to -999, A,B,C are:\")\n",
|
||||||
|
"printmat([A B C],colNames=[\"A\",\"B\",\"C\"],prec=0)\n",
|
||||||
|
"\n",
|
||||||
|
"printblue(\"\\nNotice that B changed, but C did not.\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Issue 2. A Reshaped Array Still Refers to the Original Array\n",
|
||||||
|
"\n",
|
||||||
|
"Let `A` be an array. If you create a reshaped array by either \n",
|
||||||
|
"```\n",
|
||||||
|
"B = reshape(A,n,m)\n",
|
||||||
|
"C = vec(A)\n",
|
||||||
|
"D = A'\n",
|
||||||
|
"E = fill(A,2)\n",
|
||||||
|
"```\n",
|
||||||
|
"then A, B, C, D and E contain the same values. Changing one changes the others automatically.\n",
|
||||||
|
"\n",
|
||||||
|
"This can be 'fixed' by using `copy()`\n",
|
||||||
|
"```\n",
|
||||||
|
"B = copy(reshape(A,n,m))\n",
|
||||||
|
"C = copy(vec(A))\n",
|
||||||
|
"D = copy(A')\n",
|
||||||
|
"E = [copy(A) for i=1:2]\n",
|
||||||
|
"```"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 10,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"original A: \n",
|
||||||
|
" 1 2 \n",
|
||||||
|
"\n",
|
||||||
|
"old B, C and D (each is a column): \n",
|
||||||
|
" B C D\n",
|
||||||
|
" 1 1 1\n",
|
||||||
|
" 2 2 2\n",
|
||||||
|
"\n",
|
||||||
|
"B, C and D after changing element A[2] to -999\n",
|
||||||
|
" B C D\n",
|
||||||
|
" 1 1 1\n",
|
||||||
|
" -999 -999 -999\n",
|
||||||
|
"\n",
|
||||||
|
"E[1] and E[2] after changing element A[2] to -999\n",
|
||||||
|
" 1 -999 \n",
|
||||||
|
"\n",
|
||||||
|
" 1 -999 \n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"\u001b[34m\u001b[1mNotice that B, C, D and E also changed\u001b[22m\u001b[39m\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"A = [1 2]\n",
|
||||||
|
"println(\"original A: \")\n",
|
||||||
|
"printmat(A)\n",
|
||||||
|
"\n",
|
||||||
|
"B = reshape(A,2,1)\n",
|
||||||
|
"C = vec(A)\n",
|
||||||
|
"D = A'\n",
|
||||||
|
"E = fill(A,2)\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"old B, C and D (each is a column): \")\n",
|
||||||
|
"printmat([B C D],colNames=[\"B\",\"C\",\"D\"],prec=0)\n",
|
||||||
|
"\n",
|
||||||
|
"A[2] = -999\n",
|
||||||
|
"println(\"B, C and D after changing element A[2] to -999\")\n",
|
||||||
|
"printmat([B C D],colNames=[\"B\",\"C\",\"D\"],prec=0)\n",
|
||||||
|
"println(\"E[1] and E[2] after changing element A[2] to -999\")\n",
|
||||||
|
"printmat(E[1])\n",
|
||||||
|
"printmat(E[2])\n",
|
||||||
|
"\n",
|
||||||
|
"printblue(\"\\nNotice that B, C, D and E also changed\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Issue 3. Changing an Array Inside a Function Can Have Effects *Outside* the Function"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"When you use an array as a function argument, then it is passed as a reference.\n",
|
||||||
|
"\n",
|
||||||
|
"This means that if you change some elements of the array (`A[1] = A[1]/2`, say) inside the function, then it will also affect the array outside the function (even if they have different names). \n",
|
||||||
|
"\n",
|
||||||
|
"In contrast, if you change the entire array (`A/2`, say) inside the function, then that does not affect the array outside the function.\n",
|
||||||
|
"\n",
|
||||||
|
"If you really need an independent copy of an array, create it by\n",
|
||||||
|
"\n",
|
||||||
|
"`B = copy(A)`"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 11,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"original x:\n",
|
||||||
|
" 1.000 2.000\n",
|
||||||
|
"\n",
|
||||||
|
"x (outside function) after calling f1(x):\n",
|
||||||
|
" 0.500 2.000\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"original x:\n",
|
||||||
|
" 1.000 2.000\n",
|
||||||
|
"\n",
|
||||||
|
"x (outside function) after calling f2(x):\n",
|
||||||
|
" 1.000 2.000\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"\u001b[34m\u001b[1mNotice that f1() changed x also outside the function, but f2() did not.\u001b[22m\u001b[39m\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"function f1(A)\n",
|
||||||
|
" A[1] = A[1]/2 #changes ELEMENTS of A, affects outside value\n",
|
||||||
|
" return A\n",
|
||||||
|
"end\n",
|
||||||
|
"function f2(A)\n",
|
||||||
|
" A = A/2 #changes all of A, does not affect outside value\n",
|
||||||
|
" return A\n",
|
||||||
|
"end\n",
|
||||||
|
"\n",
|
||||||
|
"x = [1.0 2.0]\n",
|
||||||
|
"printlnPs(\"original x:\")\n",
|
||||||
|
"printmat(x)\n",
|
||||||
|
"\n",
|
||||||
|
"y1 = f1(x)\n",
|
||||||
|
"printlnPs(\"x (outside function) after calling f1(x):\")\n",
|
||||||
|
"printmat(x)\n",
|
||||||
|
"\n",
|
||||||
|
"x = [1.0 2.0]\n",
|
||||||
|
"printlnPs(\"\\noriginal x:\")\n",
|
||||||
|
"printmat(x)\n",
|
||||||
|
"\n",
|
||||||
|
"y2 = f2(x)\n",
|
||||||
|
"printlnPs(\"x (outside function) after calling f2(x):\")\n",
|
||||||
|
"printmat(x)\n",
|
||||||
|
"\n",
|
||||||
|
"printblue(\"\\nNotice that f1() changed x also outside the function, but f2() did not.\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Issue 4. Arrays in Arrays Still Refer to the Underlying Arrays\n",
|
||||||
|
"\n",
|
||||||
|
"An array `a` inside another array `A`is really just a reference to the existing `a`. Changing elements of `a` will change `A`.\n",
|
||||||
|
"\n",
|
||||||
|
"Again, this can be 'fixed' by using `copy(a)` when creating `A`."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 12,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"A:\n",
|
||||||
|
" 11 12 \n",
|
||||||
|
" 21 22 \n",
|
||||||
|
"\n",
|
||||||
|
"A nice dog\n",
|
||||||
|
"\n",
|
||||||
|
" 27 \n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"\u001b[34m\u001b[1mChange a[1,1] to -999 and notice that also A changes\u001b[22m\u001b[39m\n",
|
||||||
|
"A:\n",
|
||||||
|
" -999 12 \n",
|
||||||
|
" 21 22 \n",
|
||||||
|
"\n",
|
||||||
|
"A nice dog\n",
|
||||||
|
"\n",
|
||||||
|
" 27 \n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"a = [11 12;21 22]\n",
|
||||||
|
"A = [a,\"A nice dog\",27] #try copy(a) instead\n",
|
||||||
|
"println(\"A:\")\n",
|
||||||
|
"foreach(i->printmat(A[i]),1:length(A))\n",
|
||||||
|
"\n",
|
||||||
|
"printblue(\"\\nChange a[1,1] to -999 and notice that also A changes\")\n",
|
||||||
|
"a[1,1] = -999\n",
|
||||||
|
"println(\"A:\")\n",
|
||||||
|
"foreach(i->printmat(A[i]),1:length(A))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"@webio": {
|
||||||
|
"lastCommId": null,
|
||||||
|
"lastKernelId": null
|
||||||
|
},
|
||||||
|
"anaconda-cloud": {},
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Julia 1.6.1",
|
||||||
|
"language": "julia",
|
||||||
|
"name": "julia-1.6"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"file_extension": ".jl",
|
||||||
|
"mimetype": "application/julia",
|
||||||
|
"name": "julia",
|
||||||
|
"version": "1.6.1"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 1
|
||||||
|
}
|
59
JuliaTutorial-master/jlFiles/printTeXTable.jl
Normal file
59
JuliaTutorial-master/jlFiles/printTeXTable.jl
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
#------------------------------------------------------------------------------
|
||||||
|
"""
|
||||||
|
printTeXTable(fh::IO,x;colNames=[],rowNames=[],width=12,prec=2) or
|
||||||
|
printTeXTable(x;colNames=[],rowNames=[],width=12,prec=2) or
|
||||||
|
|
||||||
|
Create LaTeX table from numeric matrix x. Print to file or screen and returns a string
|
||||||
|
with the table contents.
|
||||||
|
|
||||||
|
# Input
|
||||||
|
-`fh::IOStream`:
|
||||||
|
-`x::`:
|
||||||
|
-`colNames::`:
|
||||||
|
-`rowNames::`:
|
||||||
|
-`width::`:
|
||||||
|
-`prec::`:
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
|
function printTeXTable(fh::IO,x;colNames=[],rowNames=[],width=12,prec=2)
|
||||||
|
|
||||||
|
(m,n) = (size(x,1),size(x,2))
|
||||||
|
|
||||||
|
isempty(rowNames) && (rowNames = [string("r",i) for i = 1:m]) #create row names "r1"
|
||||||
|
isempty(colNames) && (colNames = [string("c",i) for i = 1:n]) #create column names "c1"
|
||||||
|
|
||||||
|
str = """
|
||||||
|
\\begin{table}
|
||||||
|
\\begin{tabular}{l"""
|
||||||
|
str = string(str,"r"^n,"}\n"," ")
|
||||||
|
|
||||||
|
str = string(str," & ") #empty cell above rowNames[1]
|
||||||
|
for i = 1:n-1 #column names
|
||||||
|
str = string(str,colNames[i]," & ")
|
||||||
|
end
|
||||||
|
str = string(str,colNames[n]," \\\\ \\hline \n")
|
||||||
|
|
||||||
|
for i = 1:m #loop over rows
|
||||||
|
str = string(str," ",rowNames[i]," & ") #row name
|
||||||
|
for j = 1:n-1
|
||||||
|
str = string(str,fmtNumPs(x[i,j],width,prec,"right"), " & ") #x[i,1:end-1]
|
||||||
|
end
|
||||||
|
str = string(str,fmtNumPs(x[i,n],width,prec,"right")," \\\\ \n") #x[i,end]
|
||||||
|
end
|
||||||
|
|
||||||
|
endstr = """
|
||||||
|
\\hline
|
||||||
|
\\end{tabular}
|
||||||
|
\\end{table}"""
|
||||||
|
str = string(str,endstr,"\n")
|
||||||
|
|
||||||
|
print(fh,str) #print
|
||||||
|
|
||||||
|
return str
|
||||||
|
|
||||||
|
end
|
||||||
|
#when fh is not supplied: printing to screen
|
||||||
|
printTeXTable(x;colNames=[],rowNames=[],width=12,prec=2) =
|
||||||
|
printTeXTable(stdout::IO,x;colNames,rowNames,width,prec)
|
||||||
|
#------------------------------------------------------------------------------
|
246
JuliaTutorial-master/jlFiles/printmat.jl
Normal file
246
JuliaTutorial-master/jlFiles/printmat.jl
Normal file
|
@ -0,0 +1,246 @@
|
||||||
|
#------------------------------------------------------------------------------
|
||||||
|
"""
|
||||||
|
printmat([fh::IO],x...;colNames=[],rowNames=[],
|
||||||
|
width=10,prec=3,NoPrinting=false,StringFmt="",cell00="")
|
||||||
|
|
||||||
|
Print all elements of a matrix (or several) with predefined formatting. It can also handle
|
||||||
|
OffsetArrays. StringFmt = "csv" prints using a csv format.
|
||||||
|
|
||||||
|
# Input
|
||||||
|
- `fh::IO`: (optional) file handle. If not supplied, prints to screen
|
||||||
|
- `x::Array(s)`: (of numbers, dates, strings, ...) to print
|
||||||
|
- `colNames::Array`: of strings with column headers
|
||||||
|
- `rowNames::Array`: of strings with row labels
|
||||||
|
- `width::Int`: (keyword) scalar, minimum width of printed cells
|
||||||
|
- `prec::Int`: (keyword) scalar, precision of printed cells
|
||||||
|
- `NoPrinting::Bool`: (keyword) bool, true: no printing, just return formatted string [false]
|
||||||
|
- `StringFmt::String`: (keyword) string, "", "csv"
|
||||||
|
- `cell00::String`: (keyword) string, for row 0, column 0
|
||||||
|
|
||||||
|
# Output
|
||||||
|
- str (if NoPrinting) string, (otherwise nothing)
|
||||||
|
|
||||||
|
# Examples
|
||||||
|
```
|
||||||
|
x = [11 12;21 22]
|
||||||
|
printmat(x)
|
||||||
|
```
|
||||||
|
```
|
||||||
|
x = [1 "ab"; Date(2018,10,7) 3.14]
|
||||||
|
printmat(x,width=20,colNames=["col 1","col 2"])
|
||||||
|
```
|
||||||
|
```
|
||||||
|
printmat([11,12],[21,22])
|
||||||
|
```
|
||||||
|
Can also call as
|
||||||
|
```
|
||||||
|
opt = Dict(:rowNames=>["1";"4"],:width=>10,:prec=>3,:NoPrinting=>false,:StringFmt=>"")
|
||||||
|
printmat(x;colNames=["a","b"],opt...) #notice ; and ...
|
||||||
|
```
|
||||||
|
(not all keywords are needed)
|
||||||
|
|
||||||
|
# Requires
|
||||||
|
- fmtNumPs
|
||||||
|
|
||||||
|
# Notice
|
||||||
|
- The prefixN and suffixN could potentially be made function inputs. This would allow
|
||||||
|
a fairly flexible way to format tables.
|
||||||
|
|
||||||
|
|
||||||
|
Paul.Soderlind@unisg.ch
|
||||||
|
|
||||||
|
"""
|
||||||
|
function printmat(fh::IO,x...;colNames=[],rowNames=[],
|
||||||
|
width=10,prec=3,NoPrinting=false,StringFmt="",cell00="")
|
||||||
|
|
||||||
|
isempty(x) && return nothing #do nothing is isempty(x)
|
||||||
|
|
||||||
|
typeTestQ = any(!=(eltype(x[1])),[eltype(z) for z in x]) #test if eltype(x[i]) differs
|
||||||
|
if typeTestQ #create matrix from tuple created by x...
|
||||||
|
x = hcat(Matrix{Any}(hcat(x[1])),x[2:end]...) #preserving types of x[i]
|
||||||
|
else
|
||||||
|
x = hcat(x...)
|
||||||
|
end
|
||||||
|
|
||||||
|
(m,n) = (size(x,1),size(x,2))
|
||||||
|
|
||||||
|
(length(rowNames) == 1 < m) && (rowNames = [string(rowNames[1],i) for i = 1:m]) #"ri"
|
||||||
|
(length(colNames) == 1 < n) && (colNames = [string(colNames[1],i) for i = 1:n]) #"ci"
|
||||||
|
|
||||||
|
if StringFmt == "csv"
|
||||||
|
(prefixN,suffixN) = (fill("",n),vcat(fill(",",n-1),"")) #prefix and suffix for column 1:n
|
||||||
|
(prefixC0,suffixC0) = ("",",") #prefix and suffix for column 0
|
||||||
|
else
|
||||||
|
(prefixN,suffixN) = (fill("",n),fill("",n))
|
||||||
|
(prefixC0,suffixC0) = ("","")
|
||||||
|
end
|
||||||
|
|
||||||
|
if length(rowNames) == 0 #width of column 0 (cell00 and rowNames)
|
||||||
|
col0Width = 0
|
||||||
|
else
|
||||||
|
col0Width = maximum(length,vcat(cell00,rowNames)) + length(prefixC0) + length(suffixC0)
|
||||||
|
end
|
||||||
|
|
||||||
|
colWidth = [width + length(prefixN[j]) + length(suffixN[j]) for j=1:n] #widths of column 1:n
|
||||||
|
|
||||||
|
iob = IOBuffer()
|
||||||
|
|
||||||
|
if !isempty(colNames) #print (cell00,colNames), if any
|
||||||
|
!isempty(cell00) ? txt0 = string(prefixC0,cell00,suffixC0) : txt0 = ""
|
||||||
|
print(iob,rpad(txt0,col0Width))
|
||||||
|
for j = 1:n #loop over columns
|
||||||
|
print(iob,lpad(string(prefixN[j],colNames[j],suffixN[j]),colWidth[j]))
|
||||||
|
end
|
||||||
|
print(iob,"\n")
|
||||||
|
end
|
||||||
|
#print rowNames and x
|
||||||
|
(i0,j0) = (1 - first(axes(x,1)),1 - first(axes(x,2))) #i+i0,j+j0 give traditional indices
|
||||||
|
for i in axes(x,1) #loop over rows
|
||||||
|
!isempty(rowNames) && print(iob,rpad(string(prefixC0,rowNames[i+i0],suffixC0),col0Width))
|
||||||
|
for j in axes(x,2) #loop over columns
|
||||||
|
print(iob,fmtNumPs(x[i,j],width,prec,"right",prefix=prefixN[j+j0],suffix=suffixN[j+j0]))
|
||||||
|
end
|
||||||
|
print(iob,"\n")
|
||||||
|
end
|
||||||
|
str = String(take!(iob))
|
||||||
|
|
||||||
|
if NoPrinting #no printing, just return str
|
||||||
|
return str
|
||||||
|
else #print, return nothing
|
||||||
|
print(fh,str,"\n")
|
||||||
|
return nothing
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
#when fh is not supplied: printing to screen
|
||||||
|
printmat(x...;colNames=[],rowNames=[],width=10,prec=3,NoPrinting=false,StringFmt="",cell00="") =
|
||||||
|
printmat(stdout::IO,x...;colNames,rowNames,width,prec,NoPrinting,StringFmt,cell00)
|
||||||
|
#------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
#------------------------------------------------------------------------------
|
||||||
|
"""
|
||||||
|
printlnPs([fh::IO],z...;width=10,prec=3)
|
||||||
|
|
||||||
|
Subsitute for println, with predefined formatting.
|
||||||
|
|
||||||
|
|
||||||
|
# Input
|
||||||
|
- `fh::IO`: (optional) file handle. If not supplied, prints to screen
|
||||||
|
- `z::String`: string, numbers and arrays to print
|
||||||
|
|
||||||
|
Paul.Soderlind@unisg.ch
|
||||||
|
|
||||||
|
"""
|
||||||
|
function printlnPs(fh::IO,z...;width=10,prec=3)
|
||||||
|
|
||||||
|
for x in z #loop over inputs in z...
|
||||||
|
if isa(x,AbstractArray)
|
||||||
|
iob = IOBuffer()
|
||||||
|
for i = 1:length(x)
|
||||||
|
print(iob,fmtNumPs(x[i],width,prec,"right"))
|
||||||
|
end
|
||||||
|
print(fh,String(take!(iob)))
|
||||||
|
else
|
||||||
|
print(fh,fmtNumPs(x,width,prec,"right"))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
print(fh,"\n")
|
||||||
|
|
||||||
|
end
|
||||||
|
#when fh is not supplied: printing to screen
|
||||||
|
printlnPs(z...;width=10,prec=3) = printlnPs(stdout::IO,z...;width,prec)
|
||||||
|
#------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
#------------------------------------------------------------------------------
|
||||||
|
"""
|
||||||
|
fmtNumPs(z,width=10,prec=2,justify="right";prefix="",suffix="")
|
||||||
|
|
||||||
|
Create a formatted string of a float (eg, "%10.4f"), nothing (""),
|
||||||
|
while other values are passed through. Strings are right (or left) justified
|
||||||
|
and can optionally be given prefix and suffix (eg, ",")
|
||||||
|
|
||||||
|
# Notice
|
||||||
|
- With prec > 0 and isa(z,Integer), then the string is padded with 1+prec spaces
|
||||||
|
to align with the printing of floats with the same prec.
|
||||||
|
|
||||||
|
# Requires
|
||||||
|
- Printf (for 1.6-), fmtNumPsC (for < 1.6)
|
||||||
|
|
||||||
|
"""
|
||||||
|
function fmtNumPs(z,width=10,prec=2,justify="right";prefix="",suffix="")
|
||||||
|
|
||||||
|
isa(z,Bool) && (z = convert(Int,z)) #Bool -> Int
|
||||||
|
|
||||||
|
if isa(z,AbstractFloat) #example: 101.0234, prec=3
|
||||||
|
if VERSION < v"1.6-"
|
||||||
|
fmt = "%$(width).$(prec)f"
|
||||||
|
zRound = round(z,digits=prec)
|
||||||
|
strLR = fmtNumPsC(fmt,zRound) #C fallback solution
|
||||||
|
else
|
||||||
|
fmt = Printf.Format("%$(width).$(prec)f")
|
||||||
|
strLR = Printf.format(fmt,z)
|
||||||
|
end
|
||||||
|
elseif isa(z,Nothing)
|
||||||
|
strLR = ""
|
||||||
|
elseif isa(z,Integer) && prec > 0 #integer followed by (1+prec spaces)
|
||||||
|
strLR = string(z," "^(1+prec))
|
||||||
|
else #Int, String, Date, Missing, etc
|
||||||
|
strLR = string(z)
|
||||||
|
end
|
||||||
|
|
||||||
|
strLR = string(prefix,strLR,suffix)
|
||||||
|
|
||||||
|
if justify == "left" #justification
|
||||||
|
strLR = rpad(strLR,width+length(prefix)+length(suffix))
|
||||||
|
else
|
||||||
|
strLR = lpad(strLR,width+length(prefix)+length(suffix))
|
||||||
|
end
|
||||||
|
|
||||||
|
return strLR
|
||||||
|
|
||||||
|
end
|
||||||
|
#------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
#------------------------------------------------------------------------------
|
||||||
|
"""
|
||||||
|
fmtNumPsC(fmt,z)
|
||||||
|
|
||||||
|
c fallback solution for formatting of floating point number. Used if VERSION < v"1.6-"
|
||||||
|
"""
|
||||||
|
function fmtNumPsC(fmt,z) #c fallback solution
|
||||||
|
if ismissing(z) || isnan(z) || isinf(z) #asprintf does not work for these cases
|
||||||
|
str = string(z)
|
||||||
|
else
|
||||||
|
strp = Ref{Ptr{Cchar}}(0)
|
||||||
|
len = ccall(:asprintf,Cint,(Ptr{Ptr{Cchar}},Cstring,Cdouble...),strp,fmt,z)
|
||||||
|
str = unsafe_string(strp[],len)
|
||||||
|
Libc.free(strp[])
|
||||||
|
end
|
||||||
|
return str
|
||||||
|
end
|
||||||
|
#------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
#------------------------------------------------------------------------------
|
||||||
|
function printblue(x...)
|
||||||
|
foreach(z->printstyled(z,color=:blue,bold=true),x)
|
||||||
|
print("\n")
|
||||||
|
end
|
||||||
|
function printred(x...)
|
||||||
|
foreach(z->printstyled(z,color=:red,bold=true),x)
|
||||||
|
print("\n")
|
||||||
|
end
|
||||||
|
function printmagenta(x...)
|
||||||
|
foreach(z->printstyled(z,color=:magenta,bold=true),x)
|
||||||
|
print("\n")
|
||||||
|
end
|
||||||
|
function printyellow(x...)
|
||||||
|
foreach(z->printstyled(z,color=:yellow,bold=true),x)
|
||||||
|
print("\n")
|
||||||
|
end
|
||||||
|
#------------------------------------------------------------------------------
|
||||||
|
|
BIN
Problemsets/.DS_Store
vendored
Normal file
BIN
Problemsets/.DS_Store
vendored
Normal file
Binary file not shown.
12867
Problemsets/Data/25_Portfolios_5x5_Daily.CSV
Normal file
12867
Problemsets/Data/25_Portfolios_5x5_Daily.CSV
Normal file
File diff suppressed because it is too large
Load Diff
613
Problemsets/Data/Portfolios_SGLV.csv
Normal file
613
Problemsets/Data/Portfolios_SGLV.csv
Normal file
|
@ -0,0 +1,613 @@
|
||||||
|
Date,SmallGrowth,LargeValue,Market
|
||||||
|
1970-01-01,-5.48,-4.875,-8.1
|
||||||
|
1970-02-01,1.465,7.546,5.13
|
||||||
|
1970-03-01,-7.232,1.061,-1.06
|
||||||
|
1970-04-01,-24.488,-8.481,-11.0
|
||||||
|
1970-05-01,-10.808,-4.552,-6.92
|
||||||
|
1970-06-01,-12.834,-8.749,-5.79
|
||||||
|
1970-07-01,4.862,5.726,6.93
|
||||||
|
1970-08-01,4.64,5.883,4.49
|
||||||
|
1970-09-01,21.801,3.943,4.18
|
||||||
|
1970-10-01,-12.134,-4.378,-2.28
|
||||||
|
1970-11-01,-5.227,5.534,4.6
|
||||||
|
1970-12-01,6.418,6.491,5.72
|
||||||
|
1971-01-01,15.029,3.907,4.84
|
||||||
|
1971-02-01,7.725,0.937,1.41
|
||||||
|
1971-03-01,7.754,2.246,4.13
|
||||||
|
1971-04-01,0.584,5.963,3.15
|
||||||
|
1971-05-01,-6.92,-3.829,-3.98
|
||||||
|
1971-06-01,-1.05,-0.564,-0.1
|
||||||
|
1971-07-01,-9.103,-5.887,-4.5
|
||||||
|
1971-08-01,2.874,13.117,3.79
|
||||||
|
1971-09-01,1.143,-2.519,-0.85
|
||||||
|
1971-10-01,-8.265,-4.386,-4.42
|
||||||
|
1971-11-01,-3.762,-0.087,-0.46
|
||||||
|
1971-12-01,14.068,8.569,8.71
|
||||||
|
1972-01-01,13.088,6.093,2.49
|
||||||
|
1972-02-01,2.516,-0.186,2.87
|
||||||
|
1972-03-01,2.505,2.59,0.63
|
||||||
|
1972-04-01,-1.651,-1.378,0.29
|
||||||
|
1972-05-01,-1.939,-2.966,1.25
|
||||||
|
1972-06-01,-4.608,-6.964,-2.43
|
||||||
|
1972-07-01,-7.521,-1.893,-0.8
|
||||||
|
1972-08-01,-1.859,6.016,3.26
|
||||||
|
1972-09-01,-5.28,-3.957,-1.14
|
||||||
|
1972-10-01,-4.095,0.399,0.52
|
||||||
|
1972-11-01,-0.403,13.583,4.6
|
||||||
|
1972-12-01,-2.22,-3.17,0.62
|
||||||
|
1973-01-01,-8.346,-4.609,-3.29
|
||||||
|
1973-02-01,-14.592,-3.788,-4.85
|
||||||
|
1973-03-01,-7.226,1.08,-1.3
|
||||||
|
1973-04-01,-12.277,-3.424,-5.68
|
||||||
|
1973-05-01,-11.85,-6.19,-2.94
|
||||||
|
1973-06-01,-5.898,-2.439,-1.56
|
||||||
|
1973-07-01,18.219,3.58,5.05
|
||||||
|
1973-08-01,-6.005,0.082,-3.82
|
||||||
|
1973-09-01,11.269,7.588,4.75
|
||||||
|
1973-10-01,-3.255,1.898,-0.83
|
||||||
|
1973-11-01,-25.643,-8.808,-12.75
|
||||||
|
1973-12-01,-4.478,10.494,0.61
|
||||||
|
1974-01-01,9.816,-0.712,-0.17
|
||||||
|
1974-02-01,-0.986,3.233,-0.47
|
||||||
|
1974-03-01,-0.462,-5.319,-2.81
|
||||||
|
1974-04-01,-5.388,-6.171,-5.29
|
||||||
|
1974-05-01,-8.878,-8.554,-4.68
|
||||||
|
1974-06-01,-4.299,0.97,-2.83
|
||||||
|
1974-07-01,-9.245,-5.804,-8.05
|
||||||
|
1974-08-01,-9.834,-9.243,-9.35
|
||||||
|
1974-09-01,-14.269,-7.436,-11.77
|
||||||
|
1974-10-01,16.45,0.803,16.1
|
||||||
|
1974-11-01,-6.6,-4.646,-4.51
|
||||||
|
1974-12-01,-7.854,1.178,-3.45
|
||||||
|
1975-01-01,24.883,15.084,13.66
|
||||||
|
1975-02-01,7.319,-3.674,5.56
|
||||||
|
1975-03-01,8.548,5.741,2.66
|
||||||
|
1975-04-01,6.68,4.582,4.23
|
||||||
|
1975-05-01,11.216,0.516,5.19
|
||||||
|
1975-06-01,5.331,7.015,4.83
|
||||||
|
1975-07-01,-2.088,-2.035,-6.59
|
||||||
|
1975-08-01,-7.603,-4.944,-2.85
|
||||||
|
1975-09-01,-5.759,-5.884,-4.26
|
||||||
|
1975-10-01,0.839,3.341,5.31
|
||||||
|
1975-11-01,-0.406,4.27,2.64
|
||||||
|
1975-12-01,-1.526,4.768,-1.6
|
||||||
|
1976-01-01,15.261,14.693,12.16
|
||||||
|
1976-02-01,9.544,9.098,0.32
|
||||||
|
1976-03-01,3.162,-1.411,2.32
|
||||||
|
1976-04-01,-1.853,0.724,-1.49
|
||||||
|
1976-05-01,-2.126,1.012,-1.34
|
||||||
|
1976-06-01,3.835,2.513,4.05
|
||||||
|
1976-07-01,-1.592,2.176,-1.07
|
||||||
|
1976-08-01,-4.748,0.639,-0.56
|
||||||
|
1976-09-01,1.777,-1.063,2.07
|
||||||
|
1976-10-01,-3.205,-4.421,-2.42
|
||||||
|
1976-11-01,5.318,3.951,0.36
|
||||||
|
1976-12-01,10.437,7.195,5.65
|
||||||
|
1977-01-01,2.484,-0.51,-4.05
|
||||||
|
1977-02-01,-0.83,-1.284,-1.94
|
||||||
|
1977-03-01,-0.308,-0.159,-1.37
|
||||||
|
1977-04-01,0.873,5.613,0.15
|
||||||
|
1977-05-01,-2.14,-3.383,-1.45
|
||||||
|
1977-06-01,5.403,1.592,4.71
|
||||||
|
1977-07-01,0.824,-5.293,-1.69
|
||||||
|
1977-08-01,0.59,-4.644,-1.75
|
||||||
|
1977-09-01,1.836,-0.542,-0.27
|
||||||
|
1977-10-01,-5.073,-4.086,-4.38
|
||||||
|
1977-11-01,9.05,4.19,4.0
|
||||||
|
1977-12-01,4.317,0.858,0.27
|
||||||
|
1978-01-01,-1.692,-3.42,-6.01
|
||||||
|
1978-02-01,2.814,-4.084,-1.38
|
||||||
|
1978-03-01,7.743,2.867,2.85
|
||||||
|
1978-04-01,9.715,4.738,7.88
|
||||||
|
1978-05-01,8.014,1.475,1.76
|
||||||
|
1978-06-01,0.249,-1.349,-1.69
|
||||||
|
1978-07-01,5.965,4.197,5.11
|
||||||
|
1978-08-01,9.118,-0.332,3.75
|
||||||
|
1978-09-01,-1.636,0.273,-1.43
|
||||||
|
1978-10-01,-27.398,-12.797,-11.91
|
||||||
|
1978-11-01,7.99,1.846,2.71
|
||||||
|
1978-12-01,2.789,-1.529,0.88
|
||||||
|
1979-01-01,9.216,6.868,4.23
|
||||||
|
1979-02-01,-3.65,-3.547,-3.56
|
||||||
|
1979-03-01,11.309,5.709,5.68
|
||||||
|
1979-04-01,2.378,-1.787,-0.06
|
||||||
|
1979-05-01,-3.451,-2.146,-2.21
|
||||||
|
1979-06-01,5.213,1.913,3.85
|
||||||
|
1979-07-01,1.583,2.897,0.82
|
||||||
|
1979-08-01,8.104,4.534,5.53
|
||||||
|
1979-09-01,-0.967,-0.338,-0.82
|
||||||
|
1979-10-01,-11.852,-8.037,-8.1
|
||||||
|
1979-11-01,9.941,0.011,5.21
|
||||||
|
1979-12-01,10.403,1.32,1.79
|
||||||
|
1980-01-01,11.915,9.429,5.51
|
||||||
|
1980-02-01,-0.334,1.6,-1.22
|
||||||
|
1980-03-01,-23.288,-14.096,-12.9
|
||||||
|
1980-04-01,5.008,1.603,3.97
|
||||||
|
1980-05-01,6.35,5.328,5.26
|
||||||
|
1980-06-01,4.369,-0.361,3.06
|
||||||
|
1980-07-01,11.666,4.724,6.49
|
||||||
|
1980-08-01,10.789,-0.556,1.8
|
||||||
|
1980-09-01,5.863,-0.719,2.19
|
||||||
|
1980-10-01,6.703,-0.133,1.06
|
||||||
|
1980-11-01,15.272,3.036,9.59
|
||||||
|
1980-12-01,-7.662,-1.756,-4.52
|
||||||
|
1981-01-01,-5.571,0.946,-5.04
|
||||||
|
1981-02-01,-4.41,-1.685,0.57
|
||||||
|
1981-03-01,6.077,6.957,3.56
|
||||||
|
1981-04-01,1.974,-4.037,-2.11
|
||||||
|
1981-05-01,3.874,-0.598,0.11
|
||||||
|
1981-06-01,-8.49,-0.328,-2.36
|
||||||
|
1981-07-01,-5.769,-0.637,-1.54
|
||||||
|
1981-08-01,-13.971,-1.673,-7.04
|
||||||
|
1981-09-01,-14.816,-0.76,-7.17
|
||||||
|
1981-10-01,9.22,3.597,4.92
|
||||||
|
1981-11-01,-2.194,2.697,3.36
|
||||||
|
1981-12-01,-5.144,-2.664,-3.65
|
||||||
|
1982-01-01,-3.957,0.633,-3.24
|
||||||
|
1982-02-01,-8.833,-4.84,-5.86
|
||||||
|
1982-03-01,-3.018,1.056,-1.87
|
||||||
|
1982-04-01,5.374,-0.584,3.27
|
||||||
|
1982-05-01,-4.163,-2.318,-3.99
|
||||||
|
1982-06-01,-5.372,-3.838,-3.09
|
||||||
|
1982-07-01,-4.799,-2.879,-3.19
|
||||||
|
1982-08-01,4.057,11.702,11.14
|
||||||
|
1982-09-01,1.799,-0.348,1.29
|
||||||
|
1982-10-01,17.058,5.357,11.3
|
||||||
|
1982-11-01,8.406,1.045,4.67
|
||||||
|
1982-12-01,1.965,5.254,0.55
|
||||||
|
1983-01-01,12.851,2.076,3.6
|
||||||
|
1983-02-01,2.483,1.785,2.59
|
||||||
|
1983-03-01,2.214,1.202,2.82
|
||||||
|
1983-04-01,6.654,6.525,6.67
|
||||||
|
1983-05-01,11.254,0.479,0.52
|
||||||
|
1983-06-01,3.47,-0.017,3.07
|
||||||
|
1983-07-01,-6.851,-2.91,-4.07
|
||||||
|
1983-08-01,-6.796,5.86,-0.5
|
||||||
|
1983-09-01,-3.826,-0.256,0.91
|
||||||
|
1983-10-01,-11.252,-1.434,-3.44
|
||||||
|
1983-11-01,2.986,-0.079,2.16
|
||||||
|
1983-12-01,-5.796,-0.189,-1.78
|
||||||
|
1984-01-01,-2.719,6.219,-1.92
|
||||||
|
1984-02-01,-8.141,-0.031,-4.82
|
||||||
|
1984-03-01,-1.752,-2.186,0.63
|
||||||
|
1984-04-01,-4.803,1.616,-0.51
|
||||||
|
1984-05-01,-6.983,-6.834,-5.97
|
||||||
|
1984-06-01,0.242,4.59,1.82
|
||||||
|
1984-07-01,-8.053,-3.178,-2.74
|
||||||
|
1984-08-01,10.726,12.016,10.28
|
||||||
|
1984-09-01,-3.508,2.659,-0.8
|
||||||
|
1984-10-01,-4.862,-2.404,-0.84
|
||||||
|
1984-11-01,-6.414,0.594,-1.76
|
||||||
|
1984-12-01,-0.153,1.469,1.84
|
||||||
|
1985-01-01,15.272,3.871,7.99
|
||||||
|
1985-02-01,3.237,2.092,1.22
|
||||||
|
1985-03-01,-3.291,0.943,-0.84
|
||||||
|
1985-04-01,-2.893,2.238,-0.96
|
||||||
|
1985-05-01,1.839,4.12,5.09
|
||||||
|
1985-06-01,0.039,1.669,1.27
|
||||||
|
1985-07-01,1.861,-2.971,-0.74
|
||||||
|
1985-08-01,-2.084,0.217,-1.02
|
||||||
|
1985-09-01,-8.733,-4.719,-4.54
|
||||||
|
1985-10-01,0.029,5.73,4.02
|
||||||
|
1985-11-01,4.875,4.097,6.48
|
||||||
|
1985-12-01,2.949,3.755,3.88
|
||||||
|
1986-01-01,3.24,0.85,0.65
|
||||||
|
1986-02-01,5.262,6.668,7.13
|
||||||
|
1986-03-01,4.022,3.959,4.88
|
||||||
|
1986-04-01,2.23,-3.257,-1.31
|
||||||
|
1986-05-01,3.623,5.058,4.62
|
||||||
|
1986-06-01,-0.365,2.413,1.03
|
||||||
|
1986-07-01,-10.631,-3.231,-6.45
|
||||||
|
1986-08-01,-0.173,10.315,6.07
|
||||||
|
1986-09-01,-8.411,-6.837,-8.6
|
||||||
|
1986-10-01,1.486,5.502,4.66
|
||||||
|
1986-11-01,-2.595,1.199,1.17
|
||||||
|
1986-12-01,-5.987,-3.418,-3.27
|
||||||
|
1987-01-01,10.7,11.56,12.47
|
||||||
|
1987-02-01,10.191,-1.448,4.39
|
||||||
|
1987-03-01,1.665,4.324,1.64
|
||||||
|
1987-04-01,-2.761,-0.787,-2.11
|
||||||
|
1987-05-01,-0.612,0.136,0.11
|
||||||
|
1987-06-01,-0.646,4.888,3.94
|
||||||
|
1987-07-01,1.324,2.607,3.85
|
||||||
|
1987-08-01,0.328,2.142,3.52
|
||||||
|
1987-09-01,-3.294,-3.178,-2.59
|
||||||
|
1987-10-01,-34.931,-19.329,-23.24
|
||||||
|
1987-11-01,-8.849,-7.755,-7.77
|
||||||
|
1987-12-01,2.511,5.862,6.81
|
||||||
|
1988-01-01,5.701,8.359,4.21
|
||||||
|
1988-02-01,5.532,2.396,4.75
|
||||||
|
1988-03-01,3.738,-1.249,-2.27
|
||||||
|
1988-04-01,0.524,1.814,0.56
|
||||||
|
1988-05-01,-4.132,1.703,-0.29
|
||||||
|
1988-06-01,5.97,2.518,4.79
|
||||||
|
1988-07-01,-1.381,0.506,-1.25
|
||||||
|
1988-08-01,-4.732,-3.704,-3.31
|
||||||
|
1988-09-01,0.507,3.171,3.3
|
||||||
|
1988-10-01,-3.488,3.855,1.15
|
||||||
|
1988-11-01,-5.514,-0.112,-2.29
|
||||||
|
1988-12-01,1.2,-0.128,1.49
|
||||||
|
1989-01-01,4.675,7.542,6.1
|
||||||
|
1989-02-01,-1.628,-3.134,-2.25
|
||||||
|
1989-03-01,0.619,0.617,1.57
|
||||||
|
1989-04-01,2.261,3.205,4.33
|
||||||
|
1989-05-01,2.078,2.459,3.35
|
||||||
|
1989-06-01,-3.993,0.225,-1.35
|
||||||
|
1989-07-01,2.096,5.981,7.2
|
||||||
|
1989-08-01,1.487,2.199,1.44
|
||||||
|
1989-09-01,0.545,-1.15,-0.76
|
||||||
|
1989-10-01,-5.773,-0.929,-3.67
|
||||||
|
1989-11-01,-1.32,0.427,1.03
|
||||||
|
1989-12-01,-1.436,0.41,1.16
|
||||||
|
1990-01-01,-8.814,-6.348,-7.85
|
||||||
|
1990-02-01,1.434,1.533,1.11
|
||||||
|
1990-03-01,3.757,0.495,1.83
|
||||||
|
1990-04-01,-3.17,-4.856,-3.36
|
||||||
|
1990-05-01,7.526,7.149,8.42
|
||||||
|
1990-06-01,0.646,-0.933,-1.09
|
||||||
|
1990-07-01,-5.585,-2.712,-1.9
|
||||||
|
1990-08-01,-17.13,-11.245,-10.15
|
||||||
|
1990-09-01,-11.234,-8.012,-6.12
|
||||||
|
1990-10-01,-7.165,0.262,-1.92
|
||||||
|
1990-11-01,2.734,3.776,6.35
|
||||||
|
1990-12-01,-0.361,0.206,2.46
|
||||||
|
1991-01-01,6.921,3.441,4.69
|
||||||
|
1991-02-01,14.11,7.477,7.19
|
||||||
|
1991-03-01,9.909,0.753,2.65
|
||||||
|
1991-04-01,0.645,-0.694,-0.28
|
||||||
|
1991-05-01,2.061,6.433,3.65
|
||||||
|
1991-06-01,-7.358,-4.384,-4.94
|
||||||
|
1991-07-01,2.228,-1.44,4.24
|
||||||
|
1991-08-01,3.299,1.117,2.32
|
||||||
|
1991-09-01,3.787,-3.403,-1.59
|
||||||
|
1991-10-01,6.671,0.846,1.29
|
||||||
|
1991-11-01,-2.442,-9.682,-4.19
|
||||||
|
1991-12-01,6.319,13.943,10.84
|
||||||
|
1992-01-01,14.279,6.094,-0.59
|
||||||
|
1992-02-01,-0.362,10.578,1.09
|
||||||
|
1992-03-01,-6.928,0.368,-2.66
|
||||||
|
1992-04-01,-10.41,9.846,1.07
|
||||||
|
1992-05-01,-1.57,-0.544,0.3
|
||||||
|
1992-06-01,-9.48,2.649,-2.34
|
||||||
|
1992-07-01,0.426,-0.656,3.77
|
||||||
|
1992-08-01,-4.95,-8.846,-2.38
|
||||||
|
1992-09-01,0.118,-0.16,1.19
|
||||||
|
1992-10-01,2.41,-0.234,1.02
|
||||||
|
1992-11-01,10.095,8.2,4.13
|
||||||
|
1992-12-01,1.256,2.618,1.53
|
||||||
|
1993-01-01,2.65,8.967,0.93
|
||||||
|
1993-02-01,-7.671,1.733,0.12
|
||||||
|
1993-03-01,0.336,3.114,2.3
|
||||||
|
1993-04-01,-4.558,0.811,-3.05
|
||||||
|
1993-05-01,5.254,-0.711,2.89
|
||||||
|
1993-06-01,-1.805,4.398,0.31
|
||||||
|
1993-07-01,-1.914,-0.502,-0.34
|
||||||
|
1993-08-01,3.467,3.819,3.71
|
||||||
|
1993-09-01,2.13,-0.942,-0.12
|
||||||
|
1993-10-01,5.012,-0.803,1.41
|
||||||
|
1993-11-01,-4.313,0.576,-1.89
|
||||||
|
1993-12-01,0.534,1.499,1.65
|
||||||
|
1994-01-01,2.38,2.629,2.87
|
||||||
|
1994-02-01,-3.427,-5.087,-2.55
|
||||||
|
1994-03-01,-8.029,-4.13,-4.78
|
||||||
|
1994-04-01,-4.275,1.74,0.68
|
||||||
|
1994-05-01,-2.853,3.157,0.58
|
||||||
|
1994-06-01,-6.932,-4.119,-3.03
|
||||||
|
1994-07-01,0.053,1.036,2.82
|
||||||
|
1994-08-01,3.133,3.104,4.01
|
||||||
|
1994-09-01,0.579,-6.52,-2.31
|
||||||
|
1994-10-01,-1.282,2.169,1.34
|
||||||
|
1994-11-01,-4.868,-2.988,-4.04
|
||||||
|
1994-12-01,-3.532,-0.82,0.86
|
||||||
|
1995-01-01,0.985,6.307,1.8
|
||||||
|
1995-02-01,1.815,6.895,3.63
|
||||||
|
1995-03-01,0.449,0.646,2.19
|
||||||
|
1995-04-01,1.216,5.671,2.11
|
||||||
|
1995-05-01,1.075,5.436,2.9
|
||||||
|
1995-06-01,7.425,1.484,2.72
|
||||||
|
1995-07-01,6.583,2.765,3.72
|
||||||
|
1995-08-01,3.648,3.6,0.55
|
||||||
|
1995-09-01,2.999,6.513,3.35
|
||||||
|
1995-10-01,-6.56,-4.041,-1.52
|
||||||
|
1995-11-01,1.336,4.402,3.96
|
||||||
|
1995-12-01,2.014,1.448,1.03
|
||||||
|
1996-01-01,1.165,4.029,2.26
|
||||||
|
1996-02-01,3.426,2.316,1.33
|
||||||
|
1996-03-01,2.476,0.083,0.73
|
||||||
|
1996-04-01,8.139,-2.034,2.06
|
||||||
|
1996-05-01,7.478,1.914,2.36
|
||||||
|
1996-06-01,-8.163,0.133,-1.14
|
||||||
|
1996-07-01,-15.564,-5.013,-5.97
|
||||||
|
1996-08-01,4.25,2.696,2.77
|
||||||
|
1996-09-01,1.864,4.745,5.01
|
||||||
|
1996-10-01,-6.039,4.373,0.86
|
||||||
|
1996-11-01,-1.053,6.271,6.25
|
||||||
|
1996-12-01,-1.182,-1.793,-1.7
|
||||||
|
1997-01-01,5.964,3.454,4.98
|
||||||
|
1997-02-01,-6.342,4.706,-0.49
|
||||||
|
1997-03-01,-10.017,-5.895,-5.02
|
||||||
|
1997-04-01,-5.907,0.888,4.04
|
||||||
|
1997-05-01,12.202,6.078,6.74
|
||||||
|
1997-06-01,3.662,3.626,4.1
|
||||||
|
1997-07-01,3.125,7.745,7.33
|
||||||
|
1997-08-01,5.28,-6.226,-4.15
|
||||||
|
1997-09-01,10.008,6.909,5.35
|
||||||
|
1997-10-01,-6.085,-3.352,-3.8
|
||||||
|
1997-11-01,-3.745,3.171,2.98
|
||||||
|
1997-12-01,-6.551,2.602,1.32
|
||||||
|
1998-01-01,-0.218,0.227,0.15
|
||||||
|
1998-02-01,5.869,6.609,7.04
|
||||||
|
1998-03-01,4.123,6.669,4.76
|
||||||
|
1998-04-01,2.094,-1.863,0.73
|
||||||
|
1998-05-01,-6.964,-1.33,-3.07
|
||||||
|
1998-06-01,-0.859,0.538,3.18
|
||||||
|
1998-07-01,-8.118,-2.737,-2.46
|
||||||
|
1998-08-01,-28.392,-12.946,-16.08
|
||||||
|
1998-09-01,6.596,9.46,6.15
|
||||||
|
1998-10-01,3.077,2.901,7.13
|
||||||
|
1998-11-01,9.939,4.621,6.1
|
||||||
|
1998-12-01,4.684,4.651,6.16
|
||||||
|
1999-01-01,7.543,-0.588,3.5
|
||||||
|
1999-02-01,-7.903,-1.568,-4.08
|
||||||
|
1999-03-01,-2.796,1.632,3.45
|
||||||
|
1999-04-01,7.175,1.871,4.33
|
||||||
|
1999-05-01,1.606,2.584,-2.46
|
||||||
|
1999-06-01,6.485,-0.364,4.77
|
||||||
|
1999-07-01,0.572,-9.442,-3.49
|
||||||
|
1999-08-01,-3.804,-1.284,-1.38
|
||||||
|
1999-09-01,1.475,-9.174,-2.79
|
||||||
|
1999-10-01,-0.536,5.305,6.12
|
||||||
|
1999-11-01,19.126,-0.868,3.37
|
||||||
|
1999-12-01,24.726,-1.938,7.72
|
||||||
|
2000-01-01,9.104,-9.93,-4.74
|
||||||
|
2000-02-01,36.81,-11.759,2.45
|
||||||
|
2000-03-01,-14.146,21.517,5.2
|
||||||
|
2000-04-01,-23.655,-0.676,-6.4
|
||||||
|
2000-05-01,-14.848,7.191,-4.42
|
||||||
|
2000-06-01,27.997,-4.128,4.64
|
||||||
|
2000-07-01,-9.45,1.831,-2.51
|
||||||
|
2000-08-01,8.774,5.2,7.03
|
||||||
|
2000-09-01,-11.565,-2.147,-5.45
|
||||||
|
2000-10-01,-13.026,2.813,-2.76
|
||||||
|
2000-11-01,-21.778,0.038,-10.72
|
||||||
|
2000-12-01,-10.659,-0.058,1.19
|
||||||
|
2001-01-01,27.64,1.032,3.13
|
||||||
|
2001-02-01,-13.81,3.913,-10.05
|
||||||
|
2001-03-01,-13.312,-3.065,-7.26
|
||||||
|
2001-04-01,9.228,7.051,7.94
|
||||||
|
2001-05-01,12.361,5.377,0.72
|
||||||
|
2001-06-01,2.264,-3.346,-1.94
|
||||||
|
2001-07-01,-9.11,7.271,-2.13
|
||||||
|
2001-08-01,-8.593,-8.659,-6.46
|
||||||
|
2001-09-01,-16.466,1.355,-9.25
|
||||||
|
2001-10-01,8.853,-14.857,2.46
|
||||||
|
2001-11-01,6.121,11.118,7.54
|
||||||
|
2001-12-01,13.045,1.157,1.61
|
||||||
|
2002-01-01,-8.246,-9.326,-1.44
|
||||||
|
2002-02-01,-10.629,-12.893,-2.29
|
||||||
|
2002-03-01,5.59,-0.02,4.24
|
||||||
|
2002-04-01,-10.28,-22.297,-5.2
|
||||||
|
2002-05-01,-7.108,-7.847,-1.38
|
||||||
|
2002-06-01,-8.926,-13.694,-7.21
|
||||||
|
2002-07-01,-19.652,-14.825,-8.18
|
||||||
|
2002-08-01,-0.121,8.181,0.5
|
||||||
|
2002-09-01,-11.181,-6.331,-10.35
|
||||||
|
2002-10-01,6.1,13.993,7.84
|
||||||
|
2002-11-01,17.892,6.929,5.96
|
||||||
|
2002-12-01,-10.364,-9.849,-5.76
|
||||||
|
2003-01-01,-0.238,-5.686,-2.57
|
||||||
|
2003-02-01,-6.478,-5.011,-1.88
|
||||||
|
2003-03-01,2.094,-3.05,1.09
|
||||||
|
2003-04-01,12.452,14.718,8.22
|
||||||
|
2003-05-01,20.564,7.756,6.05
|
||||||
|
2003-06-01,6.097,3.564,1.42
|
||||||
|
2003-07-01,8.68,1.236,2.35
|
||||||
|
2003-08-01,4.237,1.354,2.34
|
||||||
|
2003-09-01,3.293,-4.242,-1.24
|
||||||
|
2003-10-01,5.985,2.687,6.08
|
||||||
|
2003-11-01,2.743,2.839,1.35
|
||||||
|
2003-12-01,0.162,4.891,4.29
|
||||||
|
2004-01-01,5.966,6.662,2.15
|
||||||
|
2004-02-01,-0.059,5.476,1.4
|
||||||
|
2004-03-01,-1.642,-1.147,-1.32
|
||||||
|
2004-04-01,-4.572,-2.088,-1.83
|
||||||
|
2004-05-01,-1.778,1.179,1.17
|
||||||
|
2004-06-01,0.854,2.288,1.86
|
||||||
|
2004-07-01,-13.045,-0.993,-4.06
|
||||||
|
2004-08-01,-1.704,-0.217,0.08
|
||||||
|
2004-09-01,4.253,3.621,1.6
|
||||||
|
2004-10-01,1.131,2.535,1.43
|
||||||
|
2004-11-01,10.675,5.904,4.54
|
||||||
|
2004-12-01,5.33,2.611,3.43
|
||||||
|
2005-01-01,-6.511,0.214,-2.76
|
||||||
|
2005-02-01,-1.276,4.906,1.89
|
||||||
|
2005-03-01,-6.436,-0.755,-1.97
|
||||||
|
2005-04-01,-5.969,-2.832,-2.61
|
||||||
|
2005-05-01,6.428,3.969,3.65
|
||||||
|
2005-06-01,3.639,3.053,0.57
|
||||||
|
2005-07-01,7.424,3.973,3.92
|
||||||
|
2005-08-01,-3.127,-0.661,-1.22
|
||||||
|
2005-09-01,0.79,1.904,0.49
|
||||||
|
2005-10-01,-3.77,-1.387,-2.02
|
||||||
|
2005-11-01,4.946,2.037,3.61
|
||||||
|
2005-12-01,-0.675,-0.791,-0.25
|
||||||
|
2006-01-01,11.466,3.447,3.04
|
||||||
|
2006-02-01,-0.826,-1.514,-0.3
|
||||||
|
2006-03-01,3.696,0.445,1.46
|
||||||
|
2006-04-01,-1.394,4.986,0.73
|
||||||
|
2006-05-01,-9.914,-3.002,-3.57
|
||||||
|
2006-06-01,-1.3,0.987,-0.35
|
||||||
|
2006-07-01,-6.239,2.435,-0.78
|
||||||
|
2006-08-01,3.14,-0.622,2.03
|
||||||
|
2006-09-01,-0.593,3.872,1.84
|
||||||
|
2006-10-01,6.783,4.178,3.23
|
||||||
|
2006-11-01,0.572,-0.366,1.71
|
||||||
|
2006-12-01,0.21,2.692,0.87
|
||||||
|
2007-01-01,1.031,2.165,1.4
|
||||||
|
2007-02-01,-1.203,-3.469,-1.96
|
||||||
|
2007-03-01,0.018,-0.928,0.68
|
||||||
|
2007-04-01,2.857,4.351,3.49
|
||||||
|
2007-05-01,1.816,2.266,3.24
|
||||||
|
2007-06-01,-0.044,-2.603,-1.96
|
||||||
|
2007-07-01,-5.868,-5.29,-3.73
|
||||||
|
2007-08-01,0.584,-0.347,0.92
|
||||||
|
2007-09-01,1.844,3.887,3.22
|
||||||
|
2007-10-01,3.181,-0.752,1.8
|
||||||
|
2007-11-01,-10.235,-5.791,-4.83
|
||||||
|
2007-12-01,-1.244,1.034,-0.87
|
||||||
|
2008-01-01,-11.523,-4.773,-6.36
|
||||||
|
2008-02-01,-5.938,-5.501,-3.09
|
||||||
|
2008-03-01,-3.863,0.931,-0.93
|
||||||
|
2008-04-01,3.388,6.001,4.6
|
||||||
|
2008-05-01,5.467,1.668,1.86
|
||||||
|
2008-06-01,-6.342,-10.217,-8.44
|
||||||
|
2008-07-01,4.872,7.964,-0.77
|
||||||
|
2008-08-01,0.885,-0.478,1.53
|
||||||
|
2008-09-01,-13.399,-4.041,-9.24
|
||||||
|
2008-10-01,-23.606,-13.611,-17.23
|
||||||
|
2008-11-01,-13.07,-17.865,-7.86
|
||||||
|
2008-12-01,2.79,3.71,1.74
|
||||||
|
2009-01-01,-6.87,-14.319,-8.12
|
||||||
|
2009-02-01,-11.135,-12.35,-10.1
|
||||||
|
2009-03-01,11.056,10.91,8.95
|
||||||
|
2009-04-01,19.143,20.242,10.18
|
||||||
|
2009-05-01,6.138,9.93,5.21
|
||||||
|
2009-06-01,7.723,-2.783,0.43
|
||||||
|
2009-07-01,8.657,9.442,7.72
|
||||||
|
2009-08-01,2.944,12.003,3.33
|
||||||
|
2009-09-01,4.556,2.567,4.08
|
||||||
|
2009-10-01,-10.592,-9.417,-2.59
|
||||||
|
2009-11-01,-0.486,4.264,5.56
|
||||||
|
2009-12-01,6.133,-3.057,2.75
|
||||||
|
2010-01-01,-4.179,-2.656,-3.36
|
||||||
|
2010-02-01,4.014,5.636,3.4
|
||||||
|
2010-03-01,6.866,8.288,6.31
|
||||||
|
2010-04-01,6.485,-0.498,2.0
|
||||||
|
2010-05-01,-6.948,-8.103,-7.89
|
||||||
|
2010-06-01,-6.499,-9.347,-5.57
|
||||||
|
2010-07-01,6.253,5.325,6.93
|
||||||
|
2010-08-01,-8.726,-8.952,-4.77
|
||||||
|
2010-09-01,11.603,6.979,9.54
|
||||||
|
2010-10-01,6.453,-0.239,3.88
|
||||||
|
2010-11-01,1.535,-3.261,0.6
|
||||||
|
2010-12-01,10.168,13.91,6.82
|
||||||
|
2011-01-01,-2.948,3.314,1.99
|
||||||
|
2011-02-01,1.482,3.954,3.49
|
||||||
|
2011-03-01,3.553,-3.259,0.46
|
||||||
|
2011-04-01,2.714,1.119,2.9
|
||||||
|
2011-05-01,-1.041,-3.751,-1.27
|
||||||
|
2011-06-01,-4.507,-2.769,-1.75
|
||||||
|
2011-07-01,-5.042,-5.069,-2.35
|
||||||
|
2011-08-01,-11.94,-11.843,-5.99
|
||||||
|
2011-09-01,-11.489,-14.131,-7.59
|
||||||
|
2011-10-01,12.494,15.136,11.35
|
||||||
|
2011-11-01,-2.27,-6.567,-0.28
|
||||||
|
2011-12-01,-0.718,0.93,0.74
|
||||||
|
2012-01-01,12.409,10.024,5.05
|
||||||
|
2012-02-01,1.067,6.691,4.42
|
||||||
|
2012-03-01,2.203,7.797,3.11
|
||||||
|
2012-04-01,-3.952,-4.218,-0.85
|
||||||
|
2012-05-01,-8.355,-12.903,-6.19
|
||||||
|
2012-06-01,7.331,5.617,3.89
|
||||||
|
2012-07-01,-2.368,-1.732,0.79
|
||||||
|
2012-08-01,2.331,6.432,2.55
|
||||||
|
2012-09-01,4.591,5.426,2.73
|
||||||
|
2012-10-01,-5.355,3.309,-1.76
|
||||||
|
2012-11-01,-0.097,-1.561,0.78
|
||||||
|
2012-12-01,0.266,7.238,1.18
|
||||||
|
2013-01-01,6.567,7.355,5.57
|
||||||
|
2013-02-01,-1.633,0.697,1.29
|
||||||
|
2013-03-01,8.115,3.33,4.03
|
||||||
|
2013-04-01,-0.001,2.755,1.55
|
||||||
|
2013-05-01,7.731,8.931,2.8
|
||||||
|
2013-06-01,-0.796,-2.81,-1.2
|
||||||
|
2013-07-01,7.032,7.211,5.65
|
||||||
|
2013-08-01,-2.331,-4.49,-2.71
|
||||||
|
2013-09-01,7.162,2.223,3.77
|
||||||
|
2013-10-01,-0.825,3.716,4.18
|
||||||
|
2013-11-01,7.779,6.322,3.13
|
||||||
|
2013-12-01,1.88,1.87,2.81
|
||||||
|
2014-01-01,3.428,-4.111,-3.32
|
||||||
|
2014-02-01,4.825,2.442,4.65
|
||||||
|
2014-03-01,-4.922,3.599,0.43
|
||||||
|
2014-04-01,-8.674,-2.133,-0.19
|
||||||
|
2014-05-01,-2.733,1.242,2.06
|
||||||
|
2014-06-01,5.585,3.461,2.61
|
||||||
|
2014-07-01,-9.171,-1.368,-2.04
|
||||||
|
2014-08-01,2.879,5.14,4.24
|
||||||
|
2014-09-01,-6.993,-0.181,-1.97
|
||||||
|
2014-10-01,5.793,1.495,2.52
|
||||||
|
2014-11-01,0.163,0.989,2.55
|
||||||
|
2014-12-01,5.903,2.397,-0.06
|
||||||
|
2015-01-01,-3.49,-7.737,-3.11
|
||||||
|
2015-02-01,4.085,5.818,6.14
|
||||||
|
2015-03-01,-0.947,-1.57,-1.12
|
||||||
|
2015-04-01,-3.905,1.88,0.59
|
||||||
|
2015-05-01,3.438,1.702,1.36
|
||||||
|
2015-06-01,4.313,-0.565,-1.53
|
||||||
|
2015-07-01,-5.261,0.632,1.54
|
||||||
|
2015-08-01,-8.048,-7.682,-6.04
|
||||||
|
2015-09-01,-8.285,-5.687,-3.07
|
||||||
|
2015-10-01,6.046,7.644,7.75
|
||||||
|
2015-11-01,3.799,3.217,0.56
|
||||||
|
2015-12-01,-3.517,-4.191,-2.17
|
||||||
|
2016-01-01,-13.415,-12.122,-5.77
|
||||||
|
2016-02-01,-2.523,-6.505,-0.08
|
||||||
|
2016-03-01,4.083,7.547,6.96
|
||||||
|
2016-04-01,2.816,7.172,0.92
|
||||||
|
2016-05-01,0.556,1.898,1.78
|
||||||
|
2016-06-01,-3.901,-6.7,-0.05
|
||||||
|
2016-07-01,4.712,4.395,3.95
|
||||||
|
2016-08-01,3.068,4.569,0.5
|
||||||
|
2016-09-01,11.696,0.2,0.25
|
||||||
|
2016-10-01,-10.647,2.361,-2.02
|
||||||
|
2016-11-01,7.437,14.112,4.86
|
||||||
|
2016-12-01,1.347,4.154,1.82
|
||||||
|
2017-01-01,1.809,-0.323,1.94
|
||||||
|
2017-02-01,2.693,4.468,3.57
|
||||||
|
2017-03-01,1.957,-3.139,0.17
|
||||||
|
2017-04-01,3.816,-0.81,1.09
|
||||||
|
2017-05-01,-2.027,-2.239,1.06
|
||||||
|
2017-06-01,9.202,5.126,0.78
|
||||||
|
2017-07-01,-0.795,2.22,1.87
|
||||||
|
2017-08-01,-0.352,-1.768,0.16
|
||||||
|
2017-09-01,7.586,4.995,2.51
|
||||||
|
2017-10-01,-3.185,4.152,2.25
|
||||||
|
2017-11-01,2.208,2.332,3.12
|
||||||
|
2017-12-01,1.521,1.116,1.06
|
||||||
|
2018-01-01,2.685,5.923,5.58
|
||||||
|
2018-02-01,-2.445,-2.093,-3.65
|
||||||
|
2018-03-01,2.32,-4.761,-2.35
|
||||||
|
2018-04-01,-0.599,-0.241,0.29
|
||||||
|
2018-05-01,13.107,-1.736,2.65
|
||||||
|
2018-06-01,3.935,-2.491,0.48
|
||||||
|
2018-07-01,-1.579,5.506,3.19
|
||||||
|
2018-08-01,4.563,1.244,3.44
|
||||||
|
2018-09-01,-3.793,-1.748,0.06
|
||||||
|
2018-10-01,-14.282,-7.72,-7.68
|
||||||
|
2018-11-01,-1.29,0.422,1.69
|
||||||
|
2018-12-01,-14.041,-12.948,-9.55
|
||||||
|
2019-01-01,11.66,12.946,8.41
|
||||||
|
2019-02-01,7.392,1.562,3.4
|
||||||
|
2019-03-01,0.645,-3.368,1.1
|
||||||
|
2019-04-01,0.484,8.746,3.96
|
||||||
|
2019-05-01,-5.649,-8.963,-6.94
|
||||||
|
2019-06-01,7.558,7.775,6.93
|
||||||
|
2019-07-01,-3.385,3.015,1.19
|
||||||
|
2019-08-01,-5.044,-8.774,-2.58
|
||||||
|
2019-09-01,-3.529,5.147,1.43
|
||||||
|
2019-10-01,0.628,3.551,2.06
|
||||||
|
2019-11-01,7.87,4.762,3.87
|
||||||
|
2019-12-01,3.645,3.878,2.77
|
||||||
|
2020-01-01,-1.408,-4.369,-0.11
|
||||||
|
2020-02-01,-6.249,-11.83,-8.13
|
||||||
|
2020-03-01,-21.609,-28.521,-13.38
|
||||||
|
2020-04-01,24.436,15.535,13.65
|
||||||
|
2020-05-01,13.662,3.789,5.58
|
||||||
|
2020-06-01,11.08,2.102,2.46
|
||||||
|
2020-07-01,0.218,2.932,5.77
|
||||||
|
2020-08-01,3.567,3.473,7.63
|
||||||
|
2020-09-01,-1.28,-6.122,-3.63
|
||||||
|
2020-10-01,-2.694,0.978,-2.1
|
||||||
|
2020-11-01,25.45,21.968,12.47
|
||||||
|
2020-12-01,13.172,8.036,4.63
|
|
312
Problemsets/MC_pi.ipynb
Normal file
312
Problemsets/MC_pi.ipynb
Normal file
File diff suppressed because one or more lines are too long
206
Problemsets/MV_by_MC.ipynb
Normal file
206
Problemsets/MV_by_MC.ipynb
Normal file
File diff suppressed because one or more lines are too long
1584
Problemsets/PS01_Basics.ipynb
Normal file
1584
Problemsets/PS01_Basics.ipynb
Normal file
File diff suppressed because one or more lines are too long
340
Problemsets/PS02_Stats.ipynb
Normal file
340
Problemsets/PS02_Stats.ipynb
Normal file
File diff suppressed because one or more lines are too long
244
Problemsets/PS03_CLT.ipynb
Normal file
244
Problemsets/PS03_CLT.ipynb
Normal file
File diff suppressed because one or more lines are too long
258
Problemsets/PS04_YieldToMaturity.ipynb
Normal file
258
Problemsets/PS04_YieldToMaturity.ipynb
Normal file
File diff suppressed because one or more lines are too long
136
Problemsets/PS05_TradingStrategy.ipynb
Normal file
136
Problemsets/PS05_TradingStrategy.ipynb
Normal file
|
@ -0,0 +1,136 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"printyellow (generic function with 1 method)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"using Printf, Dates, Statistics, DelimitedFiles, StatsBase\n",
|
||||||
|
"\n",
|
||||||
|
"include(\"jlFiles/printmat.jl\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"using Plots\n",
|
||||||
|
"\n",
|
||||||
|
"gr(size=(480,320))\n",
|
||||||
|
"default(fmt = :svg)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Load Data"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 3,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"(10340, 25)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 3,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"x = readdlm(\"Data/25_Portfolios_5x5_Daily.CSV\",',',skipstart=1) #daily return data\n",
|
||||||
|
"ym = round.(Int,x[:,1]) #yearmonthday, like 20071231\n",
|
||||||
|
"\n",
|
||||||
|
"dN = Date.(string.(ym),\"yyyymmdd\") #covert to Julia date, eg. 2001-12-31\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"vv = Date(1980,1,1) .<= dN .<= Date(2020,12,31) #pick out the correct sample\n",
|
||||||
|
"ym = ym[vv]\n",
|
||||||
|
"R = x[vv,2:end] #returns\n",
|
||||||
|
"\n",
|
||||||
|
"(T,n) = size(R) #number of data points, number of assets"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Task 1: Two Simple Strategies\n",
|
||||||
|
"\n",
|
||||||
|
"`R_1`: go long each of asset 1-24 (each with the weight 1/24) and short asset 25\n",
|
||||||
|
"\n",
|
||||||
|
"`R_2`: go long asset 1 and short asset 25\n",
|
||||||
|
"\n",
|
||||||
|
"The returns of these portfolios are easy to calculate without having to explicitly construct the portfolio weights, but it still a good preparation for later to do the explicit calculations as follows:\n",
|
||||||
|
"\n",
|
||||||
|
"1. Construct the vector of portfolio weights `w`\n",
|
||||||
|
"2. The portfolio return in `t` is `w'*R[t,:]`.\n",
|
||||||
|
"\n",
|
||||||
|
"Also, do not be afraid of loops: they are quick.\n",
|
||||||
|
"\n",
|
||||||
|
"Show means and standard deviations of the two strategies. Annualize the mean by `*252` and the standard deviation by `*sqrt(252)`.\n",
|
||||||
|
"\n",
|
||||||
|
"Plot histograms with bins that are 0.25 wide. (Don't annualize anything in the histograms.)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Task 2: Another Trading Strategy\n",
|
||||||
|
"\n",
|
||||||
|
"We now do simple volatility based trading strategy.\n",
|
||||||
|
"\n",
|
||||||
|
"1. Find the 3 least volatile assets over `t-22:t-1` and give each a portfolio weight `w[t,i]=1/3`. \n",
|
||||||
|
"\n",
|
||||||
|
"2. Find the 3 most volatile assets over `t-22:t-1` and give each a portfolio weight `w[t,i]=-1/3`. \n",
|
||||||
|
"\n",
|
||||||
|
"3. The portfolio return in `t` is `w[t,:]'*R[t,:]`.\n",
|
||||||
|
"\n",
|
||||||
|
"4. Compare the average and std (annualized) with the previous portfolios, over periods `23:T`\n",
|
||||||
|
"\n",
|
||||||
|
"Hint: `v = sortperm(x)` gives indices such that `v[1:2]` are the indices of the lowest 2 elements in x. Try `sortperm([12,11,13])` to see."
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"@webio": {
|
||||||
|
"lastCommId": null,
|
||||||
|
"lastKernelId": null
|
||||||
|
},
|
||||||
|
"anaconda-cloud": {},
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Julia 1.6.2",
|
||||||
|
"language": "julia",
|
||||||
|
"name": "julia-1.6"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"file_extension": ".jl",
|
||||||
|
"mimetype": "application/julia",
|
||||||
|
"name": "julia",
|
||||||
|
"version": "1.6.2"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 1
|
||||||
|
}
|
315
Problemsets/PS05_TradingStrategy_Solution.ipynb
Normal file
315
Problemsets/PS05_TradingStrategy_Solution.ipynb
Normal file
File diff suppressed because one or more lines are too long
59
Problemsets/jlFiles/printTeXTable.jl
Normal file
59
Problemsets/jlFiles/printTeXTable.jl
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
#------------------------------------------------------------------------------
|
||||||
|
"""
|
||||||
|
printTeXTable(fh::IO,x;colNames=[],rowNames=[],width=12,prec=2) or
|
||||||
|
printTeXTable(x;colNames=[],rowNames=[],width=12,prec=2) or
|
||||||
|
|
||||||
|
Create LaTeX table from numeric matrix x. Print to file or screen and returns a string
|
||||||
|
with the table contents.
|
||||||
|
|
||||||
|
# Input
|
||||||
|
-`fh::IOStream`:
|
||||||
|
-`x::`:
|
||||||
|
-`colNames::`:
|
||||||
|
-`rowNames::`:
|
||||||
|
-`width::`:
|
||||||
|
-`prec::`:
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
|
function printTeXTable(fh::IO,x;colNames=[],rowNames=[],width=12,prec=2)
|
||||||
|
|
||||||
|
(m,n) = (size(x,1),size(x,2))
|
||||||
|
|
||||||
|
isempty(rowNames) && (rowNames = [string("r",i) for i = 1:m]) #create row names "r1"
|
||||||
|
isempty(colNames) && (colNames = [string("c",i) for i = 1:n]) #create column names "c1"
|
||||||
|
|
||||||
|
str = """
|
||||||
|
\\begin{table}
|
||||||
|
\\begin{tabular}{l"""
|
||||||
|
str = string(str,"r"^n,"}\n"," ")
|
||||||
|
|
||||||
|
str = string(str," & ") #empty cell above rowNames[1]
|
||||||
|
for i = 1:n-1 #column names
|
||||||
|
str = string(str,colNames[i]," & ")
|
||||||
|
end
|
||||||
|
str = string(str,colNames[n]," \\\\ \\hline \n")
|
||||||
|
|
||||||
|
for i = 1:m #loop over rows
|
||||||
|
str = string(str," ",rowNames[i]," & ") #row name
|
||||||
|
for j = 1:n-1
|
||||||
|
str = string(str,fmtNumPs(x[i,j],width,prec,"right"), " & ") #x[i,1:end-1]
|
||||||
|
end
|
||||||
|
str = string(str,fmtNumPs(x[i,n],width,prec,"right")," \\\\ \n") #x[i,end]
|
||||||
|
end
|
||||||
|
|
||||||
|
endstr = """
|
||||||
|
\\hline
|
||||||
|
\\end{tabular}
|
||||||
|
\\end{table}"""
|
||||||
|
str = string(str,endstr,"\n")
|
||||||
|
|
||||||
|
print(fh,str) #print
|
||||||
|
|
||||||
|
return str
|
||||||
|
|
||||||
|
end
|
||||||
|
#when fh is not supplied: printing to screen
|
||||||
|
printTeXTable(x;colNames=[],rowNames=[],width=12,prec=2) =
|
||||||
|
printTeXTable(stdout::IO,x;colNames,rowNames,width,prec)
|
||||||
|
#------------------------------------------------------------------------------
|
246
Problemsets/jlFiles/printmat.jl
Normal file
246
Problemsets/jlFiles/printmat.jl
Normal file
|
@ -0,0 +1,246 @@
|
||||||
|
#------------------------------------------------------------------------------
|
||||||
|
"""
|
||||||
|
printmat([fh::IO],x...;colNames=[],rowNames=[],
|
||||||
|
width=10,prec=3,NoPrinting=false,StringFmt="",cell00="")
|
||||||
|
|
||||||
|
Print all elements of a matrix (or several) with predefined formatting. It can also handle
|
||||||
|
OffsetArrays. StringFmt = "csv" prints using a csv format.
|
||||||
|
|
||||||
|
# Input
|
||||||
|
- `fh::IO`: (optional) file handle. If not supplied, prints to screen
|
||||||
|
- `x::Array(s)`: (of numbers, dates, strings, ...) to print
|
||||||
|
- `colNames::Array`: of strings with column headers
|
||||||
|
- `rowNames::Array`: of strings with row labels
|
||||||
|
- `width::Int`: (keyword) scalar, minimum width of printed cells
|
||||||
|
- `prec::Int`: (keyword) scalar, precision of printed cells
|
||||||
|
- `NoPrinting::Bool`: (keyword) bool, true: no printing, just return formatted string [false]
|
||||||
|
- `StringFmt::String`: (keyword) string, "", "csv"
|
||||||
|
- `cell00::String`: (keyword) string, for row 0, column 0
|
||||||
|
|
||||||
|
# Output
|
||||||
|
- str (if NoPrinting) string, (otherwise nothing)
|
||||||
|
|
||||||
|
# Examples
|
||||||
|
```
|
||||||
|
x = [11 12;21 22]
|
||||||
|
printmat(x)
|
||||||
|
```
|
||||||
|
```
|
||||||
|
x = [1 "ab"; Date(2018,10,7) 3.14]
|
||||||
|
printmat(x,width=20,colNames=["col 1","col 2"])
|
||||||
|
```
|
||||||
|
```
|
||||||
|
printmat([11,12],[21,22])
|
||||||
|
```
|
||||||
|
Can also call as
|
||||||
|
```
|
||||||
|
opt = Dict(:rowNames=>["1";"4"],:width=>10,:prec=>3,:NoPrinting=>false,:StringFmt=>"")
|
||||||
|
printmat(x;colNames=["a","b"],opt...) #notice ; and ...
|
||||||
|
```
|
||||||
|
(not all keywords are needed)
|
||||||
|
|
||||||
|
# Requires
|
||||||
|
- fmtNumPs
|
||||||
|
|
||||||
|
# Notice
|
||||||
|
- The prefixN and suffixN could potentially be made function inputs. This would allow
|
||||||
|
a fairly flexible way to format tables.
|
||||||
|
|
||||||
|
|
||||||
|
Paul.Soderlind@unisg.ch
|
||||||
|
|
||||||
|
"""
|
||||||
|
function printmat(fh::IO,x...;colNames=[],rowNames=[],
|
||||||
|
width=10,prec=3,NoPrinting=false,StringFmt="",cell00="")
|
||||||
|
|
||||||
|
isempty(x) && return nothing #do nothing is isempty(x)
|
||||||
|
|
||||||
|
typeTestQ = any(!=(eltype(x[1])),[eltype(z) for z in x]) #test if eltype(x[i]) differs
|
||||||
|
if typeTestQ #create matrix from tuple created by x...
|
||||||
|
x = hcat(Matrix{Any}(hcat(x[1])),x[2:end]...) #preserving types of x[i]
|
||||||
|
else
|
||||||
|
x = hcat(x...)
|
||||||
|
end
|
||||||
|
|
||||||
|
(m,n) = (size(x,1),size(x,2))
|
||||||
|
|
||||||
|
(length(rowNames) == 1 < m) && (rowNames = [string(rowNames[1],i) for i = 1:m]) #"ri"
|
||||||
|
(length(colNames) == 1 < n) && (colNames = [string(colNames[1],i) for i = 1:n]) #"ci"
|
||||||
|
|
||||||
|
if StringFmt == "csv"
|
||||||
|
(prefixN,suffixN) = (fill("",n),vcat(fill(",",n-1),"")) #prefix and suffix for column 1:n
|
||||||
|
(prefixC0,suffixC0) = ("",",") #prefix and suffix for column 0
|
||||||
|
else
|
||||||
|
(prefixN,suffixN) = (fill("",n),fill("",n))
|
||||||
|
(prefixC0,suffixC0) = ("","")
|
||||||
|
end
|
||||||
|
|
||||||
|
if length(rowNames) == 0 #width of column 0 (cell00 and rowNames)
|
||||||
|
col0Width = 0
|
||||||
|
else
|
||||||
|
col0Width = maximum(length,vcat(cell00,rowNames)) + length(prefixC0) + length(suffixC0)
|
||||||
|
end
|
||||||
|
|
||||||
|
colWidth = [width + length(prefixN[j]) + length(suffixN[j]) for j=1:n] #widths of column 1:n
|
||||||
|
|
||||||
|
iob = IOBuffer()
|
||||||
|
|
||||||
|
if !isempty(colNames) #print (cell00,colNames), if any
|
||||||
|
!isempty(cell00) ? txt0 = string(prefixC0,cell00,suffixC0) : txt0 = ""
|
||||||
|
print(iob,rpad(txt0,col0Width))
|
||||||
|
for j = 1:n #loop over columns
|
||||||
|
print(iob,lpad(string(prefixN[j],colNames[j],suffixN[j]),colWidth[j]))
|
||||||
|
end
|
||||||
|
print(iob,"\n")
|
||||||
|
end
|
||||||
|
#print rowNames and x
|
||||||
|
(i0,j0) = (1 - first(axes(x,1)),1 - first(axes(x,2))) #i+i0,j+j0 give traditional indices
|
||||||
|
for i in axes(x,1) #loop over rows
|
||||||
|
!isempty(rowNames) && print(iob,rpad(string(prefixC0,rowNames[i+i0],suffixC0),col0Width))
|
||||||
|
for j in axes(x,2) #loop over columns
|
||||||
|
print(iob,fmtNumPs(x[i,j],width,prec,"right",prefix=prefixN[j+j0],suffix=suffixN[j+j0]))
|
||||||
|
end
|
||||||
|
print(iob,"\n")
|
||||||
|
end
|
||||||
|
str = String(take!(iob))
|
||||||
|
|
||||||
|
if NoPrinting #no printing, just return str
|
||||||
|
return str
|
||||||
|
else #print, return nothing
|
||||||
|
print(fh,str,"\n")
|
||||||
|
return nothing
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
#when fh is not supplied: printing to screen
|
||||||
|
printmat(x...;colNames=[],rowNames=[],width=10,prec=3,NoPrinting=false,StringFmt="",cell00="") =
|
||||||
|
printmat(stdout::IO,x...;colNames,rowNames,width,prec,NoPrinting,StringFmt,cell00)
|
||||||
|
#------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
#------------------------------------------------------------------------------
|
||||||
|
"""
|
||||||
|
printlnPs([fh::IO],z...;width=10,prec=3)
|
||||||
|
|
||||||
|
Subsitute for println, with predefined formatting.
|
||||||
|
|
||||||
|
|
||||||
|
# Input
|
||||||
|
- `fh::IO`: (optional) file handle. If not supplied, prints to screen
|
||||||
|
- `z::String`: string, numbers and arrays to print
|
||||||
|
|
||||||
|
Paul.Soderlind@unisg.ch
|
||||||
|
|
||||||
|
"""
|
||||||
|
function printlnPs(fh::IO,z...;width=10,prec=3)
|
||||||
|
|
||||||
|
for x in z #loop over inputs in z...
|
||||||
|
if isa(x,AbstractArray)
|
||||||
|
iob = IOBuffer()
|
||||||
|
for i = 1:length(x)
|
||||||
|
print(iob,fmtNumPs(x[i],width,prec,"right"))
|
||||||
|
end
|
||||||
|
print(fh,String(take!(iob)))
|
||||||
|
else
|
||||||
|
print(fh,fmtNumPs(x,width,prec,"right"))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
print(fh,"\n")
|
||||||
|
|
||||||
|
end
|
||||||
|
#when fh is not supplied: printing to screen
|
||||||
|
printlnPs(z...;width=10,prec=3) = printlnPs(stdout::IO,z...;width,prec)
|
||||||
|
#------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
#------------------------------------------------------------------------------
|
||||||
|
"""
|
||||||
|
fmtNumPs(z,width=10,prec=2,justify="right";prefix="",suffix="")
|
||||||
|
|
||||||
|
Create a formatted string of a float (eg, "%10.4f"), nothing (""),
|
||||||
|
while other values are passed through. Strings are right (or left) justified
|
||||||
|
and can optionally be given prefix and suffix (eg, ",")
|
||||||
|
|
||||||
|
# Notice
|
||||||
|
- With prec > 0 and isa(z,Integer), then the string is padded with 1+prec spaces
|
||||||
|
to align with the printing of floats with the same prec.
|
||||||
|
|
||||||
|
# Requires
|
||||||
|
- Printf (for 1.6-), fmtNumPsC (for < 1.6)
|
||||||
|
|
||||||
|
"""
|
||||||
|
function fmtNumPs(z,width=10,prec=2,justify="right";prefix="",suffix="")
|
||||||
|
|
||||||
|
isa(z,Bool) && (z = convert(Int,z)) #Bool -> Int
|
||||||
|
|
||||||
|
if isa(z,AbstractFloat) #example: 101.0234, prec=3
|
||||||
|
if VERSION < v"1.6-"
|
||||||
|
fmt = "%$(width).$(prec)f"
|
||||||
|
zRound = round(z,digits=prec)
|
||||||
|
strLR = fmtNumPsC(fmt,zRound) #C fallback solution
|
||||||
|
else
|
||||||
|
fmt = Printf.Format("%$(width).$(prec)f")
|
||||||
|
strLR = Printf.format(fmt,z)
|
||||||
|
end
|
||||||
|
elseif isa(z,Nothing)
|
||||||
|
strLR = ""
|
||||||
|
elseif isa(z,Integer) && prec > 0 #integer followed by (1+prec spaces)
|
||||||
|
strLR = string(z," "^(1+prec))
|
||||||
|
else #Int, String, Date, Missing, etc
|
||||||
|
strLR = string(z)
|
||||||
|
end
|
||||||
|
|
||||||
|
strLR = string(prefix,strLR,suffix)
|
||||||
|
|
||||||
|
if justify == "left" #justification
|
||||||
|
strLR = rpad(strLR,width+length(prefix)+length(suffix))
|
||||||
|
else
|
||||||
|
strLR = lpad(strLR,width+length(prefix)+length(suffix))
|
||||||
|
end
|
||||||
|
|
||||||
|
return strLR
|
||||||
|
|
||||||
|
end
|
||||||
|
#------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
#------------------------------------------------------------------------------
|
||||||
|
"""
|
||||||
|
fmtNumPsC(fmt,z)
|
||||||
|
|
||||||
|
c fallback solution for formatting of floating point number. Used if VERSION < v"1.6-"
|
||||||
|
"""
|
||||||
|
function fmtNumPsC(fmt,z) #c fallback solution
|
||||||
|
if ismissing(z) || isnan(z) || isinf(z) #asprintf does not work for these cases
|
||||||
|
str = string(z)
|
||||||
|
else
|
||||||
|
strp = Ref{Ptr{Cchar}}(0)
|
||||||
|
len = ccall(:asprintf,Cint,(Ptr{Ptr{Cchar}},Cstring,Cdouble...),strp,fmt,z)
|
||||||
|
str = unsafe_string(strp[],len)
|
||||||
|
Libc.free(strp[])
|
||||||
|
end
|
||||||
|
return str
|
||||||
|
end
|
||||||
|
#------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
#------------------------------------------------------------------------------
|
||||||
|
function printblue(x...)
|
||||||
|
foreach(z->printstyled(z,color=:blue,bold=true),x)
|
||||||
|
print("\n")
|
||||||
|
end
|
||||||
|
function printred(x...)
|
||||||
|
foreach(z->printstyled(z,color=:red,bold=true),x)
|
||||||
|
print("\n")
|
||||||
|
end
|
||||||
|
function printmagenta(x...)
|
||||||
|
foreach(z->printstyled(z,color=:magenta,bold=true),x)
|
||||||
|
print("\n")
|
||||||
|
end
|
||||||
|
function printyellow(x...)
|
||||||
|
foreach(z->printstyled(z,color=:yellow,bold=true),x)
|
||||||
|
print("\n")
|
||||||
|
end
|
||||||
|
#------------------------------------------------------------------------------
|
||||||
|
|
Loading…
Reference in New Issue
Block a user