-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclassical.go
85 lines (70 loc) · 3.03 KB
/
classical.go
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package attribution
import (
"math/big"
)
// GetFirstTouchpointValue returns summed value of all contributions where the given touchpoints happened to be
// first in its list of contributors.
func GetFirstTouchpointValue(touchpoint Touchpoint, allContributions []Contribution) big.Float {
firstTouchpointValue := new(big.Float)
for _, contribution := range allContributions {
length := len(contribution.Touchpoints)
if length > 0 && touchpoint == contribution.Touchpoints[0] {
firstTouchpointValue.Add(firstTouchpointValue, &contribution.Value)
}
}
return *firstTouchpointValue
}
// GetLastTouchpointValue returns summed value of all contributions where the given touchpoints happened to be
// last in its list of contributors.
func GetLastTouchpointValue(touchpoint Touchpoint, allContributions []Contribution) big.Float {
lastTouchpointValue := new(big.Float)
for _, contribution := range allContributions {
length := len(contribution.Touchpoints)
if length > 0 && touchpoint == contribution.Touchpoints[length-1] {
lastTouchpointValue.Add(lastTouchpointValue, &contribution.Value)
}
}
return *lastTouchpointValue
}
// GetLinearValue returns the linear value (ignoring repetition) of a given touchpoint summed over all contributions.
// The linear value without repititions for Contribution objecs can best be calculated by first transformating them
// to ContributionSet objects with the Set() method and then applying this function.
func GetLinearValue(touchpoint Touchpoint, allContributions []ContributionSet) big.Float {
linearValue := new(big.Float)
for _, contribution := range allContributions {
// check if touchpoint was part of this contribution
for candidate, _ := range contribution.Touchpoints {
if touchpoint == candidate {
numberTouchpoints := float64(len(contribution.Touchpoints))
addedValue := contribution.Value
// distribute value equally among all contributors
addedValue.Quo(&addedValue, new(big.Float).SetFloat64(numberTouchpoints))
linearValue.Add(linearValue, &addedValue)
break
}
}
}
return *linearValue
}
// GetRepeatedLinearValue returns the linear value (with repition) of a given touchpoint summed over all contributions.
func GetRepeatedLinearValue(touchpoint Touchpoint, allContributions []Contribution) big.Float {
linearValue := new(big.Float)
for _, contribution := range allContributions {
touchpointContributions := 0
// check if touchpoint was part of this contribution
for _, candidate := range contribution.Touchpoints {
if touchpoint == candidate {
touchpointContributions++
}
}
if touchpointContributions > 0 {
numberTouchpoints := float64(len(contribution.Touchpoints))
addedValue := contribution.Value
// distribute value equally among all contributors according to their number of contributions
addedValue.Mul(&addedValue, new(big.Float).SetFloat64(float64(touchpointContributions)))
addedValue.Quo(&addedValue, new(big.Float).SetFloat64(numberTouchpoints))
linearValue.Add(linearValue, &addedValue)
}
}
return *linearValue
}