title: Calculating the relevance of a User based on Specific data
body: <p>I am currently in the process of trying to form an algorithm that will calculate the <code>relevance</code> of a <code>user</code> to another <code>user</code> based on certain bits of data.</p>
  <p>Unfortunately, my Maths skills have deteriorated since leaving school almost a decade ago, and as such, I am very much struggling with this. I have found an algorithm online that pushes 'hot' posts to the top of a newsfeed and figure this is a good place to start. This is the algorithm/calculation I found online (in MySQL):</p>
  <pre><code>LOG10(ABS(activity) + 1) * SIGN(activity) + (UNIX_TIMESTAMP(created_at) / 300000)
  </code></pre>
  <p>What I am hoping to do is adapt the above concept to work with the data and models I have in my own application. Consider this user object (trimmed down):</p>
  <pre><code>{
      &quot;id&quot;: 1
      &quot;first_name&quot;: &quot;Joe&quot;,
      &quot;last_name&quot;: &quot;Bloggs&quot;,
      &quot;counts&quot;: {
          &quot;connections&quot;: 21,
          &quot;mutual_connections&quot;: 16
      },
      &quot;mutual_objects&quot;: [
          {
              &quot;created_at&quot;: &quot;2017-03-26 13:30:47&quot;
          },
          {
              &quot;created_at&quot;: &quot;2017-03-26 14:25:32&quot;
          }
      ],
      &quot;last_seen&quot;: &quot;2017-03-26 14:25:32&quot;,
  }
  </code></pre>
  <p>There are three bits of relevant information above that need to be considered in the algorithm:</p>
  <ul>
  <li><code>mutual_connections</code></li>
  <li><code>mutual_objects</code> but taking into account that older objects should not drive up the relevance as much as newer objects, hence the <code>created_at</code> field.</li>
  <li><code>last_seen</code></li>
  </ul>
  <p>Can anyone suggest a fairly simple (if that's possible) way of doing this?</p>
  <p>This was my idea, but in all honesty, I have no idea what it is doing so I cannot be sure if it is a good solution and I have also missed out <code>last_seen</code> as I could not find a way to add this:</p>
  <pre><code>$mutual_date_sum = 0;
  
  foreach ($user-&gt;mutual_objects as $mutual_object) {
      $mutual_date_sum =+ strtotime($mutual_object-&gt;created_at);
  }
  
  $mutual_date_thing = $mutual_date_sum / (300000 * count($user-&gt;mutual_objects));
  
  $relevance = log10($user-&gt;counts-&gt;mutual_connections + 1) + $mutual_date_thing;
  </code></pre>
  <p><em><strong>Just to be clear, I am not looking to implement some sort of government level AI, 50,000 line algorithm from a mathematical genius. I am merely looking for a relatively simple solution that will do the trick for the moment.</strong></em></p>
  <h2>UPDATE</h2>
  <p>I have had a little play and have managed to build the following test. It seems the <code>mutual_objects</code> very much carries the weight in this particular algorithm as I would expect to see users 4 and 5 higher up the results list given their large number of <code>mutual_connections</code>.</p>
  <p>I don't know if this makes it easier to amend/play with, but this is probably the best I can do. Please help if you have any suggestions :-)</p>
  <pre><code>$users = [
      [
          'id' =&gt; 1,
          'mutual_connections' =&gt; 15,
          'mutual_objects' =&gt; [
              [
                  'created_at' =&gt; '2017-03-26 14:25:32'
              ],
              [
                  'created_at' =&gt; '2017-03-26 14:25:32'
              ],
              [
                  'created_at' =&gt; '2017-02-26 14:25:32'
              ],
              [
                  'created_at' =&gt; '2017-03-15 14:25:32'
              ],
              [
                  'created_at' =&gt; '2017-01-26 14:25:32'
              ],
              [
                  'created_at' =&gt; '2017-03-26 14:25:32'
              ],
              [
                  'created_at' =&gt; '2016-03-26 14:25:32'
              ],
              [
                  'created_at' =&gt; '2017-03-26 14:25:32'
              ]
          ],
          'last_seen' =&gt; '2017-03-01 14:25:32'
      ],
      [
          'id' =&gt; 2,
          'mutual_connections' =&gt; 2,
          'mutual_objects' =&gt; [
              [
                  'created_at' =&gt; '2016-03-26 14:25:32'
              ],
              [
                  'created_at' =&gt; '2015-03-26 14:25:32'
              ],
              [
                  'created_at' =&gt; '2017-02-26 14:25:32'
              ],
              [
                  'created_at' =&gt; '2017-03-15 14:25:32'
              ],
              [
                  'created_at' =&gt; '2017-01-26 14:25:32'
              ],
              [
                  'created_at' =&gt; '2017-03-26 14:25:32'
              ],
              [
                  'created_at' =&gt; '2016-03-26 14:25:32'
              ],
              [
                  'created_at' =&gt; '2016-03-26 14:25:32'
              ],
              [
                  'created_at' =&gt; '2016-03-26 14:25:32'
              ],
              [
                  'created_at' =&gt; '2017-03-15 14:25:32'
              ],
              [
                  'created_at' =&gt; '2017-02-26 14:25:32'
              ],
              [
                  'created_at' =&gt; '2017-03-15 14:25:32'
              ],
              [
                  'created_at' =&gt; '2017-01-26 14:25:32'
              ],
              [
                  'created_at' =&gt; '2017-03-12 14:25:32'
              ],
              [
                  'created_at' =&gt; '2016-03-13 14:25:32'
              ],
              [
                  'created_at' =&gt; '2017-03-17 14:25:32'
              ]
          ],
          'last_seen' =&gt; '2015-03-25 14:25:32'
      ],
      [
          'id' =&gt; 3,
          'mutual_connections' =&gt; 30,
          'mutual_objects' =&gt; [
              [
                  'created_at' =&gt; '2017-02-26 14:25:32'
              ],
              [
                  'created_at' =&gt; '2017-03-26 14:25:32'
              ]
          ],
          'last_seen' =&gt; '2017-03-25 14:25:32'
      ],
      [
          'id' =&gt; 4,
          'mutual_connections' =&gt; 107,
          'mutual_objects' =&gt; [],
          'last_seen' =&gt; '2017-03-26 14:25:32'
      ],
      [
          'id' =&gt; 5,
          'mutual_connections' =&gt; 500,
          'mutual_objects' =&gt; [],
          'last_seen' =&gt; '2017-03-26 20:25:32'
      ],
      [
          'id' =&gt; 6,
          'mutual_connections' =&gt; 5,
          'mutual_objects' =&gt; [
              [
                  'created_at' =&gt; '2017-03-26 20:55:32'
              ],
              [
                  'created_at' =&gt; '2017-03-25 14:25:32'
              ]
          ],
          'last_seen' =&gt; '2017-03-25 14:25:32'
      ]
  ];
  
  $relevance = [];
  
  foreach ($users as $user) {
  
      $mutual_date_sum = 0;
  
      foreach ($user['mutual_objects'] as $bubble) {
          $mutual_date_sum =+ strtotime($bubble['created_at']);
      }
  
      $mutual_date_thing = empty($mutual_date_sum) ? 1 : $mutual_date_sum / (300000 * count($user['mutual_objects']));
  
      $relevance[] = [
          'id' =&gt; $user['id'],
          'relevance' =&gt; log10($user['mutual_connections'] + 1) + $mutual_date_thing
      ];
  }
  
  $relevance = collect($relevance)-&gt;sortByDesc('relevance');
  
  print_r($relevance-&gt;values()-&gt;all());
  </code></pre>
  <p>This prints out:</p>
  <pre><code>Array
  (
      [0] =&gt; Array
          (
              [id] =&gt; 3
              [relevance] =&gt; 2485.7219150272
          )
  
      [1] =&gt; Array
          (
              [id] =&gt; 6
              [relevance] =&gt; 2484.8647045837
          )
  
      [2] =&gt; Array
          (
              [id] =&gt; 1
              [relevance] =&gt; 622.26175831599
          )
  
      [3] =&gt; Array
          (
              [id] =&gt; 2
              [relevance] =&gt; 310.84394042139
          )
  
      [4] =&gt; Array
          (
              [id] =&gt; 5
              [relevance] =&gt; 3.6998377258672
          )
  
      [5] =&gt; Array
          (
              [id] =&gt; 4
              [relevance] =&gt; 3.0334237554869
          )
  
  )
  </code></pre>