diff --git a/fastlib/u/pram/mog_em/faster/build.py b/fastlib/u/pram/mog_em/faster/build.py new file mode 100644 index 0000000000..6ec98e8330 --- /dev/null +++ b/fastlib/u/pram/mog_em/faster/build.py @@ -0,0 +1,26 @@ + +librule( + name = "mog_em", # this line can be safely omitted + sources = ["mog.cc"], # files that must be compiled + headers = ["mog.h","phi.h","math_functions.h"],# include files part of the 'lib' + deplibs = ["fastlib:fastlib"], # depends on fastlib core + #tests = ["mog_em_tests.cc"] # this file contains a main with test functions + ) + +binrule( + name = "mog_em_main", # the executable name + sources = ["mog_em_main.cc"], # compile main.cc + #headers = ["mog_em_main.h"], # no extra headers + deplibs = [":mog_em", "fastlib:fastlib"] # + ) + +# to build: +# 1. make sure have environment variables set up: +# $ source /full/path/to/fastlib/script/fl-env /full/path/to/fastlib +# (you might want to put this in bashrc) +# 2. fl-build main +# - this automatically will assume --mode=check, the default +# - type fl-build --help for help +# 3. ./main +# - to build same target again, type: make +# - to force recompilation, type: make clean diff --git a/fastlib/u/pram/mog_em/faster/data.arff b/fastlib/u/pram/mog_em/faster/data.arff new file mode 100644 index 0000000000..d95f367d0f --- /dev/null +++ b/fastlib/u/pram/mog_em/faster/data.arff @@ -0,0 +1,1000 @@ +-0.28992,-0.13173,-0.61575 +-1.2142,-1.1994,-0.4661 +0.031128,0.084721,-0.057328 +-0.86099,-0.79692,-0.35194 +-0.45353,-0.11696,-0.15629 +0.82319,0.31796,0.5654 +0.87603,0.62511,0.54203 +-1.8458,-1.2202,-1.6845 +1.3571,0.86032,0.95889 +-0.88157,-0.092799,-0.21391 +-1.7254,-0.80534,-1.2231 +-3.7444,-2.7681,-2.8587 +-1.9593,-1.9499,-1.6215 +0.036513,-0.65684,-0.16414 +-1.0091,-0.9538,-0.82084 +-2.6299,-2.4503,-2.0805 +1.4404,0.91235,1.0368 +2.3232,1.7905,1.3835 +-0.22953,0.29435,-0.55121 +0.52442,0.53273,0.29293 +1.9345,1.1211,1.5277 +-0.71909,-0.91217,-0.13798 +-1.0023,-0.31518,-0.83793 +0.88445,0.55393,0.63455 +-0.14286,0.75819,0.10212 +-0.57519,-0.41389,-0.16138 +-0.54536,-0.54785,0.19468 +0.18259,-0.038652,-0.11602 +1.5583,1.5063,1.1687 +0.67228,0.25056,0.28417 +1.8047,1.3339,1.8819 +1.6772,1.8611,1.5816 +0.30442,0.39109,0.31353 +-0.66552,-0.38255,-0.36366 +-0.99614,-0.31771,0.10233 +0.65813,0.72853,0.66725 +1.3433,0.87076,0.9503 +0.5952,0.54358,0.15799 +0.80337,0.48182,0.92845 +-1.0895,-0.77068,-0.43275 +-1.2246,-1.0886,-0.81559 +-3.2332,-2.2593,-1.8321 +2.36,1.5532,1.98 +-1.417,-0.72475,-1.6775 +0.48999,0.27822,0.63959 +1.7494,1.1849,1.2174 +0.098846,0.48884,0.58291 +-2.2389,-1.239,-1.5603 +-2.5684,-2.0838,-2.044 +1.833,1.9878,1.488 +-1.3408,-0.41722,-0.78372 +0.41603,-0.03137,-0.19393 +1.3308,0.97246,0.50187 +1.8763,1.4224,1.5111 +0.47414,0.35165,0.45279 +0.72983,0.62162,0.81429 +0.072535,-0.15613,0.19178 +0.24045,0.31484,0.73666 +-0.63399,-0.44661,0.16174 +-0.7575,-0.29941,-0.23175 +-0.22151,0.13724,-0.053466 +-0.61552,-0.30699,-0.18651 +0.13717,0.14514,-0.13645 +0.79648,1.0154,0.66609 +0.02903,0.11115,-0.027887 +1.2644,0.91254,0.91018 +-0.36039,-0.27273,-0.093994 +1.8088,1.6939,1.1004 +-1.4649,-1.1623,-1.3663 +0.16635,0.21012,-0.5929 +-1.9238,-0.92677,-1.2546 +-1.1433,-1.2331,-0.83555 +-1.3747,-1.2459,-0.9011 +0.66298,0.6126,0.17198 +-0.4638,-0.12425,-0.29331 +-1.1176,-0.7349,-0.67311 +0.13308,0.25935,0.25244 +-0.068732,-0.5169,-0.16423 +-0.56212,-0.51442,-0.3723 +-1.7361,-1.4933,-1.4681 +0.98691,0.45932,0.37285 +0.21825,-0.36061,-0.26815 +0.90569,0.70056,0.68695 +1.9013,1.2122,1.4488 +-1.7678,-1.0156,-1.173 +0.38102,-0.26481,0.1876 +-1.7408,-1.4657,-1.3288 +-0.40932,-0.036133,0.12221 +0.15014,0.23639,0.54873 +-1.9972,-1.75,-1.8577 +-1.8673,-1.0512,-1.2368 +0.33336,1.028,0.67552 +0.12516,0.33989,0.54363 +1.2947,1.1484,1.3026 +-2.6568,-1.9047,-1.6224 +0.80258,1.1538,0.43664 +0.68373,0.22631,-0.13506 +2.0724,1.9613,1.4781 +-0.057605,0.069219,0.048158 +0.25527,0.50668,-0.053834 +0.017855,-0.36902,-0.23829 +-1.3289,-0.86749,-1.0304 +0.66139,0.22237,0.9768 +-2.1005,-1.4237,-1.72 +-1.234,-0.94788,-1.1833 +0.10204,0.51948,0.74726 +0.89834,0.80405,1.2404 +1.012,1.4959,1.1823 +0.30367,0.5883,-0.0046778 +2.7521,1.9868,1.8043 +2.097,1.4677,0.92925 +-1.1235,-1.4098,-1.2711 +1.9084,1.657,1.1495 +0.78211,0.49159,0.83696 +2.7431,1.8348,1.6386 +0.87418,0.68548,0.7164 +0.066816,0.048781,0.49303 +0.62474,0.62775,0.61916 +2.9857,1.2037,1.6174 +0.050662,-0.24521,-0.2071 +-3.307,-2.388,-2.5187 +-2.6089,-1.4105,-1.8392 +-1.4298,-0.83004,-0.81826 +-1.9463,-0.7075,-1.1085 +-1.0799,-1.2061,-1.2246 +-0.54161,-0.25041,0.083609 +-0.99172,-0.78297,-0.77374 +-2.3501,-2.3071,-1.7381 +-0.51825,-0.2618,0.25004 +1.205,1.3003,1.2781 +-0.47089,-0.52361,0.071943 +-1.581,-1.0454,-1.438 +0.19374,0.0014186,-0.52438 +-0.41816,-0.055584,-0.097029 +0.2367,0.78135,0.15525 +0.52643,-0.097311,0.22236 +-3.2296,-2.0068,-2.1137 +0.92813,0.43725,0.67604 +0.45642,0.71451,0.11568 +-0.82585,0.051156,-0.10125 +0.86194,0.50485,0.6331 +0.53631,-0.35131,0.16827 +0.82404,0.35693,0.2579 +-1.9772,-0.79196,-0.85444 +-0.89974,-0.71668,-0.65297 +-0.91065,-0.59001,-0.6308 +-1.3831,-1.0118,-0.89703 +-0.15873,-0.020554,-0.13402 +0.95565,0.33478,0.57809 +1.6549,1.2895,1.165 +1.2448,1.0888,0.91904 +0.11118,-0.25732,0.31858 +-1.6271,-1.1146,-1.0263 +-0.29143,-0.27272,-0.71234 +0.56502,0.20699,0.73495 +0.041273,0.73347,0.24046 +0.13242,0.022178,0.46973 +-1.2083,-1.0423,-1.0835 +-2.4447,-1.4434,-2.3133 +-0.27241,-0.41174,-0.14483 +1.0588,0.39549,0.48859 +0.017031,0.30603,-0.10919 +0.72013,0.87843,0.4069 +-0.083852,0.054321,-0.3228 +0.85398,0.51678,0.74805 +1.8232,1.168,1.4461 +-1.6247,-1.3238,-1.5044 +-1.7106,-1.0363,-1.4024 +-1.5783,-0.85538,-1.0742 +1.4944,1.4094,0.90905 +1.0882,0.097209,0.051214 +-1.0669,-1.3542,-0.72685 +0.51076,0.16209,0.48204 +-1.8495,-1.21,-1.2395 +-0.22546,-0.4974,-0.017868 +3.0372,1.9304,1.8972 +-0.28054,0.059221,-0.054742 +0.93185,0.74408,0.8145 +0.52087,0.16419,0.37096 +-0.33261,-0.3717,-0.52816 +-1.547,-0.94888,-1.2357 +-2.7099,-1.1015,-1.7222 +-0.53998,-0.040121,-0.46382 +-1.1362,-0.51085,-1.1435 +-1.6958,-1.0641,-0.91265 +0.98161,0.38866,0.73232 +-1.1036,-1.0885,-1.037 +0.30512,-0.26361,0.48154 +0.59232,0.019852,0.26333 +-0.42607,0.0024313,0.17732 +0.54953,0.36402,0.68034 +2.3398,1.6947,1.5355 +-2.0822,-1.5711,-1.6259 +-0.87478,-0.86737,-0.3825 +-0.77088,-0.57907,-0.37721 +-2.1322,-1.548,-1.7005 +-0.036936,-0.0048763,-0.24452 +1.1224,0.72815,1.0709 +3.1389,2.084,1.9859 +1.9767,1.6508,1.262 +1.6246,1.1363,1.1108 +-1.5215,-1.2044,-0.87585 +1.0962,0.40736,0.56413 +1.6983,1.4339,1.5041 +-0.23987,0.020822,-0.19235 +1.0057,-0.083055,0.30357 +-2.1844,-1.4789,-1.6182 +1.2439,0.73236,0.24275 +-2.5633,-1.6744,-1.4432 +-2.2554,-1.38,-1.5194 +-0.91629,-0.86468,-0.73961 +-0.14336,-0.21176,-0.39803 +2.9825,1.4866,1.854 +1.1837,0.86268,0.92964 +-0.21804,-0.34317,0.5976 +1.3107,1.2574,1.3898 +0.15858,0.26123,0.30541 +-0.88425,-0.96351,-0.81885 +-0.54766,-0.75486,-1.1235 +-0.23576,0.031025,0.43233 +0.80035,0.01489,0.042148 +0.40758,0.29289,0.42509 +0.72433,0.26164,0.64014 +0.94726,1.3294,0.50632 +-3.0347,-2.109,-2.6678 +1.435,1.3302,1.3962 +-0.11631,-0.27703,-0.18536 +0.5284,0.16278,0.5905 +1.6569,0.9584,0.83698 +0.73339,0.58781,0.6293 +-0.053974,-0.6296,-0.30961 +2.7122,1.4535,1.7442 +1.2501,1.0896,1.159 +-0.64861,-0.77736,-0.51812 +0.67061,0.39535,0.036823 +-0.22732,-0.23807,-0.36535 +4.0119,2.9058,3.3315 +-1.1107,-1.0786,-0.90756 +2.3708,1.8353,1.6397 +-0.94586,-0.99019,-0.71511 +-0.97851,-1.0936,-0.27488 +-0.7327,-0.52786,-1.1667 +-1.4273,-1.1405,-1.223 +0.51143,0.040569,-0.26151 +-0.61625,-0.34615,-0.38091 +0.45988,0.15269,0.12729 +1.1693,1.1108,1.1646 +-2.6679,-1.8987,-1.9899 +-2.3344,-1.4175,-1.4573 +1.8448,1.8321,1.3634 +-0.76893,-1.1111,-0.90136 +-1.3488,-1.2946,-0.92539 +-0.56119,0.0089323,0.39697 +0.24172,-0.19342,0.17267 +-1.7258,-1.2037,-1.8982 +0.081487,0.12775,-0.31101 +0.62851,0.68749,0.052709 +-0.042665,-0.5202,0.28434 +-0.075984,-0.13484,-0.029599 +-2.0731,-1.6632,-1.2765 +-1.0135,-0.60056,-0.93672 +-1.64,-1.1715,-1.1609 +0.1515,0.23287,0.018319 +-0.47086,-0.088765,-0.57017 +-0.68252,-0.4449,-0.9225 +-0.88742,-0.84848,-0.84975 +-0.43817,0.052193,-0.10879 +-1.3142,-1.4088,-0.83819 +-0.52877,-0.23247,-0.49379 +-2.088,-1.2951,-1.9012 +0.2602,0.29949,0.060943 +-1.382,-0.94575,-0.60689 +-1.8747,-1.3917,-1.0491 +1.3077,0.88104,0.55847 +-1.407,-1.211,-1.0713 +0.63563,0.45859,0.19683 +-0.063227,0.21199,0.21755 +3.0411,1.8563,1.9391 +-2.8918,-1.6217,-1.8298 +0.78294,0.4287,0.42804 +0.84344,0.046024,0.64389 +1.0506,0.6435,0.5678 +0.061037,0.4905,0.16965 +-0.13804,0.58693,-0.16488 +1.6125,1.0261,1.0157 +-1.3996,-0.59624,-0.90646 +-1.0525,-0.98884,-1.1838 +0.059208,0.058424,0.5449 +0.53041,0.087684,-0.29765 +-2.6413,-1.5416,-2.0573 +0.26177,0.33753,0.46196 +0.19761,0.91216,0.11736 +-0.087715,0.19543,0.29375 +-0.25043,-0.069569,-0.20773 +-0.72471,-0.63506,-0.90416 +2.2606,1.4825,1.0713 +2.3652,1.6683,1.9505 +-1.2109,-1.1055,-0.74173 +0.54285,0.061023,0.39969 +0.2859,0.063151,0.2034 +1.1835,1.2188,0.55349 +-0.13362,0.23424,0.3584 +-0.6543,-0.18228,-0.18086 +0.069821,0.062013,-0.27292 +-1.9428,-1.0853,-1.4398 +-0.30367,0.21577,-0.17411 +-2.5954,-1.661,-1.9047 +2.0915,1.5547,0.99733 +-0.66147,0.24669,-0.91247 +1.0449,0.93571,0.67609 +2.79,1.6598,1.3183 +-1.2251,-0.86033,-0.92547 +-0.47749,-0.31028,0.28381 +-0.83917,-0.55704,-0.72098 +-0.44424,-0.20055,0.036599 +1.3774,0.44726,0.93602 +-2.7122,-1.6899,-1.971 +1.5795,1.0201,1.3349 +1.0198,0.64548,0.78483 +0.26586,0.082714,0.14744 +0.89318,0.52973,0.95247 +-0.50549,-0.28303,-0.15591 +1.9199,0.92191,2.0471 +0.16854,0.075471,-0.0013041 +-0.8304,-0.81052,-0.71957 +-0.52592,-0.13178,-0.058918 +2.0492,1.3384,1.0921 +0.096086,0.16658,0.25358 +-0.32496,-0.19822,0.047477 +0.69332,0.40949,0.34234 +-0.12205,0.11174,0.32665 +0.53087,0.70807,1.0442 +-0.21918,-0.55076,-0.66202 +0.73438,0.60177,0.67892 +1.9349,1.0854,1.4055 +-0.58814,-0.50452,-0.40968 +0.94596,0.74697,0.40055 +-0.59185,0.085186,0.24409 +-0.4935,-0.24149,0.022901 +-0.087221,-0.66966,-0.010703 +-0.38608,0.45916,0.33462 +-0.83084,-0.54879,-0.093242 +-2.0117,-1.4792,-1.8427 +-1.5199,-0.84139,-1.1674 +0.79155,1.0173,0.5015 +2.1129,1.4386,1.2234 +-1.7672,-0.78334,-1.3781 +-0.69014,-0.36502,-0.53744 +1.1194,0.45094,0.47608 +-0.13543,-0.19676,0.25159 +1.7041,1.1424,1.1918 +-1.0752,-0.47173,-0.44865 +-0.58392,-0.19985,0.27945 +-2.5241,-1.8118,-2.0555 +-1.4358,-0.95961,-0.83982 +1.6939,1.1447,0.99691 +0.42971,0.30098,0.54495 +-0.67714,-0.53533,-0.93412 +0.43609,0.48985,0.51845 +-0.89748,-0.25206,-0.7519 +0.76077,0.42051,0.41068 +-2.5801,-1.5242,-1.5598 +1.5621,0.59058,1.3707 +0.19413,0.44966,0.18559 +0.83287,0.63661,0.71923 +-0.92841,-0.9159,-0.54169 +-1.7753,-0.94777,-1.5999 +-1.8257,-1.6419,-2.0978 +-2.7582,-2.0063,-2.1803 +0.19403,0.29537,0.3785 +-2.1425,-1.4675,-1.5275 +1.6869,1.6411,1.7868 +-0.82573,-0.77426,-0.8585 +1.7926,1.2485,1.1358 +-0.07777,-0.38398,0.2103 +-1.4628,-1.5751,-0.94737 +0.15511,0.2456,-0.00015967 +2.2556,1.3402,1.3315 +-0.56449,-0.88906,-0.45505 +-0.44463,-0.27583,-0.30232 +0.56572,0.67416,0.29062 +0.7031,0.3405,0.10012 +-0.61347,0.2538,-0.45746 +0.85989,0.54276,0.17189 +-1.0203,-0.451,-0.50111 +-0.21224,-0.086635,-0.52003 +1.6299,1.6604,0.9542 +1.3413,0.85207,1.3466 +2.1215,1.5051,2.0916 +0.88292,0.85124,1.3464 +1.1126,1.188,0.71626 +-0.94161,-0.84941,-1.1167 +0.59511,0.12208,0.69344 +0.82957,0.75011,0.21263 +-0.22286,-0.10623,-0.08017 +2.223,1.0819,1.7043 +1.9981,0.84063,1.3287 +-0.51718,-0.41875,-0.75394 +-1.6022,-0.73683,-1.0581 +-2.2361,-1.473,-1.1966 +0.2003,0.21059,0.25599 +-1.2367,-0.6639,-0.74914 +-0.93371,-0.45712,-0.68751 +1.7027,1.4047,1.3026 +-1.2774,-1.2503,-0.58362 +0.46436,0.094888,0.25718 +-0.88083,-0.53589,-0.13703 +-1.7564,-1.0257,-0.9537 +1.6325,0.86482,1.1866 +1.2362,0.68078,0.64673 +1.0107,0.46862,0.37726 +0.24317,-0.20648,-0.024893 +-0.082012,-0.38708,-0.16586 +0.015307,0.4308,0.42156 +1.1262,0.74924,1.176 +-0.0027826,0.095791,-0.051756 +-1.0674,-0.63713,-1.117 +-1.1382,-1.0559,-1.1707 +2.0877,1.6548,2.0732 +0.16446,-0.085562,-0.15265 +-2.3137,-1.3404,-1.9408 +-0.72977,-0.31892,-0.49911 +-0.49288,-0.26296,-0.29842 +0.75772,0.54729,0.50426 +2.132,2.0393,0.97536 +1.9366,1.5312,1.0894 +-0.090977,0.036628,-0.079983 +-0.35219,-0.37494,-0.88215 +0.69166,0.61819,0.96796 +0.97703,0.60073,0.24864 +0.5783,-0.29955,-0.014528 +0.089841,-0.12908,-0.20564 +-1.0136,-0.64365,-0.39528 +-1.1332,-0.7829,-0.61792 +-0.082685,-0.36395,-0.18751 +-0.97684,-0.69191,-0.81254 +-1.5796,-0.8954,-1.0024 +0.27864,0.38203,0.63516 +-2.2827,-1.5331,-1.4515 +0.069051,0.2767,-0.032954 +1.3377,0.66931,1.1616 +-0.047892,0.25714,-0.061226 +-1.0783,-1.0994,-0.9987 +-0.84075,-0.24306,-0.46729 +-0.052014,-0.016192,0.72783 +-0.46478,-0.58935,0.35058 +-1.4389,-0.44225,-0.77327 +1.1587,0.8115,1.07 +1.4183,1.272,1.0778 +-0.2868,-0.67613,-0.34787 +1.0943,0.54473,0.59771 +-0.29326,0.13868,-0.49131 +2.1389,1.6149,1.013 +-1.4802,-0.36252,-1.1231 +-1.5628,-0.9868,-0.87139 +1.2949,0.59369,1.2511 +1.0876,0.47897,0.82238 +0.11324,0.46251,-0.29949 +1.1912,0.36098,0.58126 +2.4647,1.8699,1.2898 +-0.76676,-0.79698,-0.20654 +-1.2144,-1.0235,-0.98019 +3.3002,2.1348,1.8857 +-0.74422,-0.54289,-0.3079 +-3.3523,-2.2829,-2.2629 +0.66829,-0.1914,0.56563 +-1.5449,-0.82442,-0.79914 +-0.3981,-0.25903,-0.47908 +-1.0606,-0.88289,-0.93594 +-0.40896,-0.53047,-0.68026 +1.1676,0.74974,1.0582 +0.88298,0.83254,1.0817 +-0.80907,-0.8498,-0.9464 +-1.7035,-0.75615,-1.0524 +3.0057,1.8151,1.7273 +0.43203,-0.022721,-0.18269 +-2.1328,-1.496,-1.6208 +0.82725,0.46021,0.89021 +0.12842,0.20307,0.7547 +-0.35628,-1.0951,-0.21788 +0.78708,0.32786,0.35763 +-0.84097,-0.71616,-0.78011 +-0.26128,-0.28711,-0.20192 +-2.3,-1.5424,-1.6931 +0.19481,0.16885,0.48323 +-0.21151,-0.42079,-0.48766 +-3.7346,-3.0009,-2.5214 +-3.6375,-2.126,-2.9477 +1.0444,0.93172,0.6988 +-1.0959,-0.71016,-0.27227 +-0.23977,-0.11101,-0.28146 +-1.6543,-1.2306,-1.2255 +-0.17048,0.00054806,-0.19426 +-1.4264,-0.57421,-1.2455 +2.1076,1.4061,1.6389 +1.041,1.3285,0.86943 +-3.0801,-2.1722,-2.2572 +2.0788,1.7827,2.0537 +-0.22575,-0.92116,-0.91268 +0.44154,0.68306,0.71689 +1.4441,1.93,1.7792 +0.58056,0.32374,0.32775 +1.6292,1.4378,0.96915 +-1.2164,-0.95555,-0.38366 +-1.6149,-1.3085,-1.4474 +0.48657,0.20761,0.36362 +0.071001,-0.27576,-0.33847 +-1.356,-1.2666,-0.51153 +-0.095902,-0.51068,-0.15741 +-2.4017,-1.7503,-1.5108 +0.96453,0.63806,0.56845 +0.36505,0.055197,0.67804 +0.94694,0.19412,0.62757 +1.6745,0.7548,1.0322 +-0.94291,-0.63664,-0.60832 +-0.64385,-0.18336,-0.50873 +-0.13981,-0.85884,-0.26774 +0.97832,0.97541,0.6758 +1.5173,1.2477,1.1503 +0.5587,0.35433,1.3336 +-0.078039,0.082989,-0.16007 +1.6394,1.2077,0.93681 +-0.57367,-0.79813,-0.26396 +2.036,1.0049,1.1221 +-1.6846,-0.83132,-1.1526 +-0.33544,0.11605,-0.84109 +-0.57212,-0.072023,-0.10241 +-3.291,-1.716,-2.4594 +-1.0749,-0.7409,-0.61314 +-0.19901,-0.48184,-0.077611 +-0.65909,-0.50267,-0.25114 +1.0921,0.49644,0.59822 +-0.75208,-0.4143,-0.75187 +0.91208,0.80484,0.81955 +-0.59465,-0.086926,0.024274 +-1.567,-1.2596,-1.4882 +1.5991,1.3991,0.67735 +0.56413,0.47411,0.096418 +-1.4452,-0.6456,-1.0912 +0.36851,0.66575,0.85542 +-0.049404,-0.12279,-0.8877 +-0.56902,0.097645,-0.29639 +-0.86831,-0.66553,-0.71082 +0.72599,0.72612,0.40655 +1.4592,0.61515,0.65803 +-0.4995,-0.6516,-0.73246 +2.0153,1.3239,1.7313 +0.45817,0.062575,0.35973 +1.4545,1.1961,0.82625 +-0.75193,-0.6329,-0.75079 +0.77603,0.18932,0.22198 +-2.5723,-1.6475,-1.5168 +3.1285,2.3162,2.1798 +-0.29639,-0.32025,-0.17339 +1.0184,0.50337,0.45093 +1.7865,0.95189,0.86104 +1.1024,0.62887,1.0473 +-1.5952,-0.74729,-1.0423 +-0.38743,-0.4073,-0.017346 +-0.41171,-0.47334,-0.21696 +0.54783,0.34265,0.1377 +0.22118,0.62041,0.68178 +0.11464,0.25669,0.18489 +2.7897,1.5837,2.0197 +2.7931,2.2432,1.7687 +3.1972,2.364,2.2072 +1.6225,1.4032,1.3007 +-1.2668,-0.79879,-0.79138 +-2.493,-1.7277,-1.7401 +-1.0841,-0.69391,-0.99664 +1.0084,0.80389,0.96019 +-0.092502,-0.026961,-0.24254 +-0.11268,-0.11617,-0.27867 +-0.2337,-0.39813,-0.34348 +1.8125,1.1174,1.4553 +1.4619,1.0344,1.2254 +-0.16767,-0.55547,-0.41427 +0.74407,0.74255,0.63021 +2.2155,1.7229,1.445 +-0.31377,-0.54679,-0.63182 +-4.0817,-2.4425,-2.7721 +1.52,1.1267,1.3242 +-4.3414,-3.1472,-2.9687 +0.2646,-0.38891,0.0041942 +-0.11866,0.10528,0.49905 +-0.44437,-0.12049,-0.62861 +-0.96011,-0.69258,-0.83422 +0.8599,0.86347,0.43738 +1.8198,1.0826,0.79188 +0.79687,0.75117,0.97561 +0.61907,0.5121,0.86484 +-0.21279,0.24918,-0.14006 +1.4138,1.5817,0.6476 +0.82272,1.4893,1.2821 +-3.0504,-1.9808,-2.2807 +0.50188,0.31133,0.64678 +0.69242,0.18996,0.70653 +-0.69502,-0.89104,-0.61337 +-0.086855,0.39483,-0.43195 +-0.94864,-0.32641,-0.52372 +-0.2027,-0.11435,0.019343 +-0.21543,-0.70036,0.20125 +0.45425,0.40563,0.2627 +-1.5945,-1.2596,-1.1636 +0.68887,0.97224,0.8808 +0.72542,0.54717,0.51627 +-1.2184,-1.295,-0.78843 +0.050938,-0.59564,0.26942 +-0.50778,-0.33381,-0.32208 +1.2101,0.89738,1.1816 +0.19303,0.55456,0.36781 +1.0831,0.63724,0.6597 +-2.1071,-1.0357,-1.1528 +-0.6975,1.4989,-2.0576 +1.3339,3.158,0.28043 +-0.21156,1.7577,-1.1117 +0.71906,2.757,-0.16608 +-2.9855,-0.098775,-3.1358 +1.2023,3.2514,0.23115 +-1.4018,0.36674,-2.1768 +-1.3135,0.69125,-2.3323 +0.40552,2.1893,-1.0071 +1.7846,3.3717,0.52439 +1.6608,3.4361,0.42267 +-0.073565,2.2652,-0.91654 +1.7209,3.5658,0.3006 +3.1882,4.5977,1.2658 +-0.98642,1.5746,-1.7768 +-0.27873,1.9078,-0.75813 +-1.6402,0.6321,-2.0068 +-0.70304,1.7379,-1.8066 +-1.218,1.3822,-2.2175 +-1.4349,0.51271,-1.8798 +2.2768,3.5436,0.57225 +1.5041,3.1356,-0.040765 +-1.4241,1.156,-1.7804 +-0.11436,1.5936,-1.7267 +2.0326,3.2524,0.28491 +0.47749,2.5039,-0.43539 +1.9338,3.4122,-0.24489 +-2.3058,0.33874,-2.5696 +0.56056,2.3458,-0.67084 +0.83114,2.5705,-0.81666 +-1.0925,0.69583,-2.2979 +2.2531,3.6528,1.0971 +0.70366,2.8862,-0.69233 +-3.1376,-0.77693,-3.1593 +-2.0105,0.65166,-2.1592 +1.7621,3.2052,0.65159 +0.045573,2.2617,-1.1421 +1.7276,3.0103,-0.11184 +-0.14445,1.8207,-1.6921 +0.63983,2.5637,-0.88859 +-1.9887,0.61969,-2.5127 +-0.77862,1.8045,-1.3129 +-0.47354,2.0831,-1.0346 +1.6157,2.9863,-0.098237 +-0.66193,1.694,-1.5622 +0.16656,1.9625,-1.3294 +0.019699,2.3942,-0.63286 +-0.92534,0.80855,-1.6135 +1.6386,2.9127,0.12334 +2.0057,4.3021,0.98863 +0.42154,2.4704,-0.60246 +0.82108,2.811,0.2437 +1.0293,2.9829,-0.098867 +-1.813,0.67728,-1.8287 +1.783,3.1082,0.53828 +-2.8269,0.13975,-2.0776 +-1.434,1.1184,-2.6909 +-0.83207,1.3951,-1.8767 +0.56244,1.7977,-0.35071 +0.47069,2.6147,-0.092379 +-0.8347,1.2527,-1.6997 +-1.8894,0.99539,-3.1665 +0.8026,2.5401,-0.56001 +-0.91527,1.3677,-1.2284 +0.86685,2.4212,-0.52196 +0.73631,2.0199,-0.17259 +1.7798,3.0943,0.51861 +-1.5598,1.2132,-2.2322 +-1.103,0.6916,-2.3906 +0.51962,2.6022,-0.29134 +-1.5924,0.95891,-1.7576 +-0.95417,1.1914,-1.8069 +-0.12115,2.0963,-1.5694 +-0.41383,1.4943,-1.2149 +1.0867,2.4675,-0.14368 +-1.0036,1.2464,-1.7938 +0.72484,2.6398,-0.6255 +-0.85135,1.4769,-1.917 +-1.3774,1.1765,-1.0353 +2.3671,4.151,1.2582 +-0.13561,2.2608,-0.94996 +-0.23044,2.4257,-0.97189 +-0.88372,2.1966,-1.726 +-0.58,1.0952,-1.7096 +1.4994,2.8147,-0.32391 +-1.0274,1.2879,-2.0903 +-1.2796,1.5774,-1.5094 +-0.5797,1.8505,-1.12 +-2.2636,0.93248,-2.4786 +0.44527,2.4684,-0.79258 +-0.9497,1.1252,-1.845 +3.1904,4.0746,0.9626 +1.1631,2.7269,-0.16504 +-1.4672,0.83207,-1.7914 +2.8576,3.866,0.88194 +0.029754,2.0093,-0.92291 +-0.91592,1.0553,-1.8036 +-1.0762,1.5832,-1.6021 +0.18524,2.2429,-0.86124 +-0.10256,1.8428,-0.66094 +1.2697,2.7612,-0.24163 +0.11362,2.0066,-0.96279 +0.19997,2.1961,-0.8314 +-1.7592,1.0069,-2.2993 +-1.1581,1.1919,-2.1469 +-0.63746,1.4558,-2.0375 +2.334,3.4055,0.20539 +-2.5274,0.11164,-2.7256 +-1.4836,1.1463,-1.8476 +-0.58334,1.9402,-1.4465 +-0.36482,2.1239,-1.1651 +-0.52351,1.4415,-0.65888 +-1.949,0.21604,-2.2428 +3.5584,4.8393,1.1236 +1.4703,3.0185,0.49572 +-2.8211,0.43784,-2.1897 +-0.16361,2.1039,-1.3668 +2.5709,4.0932,0.87071 +0.73265,2.5907,-0.57969 +0.51022,2.1651,-1.2727 +0.76899,1.438,-0.98371 +-0.17128,1.686,-1.092 +-1.3756,1.0289,-2.1046 +1.0948,2.3309,-0.51105 +3.3282,4.1039,1.2859 +-2.0546,0.42604,-2.4117 +0.43204,2.4321,-0.94615 +0.83036,2.5444,-0.22365 +1.4318,3.6561,0.58497 +0.15545,2.3575,-0.61828 +0.98966,2.4873,-0.43824 +3.0079,3.8561,1.0926 +0.20354,2.3151,-1.1964 +-1.9902,0.65356,-1.8136 +-2.4573,0.54143,-2.7738 +-1.1332,1.4395,-1.7194 +0.92719,2.376,-0.82608 +0.92359,2.7603,0.046319 +1.1171,2.9485,-0.0073154 +-0.55831,1.2599,-1.718 +-2.3207,0.38527,-1.8988 +1.4801,2.986,-0.28044 +0.30903,1.3085,-0.55672 +-0.69243,1.3502,-1.4509 +1.3048,3.1819,0.33704 +0.91085,2.0965,-0.30135 +-0.80932,1.4345,-1.6792 +0.81549,2.4671,-0.33384 +0.38334,2.0974,-0.69991 +-0.01149,1.5004,-1.4347 +-3.0522,-0.024838,-3.4975 +-1.455,1.1918,-1.8266 +-0.85504,1.7392,-1.711 +1.5975,2.9864,0.22816 +0.14029,2.346,-0.95771 +0.29979,2.358,-0.81355 +-1.0671,1.1941,-1.6453 +-1.8216,1.0167,-1.8793 +0.84552,2.7756,-0.83241 +0.93044,2.5845,-0.55008 +-0.9566,0.62371,-1.7985 +-1.1376,1.7187,-1.1157 +-1.1067,1.5265,-1.3179 +-0.36322,1.9674,-1.1235 +1.366,2.7897,-0.055666 +0.332,2.0888,-0.83667 +-1.9542,0.9743,-2.0177 +0.096801,2.5064,-0.8073 +1.5374,3.0921,-0.22269 +0.41843,2.5863,-0.77146 +0.80082,3.0112,-0.32801 +1.6812,3.4572,-0.51982 +-0.55307,1.8701,-1.69 +-0.24206,1.3421,-1.4736 +-0.25654,2.2437,-0.99187 +0.014638,1.7969,-1.4376 +-0.1745,1.9168,-1.5279 +0.11876,1.9746,-0.49863 +1.6836,3.1278,-0.030485 +-1.7754,0.26854,-2.9489 +-1.1159,0.95009,-1.4111 +-0.8181,1.4136,-1.5501 +-0.54368,1.8649,-1.1626 +-1.1901,1.5496,-1.6707 +-2.0537,0.3338,-2.5033 +-0.24753,1.8794,-1.0107 +-0.72903,1.8224,-1.6435 +0.038166,1.9963,-0.67844 +-0.638,1.774,-1.5228 +0.42271,1.9838,-1.1418 +-1.2348,0.98551,-1.8549 +-1.7239,1.1747,-2.1135 +-0.015513,1.3959,-0.86992 +1.7031,3.0449,-0.25698 +-1.3881,1.0682,-2.1338 +-1.1969,1.7697,3.9208 +-1.2424,1.4604,3.6047 +-1.3163,0.93889,2.8365 +-2.8288,0.045368,2.2851 +-2.7684,0.13886,1.9198 +-0.60191,2.4719,3.8042 +-0.39537,1.8476,4.2524 +0.44816,2.6351,4.8098 +-1.0442,1.5827,3.8989 +-3.6402,0.14778,1.9706 +-2.45,0.78979,2.5683 +-1.7225,0.73554,3.158 +-0.69631,2.2939,4.1272 +-2.8072,-0.069145,1.7978 +-1.6416,1.5689,3.3282 +-3.0648,0.17403,2.4512 +-2.0258,0.87394,3.389 +-2.5863,0.45752,2.9173 +-1.5265,1.8141,4.0588 +-4.2433,-0.17808,1.6443 +-2.9551,0.18985,1.9159 +-1.2529,1.4453,3.5425 +-1.8285,0.64726,2.5959 +-3.1628,0.046687,1.7174 +-1.6728,1.4828,3.2985 +-1.6953,1.3002,3.2336 +-1.6847,0.97183,3.1077 +-1.5924,1.4341,3.4936 +-0.70679,1.4356,3.8973 +-2.3524,0.85287,2.8275 +-2.3738,0.65846,2.3619 +-3.539,-0.56682,2.2519 +-1.0962,1.2734,3.7768 +-3.8573,-0.16291,1.432 +-3.8752,-0.40234,2.4318 +0.33871,2.711,5.3221 +-2.0428,1.2251,3.07 +-1.9655,1.2908,2.9416 +-3.0731,0.43096,2.0358 +-1.9089,1.0828,2.672 +-1.3803,1.7463,3.5046 +-0.034521,1.9667,4.0039 +-1.1235,1.1656,3.2068 +-1.8109,1.4349,3.0943 +-2.0088,1.0847,2.9151 +-0.31351,1.8273,3.5788 +-1.295,1.4664,3.2349 +-1.2371,1.9161,4.2493 +-2.7012,0.30699,2.9857 +-3.0858,0.35541,2.2666 +-2.5561,0.22266,1.7668 +-2.0615,0.95838,2.3282 +-6.4281,-2.209,0.059564 +-0.61075,1.7083,3.6271 +0.72189,2.6927,4.7195 +-2.3989,0.63415,3.1581 +-1.0935,1.3178,3.712 +-2.2726,0.72421,2.978 +-0.70414,1.7072,3.6075 +-0.55165,1.7327,4.2121 +-1.3224,0.73273,3.2907 +-1.4177,1.6338,3.6401 +0.068642,1.954,4.1146 +-3.7918,-0.48566,1.8536 +-2.5556,0.73785,2.9208 +-1.5831,1.1366,3.1914 +0.41068,2.7159,4.8655 +-1.428,1.1453,3.0639 +-0.46294,1.8412,4.3182 +0.22348,2.0181,4.379 +-2.2658,1.0376,2.7409 +-3.591,0.30847,1.7559 +-2.0455,0.58627,2.9203 +0.93646,3.1056,5.0571 +-0.70905,1.8272,3.9991 +-1.1255,1.9211,3.5726 +0.37208,3.0153,4.8578 +0.27377,2.3156,4.8376 +-0.53999,2.2613,4.4015 +-2.1156,0.66801,2.4573 +0.015444,2.664,4.6196 +-1.7627,0.95155,3.4429 +-2.6128,0.63736,2.3568 +-7.4142,-3.0846,-0.83159 +-2.9964,-0.16066,2.233 +-2.5093,-0.088607,2.1958 +-5.7356,-1.9153,0.27291 +-1.7637,1.2192,3.3577 +-3.0144,0.33408,2.272 +-1.1035,2.0463,3.6744 +0.29377,2.7,4.0339 +7.6709,-0.65356,-6.146 +-1.2698,0.37012,3.5987 +-3.8242,-5.9383,8.3664 +6.4851,1.6129,-9.0761 +3.7499,1.954,7.6171 +0.60828,3.1314,-1.5524 +-6.4482,1.6296,-0.8863 +7.9403,5.2769,2.3478 +6.1994,-6.7288,4.8542 +-2.2174,6.795,9.9198 +-0.42004,9.9264,8.5588 +2.148,3.2027,-9.6178 +3.0381,-7.5349,-2.3754 +5.2505,1.7427,3.1399 +-1.0458,-7.0753,-9.4356 +2.8597,8.9945,2.2305 +-4.3883,4.8396,0.47878 +-1.1343,7.4522,2.4586 +-5.0359,-6.7081,-7.2111 +-6.9676,-0.5243,-0.97505 +-5.8695,-3.0673,-1.8167 +8.2832,-4.0377,8.1787 +1.6522,-9.1964,-1.9773 +3.8661,-2.368,-6.1853 +7.1298,-7.3613,6.3322 +-0.27049,-7.1403,-1.549 +-3.1454,-2.3609,-2.2842 +-1.6271,2.7362,4.3218 +-7.6258,5.6069,2.1053 +0.37131,-1.4426,-8.3637 +9.4317,-6.5751,-7.3919 +-3.4185,-2.6663,-2.6426 +-3.2166,9.5563,1.2013 +6.0375,-0.81393,4.8038 +-6.806,7.4827,9.649 +-6.8151,-6.9582,-0.28445 +-6.7267,-1.1509,-9.9922 +8.287,8.4159,5.6332 +5.5004,9.6078,-3.0566 +-1.4023,-0.72913,-4.0823 +-4.8169,-3.1102,6.6306 +2.4477,-5.8111,-8.0409 +9.705,-3.6756,-2.5232 +2.7386,7.9568,2.8053 +-5.3387,-8.5872,-9.3185 +5.69,-5.0415,-3.6123 +-9.9319,-9.6028,9.2331 +5.9482,9.9372,3.3279 +-5.8469,-7.9974,-8.2701 +-6.0094,8.3772,-4.8887 +9.5516,-7.3199,-7.1452 +2.4614,-5.899,-4.4542 +-8.8957,-6.7169,5.5289 +-7.154,5.541,4.1596 +-1.8432,1.2028,2.0585 +5.563,3.3022,-9.2116 +-6.7447,7.4713,6.9136 +-9.4759,2.1966,-5.4052 +7.0271,8.7768,-0.098737 +-4.9415,-4.2886,-9.3987 +-2.0969,5.2979,2.9589 +3.7458,-7.0135,-1.9911 +7.4198,2.3875,5.3503 +3.6197,2.4641,-2.3227 +-7.216,6.167,7.8282 +1.5398,-3.6429,-1.8442 +3.6883,-3.0284,-2.4634 +-3.6761,-4.9286,-8.231 +0.2367,8.4299,9.6515 +-1.078,4.0697,8.4339 +0.40362,4.1245,-0.76191 +-2.6794,-6.9877,1.2293 +3.0462,4.0747,-0.13222 +7.4312,-8.5241,9.8795 +7.3594,-8.9495,-9.4865 +0.59609,0.47255,-4.8097 +9.1863,2.9909,-3.4567 +1.8203,-5.3323,-7.5425 +-9.5337,-7.1973,4.9186 +-6.3554,-5.2483,2.5872 +-0.65065,7.4834,-6.0681 +2.2684,7.2147,-0.72463 +-7.2069,3.0322,-4.6809 +-5.7646,7.4828,-9.9276 +9.8014,9.2737,-7.6849 +-3.3992,8.9312,1.1783 +-1.4833,7.7265,7.8454 +-7.0749,0.75356,8.0241 +0.91467,6.2634,3.9977 +-8.1604,-5.15,-5.3245 +-1.3009,-2.721,0.59003 +-8.3264,3.5507,6.0953 +1.0982,-6.0583,3.9963 +-9.6435,-6.1097,-9.9918 +2.8821,1.0076,-4.2984 +-2.1547,-9.6324,-6.5842 +-3.4672,-6.7797,-5.9353 +-4.7036,0.70339,-8.1064 +0.20557,-1.4378,-9.2265 +-0.88981,-1.8582,-2.1328 +6.2075,6.6453,-5.3313 diff --git a/fastlib/u/pram/mog_em/faster/math_functions.h b/fastlib/u/pram/mog_em/faster/math_functions.h new file mode 100644 index 0000000000..2349bc9d06 --- /dev/null +++ b/fastlib/u/pram/mog_em/faster/math_functions.h @@ -0,0 +1,138 @@ +/** + * @author Parikshit Ram (pram@cc.gatech.edu) + * @file math_functions.h + * + * This file has certain functions that find the + * highest or lowest element in an array or + * in a row of a matrix and returns them + * + */ + +#include "fastlib/fastlib.h" +#include "fastlib/fastlib_int.h" + + +/** + * Finds the index of the minimum element + * in each row of a matrix + * + * Example use: + * @code + * Matrix& mat; + * index_t indices[mat.n_rows()]; + * + * ... + * + * min_element(mat, indices); + * @endcode + */ + +void min_element(Matrix& element, index_t *indices) { + + index_t last = element.n_cols() - 1; + index_t first, lowest; + index_t i; + + for (i = 0; i < element.n_rows(); i++) { + + first = lowest = 0; + if (first == last) { + indices[i] = last; + } + while (++first <= last) { + if (element.get(i, first) < element.get(i, lowest)) { + lowest = first; + } + } + indices[i] = lowest; + } + return; +} + +/** + * Returns the index of the maximum element + * in a float array 'array' of length 'length'. + * + * Example use: + * @code + * index_t length, index; + * float array[length]; + * ... + * index = max_element_index(array, length); + * @endcode + */ +int max_element_index(float *array, int length) { + + int last = length - 1; + int first = 0; + int highest = 0; + + if (first == last) { + return last; + } + while (++first <= last) { + if (array[first] > array[highest]) { + highest = first; + } + } + return highest; +} + +/** + * Finds the index of the maximum element in an arraylist + * of floats + * + * Example use: + * @code + * index_t index; + * ArrayList array; + * ... + * index = max_element_index(array); + * @endcode + */ +int max_element_index(ArrayList& array){ + + int last = array.size() - 1; + int first = 0; + int highest = 0; + + if (first == last) { + return last; + } + while (++first <= last) { + if (array[first] > array[highest]) { + highest = first; + } + } + return highest; +} + +/** + * Returns the index of the maximum element in + * an arraylist of doubles + * + * Example use: + * @code + * index_t index; + * ArrayList array; + * ... + * index = max_element_index(array); + * @endcode + */ +int max_element_index(ArrayList& array) { + + int last = array.size() - 1; + int first = 0; + int highest = 0; + + if (first == last) { + return last; + } + while (++first <= last) { + if(array[first] > array[highest]) { + highest = first; + } + } + return highest; +} + diff --git a/fastlib/u/pram/mog_em/faster/mog.cc b/fastlib/u/pram/mog_em/faster/mog.cc new file mode 100644 index 0000000000..85d377d2ce --- /dev/null +++ b/fastlib/u/pram/mog_em/faster/mog.cc @@ -0,0 +1,301 @@ +/** + * @author Parikshit Ram (pram@cc.gatech.edu) + * @file mog.cc + * + * Implementation for the loglikelihood function, the EM algorithm + * and also computes the K-means for getting an initial point + * + */ + +#include "mog.h" +#include "phi.h" +#include "math_functions.h" + +void MoGEM::ExpectationMaximization(Matrix& data_points) { + + // Declaration of the variables */ + index_t num_points; + index_t dim, num_gauss; + double sum, tmp; + ArrayList mu_temp, mu; + ArrayList sigma_temp, sigma; + Vector omega_temp, omega, x; + Matrix cond_prob; + long double l, l_old, best_l, INFTY = 99999, TINY = 1.0e-10; + + // Initializing values + dim = dimension(); + num_gauss = number_of_gaussians(); + num_points = data_points.n_cols(); + + // Initializing the number of the vectors and matrices + // according to the parameters input + mu_temp.Init(num_gauss); + mu.Init(num_gauss); + sigma_temp.Init(num_gauss); + sigma.Init(num_gauss); + omega_temp.Init(num_gauss); + omega.Init(num_gauss); + + // Allocating size to the vectors and matrices + // according to the dimensionality of the data + for(index_t i = 0; i < num_gauss; i++) { + mu_temp[i].Init(dim); + mu[i].Init(dim); + sigma_temp[i].Init(dim, dim); + sigma[i].Init(dim, dim); + } + x.Init(dim); + cond_prob.Init(num_gauss, num_points); + + best_l = -INFTY; + index_t restarts = 0; + // performing 5 restarts and choosing the best from them + while (restarts < 1) { + + // assign initial values to 'mu', 'sig' and 'omega' using k-means + KMeans(data_points, &mu_temp, &sigma_temp, &omega_temp, num_gauss); + + l_old = -INFTY; + + // calculates the loglikelihood value + l = Loglikelihood(data_points, mu_temp, sigma_temp, omega_temp); + + // added a check here to see if any + // significant change is being made + // at every iteration + while (l - l_old > TINY) { + // calculating the conditional probabilities + // of choosing a particular gaussian given + // the data and the present theta value + /********** + for (index_t j = 0; j < num_points; j++) { + x.CopyValues(data_points.GetColumnPtr(j)); + sum = 0; + for (index_t i = 0; i < num_gauss; i++) { + tmp = phi(x, mu_temp[i], sigma_temp[i]) * omega_temp.get(i); // can be made faster by sending all the data at once instead of looping over + cond_prob.set(i, j, tmp); + sum += tmp; + } + for (index_t i = 0; i < num_gauss; i++) { + tmp = cond_prob.get(i, j); + cond_prob.set(i, j, tmp / sum); + } + } + ******/ + + /*******FIX THIS SOON********/ + Vector phi_val; + phi_val.Init(num_points); + + for(index_t i = 0; i < num_gauss; i++) { + phi_val.CopyValues(phi(data_points, mu_temp[i], sigma_temp[i])); + la::Scale(omega_temp.get(i), &phi_val); + */ + + // calculating the new value of the mu + // using the updated conditional probabilities + for (index_t i = 0; i < num_gauss; i++) { + sum = 0; + mu_temp[i].SetZero(); + for (index_t j = 0; j < num_points; j++) { + x.CopyValues(data_points.GetColumnPtr(j)); + la::AddExpert(cond_prob.get(i, j), x, &mu_temp[i]); + sum += cond_prob.get(i, j); + } + la::Scale((1.0 / sum), &mu_temp[i]); + } + + // calculating the new value of the sig + // using the updated conditional probabilities + // and the updated mu + for (index_t i = 0; i < num_gauss; i++) { + sum = 0; + sigma_temp[i].SetZero(); + for (index_t j = 0; j < num_points; j++) { + Matrix co, ro, c; + c.Init(dim, dim); + x.CopyValues(data_points.GetColumnPtr(j)); + la::SubFrom(mu_temp[i] , &x); + co.AliasColVector(x); + ro.AliasRowVector(x); + la::MulOverwrite(co, ro, &c); + la::AddExpert(cond_prob.get(i, j), c, &sigma_temp[i]); + sum += cond_prob.get(i, j); + } + la::Scale((1.0 / sum), &sigma_temp[i]); + } + + // calculating the new values for omega + // using the updated conditional probabilities + Vector identity_vector; + identity_vector.Init(num_points); + identity_vector.SetAll(1.0 / num_points); + la::MulOverwrite(cond_prob, identity_vector, &omega_temp); + + l_old = l; + l = Loglikelihood(data_points, mu_temp, sigma_temp, omega_temp); + } + + // putting a check to see if the best one is chosen + if(l > best_l){ + best_l = l; + for (index_t i = 0; i < num_gauss; i++) { + mu[i].CopyValues(mu_temp[i]); + sigma[i].CopyValues(sigma_temp[i]); + } + omega.CopyValues(omega_temp); + } + restarts++; + } + + for (index_t i = 0; i < num_gauss; i++) { + set_mu(i, mu[i]); + set_sigma(i, sigma[i]); + } + set_omega(omega); + + NOTIFY("loglikelihood value of the estimated model: %Lf\n", best_l); + return; +} + +long double MoGEM::Loglikelihood(Matrix& data_points, ArrayList& means, + ArrayList& covars, Vector& weights) { + + index_t i, j; + Vector x; + long double likelihood, loglikelihood = 0; + + x.Init(data_points.n_rows()); + + for (j = 0; j < data_points.n_cols(); j++) { + x.CopyValues(data_points.GetColumnPtr(j)); + likelihood = 0; + for(i = 0; i < number_of_gaussians() ; i++){ + likelihood += weights.get(i) * phi(x, means[i], covars[i]); + } + loglikelihood += log(likelihood); + } + return loglikelihood; +} + +void MoGEM::KMeans(Matrix& data, ArrayList *means, + ArrayList *covars, Vector *weights, index_t value_of_k){ + + + ArrayList mu, mu_old; + double* tmpssq; + double* sig; + double* sig_best; + index_t *y; + Vector x, diff; + Matrix ssq; + index_t i, j, k, n, t, dim; + double score, score_old, sum; + + n = data.n_cols(); + dim = data.n_rows(); + mu.Init(value_of_k); + mu_old.Init(value_of_k); + tmpssq = (double*)malloc(value_of_k * sizeof( double )); + sig = (double*)malloc(value_of_k * sizeof( double )); + sig_best = (double*)malloc(value_of_k * sizeof( double )); + ssq.Init(n, value_of_k); + + for( i = 0; i < value_of_k; i++){ + mu[i].Init(dim); + mu_old[i].Init(dim); + } + x.Init(dim); + y = (index_t*)malloc(n * sizeof(index_t)); + diff.Init(dim); + + score_old = 999999; + + // putting 5 random restarts to obtain the k-means + for(i = 0; i < 5; i++){ + t = -1; + for (k = 0; k < value_of_k; k++){ + t = (t + 1 + (rand()%((n - 1 - (value_of_k - k)) - (t + 1)))); + mu[k].CopyValues(data.GetColumnPtr(t)); + for(j = 0; j < n; j++){ + x.CopyValues( data.GetColumnPtr(j)); + la::SubOverwrite(mu[k], x, &diff); + ssq.set( j, k, la::Dot(diff, diff)); + } + } + min_element(ssq, y); + + do{ + for(k = 0; k < value_of_k; k++){ + mu_old[k].CopyValues(mu[k]); + } + + for(k = 0; k < value_of_k; k++){ + index_t p = 0; + mu[k].SetZero(); + for(j = 0; j < n; j++){ + x.CopyValues(data.GetColumnPtr(j)); + if(y[j] == k){ + la::AddTo(x, &mu[k]); + p++; + } + } + + if(p == 0){ + } + else{ + double sc = 1 ; + sc = sc / p; + la::Scale(sc , &mu[k]); + } + for(j = 0; j < n; j++){ + x.CopyValues(data.GetColumnPtr(j)); + la::SubOverwrite(mu[k], x, &diff); + ssq.set(j, k, la::Dot(diff, diff)); + } + } + min_element(ssq, y); + + sum = 0; + for(k = 0; k < value_of_k; k++) { + la::SubOverwrite(mu[k], mu_old[k], &diff); + sum += la::Dot(diff, diff); + } + }while(sum != 0); + + for(k = 0; k < value_of_k; k++){ + index_t p = 0; + tmpssq[k] = 0; + for(j = 0; j < n; j++){ + if(y[j] == k){ + tmpssq[k] += ssq.get(j, k); + p++; + } + } + sig[k] = sqrt(tmpssq[k] / p); + } + + score = 0; + for(k = 0; k < value_of_k; k++){ + score += tmpssq[k]; + } + score = score / n; + + if (score < score_old) { + score_old = score; + for(k = 0; k < value_of_k; k++){ + (*means)[k].CopyValues(mu[k]); + sig_best[k] = sig[k]; + } + } + } + + for(k = 0; k < value_of_k; k++){ + x.SetAll(sig_best[k]); + (*covars)[k].SetDiagonal(x); + } + double tmp = 1; + (*weights).SetAll(tmp / value_of_k); + return; +} diff --git a/fastlib/u/pram/mog_em/faster/mog.h b/fastlib/u/pram/mog_em/faster/mog.h new file mode 100644 index 0000000000..3acefe9bca --- /dev/null +++ b/fastlib/u/pram/mog_em/faster/mog.h @@ -0,0 +1,244 @@ +/** + * @author Parikshit Ram (pram@cc.gatech.edu) + * @file mog.h + * + * Defines a Gaussian Mixture model and + * estimates the parameters of the model + */ + +#ifndef MOGEM_H +#define MOGEM_H + +#include + +/** + * A Gaussian mixture model class. + * + * This class uses maximum likelihood loss functions to + * estimate the parameters of a gaussian mixture + * model on a given data via the EM algorithm. + * + * + * Example use: + * + * @code + * MoGEM mog; + * ArrayList results; + * + * mog.Init(number_of_gaussians, dimension); + * mog.ExpectationMaximization(data, &results, optim_flag); + * @endcode + */ +class MoGEM { + + private: + + // The parameters of the mixture model + ArrayList mu_; + ArrayList sigma_; + Vector omega_; + index_t number_of_gaussians_; + index_t dimension_; + + public: + + MoGEM() { + mu_.Init(0); + sigma_.Init(0); + } + + ~MoGEM() { + } + + void Init(index_t num_gauss, index_t dimension) { + + // Initialize the private variables + number_of_gaussians_ = num_gauss; + dimension_ = dimension; + + // Resize the ArrayList of Vectors and Matrices + mu_.Resize(number_of_gaussians_); + sigma_.Resize(number_of_gaussians_); + } + + void Init(datanode *mog_em_module) { + + index_t num_gauss = fx_param_int_req(mog_em_module, "K"); + index_t dim = fx_param_int_req(mog_em_module, "D"); + Init(num_gauss, dim); + } + // The get functions + + ArrayList& mu() { + return mu_; + } + + ArrayList& sigma() { + return sigma_; + } + + Vector& omega() { + return omega_; + } + + index_t number_of_gaussians() { + return number_of_gaussians_; + } + + index_t dimension() { + return dimension_; + } + + Vector& mu(index_t i) { + return mu_[i] ; + } + + Matrix& sigma(index_t i) { + return sigma_[i]; + } + + double omega(index_t i) { + return omega_.get(i); + } + + // The set functions + + void set_mu(index_t i, Vector& mu) { + DEBUG_ASSERT(i < number_of_gaussians()); + DEBUG_ASSERT(mu.length() == dimension()); + mu_[i].Copy(mu); + return; + } + + void set_mu(index_t i, index_t length, const double *mu) { + DEBUG_ASSERT(i < number_of_gaussians()); + DEBUG_ASSERT(length == dimension()); + mu_[i].Copy(mu, length); + return; + } + + void set_sigma(index_t i, Matrix& sigma) { + DEBUG_ASSERT(i < number_of_gaussians()); + DEBUG_ASSERT(sigma.n_rows() == dimension()); + DEBUG_ASSERT(sigma.n_cols() == dimension()); + sigma_[i].Copy(sigma); + return; + } + + void set_omega(Vector& omega) { + DEBUG_ASSERT(omega.length() == number_of_gaussians()); + omega_.Copy(omega); + return; + } + + void set_omega(index_t length, const double *omega) { + DEBUG_ASSERT(length == number_of_gaussians()); + omega_.Copy(omega, length); + return; + } + + + /** + * This function outputs the parameters of the model + * to an arraylist of doubles + * + * @code + * ArrayList results; + * mog.OutputResults(&results); + * @endcode + */ + void OutputResults(ArrayList *results) { + + // Initialize the size of the output array + (*results).Init(number_of_gaussians_ * (1 + dimension_*(1 + dimension_))); + + // Copy values to the array from the private variables of the class + for (index_t i = 0; i < number_of_gaussians_; i++) { + (*results)[i] = omega(i); + for (index_t j = 0; j < dimension_; j++) { + (*results)[number_of_gaussians_ + i*dimension_ + j] = mu(i).get(j); + for (index_t k = 0; k < dimension_; k++) { + (*results)[number_of_gaussians_*(1 + dimension_) + + i*dimension_*dimension_ + j*dimension_ + + k] = sigma(i).get(j, k); + } + } + } + } + + /** + * This function prints the parameters of the model + * + * @code + * mog.Display(); + * @endcode + */ + void Display(){ + + // Output the model parameters as the omega, mu and sigma + printf(" Omega : [ "); + for (index_t i = 0; i < number_of_gaussians_; i++) { + printf("%lf ", omega(i)); + } + printf("]\n"); + printf(" Mu : \n["); + for (index_t i = 0; i < number_of_gaussians_; i++) { + for (index_t j = 0; j < dimension_ ; j++) { + printf("%lf ", mu(i).get(j)); + } + printf(";"); + if (i == (number_of_gaussians_ - 1)) { + printf("\b]\n"); + } + } + printf("Sigma : "); + for (index_t i = 0; i < number_of_gaussians_; i++) { + printf("\n["); + for (index_t j = 0; j < dimension_ ; j++) { + for(index_t k = 0; k < dimension_ ; k++) { + printf("%lf ",sigma(i).get(j, k)); + } + printf(";"); + } + printf("\b]"); + } + printf("\n"); + } + + /** + * This function calculates the parameters of the model + * using the Maximum Likelihood function via the + * Expectation Maximization (EM) Algorithm. + * + * @code + * MoG mog; + * Matrix data = "the data on which you want to fit the model"; + * ArrayList results; + * mog.ExpectationMaximization(data, &results); + * @endcode + */ + void ExpectationMaximization(Matrix& data_points); + + /** + * This function computes the loglikelihood of model. + * This function is used by the 'ExpectationMaximization' + * function. + * + */ + long double Loglikelihood(Matrix& data_points, ArrayList& means, + ArrayList& covars, Vector& weights); + + /** + * This function computes the k-means of the data and stores + * the calculated means and covariances in the ArrayList + * of Vectors and Matrices passed to it. It sets the weights + * uniformly. + * + * This function is used to obtain a starting point for + * the optimization + */ + void KMeans(Matrix& data, ArrayList *means, + ArrayList *covars, Vector *weights, index_t value_of_k); +}; + +#endif diff --git a/fastlib/u/pram/mog_em/faster/mog_em_main.cc b/fastlib/u/pram/mog_em/faster/mog_em_main.cc new file mode 100644 index 0000000000..6e64a9bf87 --- /dev/null +++ b/fastlib/u/pram/mog_em/faster/mog_em_main.cc @@ -0,0 +1,72 @@ +/** + * @author Parikshit Ram (pram@cc.gatech.edu) + * @file mog_l2e_main.cc + * + * This program test drives the L2 estimation + * of a Gaussian Mixture model. + * + * PARAMETERS TO BE INPUT: + * + * --data + * This is the file that contains the data on which + * the model is to be fit + * + * --mog_em/K + * This is the number of gaussians we want to fit + * on the data, defaults to '1' + * + * --output + * This file will contain the parameters estimated, + * defaults to 'ouotput.csv' + * + */ + +#include "mog.h" + + +int main(int argc, char* argv[]) { + + fx_init(argc, argv); + + ////// READING PARAMETERS AND LOADING DATA ////// + + const char *data_filename = fx_param_str_req(NULL, "data"); + + Matrix data_points; + data::Load(data_filename, &data_points); + + ////// MIXTURE OF GAUSSIANS USING EM ////// + + MoGEM mog; + + struct datanode* mog_em_module = fx_submodule(NULL, "mog_em", "mog_em"); + fx_param_int(mog_em_module, "K", 1); + fx_format_param(mog_em_module, "D", "%d", data_points.n_rows()); + + ////// Timing the initialization of the mixture model ////// + fx_timer_start(mog_em_module, "model_init"); + mog.Init(mog_em_module); + fx_timer_stop(mog_em_module, "model_init"); + + ////// Computing the parameters of the model using the EM algorithm ////// + ArrayList results; + + fx_timer_start(mog_em_module, "EM"); + mog.ExpectationMaximization(data_points); + fx_timer_stop(mog_em_module, "EM"); + + mog.Display(); + mog.OutputResults(&results); + + ////// OUTPUT RESULTS ////// + + const char *output_filename = fx_param_str(NULL, "output", "output.csv"); + + FILE *output_file = fopen(output_filename, "w"); + + ot::Print(results, output_file); + fclose(output_file); + fx_done(); + + return 1; +} diff --git a/fastlib/u/pram/mog_em/faster/phi.h b/fastlib/u/pram/mog_em/faster/phi.h new file mode 100644 index 0000000000..e7f9a7c2ae --- /dev/null +++ b/fastlib/u/pram/mog_em/faster/phi.h @@ -0,0 +1,142 @@ +/** + * @author Parikshit Ram (pram@cc.gatech.edu) + * @file phi.h + * + * This file computes the Gaussian probability + * density function + */ +#include "fastlib/fastlib.h" +#include "fastlib/fastlib_int.h" +#include + +/** + * Calculates the multivariate Gaussian probability density function + * + * Example use: + * @code + * Vector x, mean; + * Matrix cov; + * .... + * long double f = phi(x, mean, cov); + * @endcode + */ + +long double phi(Vector& x , Vector& mean , Matrix& cov) { + + long double det, f; + double exponent; + index_t dim; + Matrix inv; + Vector diff, tmp; + + dim = x.length(); + la::InverseInit(cov, &inv); + det = la::Determinant(cov); + + if( det < 0){ + det = -det; + } + la::SubInit(mean,x,&diff); + la::MulInit(inv, diff, &tmp); + exponent = la::Dot(diff, tmp); + long double tmp1, tmp2, tmp3; + tmp1 = 1; + tmp2 = dim; + tmp2 = tmp2/2; + tmp2 = pow((2*(math::PI)),tmp2); + tmp1 = tmp1/tmp2; + tmp3 = 1; + tmp2 = sqrt(det); + tmp3 = tmp3/tmp2; + tmp2 = -exponent; + tmp2 = tmp2 / 2; + f = (tmp1*tmp3*exp(tmp2)); + + return f; +} + +/** + * Calculates the univariate Gaussian probability density function + * + * Example use: + * @code + * double x, mean, var; + * .... + * long double f = phi(x, mean, var); + * @endcode + */ + +long double phi(double x, double mean, double var) { + + long double f; + + f = exp(-1.0*((x-mean)*(x-mean)/(2*var)))/sqrt(2*math::PI*var); + return f; +} + +/** + * Calculates the multivariate Gaussian probability density function + * and also the gradients with respect to the mean and the variance + * + * Example use: + * @code + * Vector x, mean, g_mean, g_cov; + * ArrayList d_cov; // the dSigma + * .... + * long double f = phi(x, mean, cov, d_cov, &g_mean, &g_cov); + * @endcode + */ + +long double phi(Vector& x, Vector& mean, Matrix& cov, ArrayList& d_cov, Vector *g_mean, Vector *g_cov){ + + long double det, f; + double exponent; + index_t dim; + Matrix inv; + Vector diff, tmp; + + dim = x.length(); + la::InverseInit(cov, &inv); + det = la::Determinant(cov); + + if( det < 0){ + det = -det; + } + la::SubInit(mean,x,&diff); + la::MulInit(inv, diff, &tmp); + exponent = la::Dot(diff, tmp); + long double tmp1, tmp2, tmp3; + tmp1 = 1; + tmp2 = dim; + tmp2 = tmp2/2; + tmp2 = pow((2*(math::PI)),tmp2); + tmp1 = tmp1/tmp2; + tmp3 = 1; + tmp2 = sqrt(det); + tmp3 = tmp3/tmp2; + tmp2 = -exponent; + tmp2 = tmp2 / 2; + f = (tmp1*tmp3*exp(tmp2)); + + // Calculating the g_mean values which would be a (1 X dim) vector + la::ScaleInit(f,tmp,g_mean); + + // Calculating the g_cov values which would be a (1 X (dim*(dim+1)/2)) vector + double *g_cov_tmp; + g_cov_tmp = (double*)malloc(d_cov.size()*sizeof(double)); + for(index_t i = 0; i < d_cov.size(); i++){ + Vector tmp_d; + Matrix inv_d; + long double tmp_d_cov_d_r; + + la::MulInit(d_cov[i],tmp,&tmp_d); + tmp_d_cov_d_r = la::Dot(tmp_d,tmp); + la::MulInit(inv,d_cov[i],&inv_d); + for(index_t j = 0; j < dim; j++) + tmp_d_cov_d_r += inv_d.get(j,j); + g_cov_tmp[i] = f*tmp_d_cov_d_r/2; + } + g_cov->Copy(g_cov_tmp,d_cov.size()); + + return f; +} diff --git a/fastlib/u/pram/mog_em/mog.cc b/fastlib/u/pram/mog_em/mog.cc index 86d33ac061..4aeaf2b520 100644 --- a/fastlib/u/pram/mog_em/mog.cc +++ b/fastlib/u/pram/mog_em/mog.cc @@ -51,7 +51,7 @@ void MoGEM::ExpectationMaximization(Matrix& data_points) { best_l = -INFTY; index_t restarts = 0; // performing 5 restarts and choosing the best from them - while (restarts < 5) { + while (restarts < 1) { // assign initial values to 'mu', 'sig' and 'omega' using k-means KMeans(data_points, &mu_temp, &sigma_temp, &omega_temp, num_gauss);