| 1461 | } |
| 1462 | |
| 1463 | float runEM (const vector<float>& x,const vector<float>& y,double & a0,double & a1, double & a2, double & a3, int maximalNumberOfIterations,int ploidy, int maximalNumberOfCopies) { |
| 1464 | |
| 1465 | float rmserror = -1; |
| 1466 | |
| 1467 | if (x.size() != y.size()) { |
| 1468 | cerr << "Error: object size is different"; |
| 1469 | return 0; |
| 1470 | } |
| 1471 | vector <int> cluster (x.size()); |
| 1472 | vector <float> res (maximalNumberOfCopies+1); |
| 1473 | |
| 1474 | int count = 0; |
| 1475 | |
| 1476 | for (count = 0; count < maximalNumberOfIterations; count++) { |
| 1477 | |
| 1478 | for (int i = 0; i <(int)x.size(); i++) { |
| 1479 | for (int j = 0; j <= maximalNumberOfCopies; j++) |
| 1480 | res[j] = fabs(polynomial(x[i],j*a0/ploidy,j*a1/ploidy,j*a2/ploidy,j*a3/ploidy)-y[i]); |
| 1481 | cluster[i] = get_min_index(res); |
| 1482 | } |
| 1483 | int npoints = 0; |
| 1484 | for (int i = 0; i <(int)x.size(); i++) |
| 1485 | if (cluster[i] == ploidy) |
| 1486 | npoints++; |
| 1487 | int nvars = 3; //fit by cubic polynomial |
| 1488 | ap::real_2d_array xy; |
| 1489 | xy.setlength(npoints,nvars+1); |
| 1490 | int pos = 0; |
| 1491 | for (int i = 0; i <(int)x.size(); i++) { |
| 1492 | if (cluster[i] == ploidy){ |
| 1493 | xy(pos,2) = x[i]; |
| 1494 | xy(pos,0) = x[i]*x[i]*x[i]; |
| 1495 | xy(pos,1) = x[i]*x[i]; |
| 1496 | xy(pos,3) = y[i]; |
| 1497 | pos++; |
| 1498 | } |
| 1499 | } |
| 1500 | |
| 1501 | linearmodel lm; |
| 1502 | int info; |
| 1503 | lrreport ar; |
| 1504 | lrbuild(xy,npoints,nvars,info,lm,ar); |
| 1505 | if (info != 1) { |
| 1506 | cerr << "Error in linear regression, code: " << info <<"\n"; |
| 1507 | break; |
| 1508 | } |
| 1509 | rmserror = float(ar.rmserror); |
| 1510 | ap::real_1d_array v; |
| 1511 | v.setlength(nvars+1); |
| 1512 | lrunpack(lm,v,nvars); |
| 1513 | //cout << v(0); //x |
| 1514 | //cout << v(1); //x^3 |
| 1515 | //cout << v(2); //x^2 |
| 1516 | //cout << v(3); //intercept |
| 1517 | |
| 1518 | cout << a0 - v(0) << "\t"; //x^3 |
| 1519 | cout << a1 - v(1) << "\t"; //x^2 |
| 1520 | cout << a2 - v(2) << "\t"; //x |
no test coverage detected