Search code examples
c#algorithmfrequencyfrequency-analysis

Algorithm to calculate frequency and recency of an entity?


I have a list of entities opened by various users. I keep track of each access of any entity by storing access dates and times as the following:

public class Entity
{
  public int Id { get; set; }
  public virtual ICollection<AccessInfo> Accesses { get; set; } 
    = new HashSet<AccessInfo>();
}

public class AccessInfo
{
  public int Id { get; set; }
  public AccessInfoType Type { get; set; }
  public User User { get; set; }
  public DateTime DateTime { get; set; }
}

public enum AccessInfoType
{
  Create,
  Read,
  Update,
  Delete,
}

Now I'm trying to make an algorithm that filters the most wanted contacts based on both factors: recency and frequency.

I want contacts that were accessed 5 times yesterday to be prioritized over a contact that was accessed 30 times a week ago. But in the other hand, a user that was only accessed one time today is less important.

Is there an official name for this? I'm sure people have worked on a frequency calculation like this one before, and I'd like to read about this before I spend some time coding.

I thought about calculating the sum of the access dates in recent month and sort accordingly but I'm still not sure it's the right way, I'd love to learn from the experts.

return Entities
  .OrderBy(c =>
    c.Accesses
      .Where(a => a.Employee.UserName == UserName)
      .Where(a => a.DateTime > lastMonth)
      .Select(a => a.DateTime.Ticks)
      .Sum());

Solution

  • Exponential decay is what you're looking for. See this link:

    http://www.evanmiller.org/rank-hotness-with-newtons-law-of-cooling.html