Advertisement
Guest User

Untitled

a guest
Nov 17th, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function(outer_result){
  2.   var result_US = {bens:{}, nof_regions:0, votes:0, states:{}};
  3.   var pseudoCands = [1, 0.666666, 0.6, 0.55, 0.5, 0.45, 0.4, 0.333333].reduce((a,c)=>{a['C'+(c*100).toString().split('.')[0]] = c; return a;},{});
  4.   function count(regions, cand_prop){
  5.     var bens = {};
  6.     regions.forEach(county => {
  7.       var votes = county[cand_prop||'candidates'].map(cand => count_by_cand(bens, result_US.bens, cand)).reduce((a,c)=>a+c);
  8.       for (var i in pseudoCands) count_by_cand(bens, result_US.bens, makePseudoCand(votes, i, pseudoCands[i]));
  9.       result_US.votes += votes;
  10.     });
  11.     result_US.nof_regions += regions.length;
  12.     var props = make_props(bens, regions.length);
  13.     return {bens, nof_regions:regions.length, props, diffs:make_diffs(props)};
  14.   }
  15.   var makePseudoCand;
  16.   var countByCand_1;
  17.   function count_by_cand(bens, bens2, cand){
  18.     countByCand_1(bens2, cand);
  19.     return countByCand_1(bens, cand);
  20.   }
  21.   function count_by_cand_11(bens, name, str0){
  22.     if (!bens[name]) bens[name] = {};
  23.     bens[name][str0] = (bens[name][str0]||0) +1;
  24.   }
  25.   var props_theoretical = {ex:0};
  26.   for (var i=1;i<10;i++) {
  27.     props_theoretical[i] = Math.log10(i+1)-Math.log10(i);
  28.     props_theoretical.ex += props_theoretical[i]*props_theoretical[i];
  29.   }
  30.   function make_props(bens, nof_regions){
  31.     var props = {theory:props_theoretical};
  32.     for (var c in bens){
  33.       props[c] = {};
  34.       for (var i in bens[c]) props[c][i] = bens[c][i]/nof_regions;
  35.     }
  36.     return props;
  37.   }
  38.   function make_diffs(props, ref){
  39.     var diffs = {theory:make_diffs_1(props, props_theoretical)};
  40.     for (var i in pseudoCands) diffs[i] = make_diffs_1(props, props[i]);
  41.     return diffs;
  42.   }
  43.   function make_diffs_1(props, ref){
  44.     var simple    = make_diff_1(props, (diff,i)=>Math.abs(diff),                             ref);
  45.     var weighted  = make_diff_1(props, (diff,i)=>Math.abs(diff)                     /ref[i], ref);
  46.     var weighted2 = make_diff_1(props, (diff,i)=>Math.abs(diff)*props_theoretical.ex/ref[i], ref);
  47.     return {simple, weighted, weighted2};
  48.   }
  49.   function make_diff_1(props, func, ref){
  50.     var diff = {};
  51.     for (var c in props){
  52.       diff[c] = {avg:0};
  53.       var num = 0;
  54.       for (var i in props[c]) {
  55.         diff[c][i] = func(props[c][i]-ref[i],i);
  56.         diff[c].avg += diff[c][i];
  57.         num++;
  58.       }
  59.       diff[c].avg /= num;
  60.     }
  61.     return diff;
  62.   }
  63.   function make_result(state, data, prop){
  64.     result_US.states[state] = count(data, prop);
  65.     console.log(result_US.states[state]);
  66.   }
  67.   function make_result_US(){
  68.     Promise.all(jobs).then(
  69.       _ => {
  70.         result_US.props = make_props(result_US.bens, result_US.nof_regions);
  71.         result_US.diffs = make_diffs(result_US.props);
  72.         console.log(result_US);
  73.         if (outer_result) outer_result.res = result_US;
  74.       }
  75.     );
  76.   }
  77. //  var states = ["IL"];
  78. //  var states = ["WI"];
  79. //  var states = ["MI", "WI"];
  80. //  var states = ['GA', 'NC', 'PA', 'NV', 'AZ'];
  81. //  var states = ['AZ','CO','FL','GA','IA','MI','MN','NV','NH','NC','OH','PA','TX','VA','WI'];
  82.   var states = ['VT', 'SC', 'IN', 'KY', 'GA', 'VA', 'WV', 'NC', 'OH', 'MA',
  83.                 'AL', 'FL', 'DE', 'MD', 'DC', 'NH', 'MS', 'RI', 'ME', 'NJ',
  84.                 'OK', 'PA', 'IL', 'CT', 'MO', 'TN', 'AR', 'NY', 'ND', 'CO',
  85.                 'MI', 'KS', 'NE', 'MN', 'TX', 'NM', 'WI', 'WY', 'LA', 'AZ',
  86.                 'SD', 'UT', 'MT', 'NV', 'IA', 'ID', 'OR', 'CA', 'WA', 'HI', 'AK'];
  87.   var jobs = [];
  88.   if (window.location.origin.indexOf('ap.org')!=-1) { // for https://digg.com/2020/2020-presidential-electoral-map-trump-biden
  89.     makePseudoCand = function(votes, name, approval_rate){
  90.       return {name:name, voteCount: votes*approval_rate};
  91.     };
  92.     countByCand_1 = function(bens, cand){
  93.       if (cand.voteCount==0) return 0;
  94.       count_by_cand_11(bens, cand.name || metadata.candidates[cand.candidateID].fullName, cand.voteCount.toString()[0]);
  95.       return cand.voteCount;
  96.     };
  97.     var metadata = null;
  98.     var url_meta = 'https://interactives.ap.org/elections/live-data/production/2020-11-03/president/metadata.json';
  99.     jobs.push(
  100.       fetch(url_meta)
  101.         .then(res => res.json())
  102.         .then(data => {
  103.           metadata = data;
  104.           for (var i=0;i<states.length;i++) {
  105.             var url_state = 'https://interactives.ap.org/elections/live-data/production/2020-11-03/president/'+states[i]+'.json';
  106.             jobs.push(fetch(url_state)
  107.                       .then(res => res.json())
  108.                       .then(data => {
  109.                         var counties = [];
  110.                         for (var c in data.results[0].results) counties.push(data.results[0].results[c]);
  111.                         make_result(data.results[0].raceID.slice(0,2), counties, 'results');}));
  112.           }
  113.           make_result_US();
  114.         }));
  115.   } else { // for https://edition.cnn.com/election/2020/results/state/wisconsin/president
  116.     makePseudoCand = function(votes, name, approval_rate){
  117.       return {lastName:name,
  118.               voteNum: votes*approval_rate,
  119.               voteStr:(votes*approval_rate).toString()};
  120.     };
  121.     countByCand_1 = function(bens, cand){
  122.       if (cand.voteNum===0) return 0;
  123.       count_by_cand_11(bens, cand.lastName, cand.voteStr[0]);
  124.       return cand.voteNum;
  125.     };
  126.     for (var i=0;i<states.length;i++) {
  127.       var url = 'https://politics-elex-results.data.api.cnn.io/results/view/2020-county-races-PG-'+states[i]+'.json';
  128.       jobs.push(fetch(url)
  129.                 .then(res => res.json())
  130.                 .then(data => make_result(data[0].stateAbbreviation, data)));
  131.     }
  132.     make_result_US();
  133.   }
  134. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement