Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function(outer_result){
- var result_US = {bens:{}, nof_regions:0, votes:0, states:{}};
- 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;},{});
- function count(regions, cand_prop){
- var bens = {};
- regions.forEach(county => {
- var votes = county[cand_prop||'candidates'].map(cand => count_by_cand(bens, result_US.bens, cand)).reduce((a,c)=>a+c);
- for (var i in pseudoCands) count_by_cand(bens, result_US.bens, makePseudoCand(votes, i, pseudoCands[i]));
- result_US.votes += votes;
- });
- result_US.nof_regions += regions.length;
- var props = make_props(bens, regions.length);
- return {bens, nof_regions:regions.length, props, diffs:make_diffs(props)};
- }
- var makePseudoCand;
- var countByCand_1;
- function count_by_cand(bens, bens2, cand){
- countByCand_1(bens2, cand);
- return countByCand_1(bens, cand);
- }
- function count_by_cand_11(bens, name, str0){
- if (!bens[name]) bens[name] = {};
- bens[name][str0] = (bens[name][str0]||0) +1;
- }
- var props_theoretical = {ex:0};
- for (var i=1;i<10;i++) {
- props_theoretical[i] = Math.log10(i+1)-Math.log10(i);
- props_theoretical.ex += props_theoretical[i]*props_theoretical[i];
- }
- function make_props(bens, nof_regions){
- var props = {theory:props_theoretical};
- for (var c in bens){
- props[c] = {};
- for (var i in bens[c]) props[c][i] = bens[c][i]/nof_regions;
- }
- return props;
- }
- function make_diffs(props, ref){
- var diffs = {theory:make_diffs_1(props, props_theoretical)};
- for (var i in pseudoCands) diffs[i] = make_diffs_1(props, props[i]);
- return diffs;
- }
- function make_diffs_1(props, ref){
- var simple = make_diff_1(props, (diff,i)=>Math.abs(diff), ref);
- var weighted = make_diff_1(props, (diff,i)=>Math.abs(diff) /ref[i], ref);
- var weighted2 = make_diff_1(props, (diff,i)=>Math.abs(diff)*props_theoretical.ex/ref[i], ref);
- return {simple, weighted, weighted2};
- }
- function make_diff_1(props, func, ref){
- var diff = {};
- for (var c in props){
- diff[c] = {avg:0};
- var num = 0;
- for (var i in props[c]) {
- diff[c][i] = func(props[c][i]-ref[i],i);
- diff[c].avg += diff[c][i];
- num++;
- }
- diff[c].avg /= num;
- }
- return diff;
- }
- function make_result(state, data, prop){
- result_US.states[state] = count(data, prop);
- console.log(result_US.states[state]);
- }
- function make_result_US(){
- Promise.all(jobs).then(
- _ => {
- result_US.props = make_props(result_US.bens, result_US.nof_regions);
- result_US.diffs = make_diffs(result_US.props);
- console.log(result_US);
- if (outer_result) outer_result.res = result_US;
- }
- );
- }
- // var states = ["IL"];
- // var states = ["WI"];
- // var states = ["MI", "WI"];
- // var states = ['GA', 'NC', 'PA', 'NV', 'AZ'];
- // var states = ['AZ','CO','FL','GA','IA','MI','MN','NV','NH','NC','OH','PA','TX','VA','WI'];
- var states = ['VT', 'SC', 'IN', 'KY', 'GA', 'VA', 'WV', 'NC', 'OH', 'MA',
- 'AL', 'FL', 'DE', 'MD', 'DC', 'NH', 'MS', 'RI', 'ME', 'NJ',
- 'OK', 'PA', 'IL', 'CT', 'MO', 'TN', 'AR', 'NY', 'ND', 'CO',
- 'MI', 'KS', 'NE', 'MN', 'TX', 'NM', 'WI', 'WY', 'LA', 'AZ',
- 'SD', 'UT', 'MT', 'NV', 'IA', 'ID', 'OR', 'CA', 'WA', 'HI', 'AK'];
- var jobs = [];
- if (window.location.origin.indexOf('ap.org')!=-1) { // for https://digg.com/2020/2020-presidential-electoral-map-trump-biden
- makePseudoCand = function(votes, name, approval_rate){
- return {name:name, voteCount: votes*approval_rate};
- };
- countByCand_1 = function(bens, cand){
- if (cand.voteCount==0) return 0;
- count_by_cand_11(bens, cand.name || metadata.candidates[cand.candidateID].fullName, cand.voteCount.toString()[0]);
- return cand.voteCount;
- };
- var metadata = null;
- var url_meta = 'https://interactives.ap.org/elections/live-data/production/2020-11-03/president/metadata.json';
- jobs.push(
- fetch(url_meta)
- .then(res => res.json())
- .then(data => {
- metadata = data;
- for (var i=0;i<states.length;i++) {
- var url_state = 'https://interactives.ap.org/elections/live-data/production/2020-11-03/president/'+states[i]+'.json';
- jobs.push(fetch(url_state)
- .then(res => res.json())
- .then(data => {
- var counties = [];
- for (var c in data.results[0].results) counties.push(data.results[0].results[c]);
- make_result(data.results[0].raceID.slice(0,2), counties, 'results');}));
- }
- make_result_US();
- }));
- } else { // for https://edition.cnn.com/election/2020/results/state/wisconsin/president
- makePseudoCand = function(votes, name, approval_rate){
- return {lastName:name,
- voteNum: votes*approval_rate,
- voteStr:(votes*approval_rate).toString()};
- };
- countByCand_1 = function(bens, cand){
- if (cand.voteNum===0) return 0;
- count_by_cand_11(bens, cand.lastName, cand.voteStr[0]);
- return cand.voteNum;
- };
- for (var i=0;i<states.length;i++) {
- var url = 'https://politics-elex-results.data.api.cnn.io/results/view/2020-county-races-PG-'+states[i]+'.json';
- jobs.push(fetch(url)
- .then(res => res.json())
- .then(data => make_result(data[0].stateAbbreviation, data)));
- }
- make_result_US();
- }
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement