mirror of
https://github.com/pupperpowell/bibdle.git
synced 2026-02-04 02:44:43 -05:00
added function to measure correlation between ease of solving and number of players
This commit is contained in:
@@ -2,18 +2,15 @@ import Database from 'bun:sqlite';
|
||||
|
||||
// Database path - adjust if your database is located elsewhere
|
||||
const dbPath = process.env.DATABASE_URL || './local.db';
|
||||
|
||||
console.log(`Connecting to database: ${dbPath}`);
|
||||
|
||||
const db = new Database(dbPath);
|
||||
|
||||
// Query all rows from daily_completions
|
||||
const query = db.query(`
|
||||
SELECT date, guess_count
|
||||
FROM daily_completions
|
||||
ORDER BY date
|
||||
SELECT date, guess_count
|
||||
FROM daily_completions
|
||||
ORDER BY date
|
||||
`);
|
||||
|
||||
const rows = query.all() as { date: string; guess_count: number }[];
|
||||
|
||||
if (rows.length === 0) {
|
||||
@@ -50,4 +47,60 @@ const overallAvg = (totalGuesses / totalCompletions).toFixed(2);
|
||||
console.log('--------------|-------------|-------------------');
|
||||
console.log(`Overall Average: ${overallAvg} guesses across ${totalCompletions} completions`);
|
||||
|
||||
db.close();
|
||||
// Calculate correlation between avg_guesses and completions
|
||||
function calculateCorrelation(data: { avgGuesses: number; completions: number }[]): number {
|
||||
const n = data.length;
|
||||
if (n < 2) return 0;
|
||||
|
||||
const avgX = data.reduce((sum, d) => sum + d.avgGuesses, 0) / n;
|
||||
const avgY = data.reduce((sum, d) => sum + d.completions, 0) / n;
|
||||
|
||||
let numerator = 0;
|
||||
let sumXSquared = 0;
|
||||
let sumYSquared = 0;
|
||||
|
||||
for (const d of data) {
|
||||
const xDiff = d.avgGuesses - avgX;
|
||||
const yDiff = d.completions - avgY;
|
||||
numerator += xDiff * yDiff;
|
||||
sumXSquared += xDiff * xDiff;
|
||||
sumYSquared += yDiff * yDiff;
|
||||
}
|
||||
|
||||
const denominator = Math.sqrt(sumXSquared * sumYSquared);
|
||||
return denominator === 0 ? 0 : numerator / denominator;
|
||||
}
|
||||
|
||||
// Prepare data for correlation analysis
|
||||
const allData = Array.from(dateStats.entries()).map(([date, stats]) => ({
|
||||
date,
|
||||
avgGuesses: stats.total / stats.count,
|
||||
completions: stats.count
|
||||
}));
|
||||
|
||||
// Split into pre and post marketing periods
|
||||
const marketingStartDate = '2026-01-08';
|
||||
const preMarketing = allData.filter(d => d.date < marketingStartDate);
|
||||
const postMarketing = allData.filter(d => d.date >= marketingStartDate);
|
||||
|
||||
console.log('\n=== Correlation Analysis ===\n');
|
||||
|
||||
const allCorrelation = calculateCorrelation(allData);
|
||||
console.log(`Overall correlation (avg_guesses vs completions): ${allCorrelation.toFixed(3)}`);
|
||||
|
||||
if (preMarketing.length >= 2) {
|
||||
const preCorrelation = calculateCorrelation(preMarketing);
|
||||
console.log(`Pre-marketing correlation (before ${marketingStartDate}): ${preCorrelation.toFixed(3)} (n=${preMarketing.length} days)`);
|
||||
}
|
||||
|
||||
if (postMarketing.length >= 2) {
|
||||
const postCorrelation = calculateCorrelation(postMarketing);
|
||||
console.log(`Post-marketing correlation (${marketingStartDate} onward): ${postCorrelation.toFixed(3)} (n=${postMarketing.length} days)`);
|
||||
}
|
||||
|
||||
console.log('\nInterpretation:');
|
||||
console.log(' r close to -1: Strong negative correlation (easier verses → more completions)');
|
||||
console.log(' r close to 0: No correlation');
|
||||
console.log(' r close to +1: Strong positive correlation (harder verses → more completions)');
|
||||
|
||||
db.close();
|
||||
Reference in New Issue
Block a user