diff options
Diffstat (limited to 'src/main/java')
8 files changed, 55 insertions, 93 deletions
diff --git a/src/main/java/edu/brown/cs/student/term/DatabaseQuerier.java b/src/main/java/edu/brown/cs/student/term/DatabaseQuerier.java index 3cb6c79..688270f 100644 --- a/src/main/java/edu/brown/cs/student/term/DatabaseQuerier.java +++ b/src/main/java/edu/brown/cs/student/term/DatabaseQuerier.java @@ -7,7 +7,7 @@ import java.time.Instant; import java.util.ArrayList; import java.util.List; -public class DatabaseQuerier{ +public class DatabaseQuerier { private static Connection conn = null; //TODO: Be prepared to overhaul this to account for IDs @@ -55,7 +55,13 @@ public class DatabaseQuerier{ return stocks; } - //TODO: Fill these in + /** + * Gets all the trades in by stock and buy type, ordered by time + * @param startDate - the start date of these trades + * @param endDate - the end date of these trades + * @return a list of list of trades as specified above + * @throws SQLException - if something goes wrong with connection + */ public List<List<Trade>> getAllTradesByStock(Instant startDate, Instant endDate) throws SQLException { List<List<Trade>> allTrades = new ArrayList<>(); List<String> stocks = getRecentStocks(startDate, endDate); @@ -67,6 +73,15 @@ public class DatabaseQuerier{ return allTrades; } + /** + * Gets a single stock's list of trades for that time period (either buy or sell) + * @param stock - string name of the stock to get the trades for + * @param isBuy - integer whether it's a buy or sell + * @param startDate - an Instant representing the start of the time period + * @param endDate - an Instant representing the end of the time period + * @return - a list of trades for that stock + * @throws SQLException - if issue getting connection + */ public List<Trade> getTradeByStock(String stock, int isBuy, Instant startDate, Instant endDate) throws SQLException{ List<Trade> trades = new ArrayList<>(); diff --git a/src/main/java/edu/brown/cs/student/term/Main.java b/src/main/java/edu/brown/cs/student/term/Main.java index b61d80f..49295f0 100644 --- a/src/main/java/edu/brown/cs/student/term/Main.java +++ b/src/main/java/edu/brown/cs/student/term/Main.java @@ -13,6 +13,7 @@ import joptsimple.OptionParser; import joptsimple.OptionSet; import java.time.Instant; +import java.sql.Date; import java.util.HashMap; import spark.*; @@ -117,12 +118,8 @@ public final class Main { return "OK"; }); Spark.before((request, response) -> response.header("Access-Control-Allow-Origin", "*")); -<<<<<<< HEAD Spark.post("/data", new SuspicionRankHandler()); -======= - Spark.post("/data", new DataHandler()); Spark.post("/profit", new ProfitQueryHandler()); ->>>>>>> 73ad5303d59cd93a115401b1bac4aad87dfb1cb7 } /** @@ -172,7 +169,10 @@ public final class Main { System.out.println("DBQuerier Test, couldn't connect to db???"); return "Error"; } + //TODO: Clark th is is just to fix a no return error + return "Temporary"; } + } /** diff --git a/src/main/java/edu/brown/cs/student/term/ProfitCalculation.java b/src/main/java/edu/brown/cs/student/term/ProfitCalculation.java index 19b439e..30d7f36 100644 --- a/src/main/java/edu/brown/cs/student/term/ProfitCalculation.java +++ b/src/main/java/edu/brown/cs/student/term/ProfitCalculation.java @@ -332,6 +332,10 @@ public class ProfitCalculation { while (rs.next()) { int id = rs.getInt("holder_id"); this.person = rs.getString("holder_name"); + //TODO: Temporary fix for the moneyinput divide by 0 error + if(moneyInput == 0){ + moneyInput = 1; + } profitMap.put(id, this.calculateGains() / moneyInput); } } catch (SQLException throwables) { diff --git a/src/main/java/edu/brown/cs/student/term/hub/HubSearch.java b/src/main/java/edu/brown/cs/student/term/hub/HubSearch.java index baee5af..ccefeef 100644 --- a/src/main/java/edu/brown/cs/student/term/hub/HubSearch.java +++ b/src/main/java/edu/brown/cs/student/term/hub/HubSearch.java @@ -52,15 +52,9 @@ public class HubSearch { //dampening factor double damp = 0.15; /* - In normal page rank: - links is the pages that k links to, if k links to j, then - we want to add some weight to j from k, but that - weight is diluted by the number of links that k has - in general because we don't want to count a bunch of links - from a page as highly as one targeted link from one page to another In Hub Search - leader = j, follower = k, if follower links to (follows) leader, + we have the leader and follower if follower links to (follows) leader, then we want to add some weight from follower to leader, but should that weight be diluted by the number of people who followed leader or the number of people who follower followed @@ -85,62 +79,4 @@ public class HubSearch { return sum <= 0.001; } - - - - - - - - - - /* - def pageRank(pList: List[Page]): mutable.HashMap[Int, Double] = { - val n = pList.length - val weights = new Array[Double](n) - val r = new Array[Double](n) - val rPrime = Array.fill[Double](n)(1.0 / n) - - while (!distance(r, rPrime)) { - Array.copy(rPrime, 0, r, 0, n) - for (j <- 0 to n - 1) { - rPrime(j) = 0 - for (k <- 0 to n - 1) { - val wjk = getWeight(pList(j), pList(k), n) - rPrime(j) = (rPrime(j) + (wjk * r(k))) - } - } - } - - val pageRank = new mutable.HashMap[Int, Double]() - - for (i <- 0 to rPrime.length - 1) { - pageRank.put(pList(i).getID, rPrime(i)) - } - pageRank - } - - def getWeight(j: Page, k: Page, n: Int): Double = { - - val links = k.getLinks - val nk = links.size - - if (links.contains(j.getTitle.toLowerCase)) { - ((0.15 / n) + ((1 - 0.15) * (1.0 / nk))) - } else if (nk == 0) { - ((0.15 / n) + ((1 - 0.15) * (1.0 / n))) - } else { - (0.15 / n) - } - } - - def distance(r1: Array[Double], r2: Array[Double]): Boolean = { - var sum = 0.0 - for (i <- 0 to r1.length - 1) { - sum = sum + Math.pow((r1(i) - r2(i)), 2) - } - sum <= .001 - - } - */ } diff --git a/src/main/java/edu/brown/cs/student/term/hub/LinkMapper.java b/src/main/java/edu/brown/cs/student/term/hub/LinkMapper.java index f4bc2c4..b490ea1 100644 --- a/src/main/java/edu/brown/cs/student/term/hub/LinkMapper.java +++ b/src/main/java/edu/brown/cs/student/term/hub/LinkMapper.java @@ -21,7 +21,7 @@ public class LinkMapper { /** - * Returns the follower => leaders map as is, does not update it + * Returns the follower to leaders map as is, does not update it * @return person to follower map */ public Map<Holder, Set<Holder>> getFollowerToLeaders() { @@ -29,9 +29,11 @@ public class LinkMapper { } /** - * The links between people and their followers who made the same trade + * This links a person and the set of people whose trades they followed * in the same time period - * @return the newly updated link map + * @param start - instant representing start of time period to look at + * @param end - instant representing end of time period to look at + * @return - the newly updated link map */ public Map<Holder, Set<Holder>> makeFollowerLinks(Instant start, Instant end){ if(databaseQuerier != null){ @@ -54,10 +56,10 @@ public class LinkMapper { return followerToLeaders; } - //leader => follower => List of trades in common - - //TODO: Try to create both leader => follower and follower => leader map at once - //only if necessary tho! + /** + * Converts a single trade list into entries in the follower to leader map + * @param tradeList - a list of trades for a single stock (either buy or sell) + */ private void convertTradeListToFollowerMap(List<Trade> tradeList){ List<Holder> holderList = new ArrayList<>(); diff --git a/src/main/java/edu/brown/cs/student/term/hub/SuspicionRanker.java b/src/main/java/edu/brown/cs/student/term/hub/SuspicionRanker.java index 7cb7e32..dd959f0 100644 --- a/src/main/java/edu/brown/cs/student/term/hub/SuspicionRanker.java +++ b/src/main/java/edu/brown/cs/student/term/hub/SuspicionRanker.java @@ -2,17 +2,10 @@ package edu.brown.cs.student.term.hub; import edu.brown.cs.student.term.DatabaseQuerier; import edu.brown.cs.student.term.ProfitCalculation; -import edu.brown.cs.student.term.hub.Holder; -import edu.brown.cs.student.term.hub.HubSearch; -import edu.brown.cs.student.term.hub.LinkMapper; -import edu.brown.cs.student.term.repl.Command; -import java.sql.SQLException; import java.time.Instant; import java.sql.Date; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; +import java.util.*; public class SuspicionRanker { @@ -21,6 +14,13 @@ public class SuspicionRanker { this.querier = db; } + public <K, V extends Comparable<V>> V getMaxOfMap(Map<K, V> map) { + Map.Entry<K, V> maxEntry = Collections.max(map.entrySet(), Map.Entry.comparingByValue()); + System.out.println(maxEntry); + System.out.println(maxEntry.getValue()); + return maxEntry.getValue(); + } + public List<Holder> getSuspicionScoreList(Instant start, Instant end){ List<Holder> suspicionList = new ArrayList<>(); try { @@ -34,10 +34,14 @@ public class SuspicionRanker { Map<Integer, Double> profitMap = pc.getProfitMap(); + double profitMax = getMaxOfMap(profitMap); + + double hubMax = getMaxOfMap(holderToHubScore); + for(Holder guy: holderToHubScore.keySet()){ - Double profitScore = profitMap.get(guy.getId()); - Double suspicionScore = (holderToHubScore.get(guy)*0.2 - + profitScore * 0.8) * 100; + double normalizedProfitScore = profitMap.get(guy.getId()) / profitMax; + double normalizedHubScore = holderToHubScore.get(guy) / hubMax; + double suspicionScore = normalizedHubScore * 0.6 + normalizedProfitScore * 0.4; guy.setSuspicionScore(suspicionScore); suspicionList.add(guy); } diff --git a/src/main/java/edu/brown/cs/student/term/repl/Command.java b/src/main/java/edu/brown/cs/student/term/repl/Command.java index 056c7df..b110644 100644 --- a/src/main/java/edu/brown/cs/student/term/repl/Command.java +++ b/src/main/java/edu/brown/cs/student/term/repl/Command.java @@ -1,13 +1,13 @@ package edu.brown.cs.student.term.repl; /** - * Interface implemented by all five StarsCommand commands. - * Implemented by StarsCommand, NaiveNeighbors, NaiveRadiusCommand, RadiusCommand, NeighborsCommand + * Interface implemented by all the commands that the REPL is responsible for */ public interface Command { /** - * Main run method for every command. - * @param args arguments for the command + * The function that the command calls when run from the REPL + * @param args - the arguments for the command + * @return a string representation of the output, or error message */ String run(String[] args); } diff --git a/src/main/java/edu/brown/cs/student/term/repl/REPL.java b/src/main/java/edu/brown/cs/student/term/repl/REPL.java index a1d5c23..5381d45 100644 --- a/src/main/java/edu/brown/cs/student/term/repl/REPL.java +++ b/src/main/java/edu/brown/cs/student/term/repl/REPL.java @@ -16,6 +16,7 @@ public class REPL { /** * Constructor for a REPL object. + * @param userCommands - map of string to Command object for REPL to run */ public REPL(HashMap<String, Command> userCommands) { commands = userCommands; |
