64 using namespace StringUtils;
67 inline long Stem(
double x,
double& scale) {
return (
long(x / scale)); }
71 const std::string& msg)
73 long stem, l, nout = 0, s, sM, sQ1, sQ3, sOH, sOL;
74 int i, sign,
pos, k, leaf;
86 double range = xd[
nd - 1] - xd[0];
103 else if (
range < 1.0)
117 }
while (
range * ::pow(10.0, -nscale) < 1.0 ||
118 range * ::pow(10.0, -nscale) >= 10.0);
121 scale = ::pow(10.0, nscale);
127 double OH = 2.5 * Q3 - 1.5 * Q1;
128 double OL = 2.5 * Q1 - 1.5 * Q3;
131 i = 1 + short((
range / scale) + 0.5);
132 if (xd[0] * xd[
nd - 1] < 0.0)
136 if (
nd > 8 && i < 8 && xd[
nd - 1] != xd[0])
145 for (l = 0; l <
nd; l++)
153 stem =
Stem(fabs(xd[l]), scale);
154 x = 10 * fabs(xd[l] / scale - sign * stem);
155 leaf = short(x + 0.5);
162 buf = asString<long>(stem);
163 if (len < buf.size())
180 sQ1 =
Stem(Q1, scale);
181 sQ3 =
Stem(Q3, scale);
182 sOH =
Stem(OH, scale);
183 sOL =
Stem(OL, scale);
184 for (l = 0; l <
nd; l++)
187 if (xd[l] > OH || xd[l] < OL)
196 stem =
Stem(fabs(xd[l]), scale);
197 x = 10 * fabs(xd[l] / scale - sign * stem);
198 leaf = short(x + 0.5);
207 if (start || s != stem || (s == 0 &&
pos * sign < 0.0))
212 os <<
"Stem and Leaf Plot (scale ";
213 if (nscale < -8 || nscale > 8)
215 os <<
"1.0e" << nscale;
245 os <<
", " <<
nd <<
"pts) : ";
254 while (s < stem || (s == 0 &&
pos * sign < 0))
271 buf = asString<long>(s < 0 ? -s : s);
291 for (kk = buf.size(); kk < len; kk++)
301 if (s == sM && (s != 0 ||
pos * M > 0.0))
307 if ((s == sQ3 && (s != 0 ||
pos * Q3 < 0.0)) ||
308 (s == sQ1 && (s != 0 ||
pos * Q1 < 0.0)))
314 if ((s < sOL || (s == 0 && sOL == 0 && (
pos == -1 && OL > 0.0))) ||
315 (s == sOL && (s != 0 ||
pos * OL > 0.0)))
321 (s == 0 && sOH == 0 && (
pos == 1 && OH < 0.0))) ||
322 (s == sOH && (s != 0 ||
pos * OH > 0.0)))
346 os <<
"\nEND Stem and Leaf Plot (there are " << nout <<
" outliers.)\n";
359 for (
int i = 0; i <
nd; i++)
361 f = double(8 * i + 5) / double(8 *
nd + 2);
362 xd[i] = 4.91 * (::pow(f, 0.14) - ::pow(1 - f, 0.14));
367 #define SEQUENTIAL 1 // don't see much difference in timing...
369 double *c,
double *w)
373 if (!xd || !td || !c ||
nd < 2)
379 const int maxiter = 50;
380 const double conv_limit = ::sqrt(
double(
nd)) * 1.e-3;
382 double x0 = xd[0], t0 = td[0],
mad,
median, conv;
396 for (i = 0; i <
nd; i++)
399 for (j = 1; j < N; j++)
400 P(i, j) =
P(i, j - 1) * (td[i] - t0);
412 for (i = 0; i <
nd; i++)
416 A(0, N) = (xd[i] - x0) * Wts(i);
418 A(0, 0) = 1.0 * Wts(i);
419 for (j = 1; j < N; j++)
420 A(0, j) = A(0, j - 1) * dt;
421 SrifMU<double>(R, Z, A, 1);
427 for (i = 0; i < N; i++)
429 for (j = 0; j <
nd; j++)
431 PT(i, j) *= Wts(j) * Wts(j);
446 Coeff = Cov * PT * D;
457 for (i = 0; i <
nd; i++)
461 for (j = N - 2; j >= 0; j--)
463 fit = fit * dt + Coeff(j);
465 ResCopy(i) = Res(i) = xd[i] - x0 - fit;
468 ResCopy = Res = D -
P * Coeff;
476 for (i = 0; i <
nd; i++)
494 conv =
RMS(OldWts - Wts);
503 if (niter > 2 && conv < conv_limit)
510 for (i = 0; i < N; i++)
516 for (i = 0; i <
nd; i++)
540 catch (std::exception& e)
542 Exception E(
"std except: " +
string(e.what()));
577 save =
new double[
nd];
580 Exception e(
"Could not allocate temporary array");
583 for (i = 0; i <
nd; i++)
590 double tn = double(
nd);
591 double AD = -tn, cdf;
592 for (i = 0; i <
nd; i++)
594 cdf = normalCDF<double>(mean, stddev, xd[i]);
598 AD -= ((2.0 * double(i) - 1.0) *
::log(cdf) +
599 (2.0 * (tn - double(i)) + 1.0) *
::log(1.0 - cdf)) /
603 AD *= 1.0 + (0.75 + 2.25 / tn) / tn;
607 for (i = 0; i <
nd; i++)
618 catch (std::exception& e)
620 Exception E(
"std except: " +
string(e.what()));