Step 01
Synthetic population and network
Builds 1,000 consumers in 5 communities. Friendships form more often inside a community than across communities, so the tie probabilities control how connected the groups are.
function generateNetwork(settings) {
const rng = seededRandom(settings.seed)
const people = []
for (let id = 0; id < settings.n; id++) {
const community = Math.floor(rng() * settings.communities)
const trait = rng() < 0.5 ? 1 : 0 // hidden shared trait
people.push({
id,
community,
trait,
preference: clamp(normal(rng, 0, 1) + 0.6 * trait),
priceSensitivity: clamp(normal(rng, 0, 1) - 0.4 * trait),
influence: Math.exp(normal(rng, 0, 0.6)), // a few very influential people
})
}
const friends = people.map(() => [])
for (let a = 0; a < people.length; a++) {
for (let b = a + 1; b < people.length; b++) {
const same = people[a].community === people[b].community
const chance = same ? settings.p_in : settings.p_out
if (rng() < chance) {
friends[a].push(b)
friends[b].push(a) // mutual friendship
}
}
}
return { people, friends }
}Assumptions
- Preference, price sensitivity and influence are drawn once per consumer and never change.
- A hidden shared trait nudges both preference and price sensitivity. It is what creates confounding later.
- Friendships are mutual. Within a community the tie chance is p_in; across communities it is p_out.