public IndTestFisherZPercentIndependent(List<DataSet> dataSets, double alpha) {
    this.dataSets = dataSets;
    this.variables = dataSets.get(0).getVariables();

    data = new ArrayList<TetradMatrix>();

    for (DataSet dataSet : dataSets) {
      dataSet = DataUtils.center(dataSet);
      TetradMatrix _data = dataSet.getDoubleData();
      data.add(_data);
    }

    ncov = new ArrayList<TetradMatrix>();
    for (TetradMatrix d : this.data) ncov.add(d.transpose().times(d).scalarMult(1.0 / d.rows()));

    setAlpha(alpha);
    rows = new int[dataSets.get(0).getNumRows()];
    for (int i = 0; i < getRows().length; i++) getRows()[i] = i;

    variablesMap = new HashMap<Node, Integer>();
    for (int i = 0; i < variables.size(); i++) {
      variablesMap.put(variables.get(i), i);
    }

    this.recursivePartialCorrelation = new ArrayList<RecursivePartialCorrelation>();
    for (TetradMatrix covMatrix : ncov) {
      recursivePartialCorrelation.add(new RecursivePartialCorrelation(getVariables(), covMatrix));
    }
  }
  public void insertSimple(int val, Node ptr, int i) {
    // declare two lists to store keys and ptrs
    List<Integer> tempKeys = new ArrayList<Integer>();
    List<Node> tempPtrs = new ArrayList<Node>();

    // store keys and ptrs into list
    for (int j = i; j <= this.getLast(); j++) {
      tempKeys.add(this.getKey(j));
      tempPtrs.add(this.getPtr(j));
    }

    // set keys and ptrs at position i
    this.keys[i] = val;
    this.ptrs[i] = ptr;
    // set parent reference
    this.ptrs[i].setParent(new Reference(this, i, false));

    int end = tempKeys.size();
    // add keys and ptrs to current node from previous lists
    for (int j = i + 1; j <= i + end; j++) {
      this.keys[j] = tempKeys.get(j - i - 1);
      this.ptrs[j] = tempPtrs.get(j - i - 1);
      // set parent reference
      this.ptrs[j].setParent(new Reference(this, j, false));
    }

    // increase lastindex because of insertion
    this.lastindex++;
  }
  /*
   * Method to that creates and returns and array list of the initial population of cells.
   * The number of cells in the population is determined by the integer 'population_size'
   */
  private static List<Cell> initiatePopulation(List<Cell> population, int population_size) {
    int[][][] genome = new int[haploid_number][2][2]; // The genome of unlabelled chromosomes

    for (int count_one = 0; count_one < genome.length; count_one++) {
      for (int count_two = 0; count_two < genome[count_one].length; count_two++) {
        for (int count_three = 0;
            count_three < genome[count_one][count_two].length;
            count_three++) {
          genome[count_one][count_two][count_three] = 0; // Set each DNA strand to unlabelled

          // printString("Chromosome " + Integer.toString(count_one+1) + "; Pair " +
          // Integer.toString(count_two+1) + "; Strand " + Integer.toString(count_three+1));
        }
      }
    }

    // POPULATE GENOME WITH 0's

    // Create the starting population of cells, all as generation 1
    for (int counter = 0; counter < population_size; counter++) {
      population.add(
          new Cell(
              counter,
              newest_generation + 1,
              -1,
              true,
              genome)); // Create a new Cell object with the following values.

      id_of_last_created_cell = counter; // Track the id of the lastcreated cell
    } // for
    return population;
  } // initiate_first_population()
  public boolean isIndependent(Node x, Node y, List<Node> z) {
    int[] all = new int[z.size() + 2];
    all[0] = variablesMap.get(x);
    all[1] = variablesMap.get(y);
    for (int i = 0; i < z.size(); i++) {
      all[i + 2] = variablesMap.get(z.get(i));
    }

    int sampleSize = data.get(0).rows();
    List<Double> pValues = new ArrayList<Double>();

    for (int m = 0; m < ncov.size(); m++) {
      TetradMatrix _ncov = ncov.get(m).getSelection(all, all);
      TetradMatrix inv = _ncov.inverse();
      double r = -inv.get(0, 1) / sqrt(inv.get(0, 0) * inv.get(1, 1));

      double fisherZ =
          sqrt(sampleSize - z.size() - 3.0) * 0.5 * (Math.log(1.0 + r) - Math.log(1.0 - r));
      double pValue;

      if (Double.isInfinite(fisherZ)) {
        pValue = 0;
      } else {
        pValue = 2.0 * (1.0 - RandomUtil.getInstance().normalCdf(0, 1, abs(fisherZ)));
      }

      pValues.add(pValue);
    }

    double _cutoff = alpha;

    if (fdr) {
      _cutoff = StatUtils.fdrCutoff(alpha, pValues, false);
    }

    Collections.sort(pValues);
    int index = (int) round((1.0 - percent) * pValues.size());
    this.pValue = pValues.get(index);

    //        if (this.pValue == 0) {
    //            System.out.println("Zero pvalue "+ SearchLogUtils.independenceFactMsg(x, y, z,
    // getPValue()));
    //        }

    boolean independent = this.pValue > _cutoff;

    if (verbose) {
      if (independent) {
        TetradLogger.getInstance()
            .log("independencies", SearchLogUtils.independenceFactMsg(x, y, z, getPValue()));
        //            System.out.println(SearchLogUtils.independenceFactMsg(x, y, z, getPValue()));
      } else {
        TetradLogger.getInstance()
            .log("dependencies", SearchLogUtils.dependenceFactMsg(x, y, z, getPValue()));
      }
    }

    return independent;
  }
 /** @return the list of variable varNames. */
 public List<String> getVariableNames() {
   List<Node> variables = getVariables();
   List<String> variableNames = new ArrayList<String>();
   for (Node variable1 : variables) {
     variableNames.add(variable1.getName());
   }
   return variableNames;
 }
  public ICovarianceMatrix getCov() {
    List<DataSet> _dataSets = new ArrayList<DataSet>();

    for (DataSet d : dataSets) {
      _dataSets.add(DataUtils.standardizeData(d));
    }

    return new CovarianceMatrix(DataUtils.concatenateData(dataSets));
  }
示例#7
0
 public int[] getContainedPositions(BoundingBox boundingBox) {
   List<Integer> result = new ArrayList<>();
   List<P> positions = getPositions();
   for (int i = 0; i < positions.size(); i++) {
     P position = positions.get(i);
     if (position.hasCoordinates() && boundingBox.contains(position)) result.add(i);
   }
   return toArray(result);
 }
示例#8
0
  @SuppressWarnings("UnusedDeclaration")
  public ViaMichelinRoute asViaMichelinFormat() {
    if (getFormat() instanceof ViaMichelinFormat) return (ViaMichelinRoute) this;

    List<Wgs84Position> wgs84Positions = new ArrayList<>();
    for (P position : getPositions()) {
      wgs84Positions.add(position.asWgs84Position());
    }
    return new ViaMichelinRoute(getName(), wgs84Positions);
  }
示例#9
0
  @SuppressWarnings("UnusedDeclaration")
  public OvlRoute asOvlFormat() {
    if (getFormat() instanceof OvlFormat) return (OvlRoute) this;

    List<Wgs84Position> ovlPositions = new ArrayList<>();
    for (P position : getPositions()) {
      ovlPositions.add(position.asOvlPosition());
    }
    return new OvlRoute(getCharacteristics(), getName(), ovlPositions);
  }
示例#10
0
  @SuppressWarnings("UnusedDeclaration")
  public MagicMapsPthRoute asMagicMapsPthFormat() {
    if (getFormat() instanceof MagicMapsPthFormat) return (MagicMapsPthRoute) this;

    List<GkPosition> gkPositions = new ArrayList<>();
    for (P position : getPositions()) {
      gkPositions.add(position.asGkPosition());
    }
    return new MagicMapsPthRoute(getCharacteristics(), gkPositions);
  }
示例#11
0
  @SuppressWarnings("UnusedDeclaration")
  public MagicMapsIktRoute asMagicMapsIktFormat() {
    if (getFormat() instanceof MagicMapsIktFormat) return (MagicMapsIktRoute) this;

    List<Wgs84Position> wgs84Positions = new ArrayList<>();
    for (P position : getPositions()) {
      wgs84Positions.add(position.asWgs84Position());
    }
    return new MagicMapsIktRoute(getName(), getDescription(), wgs84Positions);
  }
示例#12
0
  @SuppressWarnings("UnusedDeclaration")
  public GarminFlightPlanRoute asGarminFlightPlanFormat() {
    if (getFormat() instanceof GarminFlightPlanFormat) return (GarminFlightPlanRoute) this;

    List<GarminFlightPlanPosition> flightPlanPositions = new ArrayList<>();
    for (P position : getPositions()) {
      flightPlanPositions.add(position.asGarminFlightPlanPosition());
    }
    return new GarminFlightPlanRoute(getName(), getDescription(), flightPlanPositions);
  }
示例#13
0
  @SuppressWarnings("UnusedDeclaration")
  public TourRoute asTourFormat() {
    if (getFormat() instanceof TourFormat) return (TourRoute) this;

    List<TourPosition> tourPositions = new ArrayList<>();
    for (P position : getPositions()) {
      tourPositions.add(position.asTourPosition());
    }
    return new TourRoute(getName(), tourPositions);
  }
示例#14
0
  @SuppressWarnings("UnusedDeclaration")
  public NokiaLandmarkExchangeRoute asNokiaLandmarkExchangeFormat() {
    if (getFormat() instanceof NokiaLandmarkExchangeFormat)
      return (NokiaLandmarkExchangeRoute) this;

    List<Wgs84Position> wgs84Positions = new ArrayList<>();
    for (P position : getPositions()) {
      wgs84Positions.add(position.asWgs84Position());
    }
    return new NokiaLandmarkExchangeRoute(getName(), getDescription(), wgs84Positions);
  }
示例#15
0
 public int[] getPositionsWithinDistanceToPredecessor(double distance) {
   List<Integer> result = new ArrayList<>();
   List<P> positions = getPositions();
   if (positions.size() <= 2) return new int[0];
   P previous = positions.get(0);
   for (int i = 1; i < positions.size() - 1; i++) {
     P next = positions.get(i);
     if (!next.hasCoordinates() || next.calculateDistance(previous) <= distance) result.add(i);
     else previous = next;
   }
   return toArray(result);
 }
  private static void runSimulationExecutive(
      int sim_duration,
      int simulation_time_interval,
      List<Cell> cell_population,
      int cell_doubling_rate) {
    int population_size = cell_population.size();
    int next_div_time = cell_doubling_rate;
    int[][][] diploid_genome =
        new int[haploid_number][2]
            [2]; // The genome, chromosome->homologous pair->complementary DNA strands

    // Progress through time at selected intervals for a specified duration
    for (int current_time = 0;
        current_time < sim_duration;
        current_time += simulation_time_interval) {
      Random rand = new Random();
      // Perform division of all cells that can divide as determined by a random number generator
      // and the set division threshhold
      // As new cells are being added to the list, only go through the cells in the population
      // before new cells are added i.e use the previous population size as max number of iterations
      for (int counter = 0; counter < population_size; counter++) {
        if (cell_population.get(counter).getDivisionStatus()) {
          // Random number generator, 0 = divide, 1 = don't divide

          double coin_flip = rand.nextDouble();
          // printString(Double.toString(coin_flip));
          if (coin_flip >= 0.5) {
            // Remove one cell from the current generation and add two to the next
            // by changing the generation of the dividing cell to current_gen + 1 and
            // create a new cell object for the next generation
            cell_population.get(counter).setGen(cell_population.get(counter).getGen() + 1);
            cell_population.add(
                new Cell(
                    id_of_last_created_cell + 1, newest_generation + 1, -1, true, diploid_genome));
            id_of_last_created_cell++;
          }
        } // if the cell can divide
      } // for all items in main population array

      population_size =
          cell_population.size(); // Update the population size after the round of division
      newest_generation++; // Update the value of the most recent generation after the division
      // round
      next_div_time +=
          cell_doubling_rate; // Set the next time of division by adding the doubling rate to the
      // current time

      printString(current_time + " " + population_size);
    } // for
  } // runSimulationExecutive
示例#17
0
  public void revert() {
    List<P> positions = getPositions();
    List<P> reverted = new ArrayList<>();
    for (P position : positions) {
      reverted.add(0, position);
    }
    order(reverted);

    String routeName = getName();
    if (!routeName.endsWith(REVERSE_ROUTE_NAME_POSTFIX))
      routeName = routeName + REVERSE_ROUTE_NAME_POSTFIX;
    else
      routeName = routeName.substring(0, routeName.length() - REVERSE_ROUTE_NAME_POSTFIX.length());
    setName(routeName);
  }
示例#18
0
文件: Octree.java 项目: vlitomsk/fit
  public void add(Tp obj, int maxDepth) {
    if (!in(0, 0, 0, 2, obj)) throw new RuntimeException("Object is out of octree");

    if (maxDepth != 0) {
      for (int i = 0; i < 2; ++i)
        for (int j = 0; j < 2; ++j)
          for (int k = 0; k < 2; ++k)
            if (in(i, j, k, 1, obj)) {
              if (subtree[i][j][k] == null)
                subtree[i][j][k] = new Octree(xc[i], xc[i + 1], yc[j], yc[j + 1], zc[k], zc[k + 1]);
              subtree[i][j][k].add(obj, maxDepth - 1);
              return;
            }
    }
    // Gstate.counter++;
    container.add(obj);
    //        System.out.println("gstate counter:" + Gstate.counter);
  }
  private List<Chessboard> getPossibleMutations(Chessboard chessboard) {
    List<Chessboard> result = new ArrayList<Chessboard>();

    for (int i = 0; i < chessboard.board.length; ++i) {
      for (int j = i + 1; j < chessboard.board.length; ++j) {
        int rook1Col = chessboard.board[i];
        int rook2Col = chessboard.board[j];

        if (rook1Col > rook2Col) {
          Chessboard newChessboard =
              new Chessboard(Arrays.copyOf(chessboard.board, chessboard.board.length));
          newChessboard.board[j] = rook1Col;
          newChessboard.board[i] = rook2Col;
          result.add(newChessboard);
        }
      }
    }

    return result;
  }
示例#20
0
  public void solve() throws Exception {
    P[] ps = new P[N];
    Map<Integer, Set<Integer>> map = new HashMap<Integer, Set<Integer>>();
    for (int i = 0; i < N; i++) {
      P p = new P();
      p.x = sc.nextInt();
      p.y = sc.nextInt();
      ps[i] = p;
      if (!map.containsKey(p.x)) {
        Set<Integer> set = new HashSet<Integer>();
        map.put(p.x, set);
      }
      map.get(p.x).add(p.y);
    }
    Arrays.sort(
        ps,
        new Comparator<P>() {
          @Override
          public int compare(P p1, P p2) {
            if (p1.x != p2.x) {
              return p1.x - p2.x;
            }
            return p1.y - p2.y;
          }
        });

    List<Pair> yp = new ArrayList<Pair>();
    for (int i = 0; i < ps.length - 1; i++) {
      if (ps[i].x != ps[i + 1].x) continue;
      int lidx = i + 1;
      while (lidx + 1 < ps.length && ps[i].x == ps[lidx + 1].x) {
        lidx++;
      }
      Pair pair = new Pair();
      pair.s = ps[i].y;
      pair.l = ps[lidx].y;
      pair.base = ps[i].x;
      yp.add(pair);
      i = lidx;
    }

    Arrays.sort(
        ps,
        new Comparator<P>() {
          @Override
          public int compare(P p1, P p2) {
            if (p1.y != p2.y) {
              return p1.y - p2.y;
            }
            return p1.x - p2.x;
          }
        });
    List<Pair> xp = new ArrayList<Pair>();
    for (int i = 0; i < ps.length - 1; i++) {
      if (ps[i].y != ps[i + 1].y) continue;
      int lidx = i + 1;
      while (lidx + 1 < ps.length && ps[i].y == ps[lidx + 1].y) {
        lidx++;
      }
      Pair pair = new Pair();
      pair.s = ps[i].x;
      pair.l = ps[lidx].x;
      pair.base = ps[i].y;
      xp.add(pair);
      i = lidx;
    }

    int ans = 0;
    for (int i = 0; i < yp.size(); i++) {
      int xnow = yp.get(i).base;
      int sy = yp.get(i).s;
      int ly = yp.get(i).l;
      for (int j = 0; j < xp.size(); j++) {
        int y = xp.get(j).base;
        if (y < sy || ly < y) continue;
        if (xp.get(j).s > xnow || xp.get(j).l < xnow) continue;
        try {
          if (!map.get(xnow).contains(y)) {
            ans++;
            map.get(xnow).add(y);
          }
        } catch (Exception ex) {

        }
      }
    }
    ans += ps.length;
    out.println(ans);
  }
  private static List<String> importGenomeData(
      File genome_text_file, String target_organism, int sex) throws IOException {

    List<String> temp_genome_data =
        new ArrayList<String>(); // The genome data array that contains the sizes of each chromosome
    // int haploid_number; // Used to determine the size of the first dimension in the
    // temp_genome_data array
    boolean found_target_organism =
        false; // Used to determine what action to take when a new header line in the file is found;
    // close if true, keep reading if false
    boolean end_of_genome = false; // True when the Y chromosome has been dealt with

    // Construct BufferedReader from FileReader; search for header line of target organism and
    // obtain the haploid number then create a two dimensional array, size of the first dimension
    // equals the haploid number, size of second dimension equals two (two chromosomes to form
    // deploid organism)
    // At this point, the next lines correspond to the size of each chromosome so populate the newly
    // created array
    // with this information. Stop reading when no more new lines or when a new header line is found
    BufferedReader genome_file_reader = new BufferedReader(new FileReader(genome_text_file));

    String line = null;
    StringBuilder organism_name;
    while ((line = genome_file_reader.readLine()) != null) {
      String[] split_line = line.split(" ");

      if (split_line[0].equals(">")) // If this is a first header line
      {
        organism_name =
            new StringBuilder()
                .append(split_line[1])
                .append(" ")
                .append(
                    split_line[
                        2]); // Recreate the genus and species of the organism from the strings that
        // were separated during the splitting of the line
        if (organism_name
            .toString()
            .equals(target_organism)) // If this line refers to the organism of interest
        {
          found_target_organism = true;
          haploid_number =
              Integer.parseInt(split_line[4]); // Get the haploid number stored in the header line
        } else {
          found_target_organism = false;
          continue; // This is a header line but it is not the organism of interest
        }
      } else if (found_target_organism
          && !end_of_genome) // This is not a header line and we probably want to import this line
      {
        // boolean autosome = true;
        switch (sex) {
          case 1: // Female
            {
              if (split_line[0].equals("chrX")) {
                temp_genome_data.add(split_line[1] + "," + split_line[1]);
              } else if (split_line[0].equals("chrY")) {
                end_of_genome = true;
              } // Ignore the Y chromosome
              else
                temp_genome_data.add(
                    split_line[1] + "," + split_line[1]); // The current line is an autosome
            }
            break;
          case 2: // Male
            {
              if (split_line[0].equals("chrX")) {
                temp_genome_data.add(split_line[1] + ",");
              } else if (split_line[0].equals("chrY")) {
                String temp =
                    temp_genome_data.get(
                        temp_genome_data.size() - 1); // Store the value already there
                temp_genome_data.remove(temp_genome_data.size() - 1);

                temp = temp + split_line[1];
                temp_genome_data.add(temp);

                end_of_genome = true;
              } else
                temp_genome_data.add(
                    split_line[1] + "," + split_line[1]); // The current line is an autosome
            }
            break;
        }
      }
    } // while ((line = genome_file_reader.readLine()) != null)
    genome_file_reader.close();
    return temp_genome_data;
  } // importGenomeData