File size: 33,413 Bytes
5fc7138
 
 
 
 
 
 
 
2130e8d
5fc7138
 
 
 
 
2130e8d
 
5fc7138
 
 
 
2130e8d
 
5fc7138
 
2130e8d
 
 
 
 
 
 
 
 
 
 
 
5fc7138
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2130e8d
 
 
 
 
 
 
 
 
 
 
 
 
5fc7138
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2130e8d
 
 
 
 
 
 
 
 
 
 
5fc7138
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2130e8d
 
 
 
 
 
 
 
 
5fc7138
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2130e8d
 
 
 
 
 
 
 
 
5fc7138
 
 
 
 
 
 
 
 
 
 
 
 
 
2130e8d
 
 
 
 
 
5fc7138
 
 
 
 
 
 
 
 
2130e8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5fc7138
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2130e8d
5fc7138
2130e8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cae1c9c
2130e8d
 
 
 
 
 
 
 
 
 
 
 
cae1c9c
2130e8d
5fc7138
 
2130e8d
5fc7138
 
 
 
 
 
2130e8d
 
 
 
 
 
 
 
 
 
 
 
 
cae1c9c
 
2130e8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cae1c9c
 
 
 
 
 
 
 
 
 
 
 
5fc7138
 
2130e8d
 
 
 
 
 
 
 
 
5fc7138
 
 
 
2130e8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cae1c9c
2130e8d
 
 
 
 
 
 
 
 
 
cae1c9c
2130e8d
 
 
5fc7138
2130e8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cae1c9c
 
2130e8d
 
 
 
 
 
cae1c9c
 
 
 
 
 
 
 
 
 
 
 
 
2130e8d
 
5fc7138
2130e8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5fc7138
 
 
 
 
 
 
 
 
2130e8d
5fc7138
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2130e8d
5fc7138
 
 
 
 
2130e8d
5fc7138
 
 
2130e8d
5fc7138
 
2130e8d
 
5fc7138
 
2130e8d
 
 
5fc7138
 
2130e8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
import gradio as gr
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import plotly
import plotly.graph_objs as go
from plotly.subplots import make_subplots
import plotly.subplots as sp
from sklearn.preprocessing import StandardScaler
from causalml.inference.meta import BaseTClassifier
from sklearn.ensemble import RandomForestClassifier
from data_generator import generate_synthetic_data
from rct_analyzer import analyze_rct_results
from sklearn.model_selection import train_test_split
from rct_simulator import run_rct_simulation, electronics_products, calculate_purchase_probability

# Global variables to store generated data and RCT results
generated_data = None
rct_results = None
uplift_models = {}
last_used_features = None

def perform_eda(discount_level):
    """
    Perform Exploratory Data Analysis on the RCT results for a specific discount level.
    
    This function analyzes the impact of newsletter subscription and preferred payment method
    on purchase behavior and profitability for the selected discount level.
    
    Args:
        discount_level (str): The discount level to analyze ('5% discount', '10% discount', or '15% discount')
    
    Returns:
        tuple: Contains EDA results, including newsletter and payment method analysis dataframes and plots
    """
    global rct_results, generated_data
    if rct_results is None or generated_data is None:
        return "Please generate customer data and run RCT simulation first.", None, None, None, None

    transactions_df, variant_assignments_df = rct_results

    # Merge data
    merged_df = pd.merge(generated_data, variant_assignments_df, on='customer_id', how='inner')
    merged_df = pd.merge(merged_df, transactions_df, on=['customer_id', 'variant'], how='left')
    merged_df['purchase'] = merged_df['purchase'].fillna(0)
    merged_df['profit'] = merged_df['profit'].fillna(0)

    # Filter for control and selected discount level
    filtered_df = merged_df[merged_df['variant'].isin(['Control', discount_level])]

    # Analyze newsletter_subscription
    newsletter_results = analyze_feature(filtered_df, 'newsletter_subscription')

    # Analyze preferred_payment_method
    payment_results = analyze_feature(filtered_df, 'preferred_payment_method')

    # Create plots
    newsletter_fig = create_bar_plot(newsletter_results, 'newsletter_subscription', discount_level)
    payment_fig = create_bar_plot(payment_results, 'preferred_payment_method', discount_level)

    return (f"EDA completed for {discount_level}",
            newsletter_results, payment_results, newsletter_fig, payment_fig)

def analyze_feature(df, feature):
    """
    Analyze the impact of a specific feature on purchase behavior and profitability.
    
    This function calculates incremental purchases and profits for different values of the feature,
    comparing the treatment group (discount) to the control group.
    
    Args:
        df (pandas.DataFrame): The dataset containing customer and transaction data
        feature (str): The feature to analyze (e.g., 'newsletter_subscription' or 'preferred_payment_method')
    
    Returns:
        pandas.DataFrame: Results of the feature analysis, including incremental purchases and profits
    """
    control_df = df[df['variant'] == 'Control']
    treatment_df = df[df['variant'] != 'Control']

    control_stats = control_df.groupby(feature).agg({
        'purchase': 'sum',
        'profit': 'sum'
    }).reset_index()

    treatment_stats = treatment_df.groupby(feature).agg({
        'purchase': 'sum',
        'profit': 'sum'
    }).reset_index()

    results = pd.merge(control_stats, treatment_stats, on=feature, suffixes=('_control', '_treatment'))
    results['incremental_purchases'] = results['purchase_treatment'] - results['purchase_control']
    results['incremental_profit'] = results['profit_treatment'] - results['profit_control']

    return results

def create_bar_plot(data, feature, discount_level):
    """
    Create a bar plot to visualize the impact of a feature on incremental purchases and profits.
    
    Args:
        data (pandas.DataFrame): The data to plot
        feature (str): The feature being analyzed
        discount_level (str): The discount level being analyzed
    
    Returns:
        matplotlib.figure.Figure: The created bar plot
    """
    fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5))
    
    data[feature] = data[feature].astype(str)  # Ensure the feature is treated as a string
    
    ax1.bar(data[feature], data['incremental_purchases'])
    ax1.set_title(f'Incremental Purchases by {feature}\n({discount_level})', fontsize=14)
    ax1.set_xlabel(feature)
    ax1.set_ylabel('Incremental Purchases')
    ax1.tick_params(axis='x', rotation=45)

    ax2.bar(data[feature], data['incremental_profit'])
    ax2.set_title(f'Incremental Profit by {feature}\n({discount_level})', fontsize=14)
    ax2.set_xlabel(feature)
    ax2.set_ylabel('Incremental Profit')
    ax2.tick_params(axis='x', rotation=45)

    plt.tight_layout()
    return fig

def generate_and_display_data(num_customers):
    """
    Generate synthetic customer data and display samples of basic and extra customer information.
    
    Args:
        num_customers (int): The number of customer records to generate
    
    Returns:
        tuple: Contains sample dataframes of basic and extra customer information, and generation info
    """
    global generated_data
    generated_data = generate_synthetic_data(num_customers=num_customers)
    
    df_basic_info = generated_data[['customer_id', 'name', 'email', 'age', 'gender', 'region', 'city',
                        'registration_date', 'phone_number', 'preferred_language',
                        'newsletter_subscription', 'preferred_payment_method']]
    
    df_extra_info = generated_data[['customer_id', 'loyalty_level', 'main_browsing_device', 
                        'product_categories_of_interest', 'average_order_value', 
                        'total_orders', 'last_order_date']]
    
    sample_basic = df_basic_info.sample(n=min(10, len(df_basic_info)))
    sample_extra = df_extra_info.sample(n=min(10, len(df_extra_info)))
    
    return (sample_basic, sample_extra,
            f"Generated {num_customers} records. Displaying samples of 10 rows for each dataset.")

def run_and_display_rct(experiment_duration):
    """
    Run a Randomized Control Trial (RCT) simulation and display sample results.
    
    Args:
        experiment_duration (int): The duration of the experiment in days
    
    Returns:
        tuple: Contains sample dataframes of variant assignments and transactions, and simulation info
    """
    global generated_data, rct_results
    if generated_data is None:
        return None, None, "Please generate customer data first."
    
    transactions_df, variant_assignments_df = run_rct_simulation(generated_data, experiment_duration)
    rct_results = (transactions_df, variant_assignments_df)  # Store both DataFrames as a tuple
    
    sample_assignments = variant_assignments_df.sample(n=min(10, len(variant_assignments_df)))
    sample_transactions = transactions_df.sample(n=min(10, len(transactions_df)))
    
    return (sample_assignments, sample_transactions, 
            f"Ran RCT simulation for {experiment_duration} days. Displaying samples of 10 rows for each dataset.")

def analyze_and_display_results():
    """
    Analyze the results of the RCT simulation and display overall metrics, variant metrics, and visualizations.
    
    Returns:
        tuple: Contains overall metrics dataframe, variant metrics dataframe, visualization, and analysis info
    """
    global rct_results
    if rct_results is None:
        return None, None, None, "Please run the RCT simulation first."
    
    transactions_df, variant_assignments_df = rct_results
    overall_df, variant_df, fig = analyze_rct_results(transactions_df, variant_assignments_df)
    return overall_df, variant_df, fig, "Analysis complete. Displaying results and visualizations."

def build_uplift_model(data, features, treatment, control):
    """
    Build an uplift model to predict the impact of a treatment on customer behavior.
    
    This function prepares the data, creates dummy variables for categorical features,
    standardizes numerical features, and fits a RandomForestClassifier and a BaseTClassifier model.
    
    Args:
        data (pandas.DataFrame): The dataset containing customer and transaction data
        features (list): List of features to use in the model
        treatment (str): The treatment variant (e.g., '10% discount')
        control (str): The control variant
    
    Returns:
        tuple: Contains the fitted model, uplift scores, feature importance dataframe, and prepared features
    """
    # Prepare the data
    treatment_data = data[data['variant'] == treatment]
    control_data = data[data['variant'] == control]
    combined_data = pd.concat([treatment_data, control_data])
    
    # Create dummy variables for categorical features
    categorical_features = [f for f in features if data[f].dtype == 'object']
    X = pd.get_dummies(data[features], columns=categorical_features)
    
    # Standardize numerical features
    numerical_features = [f for f in features if data[f].dtype in ['int64', 'float64']]
    scaler = StandardScaler()
    X[numerical_features] = scaler.fit_transform(X[numerical_features])
    
    # Prepare y and treatment for the combined data
    y = combined_data['purchase']
    t = (combined_data['variant'] == treatment).astype(int)
    
    # Create and fit the RandomForestClassifier directly
    rf_model = RandomForestClassifier(n_estimators=50, max_depth=4)
    rf_model.fit(X.loc[combined_data.index], y)
    
    # Get feature importances from the RandomForestClassifier
    feature_importances = rf_model.feature_importances_
    
    # Create a dataframe with feature names and their importances
    feature_importance_df = pd.DataFrame({
        'feature': X.columns,
        'importance': feature_importances
    }).sort_values('importance', ascending=False)
    
    # Create and fit the BaseTClassifier model
    model = BaseTClassifier(RandomForestClassifier(n_estimators=50, max_depth=4))
    model.fit(X=X.loc[combined_data.index].values, treatment=t, y=y)
    
    # Predict for all data
    uplift_scores = model.predict(X.values)
    
    # Handle 2D output if necessary
    if uplift_scores.ndim == 2:
        if uplift_scores.shape[1] == 2:
            uplift_scores = uplift_scores[:, 1] - uplift_scores[:, 0]
        elif uplift_scores.shape[1] == 1:
            uplift_scores = uplift_scores.flatten()
    
    return model, uplift_scores, feature_importance_df, X

def calculate_incremental_metrics(data, uplift_scores, treatment, threshold):
    """
    Calculate incremental purchases and profits based on uplift scores and a threshold.
    
    Args:
        data (pandas.DataFrame): The dataset containing customer and transaction data
        uplift_scores (numpy.array): The uplift scores for each customer
        treatment (str): The treatment variant (e.g., '10% discount')
        threshold (float): The uplift score threshold for targeting
    
    Returns:
        tuple: Contains incremental purchases and incremental profits
    """
    treated = data[data['variant'] == treatment]
    control = data[data['variant'] == 'Control']
    
    targeted = data[uplift_scores > threshold]
    targeted_treated = targeted[targeted['variant'] == treatment]
    targeted_control = targeted[targeted['variant'] == 'Control']
    
    inc_purchases = (targeted_treated['purchase'].mean() - targeted_control['purchase'].mean()) * len(targeted)
    inc_profit = (targeted_treated['profit'].mean() - targeted_control['profit'].mean()) * len(targeted)
    
    return inc_purchases, inc_profit


def build_models_and_display(selected_features):

    """
    Build uplift models for all discount levels and display results.
    
    This function builds uplift models for 5%, 10%, and 15% discounts, calculates feature importance,
    and creates visualizations to compare model performance.
    
    Args:
        selected_features (list): List of features to use in the models
    
    Returns:
        tuple: Contains model information, feature importance plot, and uplift plot
    """
    
    global rct_results, generated_data, uplift_models, last_used_features
    if rct_results is None or generated_data is None:
        return "Please generate customer data and run RCT simulation first.", None, None

    transactions_df, variant_assignments_df = rct_results
    
    # Prepare the data
    df_with_variant = pd.merge(generated_data, variant_assignments_df, on='customer_id', how='inner')
    transactions_df['purchase'] = 1
    final_df = pd.merge(df_with_variant, transactions_df, on=['customer_id', 'variant'], how='left')
    final_df[['purchase', 'price', 'discounted_price', 'cost', 'profit']] = final_df[['purchase', 'price', 'discounted_price', 'cost', 'profit']].fillna(0)
    
    # Perform train/test split at customer ID level
    train_ids, test_ids = train_test_split(final_df['customer_id'].unique(), test_size=0.5, random_state=42)
    train_df = final_df[final_df['customer_id'].isin(train_ids)]
    test_df = final_df[final_df['customer_id'].isin(test_ids)]
    
    treatments = ['5% discount', '10% discount', '15% discount']
    colors = ['blue', 'green', 'purple']
    
    all_feature_importance = []
    uplift_models = {}  # Store models for each treatment
    
    # Create Matplotlib figure for uplift plots
    fig_uplift, axs = plt.subplots(2, 1, figsize=(10, 12))
    
    for treatment, color in zip(treatments, colors):
        model, train_uplift_scores, feature_importance_df, X_train = build_uplift_model(train_df, selected_features, treatment, 'Control')
        uplift_models[treatment] = model  # Store the model
        
        feature_importance_df['treatment'] = treatment
        all_feature_importance.append(feature_importance_df)
        
        X_test = pd.get_dummies(test_df[selected_features], columns=[f for f in selected_features if test_df[f].dtype == 'object'])
        X_test = X_test.reindex(columns=X_train.columns, fill_value=0)
        
        scaler = StandardScaler()
        X_test.loc[:, X_test.dtypes != 'uint8'] = scaler.fit_transform(X_test.loc[:, X_test.dtypes != 'uint8'])
        
        test_uplift_scores = model.predict(X_test.values)
        if test_uplift_scores.ndim == 2:
            test_uplift_scores = test_uplift_scores[:, 1] - test_uplift_scores[:, 0] if test_uplift_scores.shape[1] == 2 else test_uplift_scores.flatten()
        
        for i, (dataset, uplift_scores) in enumerate([(train_df, train_uplift_scores), (test_df, test_uplift_scores)]):
            thresholds = np.linspace(np.min(uplift_scores), np.max(uplift_scores), 100)
            inc_purchases, inc_profits = zip(*[calculate_incremental_metrics(dataset, uplift_scores, treatment, threshold) for threshold in thresholds])
            
            axs[i].plot(inc_purchases, inc_profits, label=f'{treatment} Model', color=color)
            axs[i].plot([0, inc_purchases[0]], [0, inc_profits[0]], label=f'{treatment} Random', color=color, linestyle='--')
    
    # Customize uplift plots
    for i, title in enumerate(["Train Set Performance", "Test Set Performance"]):
        axs[i].set_title(title)
        axs[i].set_xlabel("Incremental Purchases")
        axs[i].set_ylabel("Incremental Profit")
        axs[i].legend()
        axs[i].grid(True)
    
    plt.tight_layout()
    
    # Create feature importance plot
    fig_importance, ax = plt.subplots(figsize=(12, 8))
    combined_feature_importance = pd.concat(all_feature_importance)
    treatment_order = ['5% discount', '10% discount', '15% discount']
    feature_order = combined_feature_importance[combined_feature_importance['treatment'] == '5% discount'].sort_values('importance', ascending=False)['feature'].unique()
    
    sns.barplot(x='importance', y='feature', hue='treatment', data=combined_feature_importance,
                hue_order=treatment_order, order=feature_order, ax=ax)
    
    ax.set_title('Feature Importance for All Treatments vs Control (Train Set)')
    ax.set_xlabel('Importance')
    ax.set_ylabel('Feature')
    plt.tight_layout()
    
    last_used_features = selected_features  # Store the last used features
    
    info = f"Uplift models built using {len(selected_features)} features.\n"
    info += f"Treatments: 5%, 10%, and 15% discounts vs Control\n"
    info += f"Number of samples: Train set - {len(train_df)}, Test set - {len(test_df)}\n"
    info += f"Displaying results for both Train and Test sets"
    
    return info, fig_importance, fig_uplift

def run_targeting_policy(discount_level, target_percentage, experiment_duration):
    """
    Run a targeting policy experiment based on the uplift model predictions.

    This function applies the uplift model to predict customer responses to a discount,
    targets a specified percentage of customers, and simulates the experiment results.

    Args:
        discount_level (str): The discount level to apply (e.g., '10% discount')
        target_percentage (float): The percentage of customers to target with the discount
        experiment_duration (int): The duration of the experiment in days

    Returns:
        tuple: Contains the experiment results DataFrame, transactions DataFrame, and experiment info
    """
    global generated_data, uplift_models, last_used_features
    if generated_data is None or not uplift_models:
        return None, "Please generate customer data and build uplift models first."
    
    # Prepare the data
    df = generated_data.copy()
    
    # Use the uplift model to make predictions
    model = uplift_models.get(discount_level)
    if model is None:
        return None, f"No uplift model found for {discount_level}. Please build the model first."
    
    # Prepare features for prediction
    X = pd.get_dummies(df[last_used_features], columns=[f for f in last_used_features if df[f].dtype == 'object'])
    
    # Standardize numerical features
    numerical_features = [f for f in last_used_features if df[f].dtype in ['int64', 'float64']]
    scaler = StandardScaler()
    X[numerical_features] = scaler.fit_transform(X[numerical_features])
    
    # Get uplift scores
    uplift_scores = model.predict(X.values)
    if uplift_scores.ndim == 2:
        uplift_scores = uplift_scores[:, 1] - uplift_scores[:, 0] if uplift_scores.shape[1] == 2 else uplift_scores.flatten()
    
    # Calculate the threshold based on the target percentage
    model_threshold = get_threshold_for_percentage(uplift_scores, target_percentage)
    
    # Assign variants
    all_variants = ['Control', '5% discount', '10% discount', '15% discount', 'Targeted']
    variant_probabilities = [0.2] * 5  # Equal probability for all variants
    
    df['experiment_variant'] = np.random.choice(all_variants, size=len(df), p=variant_probabilities)
    
    # For customers in the Targeted group, determine if they get a discount
    df['gets_targeted_discount'] = (df['experiment_variant'] == 'Targeted') & (uplift_scores > model_threshold)
    
    # Run simulation
    transactions_df = run_targeted_simulation(df, experiment_duration, discount_level)
    
    return df, transactions_df, f"Ran targeting policy simulation for {experiment_duration} days with {discount_level} and targeting {target_percentage}% of the audience."

def run_targeted_simulation(df, experiment_duration, discount_level):
    """
    Run a targeted simulation based on the assigned variants and uplift scores.

    This function simulates customer purchases during the experiment period,
    taking into account the assigned variants and whether customers receive targeted discounts.

    Args:
        df (pandas.DataFrame): The customer data with assigned variants and targeting information
        experiment_duration (int): The duration of the experiment in days
        discount_level (str): The discount level being applied (e.g., '10% discount')

    Returns:
        pandas.DataFrame: A DataFrame containing the simulated transactions
    """
    transactions = []
    for _, customer in df.iterrows():
        variant = customer['experiment_variant']
        if variant == 'Targeted':
            discount = float(discount_level.split('%')[0]) / 100 if customer['gets_targeted_discount'] else 0
        elif '%' in variant:
            discount = float(variant.split('%')[0]) / 100
        else:
            discount = 0
        
        # Simulate purchases
        num_purchases = np.random.poisson(experiment_duration / 10)
        for _ in range(num_purchases):
            product = np.random.choice(electronics_products)
            if np.random.random() < calculate_purchase_probability(customer, discount):
                price = product['price']
                discounted_price = price * (1 - discount)
                cost = product['cost']
                profit = discounted_price - cost
                transactions.append({
                    'customer_id': customer['customer_id'],
                    'variant': variant,
                    'product': product['name'],
                    'price': price,
                    'discounted_price': discounted_price,
                    'cost': cost,
                    'profit': profit
                })
    
    return pd.DataFrame(transactions)

def analyze_targeting_results(assignment_df, transactions_df):

    """
    Analyze the results of the targeting policy experiment.
    This function calculates various metrics for each variant, including conversion rates,
    average revenue and profit per customer, and incremental purchases and profits.
    Args:
        assignment_df (pandas.DataFrame): The DataFrame containing variant assignments
        transactions_df (pandas.DataFrame): The DataFrame containing transaction data
    Returns:
        tuple: Contains a DataFrame with variant metrics and a plotly Figure object
    """
    
    # Calculate metrics for assigned customers
    assigned_customers = assignment_df.groupby('experiment_variant')['customer_id'].nunique().reset_index()
    assigned_customers.columns = ['variant', 'assigned_customers']
    
    # Calculate metrics for purchases
    purchase_metrics = transactions_df.groupby('variant').agg({
        'customer_id': 'nunique',
        'discounted_price': 'sum',
        'profit': 'sum'
    }).reset_index()
    purchase_metrics.columns = ['variant', 'purchasing_customers', 'revenue', 'profit']
    
    # Merge assigned customers with purchase metrics
    variant_metrics = pd.merge(assigned_customers, purchase_metrics, on='variant', how='left')
    variant_metrics = variant_metrics.fillna(0)  # Fill NaN values with 0 for variants with no purchases
    
    # Calculate additional metrics
    variant_metrics['conversion_rate'] = variant_metrics['purchasing_customers'] / variant_metrics['assigned_customers']
    variant_metrics['avg_revenue_per_customer'] = variant_metrics['revenue'] / variant_metrics['assigned_customers']
    variant_metrics['avg_profit_per_customer'] = variant_metrics['profit'] / variant_metrics['assigned_customers']
    
    # Calculate incremental metrics compared to control
    control_metrics = variant_metrics[variant_metrics['variant'] == 'Control'].iloc[0]
    variant_metrics['incremental_purchases'] = variant_metrics['purchasing_customers'] - control_metrics['purchasing_customers']
    variant_metrics['incremental_profit'] = variant_metrics['profit'] - control_metrics['profit']
    
    # Create visualization using Matplotlib
    fig, ax = plt.subplots(figsize=(10, 6))
    
    colors = {'Control': 'blue', '5% discount': 'green', '10% discount': 'orange', 
              '15% discount': 'red', 'Targeted': 'purple'}
    
    for variant in variant_metrics['variant']:
        variant_data = variant_metrics[variant_metrics['variant'] == variant]
        ax.scatter(variant_data['incremental_purchases'], variant_data['incremental_profit'],
                   label=variant, color=colors.get(variant, 'gray'))
        ax.annotate(variant, (variant_data['incremental_purchases'].values[0],
                              variant_data['incremental_profit'].values[0]),
                    xytext=(5, 5), textcoords='offset points')
    
    ax.set_title('Incremental Profit vs Incremental Purchases by Variant')
    ax.set_xlabel('Incremental Purchases')
    ax.set_ylabel('Incremental Profit')
    ax.legend(loc='lower left')    
    ax.grid(True)
    
    plt.tight_layout()
    
    return variant_metrics, fig

def get_threshold_for_percentage(uplift_scores, percentage):
    """
    Calculate the threshold that targets the specified percentage of the audience.

    Args:
        uplift_scores (numpy.array): Array of uplift scores for all customers
        percentage (float): The desired percentage of customers to target

    Returns:
        float: The uplift score threshold that targets the specified percentage of customers
    """
    if percentage == 100:
        return np.min(uplift_scores) - 1e-10  # Return a value slightly lower than the minimum
    elif percentage == 0:
        return np.max(uplift_scores) + 1e-10  # Return a value slightly higher than the maximum
    else:
        sorted_scores = np.sort(uplift_scores)[::-1]  # Sort in descending order
        index = int(len(sorted_scores) * (percentage / 100)) - 1  # Subtract 1 to avoid index out of bounds
        return sorted_scores[index]
    
with gr.Blocks() as demo:
    gr.Markdown("# Causal AI - Synthetic Customer Data Generator and RCT Simulator")
    
    with gr.Tab("Generate Customer Data"):
        gr.Markdown("# Generate Synthetic Customers data")
        gr.Markdown("In this section we generate typical data of customers that are registered to our store.")
        gr.Markdown("First we generate some basic attributes that are defined when the customer first registers, such as Name, City or Preferred Language.")
        gr.Markdown("Then we add some extra information that is usually the result of the customer past behavior, such as Loyalty Level, Past Purchases or Categories of interest.")
        gr.Markdown("## Select the number of customers that you want to Generate")
        num_customers_input = gr.Slider(minimum=10000, maximum=500000, value=200000, step=1000, label="Number of Customer Records")
        generate_btn = gr.Button("Generate Customer Data")
        gr.Markdown("## Basic Customer Info Sample")
        basic_info_output = gr.DataFrame()
        gr.Markdown("## Extra Customer Info Sample")
        extra_info_output = gr.DataFrame()
        generate_info = gr.Textbox(label="Generation Info")
        
        generate_btn.click(fn=generate_and_display_data, 
                           inputs=num_customers_input, 
                           outputs=[basic_info_output, extra_info_output, generate_info])
    
    with gr.Tab("Run RCT Simulation"):
        gr.Markdown("# Run a Randomized Control Experiment for data collection and analysis")
        gr.Markdown("In this section we simulate running an Experiment where we offer customers different levels of discounts in the Electronics department.")
        gr.Markdown("We randomly split the customers in 4 groups: Control, 5% discount, 10% discount and 15% discount")
        gr.Markdown("During the experiment runtime we record all the purchases made by the customers. We can decide how long to run the experiment for, where longer periods lead to less noise and more significance in the results.")
        experiment_duration_input = gr.Slider(minimum=10, maximum=60, value=30, step=1, label="Experiment Duration (days)")
        rct_btn = gr.Button("Run RCT Simulation")
        gr.Markdown("## Customer assigment to experiment group:")
        assignments_output = gr.DataFrame()
        gr.Markdown("## Purchases made during experiment runtime:")
        transactions_output = gr.DataFrame()
        rct_info = gr.Textbox(label="RCT Simulation Info")
        rct_btn.click(fn=run_and_display_rct, 
                      inputs=experiment_duration_input, 
                      outputs=[assignments_output, transactions_output, rct_info])

    with gr.Tab("Analyze RCT Results"):
        gr.Markdown("# Experiment Analysis")
        gr.Markdown("In this section we analyze the experiment results. We measure, per each discount value (5%, 10%, 15%) what is the incremental number of Purchases and the incremental Profit compared to the Control group.")
        analyze_btn = gr.Button("Analyze RCT Results")
        gr.Markdown("## Overall metrics")
        overall_metrics_output = gr.DataFrame()
        gr.Markdown("## Metrics by Variant")
        variant_metrics_output = gr.DataFrame()
        gr.Markdown("## Metrics per Variant visualization")
        gr.Markdown("## To-Do: Add confidence intervals")
        plot_output = gr.Plot()
        analysis_info = gr.Textbox(label="Analysis Info")
        
        analyze_btn.click(fn=analyze_and_display_results,
                          inputs=[],
                          outputs=[overall_metrics_output, variant_metrics_output, plot_output, analysis_info])

    with gr.Tab("Exploratory Data Analysis"):
        gr.Markdown("# Exploratory Data Analysis")
        gr.Markdown("In this section, we explore the impact of discounts on different customer segments.")
        
        discount_dropdown = gr.Dropdown(
            choices=['5% discount', '10% discount', '15% discount'],
            label="Select discount level to analyze",
            value='10% discount'
        )
        
        eda_btn = gr.Button("Perform EDA")
        
        eda_info = gr.Textbox(label="EDA Information")
        
        gr.Markdown("## Newsletter Subscription Analysis")
        newsletter_results = gr.DataFrame(label="Newsletter Subscription Results")
        newsletter_plot = gr.Plot(label="Newsletter Subscription Plot")
        
        gr.Markdown("## Preferred Payment Method Analysis")
        payment_results = gr.DataFrame(label="Preferred Payment Method Results")
        payment_plot = gr.Plot(label="Preferred Payment Method Plot")
        
        eda_btn.click(
            fn=perform_eda,
            inputs=[discount_dropdown],
            outputs=[eda_info, newsletter_results, payment_results, newsletter_plot, payment_plot]
        ) 
        
    with gr.Tab("Build Uplift Model"):
        gr.Markdown("## Build Uplift Models for All Discount Levels")
        
        feature_checklist = gr.CheckboxGroup(
            choices=['age', 'gender', 'region', 'preferred_language', 'newsletter_subscription', 
                     'preferred_payment_method', 'loyalty_level', 'main_browsing_device', 
                     'average_order_value', 'total_orders'],
            label="Select features for the models",
            value=['age', 'gender', 'loyalty_level', 'average_order_value', 'total_orders']
        )
        
        build_model_btn = gr.Button("Build Uplift Models")
        
        model_info = gr.Textbox(label="Model Information")
        feature_importance_plot = gr.Plot(label="Feature Importance for All Models (Train Set)")
        uplift_plot = gr.Plot(label="Incremental Profit vs Incremental Purchases (All Models)")
        
        build_model_btn.click(
            fn=build_models_and_display,
            inputs=[feature_checklist],
            outputs=[model_info, feature_importance_plot, uplift_plot]
        )

    with gr.Tab("Run Targeting Policy"):
        gr.Markdown("# Run Targeting Policy Experiment")
        gr.Markdown("In this section, we run an experiment using a targeted policy based on the uplift models.")
        
        discount_level = gr.Dropdown(
            choices=['5% discount', '10% discount', '15% discount'],
            label="Select discount level for targeting",
            value='10% discount',
            interactive=True
        )
        target_percentage = gr.Slider(minimum=0, maximum=100, value=40, step=1, label="Percentage of Audience Targeted", interactive=True)
        experiment_duration = gr.Slider(minimum=10, maximum=60, value=30, step=1, label="Experiment Duration (days)", interactive=True)
        
        run_targeting_btn = gr.Button("Run Targeting Policy Experiment")
        targeting_info = gr.Textbox(label="Targeting Experiment Info")
        
        gr.Markdown("## Experiment Results")
        targeting_results = gr.DataFrame(label="Targeting Experiment Results")
        targeting_plot = gr.Plot(label="Incremental Profit vs Incremental Purchases by Variant")
        
        def run_and_analyze_targeting(discount, percentage, duration):
            assignment_df, transactions_df, info = run_targeting_policy(discount, percentage, duration)
            if transactions_df is None:
                return info, None, None
            results, plot = analyze_targeting_results(assignment_df, transactions_df)
            return info, results, plot
        
        run_targeting_btn.click(
            fn=run_and_analyze_targeting,
            inputs=[discount_level, target_percentage, experiment_duration],
            outputs=[targeting_info, targeting_results, targeting_plot]
        )
        
demo.launch()