Advertisement
Guest User

Untitled

a guest
Nov 20th, 2020
63
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 prep_scales(obj, prop){
  5.     Object.defineProperty(obj, prop||'SCALED', {value:{Linear:{}, Log:{}, Log4060:{}, Log4555:{}}, writable:true, configurable:true});
  6.   }
  7.   var scales = {Linear:{}, Log:{}, Log4060:{}, Log4555:{}};
  8.   var nof_scales = 100;
  9.   for (var i=0;i<nof_scales;i++) {
  10.     scales.Linear[i] = 1+9*i/nof_scales;
  11.     scales.Log[i] = Math.pow(10, i/nof_scales);
  12.     scales.Log4060[i] = 4*Math.pow(1.5, i/nof_scales);
  13.     scales.Log4555[i] = 4.5*Math.pow((55/45), i/nof_scales);
  14.   }
  15.   var remove_SCALED = nof_scales>10; // for less memory usage
  16.   function count(regions, cand_prop){
  17.     var bens = {};
  18.     regions.forEach(county => {
  19.       var votes = county[cand_prop||'candidates'].map(cand => count_by_cand(bens, result_US.bens, cand)).reduce((a,c)=>a+c);
  20.       for (var i in pseudoCands) count_by_cand(bens, result_US.bens, makePseudoCand(votes, i, pseudoCands[i]));
  21.       result_US.votes += votes;
  22.     });
  23.     result_US.nof_regions += regions.length;
  24.     var props = make_props(bens);
  25.     return {bens, nof_regions:regions.length, props, diffs:make_diffs(props)};
  26.   }
  27.   var makePseudoCand;
  28.   var countByCand_1;
  29.   function count_by_cand(bens, bens2, cand){
  30.     countByCand_1(bens2, cand);
  31.     return countByCand_1(bens, cand);
  32.   }
  33.   function count_by_cand_11(bens, name, votes){
  34.     count_by_cand_111(bens, name, votes);
  35.     if (!bens[name].SCALED) prep_scales(bens[name]);
  36.     for (var s in scales) for (var i in scales[s]) count_by_cand_111(bens[name].SCALED[s], i, votes*scales[s][i]);
  37.   }
  38.   function count_by_cand_111(bens, name, votes){
  39.     if (!bens[name]) bens[name] = Object.defineProperty({}, 'votes', {value:0, writable:true, configurable:true});
  40.     bens[name].votes += votes;
  41.     var str0 = votes.toString()[0];
  42.     bens[name][str0] = (bens[name][str0]||0) +1;
  43.   }
  44.   var props_theoretical = {ex:0};
  45.   for (var i=1;i<10;i++) {
  46.     props_theoretical[i] = Math.log10(i+1)-Math.log10(i);
  47.     props_theoretical.ex += props_theoretical[i]*props_theoretical[i];
  48.   }
  49.   function make_props(bens){
  50.     var props = {theory:props_theoretical};
  51.     for (var c in bens){
  52.       var num = 0;
  53.       for (var i in bens[c]) num += bens[c][i];
  54.       props[c] = Object.defineProperty({}, 'nof_regions', {value:num, writable:true, configurable:true});
  55.       for (var i in bens[c]) props[c][i] = bens[c][i]/num;
  56.       if (bens[c].SCALED) {
  57.         prep_scales(props[c]);
  58.         for (var s in bens[c].SCALED) props[c].SCALED[s] = make_props(bens[c].SCALED[s]);
  59.         if (remove_SCALED) delete bens[c].SCALED;
  60.       }
  61.     }
  62.     return props;
  63.   }
  64.   function make_diffs(props, ref){
  65.     var diffs = {theory:make_diffs_1(props, props_theoretical)};
  66.     for (var i in pseudoCands) diffs[i] = make_diffs_1(props, props[i]);
  67.     return diffs;
  68.   }
  69.   function make_diffs_1(props, ref){
  70.     var simple    = make_diff_1(props, (diff,i)=>Math.abs(diff),                             ref);
  71.     var weighted  = make_diff_1(props, (diff,i)=>Math.abs(diff)                     /ref[i], ref);
  72.     var weighted2 = make_diff_1(props, (diff,i)=>Math.abs(diff)*props_theoretical.ex/ref[i], ref);
  73.     if (remove_SCALED) for (var c in props) delete props[c].SCALED;
  74.     return {simple, weighted, weighted2};
  75.   }
  76.   function make_diff_1(props, func, ref){
  77.     var diff = {};
  78.     for (var c in props){
  79.       diff[c] = {avg:0};
  80.       for (var i=1;i<10;i++) {//  in props[c]) {
  81.         diff[c][i] = func((props[c][i]||0)-ref[i],i);
  82.         diff[c].avg += diff[c][i];
  83.       }
  84.       diff[c].avg /= Object.keys(props[c]).length;
  85.       if (props[c].SCALED) {
  86.         prep_scales(diff[c]);
  87.         for (var s in diff[c].SCALED) diff[c].SCALED[s] = make_diff_1(props[c].SCALED[s], func, ref);
  88.         prep_scales(diff[c], 'STATS');
  89.         for (var s in diff[c].SCALED) diff[c].STATS[s] = make_stats(diff[c].SCALED[s]);
  90.         if (remove_SCALED) delete diff[c].SCALED;
  91.       }
  92.     }
  93.     return diff;
  94.   }
  95.   function make_stats(scales){
  96.     var ex = 0;
  97.     var max = -Infinity;
  98.     var min = Infinity;
  99.     var num = 0;
  100.     var max_idx = -1;
  101.     var min_idx = -1;
  102.     for (var i=0;i<nof_scales;i++) if (i in scales) {
  103.       var avg = scales[i].avg;
  104.       ex += avg;
  105.       num++;
  106.       if (avg>max) {max = avg; max_idx = i;}
  107.       if (avg<min) {min = avg; min_idx = i;}
  108.     }
  109.     ex /= num;
  110.     var sigma = 0;
  111.     for (var i=0;i<nof_scales;i++) if (i in scales) {
  112.       var diff = scales[i].avg - ex;
  113.       sigma += diff * diff;
  114.     }
  115.     return {ex, num, max, min, sigma:Math.sqrt(sigma/num), max_idx, min_idx};
  116.   }
  117.   function make_result(state, data, prop){
  118.     result_US.states[state] = count(data, prop);
  119.     console.log(result_US.states[state]);
  120.   }
  121.   function make_result_US(){
  122.     Promise.all(jobs).then(
  123.       _ => {
  124.         result_US.props = make_props(result_US.bens);
  125.         result_US.diffs = make_diffs(result_US.props);
  126.         console.log(result_US);
  127.         if (outer_result) outer_result.res = result_US;
  128.       }
  129.     );
  130.   }
  131. //  var states = ["IL"];
  132. //  var states = ["WI"];
  133. //  var states = ["MI", "WI"];
  134. //  var states = ['GA', 'NC', 'PA', 'NV', 'AZ'];
  135. //  var states = ['AZ','CO','FL','GA','IA','MI','MN','NV','NH','NC','OH','PA','TX','VA','WI'];
  136.   var states = ['VT', 'SC', 'IN', 'KY', 'GA', 'VA', 'WV', 'NC', 'OH', 'MA',
  137.                 'AL', 'FL', 'DE', 'MD', 'DC', 'NH', 'MS', 'RI', 'ME', 'NJ',
  138.                 'OK', 'PA', 'IL', 'CT', 'MO', 'TN', 'AR', 'NY', 'ND', 'CO',
  139.                 'MI', 'KS', 'NE', 'MN', 'TX', 'NM', 'WI', 'WY', 'LA', 'AZ',
  140.                 'SD', 'UT', 'MT', 'NV', 'IA', 'ID', 'OR', 'CA', 'WA', 'HI', 'AK'];
  141.   var jobs = [];
  142.   if (window.location.origin.indexOf('ap.org')!=-1) { // for https://digg.com/2020/2020-presidential-electoral-map-trump-biden
  143.     makePseudoCand = function(votes, name, approval_rate){
  144.       return {name:name, voteCount: votes*approval_rate};
  145.     };
  146.     countByCand_1 = function(bens, cand){
  147.       if (cand.voteCount==0) return 0;
  148.       count_by_cand_11(bens, cand.name || metadata.candidates[cand.candidateID].fullName, cand.voteCount);
  149.       return cand.voteCount;
  150.     };
  151.     var metadata = null;
  152.     var url_meta = 'https://interactives.ap.org/elections/live-data/production/2020-11-03/president/metadata.json';
  153.     jobs.push(
  154.       fetch(url_meta)
  155.         .then(res => res.json())
  156.         .then(data => {
  157.           metadata = data;
  158.           for (var i=0;i<states.length;i++) {
  159.             var url_state = 'https://interactives.ap.org/elections/live-data/production/2020-11-03/president/'+states[i]+'.json';
  160.             jobs.push(fetch(url_state)
  161.                       .then(res => res.json())
  162.                       .then(data => {
  163.                         var counties = [];
  164.                         for (var c in data.results[0].results) counties.push(data.results[0].results[c]);
  165.                         make_result(data.results[0].raceID.slice(0,2), counties, 'results');}));
  166.           }
  167.           make_result_US();
  168.         }));
  169.   } else { // for https://edition.cnn.com/election/2020/results/state/wisconsin/president
  170.     makePseudoCand = function(votes, name, approval_rate){
  171.       return {lastName:name,
  172.               voteNum: votes*approval_rate,
  173.               voteStr:(votes*approval_rate).toString()};
  174.     };
  175.     countByCand_1 = function(bens, cand){
  176.       if (cand.voteNum===0) return 0;
  177.       count_by_cand_11(bens, cand.lastName, cand.voteNum) //;Str[0]);
  178.       return cand.voteNum;
  179.     };
  180.     for (var i=0;i<states.length;i++) {
  181.       var url = 'https://politics-elex-results.data.api.cnn.io/results/view/2020-county-races-PG-'+states[i]+'.json';
  182.       jobs.push(fetch(url)
  183.                 .then(res => res.json())
  184.                 .then(data => make_result(data[0].stateAbbreviation, data)));
  185.     }
  186.     make_result_US();
  187.   }
  188. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement