Example #1
0
 protected void compute() {
   nny = (int) ni[0].parseInput();
   df.writeLine("Number of events is: n = " + nny);
   y = DatanRandom.standardNormal(nny);
   out(y);
   mllg = new MinLogLikeGauss(y);
   x0 = new DatanVector(2);
   x0.setElement(0, 1.);
   x0.setElement(1, 2.);
   MinSim ms = new MinSim(x0, mllg);
   x = ms.getMinPosition();
   fcont = ms.getMinimum();
   df.writeLine("Minimization with MinSim yields x = ");
   df.writeLine(x.toString());
   // covariance matrix
   MinCov mc = new MinCov(x, mllg);
   cx = mc.getCovarianceMatrix(1.);
   df.writeLine("Covariance Matrix cx = ");
   df.writeLine(cx.toString());
   // asymmetric errors
   fcont = fcont + 0.5;
   MinAsy ma = new MinAsy(x, cx, mllg);
   dxasy = ma.getAsymmetricErrors(fcont);
   DatanMatrix as = new DatanMatrix(dxasy);
   df.writeLine("Asymmetic errors:");
   df.writeLine(as.toString());
   df.writeLine("ma.hasConverged() = " + ma.hasConverged());
   plotScatterDiagram();
   plotParameterPlane();
 }
Example #2
0
 private void computeMatrixOfDerivatives() {
   double del, der, fp, fm, arg, sav;
   int i2, iy;
   fp = 0.;
   fm = 0.;
   for (int im = 0; im < m; im++) {
     i2 = -1;
     for (int il = 0; il < n + r; il++) {
       if (il < r) {
         if (list[il] != 0) {
           i2++;
           sav = x.getElement(il);
           arg = Math.abs(sav);
           if (arg < CUT) arg = CUT;
           del = DELTA * arg;
           x.setElement(il, sav + del);
           fp = uf.getValue(y, x, im);
           x.setElement(il, sav - del);
           fm = uf.getValue(y, x, im);
           der = (fp - fm) / (del + del);
           e.setElement(im, i2, der);
           x.setElement(il, sav);
         }
       } else {
         i2++;
         iy = il - r;
         sav = y.getElement(iy);
         arg = Math.abs(sav);
         if (arg < CUT) arg = CUT;
         del = DELTA * arg;
         y.setElement(iy, sav + del);
         fp = uf.getValue(y, x, im);
         y.setElement(iy, sav - del);
         fm = uf.getValue(y, x, im);
         der = (fp - fm) / (del + del);
         e.setElement(im, i2, der);
         y.setElement(iy, sav);
       }
     }
   }
 }
Example #3
0
 protected void plotScatterDiagram() {
   // plot sample as one dimensional scatter plot and Gaussian
   double xmax = 5.;
   double xmin = -5.;
   DatanGraphics.openWorkstation(getClass().getName(), "E3Min_1.ps");
   DatanGraphics.setFormat(0., 0.);
   DatanGraphics.setWindowInComputingCoordinates(xmin, xmax, 0., .5);
   DatanGraphics.setViewportInWorldCoordinates(-.15, .9, .16, .86);
   DatanGraphics.setWindowInWorldCoordinates(-.414, 1., 0., 1.);
   DatanGraphics.setBigClippingWindow();
   DatanGraphics.chooseColor(2);
   DatanGraphics.drawFrame();
   DatanGraphics.drawScaleX("y");
   DatanGraphics.drawScaleY("f(y)");
   DatanGraphics.drawBoundary();
   double xpl[] = new double[2];
   double ypl[] = new double[2];
   // plot scatter diagram
   DatanGraphics.chooseColor(1);
   for (int i = 0; i < y.length; i++) {
     xpl[0] = y[i];
     xpl[1] = y[i];
     ypl[0] = 0.;
     ypl[0] = .1;
     DatanGraphics.drawPolyline(xpl, ypl);
   }
   // draw Gaussian corresponding to solution
   int npl = 100;
   xpl = new double[npl];
   ypl = new double[npl];
   double fact = 1. / (Math.sqrt(2. * Math.PI) * x.getElement(1));
   double dpl = (xmax - xmin) / (double) (npl - 1);
   for (int i = 0; i < npl; i++) {
     xpl[i] = xmin + (double) i * dpl;
     ypl[i] = fact * Math.exp(-.5 * Math.pow((xpl[i] - x.getElement(0)) / x.getElement(1), 2.));
   }
   DatanGraphics.chooseColor(5);
   DatanGraphics.drawPolyline(xpl, ypl);
   // draw caption
   String sn = "N = " + nny;
   numForm.setMaximumFractionDigits(3);
   numForm.setMinimumFractionDigits(3);
   String sx1 = ", x_1# = " + numForm.format(x.getElement(0));
   String sx2 = ", x_2# = " + numForm.format(x.getElement(1));
   String sdx1 = ", &D@x_1# = " + numForm.format(Math.sqrt(cx.getElement(0, 0)));
   String sdx2 = ", &D@x_2# = " + numForm.format(Math.sqrt(cx.getElement(1, 1)));
   caption = sn + sx1 + sx2 + sdx1 + sdx2;
   DatanGraphics.setBigClippingWindow();
   DatanGraphics.chooseColor(2);
   DatanGraphics.drawCaption(1., caption);
   DatanGraphics.closeWorkstation();
 }
Example #4
0
  protected void plotParameterPlane() {
    double x1 = x.getElement(0);
    double x2 = x.getElement(1);
    double dx1 = Math.sqrt(cx.getElement(0, 0));
    double dx2 = Math.sqrt(cx.getElement(1, 1));
    double rho = (cx.getElement(1, 0)) / (dx1 * dx2);
    // prepare size of plot
    double xmin = x1 - 2. * dx1;
    double xmax = x1 + 2. * dx1;
    double ymin = x2 - 2. * dx2;
    double ymax = x2 + 2. * dx2;
    DatanGraphics.openWorkstation(getClass().getName(), "E3Min_2.ps");
    DatanGraphics.setFormat(0., 0.);
    DatanGraphics.setWindowInComputingCoordinates(xmin, xmax, ymin, ymax);
    DatanGraphics.setViewportInWorldCoordinates(.2, .9, .16, .86);
    DatanGraphics.setWindowInWorldCoordinates(-.414, 1., 0., 1.);
    DatanGraphics.setBigClippingWindow();
    DatanGraphics.chooseColor(2);
    DatanGraphics.drawFrame();
    DatanGraphics.drawScaleX("x_1");
    DatanGraphics.drawScaleY("x_2");
    DatanGraphics.drawBoundary();
    DatanGraphics.chooseColor(5);
    // draw data point with errors (and correlation)
    DatanGraphics.drawDatapoint(1, 1., x1, x2, dx1, dx2, rho);
    DatanGraphics.chooseColor(2);
    DatanGraphics.drawCaption(1., caption);
    // draw confidence contour
    double fcont = mllg.getValue(x) + .5;
    int nx = 100;
    int ny = 100;
    double dx = (xmax - xmin) / (int) nx;
    double dy = (ymax - ymin) / (int) ny;
    MinLogLikeGaussCont mllgc = new MinLogLikeGaussCont();
    //      System.out.println(" x = " + x.toString() + ", mllgc.getValue(x) = " +
    // mllgc.getValue(x.getElement(0), x.getElement(1)));
    DatanGraphics.setBigClippingWindow();
    DatanGraphics.chooseColor(1);
    DatanGraphics.drawContour(xmin, ymin, dx, dy, nx, ny, fcont, mllgc);
    // draw asymmetric errors as horiyontal and vertical bars
    DatanGraphics.chooseColor(3);
    double[] xpl = new double[2];
    double[] ypl = new double[2];
    for (int i = 0; i < 2; i++) {
      if (i == 0) xpl[0] = x1 - dxasy[0][0];
      else xpl[0] = x1 + dxasy[0][1];
      xpl[1] = xpl[0];
      ypl[0] = ymin;
      ypl[1] = ymax;
      DatanGraphics.drawPolyline(xpl, ypl);
    }
    for (int i = 0; i < 2; i++) {
      if (i == 0) ypl[0] = x2 - dxasy[1][0];
      else ypl[0] = x2 + dxasy[1][1];
      ypl[1] = ypl[0];
      xpl[0] = xmin;
      xpl[1] = xmax;
      DatanGraphics.drawPolyline(xpl, ypl);
    }

    DatanGraphics.closeWorkstation();
  }
Example #5
0
 /**
  * @param y vector of measurements.
  * @param cy covaariance matrix of measurements.
  * @param x vector of first approximations of unknowns.
  * @param list array containing the elements of a list specifying which of the n variables are
  *     fixed (list element = 0) and which are adjustable (list element = 1). m number of constaint
  *     equations
  * @param uf user function which must be an extension of the abstract class DatanUserFunction.
  */
 public LsqGen(
     DatanVector y, DatanMatrix cy, DatanVector x, int[] list, int m, DatanUserFunction uf) {
   this.y = y;
   this.cy = cy;
   this.x = x;
   this.list = list;
   this.m = m;
   this.uf = uf;
   n = y.getNumberOfElements();
   r = list.length;
   nred = 0;
   for (int i = 0; i < r; i++) {
     if (list[i] == 1) nred++;
   }
   l = n + nred;
   t = new DatanVector(l);
   f = new DatanMatrix(cy);
   f.choleskyInversion();
   fy = f.choleskyDecomposition();
   mfvec = new DatanVector(1);
   ok = new boolean[1];
   mf = 0.;
   nstep = 100;
   d = new DatanVector(m);
   loop:
   for (int istep = 0; istep < nstep; istep++) {
     mflast = mf;
     f = new DatanMatrix(l, l);
     f.putSubmatrix(nred, nred, fy);
     e = new DatanMatrix(m, l);
     computeMatrixOfDerivatives();
     for (int k = 0; k < m; k++) {
       d.setElement(k, -uf.getValue(y, x, k));
     }
     b = f.choleskyMultiply(t);
     b = b.multiply(-1.);
     u = f.leastSquaresWithConstraints(b, e, d, mfvec, 0., ok);
     converged = ok[0];
     if (!converged) break loop;
     mf = mfvec.getElement(0);
     if (nred > 0) {
       // update x
       int ired = -1;
       for (int i = 0; i < r; i++) {
         if (list[i] != 0) {
           ired++;
           x.setElement(i, x.getElement(i) + u.getElement(ired));
         }
       }
     }
     // update y and t
     for (int i = 0; i < n; i++) {
       y.setElement(i, y.getElement(i) + u.getElement(i + nred));
       t.setElement(i + nred, t.getElement(i + nred) + u.getElement(i + nred));
     }
     // test for convergence
     if (istep > 0 && Math.abs(mf - mflast) < mf * EPSILON + T) break loop;
   }
   if (converged) {
     computeMatrixOfDerivatives();
     a = e.getSubmatrix(m, n, 0, nred);
     help = cy.multiplyWithTransposed(a);
     gb = a.multiply(help);
     gb.choleskyInversion();
     if (nred > 0) {
       help2 = e.getSubmatrix(m, nred, 0, 0);
       help3 = help2.multiplyTransposedWith(gb);
       cx = help3.multiply(help2);
       cx.choleskyInversion();
       // cx contains covariance matrx of unknowns
     } else {
       cx = new DatanMatrix(1, 1);
       // for nred ==0 the matrix cx is set to be the (1 x 1) zero matrix
     }
     a = e.getSubmatrix(m, n, 0, nred);
     help = a.multiply(cy);
     help2 = gb.multiply(a.multiply(cy));
     help3 = help.multiplyTransposedWith(help2);
     cyimproved = cy.sub(help3);
     // cyimproved contains covariance matrix of "improved" measurements
   }
 }
 /**
  * @param y data
  * @param k parameter giving the length 2 * k + 1 of the averaging interval
  * @param l oder of the averaging polynomial
  * @param p probability defining confidence limits
  */
 public TimeSeries(double[] y, int k, int l, double p) {
   this.y = y;
   this.k = k;
   this.l = l;
   this.p = p;
   n = y.length;
   eta = new double[n + 2 * k];
   coneta = new double[n + 2 * k];
   // quantile of Student's distribution
   pprime = 0.5 * (p + 1.);
   nf = 2 * k - l;
   talpha = StatFunct.quantileStudent(pprime, nf);
   // compute matrices depending on k and l
   k21 = 2 * k + 1;
   l1 = l + 1;
   a = new DatanMatrix(k21, l1);
   for (int i = 0; i < k21; i++) {
     for (int j = 0; j < l1; j++) {
       if (j == 0) a.setElement(i, j, -1.);
       else a.setElement(i, j, a.getElement(i, j - 1) * (double) (i - k));
     }
   }
   ata1 = a.multiplyTransposedWith(a);
   ata1.choleskyInversion();
   ata1at = ata1.multiplyWithTransposed(a);
   ata1at = ata1at.multiply(-1.);
   // moving averages and confidence limits for inner part
   ymat = new DatanMatrix(y);
   for (int i = 2 * k; i < n; i++) {
     ytmp = ymat.getSubmatrix(k21, 1, i - 2 * k, 0);
     x = ata1at.multiply(ytmp);
     eta[i] = x.getElement(0, 0);
     etatmp = a.multiply(x);
     etatmp = etatmp.add(ytmp);
     sy2 = etatmp.multiplyTransposedWith(etatmp);
     double s = sy2.getElement(0, 0) / (double) nf;
     double a0 = Math.sqrt(Math.abs(ata1.getElement(0, 0)));
     coneta[i] = a0 * Math.sqrt(s) * talpha;
     // moving averages and confidence limits for end sections
     if (i == 2 * k || i == n - 1) {
       tt = new double[l + 1];
       if (i == 2 * k) {
         iadd = 2 * k;
         is = -1;
       } else {
         iadd = n - 1;
         is = 1;
       }
       for (int i1 = 1; i1 <= 2 * k; i1++) {
         j = is * i1;
         for (int i2 = 0; i2 < l + 1; i2++) {
           for (int i3 = 0; i3 <= i2; i3++) {
             if (i3 == 0) tt[i2] = 1.;
             else tt[i2] = tt[i2] * (double) j;
           }
         }
         tmat = new DatanMatrix(tt);
         seta2 = tmat.multiplyTransposedWith(ata1.multiply(tmat));
         double se2 = s * seta2.getElement(0, 0);
         etai = tmat.multiplyTransposedWith(x);
         eta[iadd + j] = etai.getElement(0, 0);
         coneta[iadd + j] = Math.sqrt(Math.abs(se2)) * talpha;
       }
     }
   }
 }
Example #7
0
 protected void compute() {
   n = (int) ni[0].parseInput();
   t0 = ni[1].parseInput();
   deltat = ni[2].parseInput();
   x1 = ni[3].parseInput();
   x2 = ni[4].parseInput();
   sigma = ni[5].parseInput();
   // generate data points
   t = new DatanVector(n);
   y = new DatanVector(n);
   dy = new DatanVector(n);
   rand = DatanRandom.standardNormal(n);
   for (int i = 0; i < n; i++) {
     t.setElement(i, t0 + (double) i * deltat);
     y.setElement(i, x1 * Math.pow(t.getElement(i), x2) + sigma * rand[i]);
     dy.setElement(i, sigma);
   }
   // find 1st approximation of unknowns by method of log-log plot
   tlog = new double[n];
   ylog = new double[n];
   dellog = new double[n];
   int npos = 0;
   for (int i = 0; i < n; i++) {
     if (t.getElement(i) > 0. && y.getElement(i) > 0.) {
       tlog[npos] = Math.log(t.getElement(i));
       ylog[npos] = Math.log(y.getElement(i));
       dellog[npos] = 1.;
       npos++;
     }
   }
   DatanVector vtlog = new DatanVector(npos);
   DatanVector vylog = new DatanVector(npos);
   DatanVector vdellog = new DatanVector(npos);
   for (int j = 0; j < npos; j++) {
     vtlog.setElement(j, tlog[j]);
     vylog.setElement(j, ylog[j]);
     vdellog.setElement(j, dellog[j]);
   }
   LsqPol lp = new LsqPol(vtlog, vylog, vdellog, 2);
   x = lp.getResult();
   x.setElement(0, Math.exp(x.getElement(0)));
   df.writeLine(" x = " + x.toString());
   // perform fit
   int[] list = {1, 1};
   powerlaw = new Powerlaw();
   LsqNon ln = new LsqNon(t, y, dy, x, list, powerlaw);
   x = ln.getResult();
   x1 = x.getElement(0);
   x2 = x.getElement(1);
   cov = ln.getCovarianceMatrix();
   delx1 = Math.sqrt(cov.getElement(0, 0));
   delx2 = Math.sqrt(cov.getElement(1, 1));
   rho = cov.getElement(1, 0) / (delx1 * delx2);
   m = ln.getChiSquare();
   p = 1. - StatFunct.cumulativeChiSquared(m, n - 1);
   // curve of fitted exponential
   xpl = new double[1001];
   ypl = new double[1001];
   dpl = (double) (n - 1) * deltat / 1000.;
   for (int i = 0; i < 1001; i++) {
     xpl[i] = t0 + (double) i * dpl;
     ypl[i] = x1 * Math.pow(xpl[i], x2);
   }
   // prepare data points for plotting
   datx = new double[n];
   daty = new double[n];
   datsx = new double[n];
   datsy = new double[n];
   datrho = new double[n];
   for (int i = 0; i < n; i++) {
     datx[i] = t.getElement(i);
     daty[i] = y.getElement(i);
     datsx[i] = 0.;
     datsy[i] = dy.getElement(i);
     datrho[i] = 0.;
   }
   // display data and fitted curve
   caption =
       "x_1#="
           + String.format(Locale.US, "%5.2f", x1)
           + ", x_2#="
           + String.format(Locale.US, "%5.2f", x2)
           + ", &D@x_1#="
           + String.format(Locale.US, "%5.2f", delx1)
           + ", &D@x_2#="
           + String.format(Locale.US, "%5.2f", delx2)
           + ", &r@="
           + String.format(Locale.US, "%5.2f", rho)
           + ", M="
           + String.format(Locale.US, "%5.2f", m)
           + ", P="
           + String.format(Locale.US, "%6.4f", p);
   new GraphicsWithDataPointsAndPolyline(
       getClass().getName(),
       "",
       xpl,
       ypl,
       1,
       .3,
       datx,
       daty,
       datsx,
       datsy,
       datrho,
       "t",
       "y",
       caption);
 }