Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/versx/RDMopole2
Browse files Browse the repository at this point in the history
  • Loading branch information
versx committed Dec 2, 2020
2 parents 74fbd76 + 4d71a84 commit 18468c2
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 12 deletions.
5 changes: 0 additions & 5 deletions src/data/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,18 +252,13 @@ async function getShinyRates(filter) {
const name = Localizer.instance.getPokemonName(pokemonId);
const shiny = (row.count || 0);
const total = (getTotalCount(pokemonId) || 0);
const rate = shiny === 0 || total === 0 ? 0 : Math.round(total / shiny);
const imageUrl = await Localizer.instance.getPokemonIcon(pokemonId);
data.push({
id: {
formatted: `#${pokemonId}`,
sort: pokemonId
},
pokemon: `<img src="${imageUrl}" style="width: 32px; height: 32px; object-fit: contain;" />&nbsp;${name}`,
rate: {
formatted: `1/${rate}`,
sort: -rate,
},
shiny: shiny,
total: total,
});
Expand Down
77 changes: 70 additions & 7 deletions src/views/pokemon.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,11 @@
<table id="table" class="table table-{{style}} table-striped table-bordered table-hover dt-responsive nowrap" style="position: center; width:100%">
<thead class="thead-dark">
<tr>
<th class="all">{{ID}}</th>
<th>{{ID}}</th>
<th class="all">{{Pokemon}}</th>
<th class="all">{{Shiny Rate}}</th>
<th>{{Shiny}}</th>
<th>{{Shiny Rate}}</th>
<th class="all">{{Shiny Rate 95% Confidence}}</th>
<th class="all">{{Shiny}}</th>
<th>{{Total}}</th>
</tr>
</thead>
Expand Down Expand Up @@ -470,6 +471,70 @@
var shinyDate = $('#shiny-date').val();
var query = `?date=${shinyDate}`;
const withPrecision = (num, denominator) => Number(num.toPrecision(Math.ceil(Math.log10(1 + denominator))));
const computeShinyRate = (row, type, set, meta) => type === 'sort'
? row.shiny / row.total
: row.shiny > 0 ? `1/${withPrecision(row.total / row.shiny, row.total)}` : '0';
/**
* Computes exact confidence interval around proportion [lowerEstimate, mostLikely, upperEstimate].
* The confidence level computed will be (1 - upperTail - lowerTail).
*
* @param numerator An integer indicating the number of events.
* @param denominator An integer indicating the number of trials.
* @param lowerTail A real number in [0, 1] indicating % area of lower tail.
* @param upperTail A real number in [0, 1] indicating % area of upper tail.
* @param accuracy Accuracy of the estimate. Omit to determine automatically based on denominator (.1/denominator).
*/
function computeBinomialConfidenceIntervals(numerator, denominator, lowerTail = .05, upperTail = 0, accuracy = null) {
if (accuracy === null) {
accuracy = .1 / denominator;
}
const computeBinomialLikelihood = (N, p, lower, higher) => {
const q = p / (1 - p);
let sum = 0, total = 0;
for (let k = 0, v = 1; k <= N; ++k, v = v * q * (N + 1 - k) / k) {
total += v;
if (k >= lower && k <= higher) {
sum += v;
}
if (total > 1e30) {
sum /= 1e30;
total /= 1e30;
v /= 1e30;
}
}
return sum / total;
};
const binarySearch = (lower, higher, predicateIsHigh) => {
let v = (lower + higher) / 2;
while (higher - lower > accuracy) {
if (predicateIsHigh(v)) {
higher = v;
} else {
lower = v;
}
v = (lower + higher) / 2;
}
return v;
};
let lower = 0, upper = 1;
const likely = numerator / denominator;
if (numerator > 0 && lowerTail > 0) {
lower = binarySearch(0, likely, (p) => computeBinomialLikelihood(denominator, p, numerator, denominator) > lowerTail);
}
if (numerator < denominator && upperTail > 0) {
upper = binarySearch(likely, 1, (p) => computeBinomialLikelihood(denominator, p, 0, numerator) < upperTail);
}
return [lower, likely, upper];
}
const computeShinyRateConfidence = (row, type, set, meta) => {
const rate = computeBinomialConfidenceIntervals(row.shiny, row.total)[0];
return type === 'sort'
? rate
: row.shiny > 0 ? `1/${withPrecision(1 / rate, row.total)}` : '0';
};
const table = $('#table').DataTable({
"ajax": {
"url": "/api/pokemon/shiny" + query,
Expand Down Expand Up @@ -505,10 +570,8 @@
sort: "id.sort"
} },
{ "data": "pokemon" },
{ data: {
_: "rate.formatted",
sort: "rate.sort"
} },
{ data: computeShinyRate },
{ data: computeShinyRateConfidence },
{ "data": "shiny" },
{ "data": "total" },
],
Expand Down
1 change: 1 addition & 0 deletions static/locales/_de.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"Search by Pokemon name...": "Search by Pokemon name...",
"Type in a Pokemon name": "Type in a Pokemon name",
"Shiny Rate": "Shiny Rate",
"Shiny Rate 95% Confidence": "Shiny Rate 95% Confidence",
"Shiny": "Shiny",
"Total": "Total",
"Gyms Under Attack!": "Gyms Under Attack!",
Expand Down
1 change: 1 addition & 0 deletions static/locales/_en.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"Search by Pokemon name...": "Search by Pokemon name...",
"Type in a Pokemon name": "Type in a Pokemon name",
"Shiny Rate": "Shiny Rate",
"Shiny Rate 95% Confidence": "Shiny Rate 95% Confidence",
"Shiny": "Shiny",
"Total": "Total",
"Gyms Under Attack!": "Gyms Under Attack!",
Expand Down
1 change: 1 addition & 0 deletions static/locales/_es.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"Search by Pokemon name...": "Search by Pokemon name...",
"Type in a Pokemon name": "Type in a Pokemon name",
"Shiny Rate": "Shiny Rate",
"Shiny Rate 95% Confidence": "Shiny Rate 95% Confidence",
"Shiny": "Shiny",
"Total": "Total",
"Gyms Under Attack!": "Gyms Under Attack!",
Expand Down

0 comments on commit 18468c2

Please sign in to comment.