-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFadingConstant.py
50 lines (40 loc) · 1.95 KB
/
FadingConstant.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from pymongo import MongoClient
from ShowAd import overallExpectancy
myClient = MongoClient()
RawData = myClient.AdNetwork.RawData
AdsSummary = myClient.AdNetwork.AdsSummary
UserSummary = myClient.AdNetwork.UserSummary
def fadingCCalculator():
fadingConstant = {}
overall = overallExpectancy()
users = UserSummary.distinct("_id", {"acts": {"$exists": True}})
# Find users with at least one action.
for action in overall.keys():
shownAfter = 0
convertedAfter = 0
for user in users:
# Find all the ads, this user has acted on
actionedAds = RawData.distinct("adId",
{
"userId": user,
"converted": True,
"adAction": action
})
for ad in actionedAds:
# Find the time of his/her first action on the ad
firstAction = RawData.find_one({"userId": user, "adId": ad, "converted": True}, sort=[("timestamp", 1)])
# List all the events after that action (for the same user and ad)
seriesAfter = RawData.find({"userId": user, "adId": ad, "timestamp": {"$gt": firstAction["timestamp"]}})
for event in seriesAfter:
# Count actions and impressions for the same ad and the same user, after his/her first action on that ad
shownAfter += 1
convertedAfter += 1 if event["converted"] else 0
# Calculate CR
print(convertedAfter, shownAfter, overall[action], action)
fadingConstant[action] = convertedAfter / shownAfter / overall[action]
print(fadingConstant)
# It gave me:
# {'install': 0.0, 'click': 0.36053731463248, 'completeView': 0.9749502626134012}
return fadingConstant
if __name__ == "__main__":
fadingCCalculator()