Skip to content

Commit

Permalink
[#12065] Student viewing rubric results: average doesn't match (#12066)
Browse files Browse the repository at this point in the history
* Fix rubric stats calculation

* Fix tests
  • Loading branch information
zhaojj2209 authored Feb 10, 2023
1 parent 25f6327 commit 50fff21
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,19 @@ export class RubricQuestionStatisticsCalculation
for (const recipient of Object.keys(this.perRecipientStatsMap)) {
const perRecipientStats: PerRecipientStats = this.perRecipientStatsMap[recipient];

// Answers sum = number of answers in each column
perRecipientStats.answersSum = this.calculateAnswersSum(perRecipientStats.answers);
perRecipientStats.percentages = this.calculatePercentages(perRecipientStats.answers);
perRecipientStats.percentagesAverage =
this.calculatePercentagesAverage(perRecipientStats.percentages,
this.calculateNumResponses(perRecipientStats.answersSum));
perRecipientStats.percentagesAverage = this.calculatePercentagesAverage(perRecipientStats.answersSum);
perRecipientStats.subQuestionWeightAverage =
this.calculateSubQuestionWeightAverage(perRecipientStats.answers);
perRecipientStats.weightsAverage = this.calculateWeightsAverage(this.weights);
perRecipientStats.overallWeightedSum = this.calculateWeightedSum(perRecipientStats.percentagesAverage,
perRecipientStats.answersSum, perRecipientStats.weightsAverage);
// Overall weighted sum = sum of total chosen weight for all sub questions
perRecipientStats.overallWeightedSum =
+(perRecipientStats.subQuestionTotalChosenWeight.reduce((a, b) => a + b)).toFixed(2);
// Overall weighted average = overall weighted sum / total number of responses
perRecipientStats.overallWeightAverage = +(perRecipientStats.overallWeightedSum
/ (this.calculateNumResponses(perRecipientStats.answersSum))).toFixed(2);
/ this.calculateNumResponses(perRecipientStats.answersSum)).toFixed(2);
}
}

Expand Down Expand Up @@ -195,32 +196,19 @@ export class RubricQuestionStatisticsCalculation
}

// Calculate percentage average for each column
private calculatePercentagesAverage(percentages: number[][], numAnswers: number): number[] {
// Calculate sum of percentages for each column
const sums: number[] = this.calculateAnswersSum(percentages);
private calculatePercentagesAverage(answersSum: number[]): number[] {
// Calculate total number of responses
const numResponses = this.calculateNumResponses(answersSum);
const averages: number[] = [];
// Divide each percentage by the number of answers
for (let i: number = 0; i < sums.length; i += 1) {
averages[i] = numAnswers === 0 ? 0 : +(sums[i] / numAnswers).toFixed(2);
// Divide each column sum by total number of responses, then convert to percentage
for (let i: number = 0; i < answersSum.length; i += 1) {
averages[i] = numResponses === 0 ? 0 : +(answersSum[i] * 100 / numResponses).toFixed(2);
}
return averages;
}

// Calculate weighted sum of responses
private calculateWeightedSum(percentages: number[], answers: number[], weights: number[]): number {
let sum: number = 0;
for (let i: number = 0; i < answers.length; i += 1) {
sum += (percentages[i] / 100) * answers[i] * weights[i];
}
return +sum.toFixed(2);
}

// Calculate total number of responses
private calculateNumResponses(answersSum: number[]): number {
let num: number = 0;
for (let i = 0; i < answersSum.length; i += 1) {
num += answersSum[i];
}
return num;
return answersSum.reduce((a, b) => a + b);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@
"percentages": [
[50, 50], [50, 50], [100, 0]
],
"percentagesAverage": [33.33, 16.67],
"percentagesAverage": [66.67, 33.33],
"recipientEmail": "alice@gmail.com",
"recipientName": "Alice",
"recipientTeam": "Team 1",
"subQuestionTotalChosenWeight": [1, 1, 0.8],
"subQuestionWeightAverage": [0.5, 0.5, 0.4],
"weightsAverage": [0.23, 0.77],
"overallWeightedSum": 0.56,
"overallWeightAverage": 0.09
"overallWeightedSum": 2.8,
"overallWeightAverage": 0.47
},
"Bob": {
"answers": [
Expand All @@ -84,15 +84,15 @@
"percentages": [
[100, 0], [50, 50], [100, 0]
],
"percentagesAverage": [41.67, 8.33],
"percentagesAverage": [83.33, 16.67],
"recipientEmail": "bob@gmail.com",
"recipientName": "Bob",
"recipientTeam": "Team 2",
"subQuestionTotalChosenWeight": [0.4, 1, 0.8],
"subQuestionWeightAverage": [0.2, 0.5, 0.4],
"weightsAverage": [0.23, 0.77],
"overallWeightedSum": 0.54,
"overallWeightAverage": 0.09
"overallWeightedSum": 2.2,
"overallWeightAverage": 0.37
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -392,8 +392,8 @@ Team,Recipient Name,Recipient Email,Sub Question,Yes,No,Total,Average
Per Recipient Statistics (Overall)
Team,Recipient Name,Recipient Email,Yes,No,Total,Average
\\"Team 1.1</td></div>'\\"\\"\\",\\"student1 In Course1</td></div>'\\"\\"\\",student1InCourse1@gmail.tmt,50% (1) [1.25],50% (1) [-1.7],-0.22,-0.11
\\"Team 1.1</td></div>'\\"\\"\\",student2 In Course1,student2InCourse1@gmail.tmt,50% (1) [1.25],50% (1) [-1.7],-0.22,-0.11
\\"Team 1.1</td></div>'\\"\\"\\",\\"student1 In Course1</td></div>'\\"\\"\\",student1InCourse1@gmail.tmt,50% (1) [1.25],50% (1) [-1.7],-0.45,-0.23
\\"Team 1.1</td></div>'\\"\\"\\",student2 In Course1,student2InCourse1@gmail.tmt,50% (1) [1.25],50% (1) [-1.7],-0.45,-0.23
\\"Team 1.1</td></div>'\\"\\"\\",student3 In Course1,student3InCourse1@gmail.tmt,100% (2) [1.25],0% (0) [-1.7],2.5,1.25
\\"Team 1.1</td></div>'\\"\\"\\",student4 In Course1,student4InCourse1@gmail.tmt,100% (1) [1.25],0% (0) [-1.7],1.25,1.25
Expand Down

0 comments on commit 50fff21

Please sign in to comment.