Skip to content

Commit

Permalink
Added extra period so we reach the requested horizon t, instead of sh…
Browse files Browse the repository at this point in the history
…ifting age and recency vectors
  • Loading branch information
DaniGate committed Mar 27, 2018
1 parent 9d72189 commit cbd445a
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions lifetimes/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,18 +278,19 @@ def calculate_alive_path(model, transactions, datetime_col, t, freq='D'):
purchase_history = (customer_history.resample(freq).sum().replace(np.nan, 0)
['transactions'].values)

extra_columns = t - len(purchase_history)
extra_columns = t + 1 - len(purchase_history)
customer_history = pd.DataFrame(np.append(purchase_history, [0] * extra_columns), columns=['transactions'])
# add T column
customer_history['T'] = np.arange(start=1,stop=customer_history.shape[0]+1)
customer_history['T'] = np.arange(customer_history.shape[0])
# add cumulative transactions column
customer_history['transactions'] = customer_history['transactions'].apply(lambda t: int(t>0))
customer_history['frequency'] = customer_history['transactions'].cumsum() - 1 # first purchase is ignored
# Add t_x column
customer_history['recency'] = customer_history.apply(lambda row: row['T']-1 if row['transactions'] != 0 else np.nan, axis=1)
customer_history['recency'] = customer_history.apply(lambda row: row['T'] if row['transactions'] != 0 else np.nan, axis=1)
customer_history['recency'] = customer_history['recency'].fillna(method='ffill').fillna(0)
return customer_history.apply(lambda row: model.conditional_probability_alive(row['frequency'], row['recency'], row['T']), axis=1)

return customer_history.apply(
lambda row: model.conditional_probability_alive(row['frequency'], row['recency'], row['T']),
axis=1)

def _fit(minimizing_function, minimizing_function_args, iterative_fitting,
initial_params, params_size, disp, tol=1e-8, fit_method='Nelder-Mead',
Expand Down

0 comments on commit cbd445a

Please sign in to comment.