UNPKG

11.4 kBMarkdownView Raw
1
2# Good Enough Recommendations (GER)
3<img src="./assets/ger300x200.png" align="right" alt="GER logo" />
4
5Providing good recommendations can get greater user engagement and provide an opportunity to add value that would otherwise not exist. The main reason why many applications don't provide recommendations is the difficulty in either implementing a custom engine or using an existing engine.
6
7Good Enough Recommendations (**GER**) is a recommendation engine that is scalable, easily usable and easy to integrate. GER's goal is to generate **good enough** recommendations for your application or product, so that you can provide value quickly and painlessly.
8
9##Quick Start Guide
10
11**Note: functions from GER return a promises**
12
13Install `ger` and `coffee-script` with `npm`:
14
15```bash
16npm install ger
17```
18
19In your javascript code, first require `ger`:
20
21```javascript
22var g = require('ger')
23```
24
25Initialize an in memory Event Store Manager (ESM) and create a Good Enough Recommender (GER):
26
27```javascript
28var esm = new g.MemESM()
29var ger = new g.GER(esm);
30```
31
32The next step is to initialize a namespace, e.g. `movies`. *A namespace is a bucket of events that will not interfere with other buckets*.
33
34```javascript
35ger.initialize_namespace('movies')
36```
37
38Next add events to the namespace. *An event is a triple (person, action, thing)* e.g. `bob` `likes` `xmen`.
39
40```javascript
41ger.events([{
42 namespace: 'movies',
43 person: 'bob',
44 action: 'likes',
45 thing: 'xmen',
46 expires_at: '2020-06-06'
47}])
48```
49
50An event is used by GER in two ways:
51
521. to compare two people by looking at their history, e.g. `bob` and `alice` `like` similar movies, so `bob` and `alice` are similar
532. provide recommendations from a persons history, e.g. `bob` `liked` a movie `alice` might like, so we can recommend that movie to `alice`
54
55There are two caveats with using events as recommendations:
56
571. an action may be negative, e.g. `bob` `dislikes` `xmen` which is **not** a recommendation
582. a recommendation ALWAYS expires, e.g. `bob` `likes` `xmen` occurred 15 years ago, so maybe he wouldn't recommend `xmen` now
59
60So GER has the rule: *If an event has an expiry date it is treated as a recommendation until it expires*.
61
62GER can generate recommendations for a person, e.g. *what would alice like?*
63
64```
65ger.recommendations_for_person('movies', 'alice', {actions: {likes: 1}
66```
67
68and recommendations for a thing, e.g. *what would a person who likes xmen like?*
69
70```
71ger.recommendations_for_thing('movies', 'xmen', {actions: {likes: 1}})
72```
73
74
75Lets put it all together:
76
77
78```javascript
79var g = require('ger')
80var esm = new g.MemESM()
81var ger = new g.GER(esm);
82
83ger.initialize_namespace('movies')
84.then( function() {
85 return ger.events([
86 {
87 namespace: 'movies',
88 person: 'bob',
89 action: 'likes',
90 thing: 'xmen',
91 expires_at: '2020-06-06'
92 },
93 {
94 namespace: 'movies',
95 person: 'bob',
96 action: 'likes',
97 thing: 'avengers',
98 expires_at: '2020-06-06'
99 },
100 {
101 namespace: 'movies',
102 person: 'alice',
103 action: 'likes',
104 thing: 'xmen',
105 expires_at: '2020-06-06'
106 },
107 ])
108})
109.then( function() {
110 // What things might alice like?
111 return ger.recommendations_for_person('movies', 'alice', {actions: {likes: 1}})
112})
113.then( function(recommendations) {
114 console.log("\nRecommendations For 'alice'")
115 console.log(JSON.stringify(recommendations,null,2))
116})
117.then( function() {
118 // What things are similar to xmen?
119 return ger.recommendations_for_thing('movies', 'xmen', {actions: {likes: 1}})
120})
121.then( function(recommendations) {
122 console.log("\nRecommendations Like 'xmen'")
123 console.log(JSON.stringify(recommendations,null,2))
124})
125```
126
127This will output:
128
129```json
130Recommendations For 'alice'
131{
132 "recommendations": [
133 {
134 "thing": "xmen",
135 "weight": 1.5,
136 "last_actioned_at": "2015-07-09T14:33:37+01:00",
137 "last_expires_at": "2020-06-06T01:00:00+01:00",
138 "people": [
139 "alice",
140 "bob"
141 ]
142 },
143 {
144 "thing": "avengers",
145 "weight": 0.5,
146 "last_actioned_at": "2015-07-09T14:33:37+01:00",
147 "last_expires_at": "2020-06-06T01:00:00+01:00",
148 "people": [
149 "bob"
150 ]
151 }
152 ],
153 "neighbourhood": {
154 "bob": 0.5,
155 "alice": 1
156 },
157 "confidence": 0.0007147696406599602
158}
159
160Recommendations Like 'xmen'
161{
162 "recommendations": [
163 {
164 "thing": "avengers",
165 "weight": 0.5,
166 "last_actioned_at": "2015-07-09T14:33:37+01:00",
167 "last_expires_at": "2020-06-06T01:00:00+01:00",
168 "people": [
169 "bob"
170 ]
171 }
172 ],
173 "neighbourhood": {
174 "avengers": 0.5
175 },
176 "confidence": 0.0007923350883032776
177}
178```
179
180In the recommendations for `alice`, `xmen` is the highest rated recommendations because alice has `liked` it before, so she probably likes it now. You can filter out recommendations that have been actioned before using the `filter_previous_actions` configuration key described below.
181
182*This code for this example is in the `./examples/basic_recommendations_exmaple.js` script*
183
184## Configuration
185
186GER lets you set some values to customize recommendations generation using a `configuration`. Below is a description of all the configurable keys and their defaults:
187
188| Key | Default
189|--- |---
190| `actions` | `{}`
191| `minimum_history_required` | `0`
192| `neighbourhood_search_size` | `100`
193| `similarity_search_size` | `100`
194| `neighbourhood_size` | `25`
195| `recommendations_per_neighbour` | `10`
196| `filter_previous_actions` | `[]`
197| `event_decay_rate` | `1`
198| `time_until_expiry` | `0`
199| `current_datetime` | `now()`
200
201
2022. `actions` is an object where the keys are actions names, and the values are action weights that represent the importance of the action
2033. `minimum_history_required` is the minimum amount of events a person has to have to even bother generating recommendations. It is good to stop low confidence recommendations being generated.
2044. `neighbourhood_search_size` the amount of events in the past that are used to search for the neighborhood. This value has the highest impact on performance but past a certain point has no (or negative) impact on recommendations.
2055. `similarity_search_size` is the amount of events in the history used to calculate the similarity between things or people.
2065. `neighbourhood_size` the number of similar people (or things) that are searched for. This value has a significant performance impact, and increasing it past a point will also gain diminishing returns.
2076. `recommendations_per_neighbour` the number of recommendations each similar person can offer. This is to stop a situation where a single highly similar person provides all recommendations.
2087. `filter_previous_actions` it removes recommendations that the person being recommended already has in their history. For example, if a person has already liked `xmen`, then if `filter_previous_actions` is `["liked"]` they will not be recommended `xmen`.
2098. `event_decay_rate` the rate at which event weight will decay over time, `weight * event_decay_rate ^ (- days since event)`
2109. `time_until_expiry` is the number (in seconds) from `now()` where recommendations that expire will be removed. For example, recommendations on a website might be valid for minutes, where in a email you might recommendations valid for days.
21110. `current_datetime` defines a "simulated" current time that will not use any events that are performed after `current_datetime` when generating recommendations.
212
213For example, generating recommendations with a configuration from GER:
214
215```javascript
216ger.recommendations_for_person('movies', 'alice', {
217 "actions": {
218 "like": 1,
219 "watch": 5
220 },
221 "minimum_history_required": 5,
222 "similarity_search_size": 50,
223 "neighbourhood_size": 20,
224 "recommendations_per_neighbour": 10,
225 "filter_previous_actions": ["watch"],
226 "event_decay_rate": 1.05,
227 "time_until_expiry": 180
228})
229```
230
231## Technology
232
233GER is implemented in Coffee-Script on top of Node.js ([here](http://www.maori.geek.nz/post/why_should_you_use_coffeescript_instead_of_javascript) are my reasons for using Coffee-Script). The core logic is implemented in an abstractions called an Event Store Manager (**ESM**), this is the persistency and many calculations occur.
234
235Currently there is an in memory ESM and a PostgreSQL ESM. There is also a RethinkDB ESM in the works being implemented by the awesome [linuxlich](https://github.com/thelinuxlich/ger).
236
237## Event Store Manager
238
239If you ask
240
241> Why is GER not available on X?
242
243Where X is some database or store (e.g. Redis, Mongo, Cassandra ...). The way to make it available on these systems is to implement your own ESM for it.
244
245The API for an ESM is:
246
247*Initialization*:
248
2491. `esm = new ESM(options)` where options is used to setup connections and such.
2502. `initialize(namespace)` will create a `namespace` for events.
2513. `destroy(namespace)` will destroy all resources for ESM in namespace
2524. `exists(namespace)` will check if the namespace exists
2535. `list_namespaces` returns a list of namespaces
254*Events*:
255
2561. `add_events`
2572. `add_event`
2583. `find_events`
2594. `delete_events`
260
261*Thing Recommendations*:
262
2631. `thing_neighbourhood`
2641. `calculate_similarities_from_thing`
265
266*Person Recommendations*
267
2681. `person_neighbourhood`
2691. `calculate_similarities_from_person`
2701. `filter_things_by_previous_actions`
2711. `recent_recommendations_by_people`
272
273*Compacting*:
274
2751. pre_compact
2762. compact_people
2773. compact_things
2784. post_compact
279
280## Additional Reading
281
282Posts about (or related to) GER:
283
2841. Demo Movie Recommendations Site: [Yeah, Nah](http://yeahnah.maori.geek.nz/)
2851. Overall description and motivation of GER: [Good Enough Recommendations with GER](http://maori.geek.nz/post/good_enough_recomendations_with_ger)
2862. How GER works [GER's Anatomy: How to Generate Good Enough Recommendations](http://www.maori.geek.nz/post/how_ger_generates_recommendations_the_anatomy_of_a_recommendations_engine)
2872. Testing frameworks being used to test GER: [Testing Javascript with Mocha, Chai, and Sinon](http://www.maori.geek.nz/post/introduction_to_testing_node_js_with_mocha_chai_and_sinon)
2884. [Postgres Upsert (Update or Insert) in GER using Knex.js](http://www.maori.geek.nz/post/postgres_upsert_update_or_insert_in_ger_using_knex_js)
2895. [List of Recommender Systems](https://github.com/grahamjenson/list_of_recommender_systems)
290
291## Changelog
292
2932015-07-09 - updated readme and fixed basicmem ESM bug.
294
2952015-02-01 - fixed bug with set_namespace and added tests
296
2972015-01-30 - added a few helper methods for namespaces, and removed caches to be truly stateless.
298
2992014-12-30 - added find and delete events methods.
300
3012014-12-22 - added exists to check if namespace is initilaized. also changed some indexes in rethinkdb, and changed some semantics around initialize
302
3032014-12-22 - Added Rethink DB Event Store Manager.
304
3052014-12-9 - Added more explanation to the returned recommendations so they can be reasoned about externally
306
3072014-12-4 - Changed ESM API to be more understandable and also updated README
308
3092014-11-27 - Started returning the last actioned at date with recommendations
310
3112014-11-25 - Added better way of selecting recommendations from similar people.
312
3132014-11-12 - Added better heuristic to select related people. Meaning less related people need to be selected to find good values