Skip to content

Commit

Permalink
add median computation
Browse files Browse the repository at this point in the history
  • Loading branch information
bg117 committed Mar 4, 2023
1 parent a2f6ebd commit c61f71b
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions FrequencyDistributionTable/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ private void UpdateNonSetProps()
{
RaisePropertyChangedEvent(nameof(TotalFrequency));
RaisePropertyChangedEvent(nameof(Mean));
RaisePropertyChangedEvent(nameof(Median));
}

public int ClassInterval
Expand Down Expand Up @@ -66,6 +67,24 @@ public bool CanChangeClassInterval
// (sum of (class midpoint * frequency)) / total frequency
TotalFrequency != 0 ? Classes.Select(x => x.Class.Midpoint * x.Class.Frequency).Sum() / TotalFrequency : 0;

public decimal Median
{
get
{
if (Classes.Count < 2)
return 0;

var halfOfTotalFreq = TotalFrequency / 2m; // half of total frequency
var medianClass = Classes.FirstOrDefault(x => x.CumulativeFrequencyLess >= halfOfTotalFreq); // first class where cumulative frequency is greater than half of total frequency
var belowLessCumulFreq = Classes[Classes.IndexOf(medianClass) - 1].CumulativeFrequencyLess; // cumulative frequency of class below median class
var medianClassFreq = medianClass.Class.Frequency; // frequency of median class
var medianClassLb = medianClass.Class.LowerBoundary; // lower bound of median class

// medianClassLb + ((halfOfTotalFreq - belowLessCumulFreq) / medianClassFreq) * class interval
return medianClassLb + ((halfOfTotalFreq - belowLessCumulFreq) / medianClassFreq) * ClassInterval;
}
}

public ICommand AddClassCommand => new CommandBase(x => AddClass());
public ICommand RemoveClassCommand => new CommandBase(x => RemoveClass(x as int? ?? -1));
public ICommand ClearClassesCommand => new CommandBase(x => ClearClasses());
Expand Down

0 comments on commit c61f71b

Please sign in to comment.