<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Laurin Brechter on Medium]]></title>
        <description><![CDATA[Stories by Laurin Brechter on Medium]]></description>
        <link>https://medium.com/@brechterlaurin?source=rss-4a7fc9f4bfd4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*QHmK9X-MGra2e-1yZwCa2Q.jpeg</url>
            <title>Stories by Laurin Brechter on Medium</title>
            <link>https://medium.com/@brechterlaurin?source=rss-4a7fc9f4bfd4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 13:44:22 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@brechterlaurin/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Data-Driven Journey Optimization: Using Deep Learning to Design Customer Journeys]]></title>
            <link>https://medium.com/data-science/data-driven-journey-optimization-using-deep-learning-to-design-customer-journeys-93a3f8e92956?source=rss-4a7fc9f4bfd4------2</link>
            <guid isPermaLink="false">https://medium.com/p/93a3f8e92956</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[pytorch]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[marketing-analytics]]></category>
            <dc:creator><![CDATA[Laurin Brechter]]></dc:creator>
            <pubDate>Wed, 06 Nov 2024 18:09:28 GMT</pubDate>
            <atom:updated>2024-11-06T18:09:28.219Z</atom:updated>
            <content:encoded><![CDATA[<h4>Can ML models learn to construct optimal customer journeys?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_yHXbhpXlXtaOJLW8_HTmw.png" /><figcaption>Optimizing Customer Journeys with Beam Search</figcaption></figure><p>Marketing attribution has traditionally been backward-looking: analyzing past customer journeys to understand which touchpoints contributed to conversion. But what if we could use this historical data to design optimal future journeys? In this post, I’ll show how we can combine deep learning with optimization techniques to design high-converting customer journeys while respecting real-world constraints. We will do so by using an LSTM to predict journeys with high conversion probability and then using beam search to find sequences with good chances of conversion. All images are created by the author.</p><h3>Introduction</h3><p>Customers interact with businesses on what we can call a customer journey. On this journey, they come into contact with the company through so-called touchpoints (e.g., Social Media, Google Ads, …). At any point, users could convert (e.g. by buying your product). We want to know what touchpoints along that journey contributed to the conversion to optimize the conversion rate.</p><h3>The Limitations of Traditional Attribution</h3><p>Before diving into our solution, it’s important to understand why traditional attribution models fall short.</p><h4>1. Position-Agnostic Attribution</h4><p>Traditional attribution models (first-touch, last-touch, linear, etc.) typically assign a single importance score to each channel, regardless of where it appears in the customer journey. This is fundamentally flawed because:</p><ul><li>A social media ad might be highly effective early in the awareness stage but less impactful during consideration</li><li>Email effectiveness often depends on whether it’s a welcome email, nurture sequence, or re-engagement campaign</li><li>Retargeting ads make little sense as a first touchpoint but can be powerful later in the journey</li></ul><h4>2. Context Blindness</h4><p>Most attribution models (even data-driven ones) ignore crucial contextual factors:</p><ul><li><strong>Customer Characteristics</strong>: A young tech-savvy customer might respond differently to digital channels compared to traditional ones</li></ul><pre>Customer 1 (Young, Urban): Social → Video → Purchase<br>Customer 2 (Older, Rural): Print → Email → Purchase</pre><ul><li><strong>Previous Purchase History</strong>: Existing customers often require different engagement strategies than new prospects</li><li><strong>Time of Day/Week</strong>: Channel effectiveness can vary significantly based on timing</li><li><strong>Device/Platform</strong>: The same channel might perform differently across different platforms</li><li><strong>Geographic/Cultural Factors</strong>: What works in one market might fail in another</li></ul><h4>3. Static Channel Values</h4><p>Traditional models assume channel effectiveness can be expressed as a single number where all other factors influencing the effectiveness are marginalized. As mentioned above, channel effectiveness is highly context-dependent and should be a function of said context (e.g. position, other touchpoints, …).</p><h3>Deep Learning Enters the Stage</h3><p>Customer journeys are inherently sequential — the order and timing of touchpoints matter. We can frame attribution modeling as a <em>binary time series classification</em> task where we want to predict from the sequence of touchpoints whether a customer converted or not. This makes them perfect candidates for sequence modeling using R<em>ecurrent Neural Networks</em> (RNNs), specifically <em>Long Short-Term Memory</em> (LSTM) networks. These models can capture complex patterns in sequential data, including:</p><ul><li>The effectiveness of different channel combinations</li><li>The importance of touchpoint ordering</li><li>Timing sensitivities</li><li>Channel interaction effects</li></ul><h4>Learning from Historical Data</h4><p>The first step is to train an LSTM model on historical customer journey data. For each customer, we need:</p><ol><li>The sequence of touchpoints they encountered</li><li>Whether they ultimately converted</li><li>Characteristics of the customer</li></ol><p>The LSTM learns to predict conversion probability given any sequence of touchpoints. This gives us a powerful “simulator” that can evaluate the likely effectiveness of any proposed customer journey.</p><p>As I did not find a suitable dataset (especially one that contains customer characteristics as the contextual data), I decided to generate my own synthetic data. The notebook for the data generation can be found <a href="https://github.com/LaurinBrechter/PatternMining/blob/main/attribution_modelling.ipynb">here</a>. We generate some characteristics and a random number of customer journeys for each customer. The journeys are of random length. At each point in the journey, the customer interacts with a touchpoint and has a probability of converting. This probability is composed of multiple factors.</p><ul><li>The base conversion rate of the channel</li><li>A positional multiplier. Some channels are more or less effective in some positions of the journey.</li><li>A segment multiplier. The channel&#39;s effectiveness depends on the segment of the customer.</li><li>We also have interaction effects. E.g. if the user is young, touchpoints such as social and search will be more effective.</li><li>Additionally, the previous touchpoint matters for the effectiveness of the current touchpoint.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/456/1*lawcSYyX_BoBrueT8FJqAQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/443/1*fBfx797J4AjkMeI-14FuZQ.png" /><figcaption>Journey Data (Left) and User Data (Right)</figcaption></figure><p>We then preprocess the data by merging the two tables, scaling the numerical features, and OneHotEncoding the categorical features. We can then set up an LSTM model that processes the sequences of touchpoints after embedding them. In the final fully connected layer, we also add the contextual features of the customer. The full code for preprocessing and training can be found in this <a href="https://github.com/LaurinBrechter/PatternMining/blob/main/attribution_modelling.ipynb">notebook</a>.</p><p>We can then train the neural network with a binary cross-entropy loss. I have plotted the recall achieved on the test set below. In this case, we care more about recall than accuracy as we want to detect as many converting customers as possible. Wrongly predicting that some customers will convert if they don’t is not as bad as missing high-potential customers.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/567/1*XS2DnUHaeTw8ZYrmg8AVVA.png" /><figcaption>Training the JourneyLSTM</figcaption></figure><p>Additionally, we will find that most journeys do not lead to a conversion. We will typically see conversion rates from 2% to 7% which means that we have a highly imbalanced dataset. For the same reason, accuracy isn’t all that meaningful. Always predicting the majority class (in this case ‘no conversion’) will get us a very high accuracy but we won’t find any of the converting users.</p><h3>From Prediction to Optimization</h3><p>Once we have a trained model, we can use it to design optimal journeys. We can impose a sequence of channels (in the example below channel 1 then 2) on a set of customers and look at the conversion probability predicted by the model. We can already see that these vary a lot depending on the characteristics of the customer. Therefore, we want to optimize the journey for each customer individually.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/689/1*EoABJFj4gJeQ5Q-PkWHd0g.png" /><figcaption>Imposed Journeys and Predicted Conversion Probabilities</figcaption></figure><p>Additionally, we can’t just pick the highest-probability sequence. Real-world marketing has constraints:</p><ul><li>Channel-specific limitations (e.g., email frequency caps)</li><li>Required touchpoints at specific positions</li><li>Budget constraints</li><li>Timing requirements</li></ul><p>Therefore, we frame this as a constrained combinatorial optimization problem: find the sequence of touchpoints that maximizes the model’s predicted conversion probability while satisfying all constraints. In this case, we will only constrain the occurrence of touchpoints at certain places in the journey. That is, we have a mapping from position to touchpoint that specifies that a certain touchpoint must occur at a given position.</p><p>Note also that we aim to optimize for a predefined journey length rather than journeys of arbitrary length. By the nature of the simulation, the overall conversion probability will be strictly monotonically increasing as we have a non-zero conversion probability at each touchpoint. Therefore, a longer journey (more non-zero entries) would trump a shorter journey most of the time and we would construct infinitely long journeys.</p><h4>Optimization using Beam Search</h4><p>Below is the implementation for beam search using recursion. At each level, we optimize a certain position in the journey. If the position is in the constraints and already fixed, we skip it. If we have reached the maximum length we want to optimize, we stop recursing and return.</p><p>At each level, we look at current solutions and generate candidates. At any point, we keep the best K candidates defined by the beam width. Those best candidates are then used as input for the next round of beam search where we optimize the next position in the sequence.</p><pre>def beam_search_step(<br>        model: JourneyLSTM, <br>        X: torch.Tensor, <br>        pos: int, <br>        num_channels: int, <br>        max_length: int, <br>        constraints:dict[int, int], <br>        beam_width: int = 3<br>    ):<br>    if pos &gt; max_length:<br>        return X<br>    <br>    if pos in constraints:<br>        return beam_search_step(model, X, pos + 1, num_channels, max_length, constraints, beam_width)<br>    <br>    candidates = []  # List to store (sequence, score) tuples<br>    <br>    for sequence_idx in range(min(beam_width, len(X))):<br>        X_current = X[sequence_idx:sequence_idx+1].clone()<br>        <br>        # Try each possible channel<br>        for channel in range(num_channels):<br>            X_candidate = X_current.clone()<br>            X_candidate[0, extra_dim + pos] = channel<br>            <br>            # Get prediction score<br>            pred = model(X_candidate)[0].item()<br>            candidates.append((X_candidate, pred))<br>    <br>    candidates.sort(key=lambda x: x[1], reverse=True)<br>    best_candidates = candidates[:beam_width]<br>    <br>    X_next = torch.cat([cand[0] for cand in best_candidates], dim=0)<br>    <br>    # Recurse with best candidates<br>    return beam_search_step(model, X_next, pos + 1, num_channels, max_length, constraints, beam_width)</pre><p>This optimization approach is greedy and we are likely to miss high-probability combinations. Nonetheless, in many scenarios, especially with many channels, brute forcing an optimal solution may not be feasible as the number of possible journeys grows exponentially with the journey length.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_yHXbhpXlXtaOJLW8_HTmw.png" /><figcaption>Optimizing Customer Journeys with Beam Search</figcaption></figure><p>In the image above, we optimized the conversion probability for a single customer. In position 0, we have specified ‘email’ as a fixed touchpoint. Then, we explore possible combinations with email. Since we have a beam width of five, all combinations (e.g. email -&gt; search) go into the next round. In that round, we discovered the high-potential journey which would display the user two times email and finally retarget.</p><h3>Conclusion</h3><p>Moving from prediction to optimization in attribution modeling means we are going from predictive to prescriptive modeling where the model tells us actions to take. This has the potential to achieve much higher conversion rates, especially when we have highly complex scenarios with many channels and contextual variables.</p><p>At the same time, this approach has several drawbacks. Firstly, if we do not have a model that can detect converting customers sufficiently well, we are likely to harm conversion rates. Additionally, the probabilities that the model outputs have to be calibrated well. Otherwiese, the conversion probabilities we are optimizing for are likely not meanningful. Lastly, we will encounter problems when the model has to predict journeys that are outside of its data distribution. It would therefore also be desirable to use a Reinforcement Learning (RL) approach, where the model can actively generate new training data.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=93a3f8e92956" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/data-driven-journey-optimization-using-deep-learning-to-design-customer-journeys-93a3f8e92956">Data-Driven Journey Optimization: Using Deep Learning to Design Customer Journeys</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Self-Service ML with Relational Deep Learning]]></title>
            <link>https://medium.com/data-science/self-service-ml-with-relational-deep-learning-beb693a21d5b?source=rss-4a7fc9f4bfd4------2</link>
            <guid isPermaLink="false">https://medium.com/p/beb693a21d5b</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[relational-databases]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[Laurin Brechter]]></dc:creator>
            <pubDate>Tue, 22 Oct 2024 17:42:56 GMT</pubDate>
            <atom:updated>2024-10-22T17:42:56.929Z</atom:updated>
            <content:encoded><![CDATA[<h4>Do ML directly on your relational database</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/443/1*NXb0TreqAHZgq-ojBBFzjQ.png" /><figcaption>Relational Schema of our Dataset, <a href="https://www.kaggle.com/datasets/mmohaiminulislam/ecommerce-data-analysis/data">source</a>; Image by Author</figcaption></figure><p>In this blog post, we will dive into an interesting new approach to Deep Learning (DL) called Relational Deep Learning (RDL). We will also gain some hands-on experience by doing some RDL on a real-world database (not a dataset!) of an e-commerce company.</p><h3>Introduction</h3><p>In the real world, we usually have a relational database against which we want to run some ML task. But especially when the database is highly normalized, this implies lots of time-consuming feature engineering and loss of granularity as we have to do many aggregations. What’s more, there’s a myriad of possible combinations of features that we can construct each of which might yield good performance [2]. That means we are likely to leave some information relevant to the ML task on the table.</p><p>This is similar to the early days of computer vision, before the advent of deep neural networks where features were hand-crafted from the pixel values. Nowadays, models work directly with the raw pixels instead of relying on this intermediate layer.</p><h3>Relational Deep Learning</h3><p>RDL promises to do the same for tabular learning. That is, it removes the extra step of constructing a feature matrix by learning directly on top of your relational database. It does so by transforming the database with its relations into a graph where a row in a table becomes a node and relations between tables become edges. The row values are stored inside the nodes as node features.</p><p><em>In this blog post, we will be using this </em><a href="https://www.kaggle.com/datasets/mmohaiminulislam/ecommerce-data-analysis/data"><em>e-commerce dataset</em></a><em> from kaggle which contains transactional data about an e-commerce platform in a star schema with a central fact table (transactions) and some dimension tables. The full code can be found in this </em><a href="https://github.com/LaurinBrechter/GraphTheory/blob/main/rdl/rdl_ecommerce.ipynb"><em>notebook</em></a><em>.</em></p><p>Throughout this blog post, we will be using the <a href="https://github.com/snap-stanford/relbench">relbench</a> library to do <a href="https://relbench.stanford.edu/">RDL</a>. The first thing we have to do in relbench is to specify the schema of our relational database. Below is an example of how we can do so for the ‘transactions’ table in the database. We give the table as a pandas dataframe and specify the primary key and the timestamp column. The primary key column is used to uniquely identify the entity. The timestamp ensures that we can only learn from past transactions when we want to forecast future transactions. In the graph, this means that information can only flow from nodes with a lower timestamp (i.e. in the past) to ones with a higher timestamp. Additionally, we specify the foreign keys that exist in the relation. In this case, the transactions table has the column ‘customer_key’ which is a foreign key that points to the ‘customer_dim’ table.</p><pre>tables[&#39;transactions&#39;] = Table(<br>            df=pd.DataFrame(t),<br>            pkey_col=&#39;t_id&#39;,<br>            fkey_col_to_pkey_table={<br>                &#39;customer_key&#39;: &#39;customers&#39;,<br>                &#39;item_key&#39;: &#39;products&#39;,<br>                &#39;store_key&#39;: &#39;stores&#39;<br>            },<br>            time_col=&#39;date&#39;<br>        )</pre><p>The rest of the tables need to be defined in the same way. Note that this could also be automated if you already have a database schema. Since the dataset is from Kaggle, I needed to create the schema manually. We also need to convert the date columns to actual pandas datetime objects and remove any NaN values.</p><pre>class EcommerceDataBase(Dataset):<br>    # example of creating your own dataset: https://github.com/snap-stanford/relbench/blob/main/tutorials/custom_dataset.ipynb<br><br>    val_timestamp = pd.Timestamp(year=2018, month=1, day=1)<br>    test_timestamp = pd.Timestamp(year=2020, month=1, day=1)<br><br>    def make_db(self) -&gt; Database:<br><br>        tables = {}<br><br>        customers = load_csv_to_db(BASE_DIR + &#39;/customer_dim.csv&#39;).drop(columns=[&#39;contact_no&#39;, &#39;nid&#39;]).rename(columns={&#39;coustomer_key&#39;: &#39;customer_key&#39;})<br>        stores = load_csv_to_db(BASE_DIR + &#39;/store_dim.csv&#39;).drop(columns=[&#39;upazila&#39;])<br>        products = load_csv_to_db(BASE_DIR + &#39;/item_dim.csv&#39;)<br>        transactions = load_csv_to_db(BASE_DIR + &#39;/fact_table.csv&#39;).rename(columns={&#39;coustomer_key&#39;: &#39;customer_key&#39;})<br>        times = load_csv_to_db(BASE_DIR + &#39;/time_dim.csv&#39;)<br><br>        t = transactions.merge(times[[&#39;time_key&#39;, &#39;date&#39;]], on=&#39;time_key&#39;).drop(columns=[&#39;payment_key&#39;, &#39;time_key&#39;, &#39;unit&#39;])<br>        t[&#39;date&#39;] = pd.to_datetime(t.date)<br>        t = t.reset_index().rename(columns={&#39;index&#39;: &#39;t_id&#39;})<br>        t[&#39;quantity&#39;] = t.quantity.astype(int)<br>        t[&#39;unit_price&#39;] = t.unit_price.astype(float)<br>        products[&#39;unit_price&#39;] = products.unit_price.astype(float)<br>        t[&#39;total_price&#39;] = t.total_price.astype(float)<br><br>        print(t.isna().sum(axis=0))<br>        print(products.isna().sum(axis=0))<br>        print(stores.isna().sum(axis=0))<br>        print(customers.isna().sum(axis=0))<br><br>        tables[&#39;products&#39;] = Table(<br>            df=pd.DataFrame(products),<br>            pkey_col=&#39;item_key&#39;,<br>            fkey_col_to_pkey_table={},<br>            time_col=None<br>        )<br><br>        tables[&#39;customers&#39;] = Table(<br>            df=pd.DataFrame(customers),<br>            pkey_col=&#39;customer_key&#39;,<br>            fkey_col_to_pkey_table={},<br>            time_col=None<br>        )<br><br>        tables[&#39;transactions&#39;] = Table(<br>            df=pd.DataFrame(t),<br>            pkey_col=&#39;t_id&#39;,<br>            fkey_col_to_pkey_table={<br>                &#39;customer_key&#39;: &#39;customers&#39;,<br>                &#39;item_key&#39;: &#39;products&#39;,<br>                &#39;store_key&#39;: &#39;stores&#39;<br>            },<br>            time_col=&#39;date&#39;<br>        )<br><br>        tables[&#39;stores&#39;] = Table(<br>            df=pd.DataFrame(stores),<br>            pkey_col=&#39;store_key&#39;,<br>            fkey_col_to_pkey_table={}<br>        )<br><br>        return Database(tables)</pre><p>Crucially, the authors introduce the idea of a training table. This training table essentially defines the ML task. The idea here is that we want to predict the future state (i.e. a future value) of some entity in the database. We do this by specifying a table where each row has a timestamp, the identifier of the entity, and some value we want to predict. The id serves to specify the entity, the timestamp specifies at which point in time we need to predict the entity. This will also limit the data that can be used to infer the value of this entity (i.e. only past data). The value itself is what we want to predict (i.e. ground truth).</p><p>In our case, we have an online platform with customers. We want to predict a customer’s revenue in the next 30 days. We can create the training table with a SQL statement executed with DuckDB. This is the big advantage of RDL as we could create any kind of ML task with just SQL. For example, we can define a query to select the number of purchases of buyers in the next 30 days to make a churn prediction.</p><pre>df = duckdb.sql(f&quot;&quot;&quot;<br>            select<br>                timestamp,<br>                customer_key,<br>                sum(total_price) as revenue<br>            from<br>                timestamp_df t<br>            left join<br>                transactions ta<br>            on<br>                ta.date &lt;= t.timestamp + INTERVAL &#39;{self.timedelta}&#39;<br>                and ta.date &gt; t.timestamp<br>            group by timestamp, customer_key<br>        &quot;&quot;&quot;).df().dropna()</pre><p>The result will be a table that has the seller_id as the key of the entity that we want to predict, the revenue as the target, and the timestamp as the time at which we need to make the prediction (i.e. we can only use data up until this point to make the prediction).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/353/1*xiL9m5CEJvnB4GBMZrgHgA.png" /><figcaption>Training Table; Image by Author</figcaption></figure><p>Below is the complete code for creating the ‘customer_revenue’ task.</p><pre>class CustomerRevenueTask(EntityTask):<br>    # example of custom task: https://github.com/snap-stanford/relbench/blob/main/tutorials/custom_task.ipynb<br><br><br>    task_type = TaskType.REGRESSION<br>    entity_col = &quot;customer_key&quot;<br>    entity_table = &quot;customers&quot;<br>    time_col = &quot;timestamp&quot;<br>    target_col = &quot;revenue&quot;<br>    timedelta = pd.Timedelta(days=30) # how far we want to predict revenue into the future.<br>    metrics = [r2, mae]<br>    num_eval_timestamps = 40<br><br>    def make_table(self, db: Database, timestamps: &quot;pd.Series[pd.Timestamp]&quot;) -&gt; Table:<br><br>        timestamp_df = pd.DataFrame({&quot;timestamp&quot;: timestamps})<br><br>        transactions = db.table_dict[&quot;transactions&quot;].df<br><br>        df = duckdb.sql(f&quot;&quot;&quot;<br>            select<br>                timestamp,<br>                customer_key,<br>                sum(total_price) as revenue<br>            from<br>                timestamp_df t<br>            left join<br>                transactions ta<br>            on<br>                ta.date &lt;= t.timestamp + INTERVAL &#39;{self.timedelta}&#39;<br>                and ta.date &gt; t.timestamp<br>            group by timestamp, customer_key<br>        &quot;&quot;&quot;).df().dropna()<br><br>        print(df)<br><br>        return Table(<br>            df=df,<br>            fkey_col_to_pkey_table={self.entity_col: self.entity_table},<br>            pkey_col=None,<br>            time_col=self.time_col,<br>        )</pre><p>With that, we have done the bulk of the work. The rest of the workflow will be similar, independent of the ML task. I was able to copy most of the code from the <a href="https://github.com/snap-stanford/relbench/blob/main/tutorials/train_model.ipynb">example notebook</a> that relbench provides.</p><p>For example, we need to encode the node features. Here, we can use glove embeddings to encode all the text features such as the product descriptions and the product names.</p><pre>from typing import List, Optional<br>from sentence_transformers import SentenceTransformer<br>from torch import Tensor<br><br><br>class GloveTextEmbedding:<br>    def __init__(self, device: Optional[torch.device<br>                                       ] = None):<br>        self.model = SentenceTransformer(<br>            &quot;sentence-transformers/average_word_embeddings_glove.6B.300d&quot;,<br>            device=device,<br>        )<br><br>    def __call__(self, sentences: List[str]) -&gt; Tensor:<br>        return torch.from_numpy(self.model.encode(sentences))</pre><p>After that, we can apply those transformations to our data and build out the graph.</p><pre>from torch_frame.config.text_embedder import TextEmbedderConfig<br>from relbench.modeling.graph import make_pkey_fkey_graph<br><br>text_embedder_cfg = TextEmbedderConfig(<br>    text_embedder=GloveTextEmbedding(device=device), batch_size=256<br>)<br><br>data, col_stats_dict = make_pkey_fkey_graph(<br>    db,<br>    col_to_stype_dict=col_to_stype_dict,  # speficied column types<br>    text_embedder_cfg=text_embedder_cfg,  # our chosen text encoder<br>    cache_dir=os.path.join(<br>        root_dir, f&quot;rel-ecomm_materialized_cache&quot;<br>    ),  # store materialized graph for convenience<br>)</pre><p>The rest of the code will be building the GNN from standard layers, coding the training loop, and doing some evaluations. I will leave this code out of this blog post for brevity since it is very standard and will be the same across tasks. You can check out the notebook <a href="https://github.com/LaurinBrechter/GraphTheory/tree/main/rdl">here</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/863/1*Z2OfyFaiI7Yr84mXjoi22w.png" /><figcaption>Result of Training, Image by Author</figcaption></figure><p>As a result, we can train this GNN to reach an r2 of around 0.3 and an MAE of 500. This means that it predicts the seller’s revenue in the next 30 days with an average error of +- $500. Of course, we can’t know if this is good or not, maybe we could have gotten an r2 of 80% with a combination of classical ML and feature engineering.</p><h3>Conclusion</h3><p>Relational Deep Learning is an interesting new approach to ML especially when we have a complex relational schema where manual feature engineering would be too laborious. It gives us the ability to define an ML task with just SQL which can be especially useful for individuals that are not deep into data science but know some SQL. This also means that we can iterate quickly and experiment a lot with different tasks.</p><p>At the same time, this approach presents its own problems such as the difficulty of training GNNs and constructing the graph from the relational schema. Additionally, the question is to what extent RDL can compete in terms of performance with classical ML models. In the past, we have seen that models such as XGboost have proven to be better than neural networks on tabular prediction problems.</p><h3>References</h3><ul><li>[1] Robinson, Joshua, et al. “RelBench: A Benchmark for Deep Learning on Relational Databases.” <em>arXiv</em>, 2024, <a href="https://arxiv.org/abs/2407.20060">https://arxiv.org/abs/2407.20060</a>.</li><li>[2] Fey, Matthias, et al. “Relational deep learning: Graph representation learning on relational databases.” <em>arXiv preprint arXiv:2312.04615</em> (2023).</li><li>[3] Schlichtkrull, Michael, et al. “Modeling relational data with graph convolutional networks.” <em>The semantic web: 15th international conference, ESWC 2018, Heraklion, Crete, Greece, June 3–7, 2018, proceedings 15</em>. Springer International Publishing, 2018.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=beb693a21d5b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/self-service-ml-with-relational-deep-learning-beb693a21d5b">Self-Service ML with Relational Deep Learning</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Market Basket Analysis Using High Utility Itemset Mining]]></title>
            <link>https://medium.com/data-science/market-basket-analysis-using-high-utility-itemset-mining-df233b297c0d?source=rss-4a7fc9f4bfd4------2</link>
            <guid isPermaLink="false">https://medium.com/p/df233b297c0d</guid>
            <category><![CDATA[marketing]]></category>
            <category><![CDATA[market-basket-analysis]]></category>
            <category><![CDATA[data-mining]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Laurin Brechter]]></dc:creator>
            <pubDate>Wed, 11 Sep 2024 16:58:12 GMT</pubDate>
            <atom:updated>2024-09-11T16:58:12.492Z</atom:updated>
            <content:encoded><![CDATA[<h4>Finding high-value patterns in transactions</h4><p><em>In this post, I will give an alternative to popular techniques in market basket analysis that can help practitioners find high-value patterns rather than just the most frequent ones. We will gain some intuition into different pattern mining problems and look at a real-world example. The full code can be found </em><a href="https://github.com/LaurinBrechter/PatternMining/blob/main/market_basket_analysis.ipynb"><em>here</em></a><em>. All images are created by the author.</em></p><h3>Introduction</h3><p>I have written a more <a href="https://medium.com/@brechterlaurin/customer-analytics-pattern-mining-on-clickstream-data-in-python-1bcd2de15a5d">introductory article</a> about pattern mining already; if you’re not familiar with some of the concepts that come up here, feel free to check that one out first.</p><p>In short, pattern mining tries to find patterns in data (duuh). Most of the time, this data comes in the form of (multi-)sets or sequences. In my last article, for example, I looked at the sequence of actions that a user performs on a website. In this case, we <strong>would</strong> care about the ordering of the items.</p><p>In other cases, such as the one we will discuss below, we do not care about the ordering of the items. We only list all the items that were in the transaction and how often they appeared.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/518/1*Fcrf3GfAgqbU07NgcNInkw.png" /><figcaption>Example Transacton Database</figcaption></figure><p>So for example, transaction 1 contained 🥪 3 times and 🍎 once. As we see, we lose information about the ordering of the items, but in many scenarios (as the one we will discuss below), there is no logical ordering of the items. This is similar to a bag of words in NLP.</p><h3>Market Basket Analysis</h3><p><em>Market Basket Analysis</em> (MBA) is a data analysis technique commonly used in retail and marketing to uncover relationships between products that customers tend to purchase together. It aims to identify patterns in customers’ shopping baskets or transactions by analyzing their purchasing behavior. The central idea is to understand the co-occurrence of items in shopping transactions, which helps businesses optimize their strategies for product placement, cross-selling, and targeted marketing campaigns.</p><h3>Frequent Itemset Mining</h3><p><em>Frequent Itemset Mining </em>(FIM) is the process of finding frequent patterns in transaction databases. We can look at the frequency of a pattern (i.e. a set of items) by calculating its support. In other words, the support of a pattern X is the number of transactions T that contain X (and are in the database D). That is, we are simply looking at how often the pattern X appears in the database.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/257/1*DLuZvrhObGXqFE1N6uqITA.png" /><figcaption>Definition of the support.</figcaption></figure><p>In FIM, we then want to find all the sequences that have a support bigger than some threshold (often called <em>minsup</em>). If the support of a sequence is higher than minsup, it is considered frequent.</p><h4>Limitations</h4><p>In FIM, we only look at the existence of an item in a sequence. That is, whether an item appears two times or 200 times does not matter, we simply represent it as a one. But we often have cases (such as MBA), where not only the existence of an item in a transaction is relevant but also how many times it appeared in the transaction.</p><p>Another problem is that frequency does not always imply relevance. In that sense, FIM assumes that all items in the transaction are equally important. However, it is reasonable to assume that someone buying caviar might be more important for a business than someone buying bread, as caviar is potentially a high ROI/profit item.</p><h3>High Utility Itemset Mining</h3><p>These limitations directly bring us to <em>High Utility Itemset Mining</em> (HUIM) and <a href="https://data-mining.philippe-fournier-viger.com/an-introduction-to-high-utility-quantitative-itemset-mining/"><em>High Utility Quantitative Itemset Mining</em></a> (HUQIM) which are generalizations of FIM that try to address some of the problems of normal FIM.</p><p>Our first generalization is that items can appear more than once in a transaction (i.e. we have a multiset instead of a simple set). As said before, in normal itemset mining, we transform the transaction into a set and only look at whether the item exists in the transaction or not. So for example the two transactions below would have the same representation.</p><pre>t1 = [a,a,a,a,a,b] # repr. as {a,b} in FIM<br>t2 = [a,b] # repr. as {a,b} in FIM</pre><p>Above, both these two transactions would be represented as [a,b] in regular FIM. We quickly see that, in some cases, we could miss important details. For example, if a and b were some items in a customer’s shopping cart, it would matter a lot whether we have <em>a</em> (e.g. a loaf of bread) five times or only once. Therefore, we represent the transaction as a multiset in which we write down, how many times each item appeared.</p><pre># multiset representation<br>t1_ms = {(a,5),(b,1)}<br>t2_ms = {(a,1),(b,1)}</pre><p>This is also efficient if the items can appear in a large number of items (e.g. 100 or 1000 times). In that case, we need not write down all the a’s or b’s but simply how often they appear.</p><p>The generalization that both the quantitative and non-quantitative methods make, is to assign every item in the transaction a utility (e.g. profit or time). Below, we have a table that assigns every possible item a unit profit.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/296/1*l1vMt7VrLaWQlF9HGWX7Lw.png" /><figcaption>Utility of Items</figcaption></figure><p>We can then calculate the utility of a specific pattern such as {🥪, 🍎} by summing up the utility of those items in the transactions that contain them. In our example we would have:</p><p>(3🥪 * $1 + 1🍎 * $2) +</p><p>(1 🥪 * $1 + 2🍎 * $2) = $10</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/518/1*Fcrf3GfAgqbU07NgcNInkw.png" /><figcaption>Transacton Database from Above</figcaption></figure><p>So, we get that this pattern has a utility of $10. With FIM, we had the task of finding frequent patterns. Now, we have to find patterns with high utility. This is mainly because we assume that frequency does not imply importance. In regular FIM, we might have missed rare (infrequent) patterns that provide a high utility (e.g. the diamond), which is not true with HUIM.</p><p>We also need to define the notion of a <em>transaction utility</em>. This is simply the sum of the utility of all the items in the transaction. For our transaction 3 in the database, this would be</p><p>1🥪 * $1 + 2🦞*$10 + 2🍎*$2 = $25</p><p>Note that solving this problem and finding all high-utility items is more difficult than regular FPM. This is because the utility does not follow the Apriori property.</p><h4>The Apriori Property</h4><p>Let X and Y be two patterns occurring in a transaction database D. The apriori property says that if X is a subset of Y, then the support of X must be at least as big as Y&#39;s.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/257/1*62-wFmxk6Qz5PDjoyYQ6VA.png" /><figcaption>Apriori property.</figcaption></figure><p>This means that if a subset of Y is infrequent, Y itself must be infrequent since it must have a smaller support. Let’s say we have X = {a} and Y = {a,b}. If Y appears 4 times in our database, then X must appear at least 4 times, since X is a subset of Y. This makes sense since we are making the pattern less general / more specific by adding an item which means that it will fit less transactions. This property is used in most algorithms as it implies that if {a} is infrequent all supersets are also infrequent and we can eliminate them from the search space [3].</p><p>This property does not hold when we are talking about utility. A superset Y of transaction X could have more or less utility. If we take the example from above, {🥪} has a utility of $4. But this does not mean we cannot look at supersets of this pattern. For example, the superset we looked at {🥪, 🍎} has a higher utility of $10. At the same time, a superset of a pattern won’t always have more utility since it might be that this superset just doesn’t appear very often in the DB.</p><h4>Idea Behind HUIM</h4><p>Since we can’t use the apriori property for HUIM directly, we have to come up with some other upper bound for narrowing down the search space. One such bound is called <em>Transaction-Weighted Utilization</em> (TWU). To calculate it, we sum up the transaction utility of the transactions that contain the pattern X of interest. Any superset Y of X can’t have a higher utility than the TWU. Let’s make this clearer with an example. The TWU of {🥪,🍎} is $30 ($5 from transaction 1 and $5 from transaction 3). When we look at a superset pattern Y such as {🥪 🦞 🍎} we can see that there is no way it would have more utility since all transactions that have Y in them also have X in them.</p><p>There are now various algorithms for solving HUIM. All of them receive a minimum utility and produce the patterns that have at least that utility as their output. In this case, I have used the <a href="https://www.philippe-fournier-viger.com/EFIM_JOURNAL_VERSION%20KAIS%202016.pdf">EFIM</a> algorithm since it is fast and memory efficient.</p><h3>Implementation</h3><p>For this article, I will work with the <a href="https://www.kaggle.com/datasets/aslanahmedov/market-basket-analysis/data">Market Basket Analysis</a> dataset from Kaggle (used with permission from the original dataset author).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/580/1*noJcAMZGJfEhj39rG8FhXw.png" /></figure><p>Above, we can see the distribution of transaction values found in the data. There is a total of around 19,500 transactions with an average transaction value of $526 and 26 distinct items per transaction. In total, there are around 4000 unique items. We can also make an <a href="https://en.wikipedia.org/wiki/ABC_analysis">ABC analysis</a> where we put items into different buckets depending on their share of total revenue. We can see that around 500 of the 4000 items make up around 70% of the revenue (A-items). We then have a long right-tail of items (around 2250) that make up around 5% of the revenue (C-items).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/567/1*S30izXD7mc3XjzIOYDvIUA.png" /></figure><h4>Preprocessing</h4><p>The initial data is in a long format where each row is a line item within a bill. From the BillNo we can see to which transaction the item belongs.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/941/1*iruCPTLBdbt2YfNQ5-qluA.png" /><figcaption>Initial Data Format</figcaption></figure><p>After some preprocessing, we get the data into the format required by <a href="https://github.com/UdayLab/PAMI">PAMI</a> which is the Python library we are going to use for applying the EFIM algorithm.</p><pre>data[&#39;item_id&#39;] = pd.factorize(data.Itemname)[0].astype(str) # map item names to id<br>data[&quot;Value_Int&quot;] = data[&quot;Value&quot;].astype(int).astype(str)<br>data = data.loc[data.Value_Int != &#39;0&#39;] # exclude items w/o utility<br><br>transaction_db = data.groupby(&#39;BillNo&#39;).agg(<br>    items=(&#39;item_id&#39;, lambda x: &#39; &#39;.join(list(x))),<br>    total_value=(&#39;Value&#39;, lambda x: int(x.sum())),<br>    values=(&#39;Value_Int&#39;, lambda x: &#39; &#39;.join(list(x))),<br>)<br><br># filter out long transactions, only use subset of transactions<br>transaction_db = transaction_db.loc[transaction_db.num_items &lt; 10].iloc[:1000]</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/695/1*1r_OqVgiZR7kGA-5rhE9yw.png" /><figcaption>Transaction Database</figcaption></figure><p>We can then apply the <a href="https://udaylab.github.io/PAMI/highUtilityPatternMining.html">EFIM</a> algorithm.</p><pre>import PAMI.highUtilityPattern.basic.EFIM as efim <br><br>obj = efim.EFIM(&#39;tdb.csv&#39;, minUtil=1000, sep=&#39; &#39;) <br>obj.startMine()                       #start the mining process <br>obj.save(&#39;out.txt&#39;)               #store the patterns in file <br>results = obj.getPatternsAsDataFrame()     #Get the patterns discovered into a dataframe <br>obj.printResults()   </pre><p>The algorithm then returns a list of patterns that meet this minimum utility criterion.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5LLd8Um408r2iQaoGLP_Uw.png" /></figure><p>This is the point at which a subject matter expert would have to look at these patterns to see if there is something worth taking a closer look at. We can for example see that the combination of VINTAGE BEAD PINK PURSE’ and ‘ANT COPPER RED BOUDICCA BRACELET’ seems to perform quite well. In that case, we might think about offering these items as a bundle or recommending them together.</p><h3>Conclusion</h3><p>In this blog post, we looked at classical FIM and its limitations. Especially when we care about the transaction value of a pattern and the potential revenue it can provide, it can be beneficial to use HUIM and mine patterns with high utility.</p><h3>Resources &amp; Further Reading</h3><ul><li>[1] <a href="https://www.youtube.com/watch?v=dty9ircNDqI&amp;list=PLQWbHaW8RsA1wND30_Re9V5-c8pr1H56H&amp;index=9&amp;ab_channel=PhilippeFournier-Viger">High Utility Itemset Mining</a></li><li>[2] <a href="https://www.youtube.com/playlist?list=PLQWbHaW8RsA1wND30_Re9V5-c8pr1H56H">The Pattern Mining Course</a></li><li>[3] <a href="https://www.philippe-fournier-viger.com/EFIM_JOURNAL_VERSION%20KAIS%202016.pdf">EFIM: A Fast and Memory Efficient Algorithm for High-Utility Itemset Mining</a></li><li>[4] <a href="https://www.coursera.org/learn/data-patterns?specialization=data-mining">Coursera Course on Pattern Mining</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=df233b297c0d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/market-basket-analysis-using-high-utility-itemset-mining-df233b297c0d">Market Basket Analysis Using High Utility Itemset Mining</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[An Introduction to Bayesian A/B Testing]]></title>
            <link>https://medium.com/data-science/an-introduction-to-bayesian-a-b-testing-37bb62b0f871?source=rss-4a7fc9f4bfd4------2</link>
            <guid isPermaLink="false">https://medium.com/p/37bb62b0f871</guid>
            <category><![CDATA[statistics]]></category>
            <category><![CDATA[marketing-analytics]]></category>
            <category><![CDATA[a-b-testing]]></category>
            <category><![CDATA[bayesian-statistics]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Laurin Brechter]]></dc:creator>
            <pubDate>Thu, 05 Sep 2024 17:18:14 GMT</pubDate>
            <atom:updated>2024-09-05T17:18:14.545Z</atom:updated>
            <content:encoded><![CDATA[<h4>Gain better insights from your data</h4><p>A/B testing, also known as split testing, allows businesses to experiment with different versions of a webpage or marketing asset to determine which one performs better in terms of user engagement, click-through rates, and, most importantly, conversion rates.</p><p>Conversion rates — the percentage of visitors who complete a desired action, such as making a purchase or signing up for a newsletter — are often the key metrics that determine the success of online campaigns. By carefully testing variations of a webpage, businesses can make data-driven decisions that significantly improve these rates. Whether it’s tweaking the color of a call-to-action button, changing the headline, or rearranging the layout, A/B testing provides actionable insights that can transform the effectiveness of your online presence.</p><p>In this post, I will show how to do Bayesian A/B testing for looking at conversion rates. We will also look at a more complicated example where we will look at the differences in changes of customer behavior after an intervention. We will also look at the differences when comparing this approach to a frequentist approach and what the possible advantages or disadvantages are.</p><h3>Comparing Conversion Rates</h3><p>Let’s say we want to improve upon our e-commerce website. We do so by exposing two groups of customers to two versions of our website where we e.g. change a button. We then stop this experiment after having exposed a certain number of visitors to both these versions. After that, we get a binary array with a 1 indicating conversion and a 0 if there was no conversion.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/659/1*qDfUTYjvHnOlFAZfy_ujbg.png" /><figcaption>Observed Data after A/B Test</figcaption></figure><p>We can summarize the data in a contingency table that shows us the (relative) frequencies.</p><pre>contingency = np.array([[obsA.sum(), (1-obsA).sum()], [obsB.sum(), (1-obsB).sum()]])</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/269/1*MgVQrT1qzbcEvlKKSM7SgA.png" /><figcaption>Contingency Table</figcaption></figure><p>In our case, we showed each variation to 100 customers. In the first variation, 5 (or 5%) converted, and in the second variation 3 converted.</p><h4>Frequentist Setting</h4><p>We will do a statistical test to measure if this result is significant or due to chance. In this case, we will use a Chi2 test which compares the observed frequencies to the ones that might be expected if there were no true differences between the two versions (the null hypothesis). For more information, one can look at <a href="https://medium.com/p/6599271a2c31">this blog</a> post that goes into more detail.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1023/1*tghvwKWpuML7bLTkK3_GjA.png" /></figure><p>In this case, the p-value does not fall under the threshold for significance (e.g. 5%) and therefore we cannot reject the null hypothesis that the two variants differ in their effect on the conversion rate.</p><p>Now, there are some pitfalls when using the Chi2 test that can make the insights gained from it erroneous. Firstly, it is very sensitive to the sample size. With a large sample size even tiny differences will become significant whereas with a small sample size, the test may fail to detect differences. This is especially the case if the calculated expected frequencies for any of the fields are smaller than five. In this case, one has to use some other test. Additionally, the test does not provide information on the magnitude or practical significance of the difference. When conducting multiple A/B tests simultaneously, the probability of finding at least one significant result due to chance increases. The Chi2 test does not account for this multiple comparisons problem, which can lead to false positives if not properly controlled (e.g., through Bonferroni correction).</p><p>Another common pitfall occurs when interpreting the results of the Chi2 test (or any statistical test for that matter). The p-value gives us the probability of observing the data, given that the null hypothesis is true. It does not make a statement about the distribution of conversion rates or their difference. And this is a major problem. We cannot make statements such as “the probability that the conversion rate of variant B is 2% is X%” because for that we would need the probability distribution of the conversion rate (conditioned on the observed data).</p><p>These pitfalls highlight the importance of understanding the limitations of the Chi2 test and using it appropriately within its constraints. When applying this test, it is crucial to complement it with other statistical methods and contextual analysis to ensure accurate and meaningful conclusions.</p><h4>Bayesian Setting</h4><p>After looking at the frequentist way of dealing with A/B testing, let’s look at the Bayesian version. Here, we are modeling the data-generating process (and therefore the conversion rate) directly. That is, we are specifying a likelihood and a prior that could lead to the observed outcome. Think of this as specifying a ‘story’ for how the data could have been created.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/351/1*a_kiT5otelfvIalV3SntKg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/269/1*53bIc0pkic6cR7tcSU3hdQ.png" /><figcaption>Bayes Formula</figcaption></figure><p>In this case, I am using the Python package <a href="https://www.pymc.io/welcome.html">PyMC</a> for modeling since it has a clear and concise syntax. Inside the ‘with’ statement, we specify distributions that we can combine and that give rise to a data-generating process.</p><pre>with pm.Model() as ConversionModel:<br>    # priors<br>    pA = pm.Uniform(&#39;pA&#39;, 0, 1)<br>    pB = pm.Uniform(&#39;pB&#39;, 0, 1)<br>    <br>    delta = pm.Deterministic(&#39;delta&#39;, pA - pB)<br>    <br>    obsA = pm.Bernoulli(&#39;obsA&#39;, pA, observed=obsA)<br>    obsB = pm.Bernoulli(&#39;obsB&#39;, pB, observed=obsB)<br>    <br>    trace = pm.sample(2000)</pre><p>We have pA and pB which are the probabilities of conversion in groups A and B respectively. With pm.Uniform we specify our prior belief about these parameters. This is where we could encode prior knowledge. In our case, we are being neutral and allowing for any conversion rate between 0 and 1 to be equally likely.</p><p>PyMC then allows us to draw samples from the posterior distribution which is our updated belief about the parameters after seeing the data. We now obtain a full probability distribution for the conversion probabilities.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/950/1*HansR3KX96fsei2EgsFhOQ.png" /><figcaption>Posterior Distributions for Conversion Rates</figcaption></figure><p>From these distributions, we can directly read quantities of interest such as credible intervals. This allows us to answer questions such as “What is the likelihood of a conversion rate between X% and Y%?”.</p><p>The Bayesian approach allows for much more flexibility as we will see later. Interpreting the results is also more straightforward and intuitive than in the frequentist setting.</p><h3>Model Arbitrary Data-generating Processes</h3><p>We will now look at a more complicated example of A/B testing. Let’s say we expose subjects to some intervention at the beginning of the observation period. This would be the A/B part where one group gets intervention A and the other intervention B. We then look at the interaction of the 2 groups with our platform in the next 100 days (maybe something like the number of logins). What we might see is the following.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/841/1*G4r_abIaZaaj0JXq1tbZ_Q.png" /></figure><p>We now want to know if these two groups show a meaningful difference in their response to the intervention. How would we solve this with a statistical test? Frankly, I don’t know. Someone would have to come up with a statistical test for exactly this scenario. The alternative is to again come back to a Bayesian setting, where we will first come up with a data-generating process. We will assume, that each individual is independent and its interactions with the platform are normally distributed. They have a switch point where they change their behavior. This switch point occurs only once but can happen at any given point in time. Before the switch point, we assume a mean interaction intensity of mu1 and after that an intensity of mu2. The syntax might look a bit complicated especially if you have never used PyMC before. In that case, I would recommend checking out their <a href="https://www.pymc.io/projects/docs/en/stable/learn.html">learning material</a>.</p><pre>with pm.Model(coords={<br>    &#39;ind_id&#39;: ind_id,<br>}) as SwitchPointModel:<br><br>    sigma = pm.HalfCauchy(&quot;sigma&quot;, beta=2, dims=&quot;ind_id&quot;)<br><br>    # draw a switchpoint from a uniform distribution for each individual<br>    switchpoint = pm.DiscreteUniform(&quot;switchpoint&quot;, lower=0, upper=100, dims=&quot;ind_id&quot;)<br><br>    # priors for the two groups<br>    mu1 = pm.HalfNormal(&quot;mu1&quot;, sigma=10, dims=&quot;ind_id&quot;)<br>    mu2 = pm.HalfNormal(&quot;mu2&quot;, sigma=10, dims=&quot;ind_id&quot;)<br>    <br>    <br>    diff = pm.Deterministic(&quot;diff&quot;, mu1 - mu2)<br><br>    # create a deterministic variable for the <br>    intercept = pm.math.switch(switchpoint &lt; X.T, mu1, mu2)<br><br>    obsA = pm.Normal(&quot;y&quot;, mu=intercept, sigma=sigma, observed=obs)<br><br>    trace = pm.sample()</pre><p>The model can then show us the distribution for the switch point location as well as the distribution of differences before and after the switch point.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/950/1*yP-qdC7PVheLFgGr28SA1A.png" /></figure><p>We can take a closer look at those differences with a forest plot.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/553/1*2wsfK2CoxifRmWJkR5hL_A.png" /></figure><p>We can nicely see how the differences between Group A (id 0 through 9) and Group B (10 through 19) are very much different where group B shows a much greater response to the intervention.</p><h3>Conclusion</h3><p>Bayesian Inference offers a lot of flexibility when it comes to modeling situations in which we do not have a lot of data and where we care about modeling uncertainty. Additionally, we have to make our assumptions explicit and think about them. In more simple scenarios, frequentist statistical tests are often simpler to use but one has to be aware of the assumptions that come along with them.</p><p><em>All code used in this article can be found on my </em><a href="https://github.com/LaurinBrechter/BayesianInference/blob/main/hierarchical_switchpoint.ipynb"><em>GitHub</em></a><em>. Unless otherwise stated, all images are created by the author.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=37bb62b0f871" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/an-introduction-to-bayesian-a-b-testing-37bb62b0f871">An Introduction to Bayesian A/B Testing</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Useful Visualizations and Statistics for Marketing Analytics]]></title>
            <link>https://medium.com/@brechterlaurin/useful-visualizations-and-statistics-for-marketing-analytics-58980a9c6d06?source=rss-4a7fc9f4bfd4------2</link>
            <guid isPermaLink="false">https://medium.com/p/58980a9c6d06</guid>
            <category><![CDATA[marketing-analytics]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[visualization]]></category>
            <category><![CDATA[data-analytics]]></category>
            <category><![CDATA[matplotlib]]></category>
            <dc:creator><![CDATA[Laurin Brechter]]></dc:creator>
            <pubDate>Fri, 30 Aug 2024 15:40:15 GMT</pubDate>
            <atom:updated>2024-10-04T19:32:20.348Z</atom:updated>
            <content:encoded><![CDATA[<p>In this article, I have compiled a list of useful visualizations and statistics for marketing/customer analytics. I will provide a short description and explanation for each of them and describe the scenarios in which they are useful. I have tried to focus on charts that are specific to marketing analytics and ones that are more interesting than your standard bar/line/scatter plot.</p><p><em>Disclaimer: This article is a work-in-progress and I will add new visualizations and statistics as time goes by (and as I discover them myself). You can find the full code that is used to generate them on my </em><a href="https://github.com/LaurinBrechter/BayesianInference/blob/main/marketing_visualizations.ipynb"><em>GitHub</em></a><em>. Many of the example visualizations are based on synthetic data and some of them have been generated with ChatGPT.</em></p><p><em>Update 2024/10/04:</em> I have made an <a href="https://www.laurin-brechter.com/de/blog/marketing-analytics/useful-visualizations">interactive version</a> of this blog post on my personal website, converting the static images of the plotly graphs to PlotlyJS. Feel free to check it out!</p><h3>Customer Journey Mapping</h3><p>The <em>customer journey</em> (CJ) can be understood as the sequence of touchpoints, the customer goes through before establishing a commercial relationship with the company. In general, we want to graph the CJ from a more abstract, high-level point of view. That is, we don’t care about every single mouse click the customer made but rather about the rough stages through which they transitioned.</p><p>We will plot different touchpoints in one dimension and frequencies in the other dimension. For this type of chart, we need the aggregated interactions of our customer base grouped by touchpoints. The more common way of displaying the CJ is via a so-called funnel chart (in that case we might also call it a marketing funnel).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1022/1*CK8LitGJmwepMo2h5VGZCw.png" /><figcaption>Mapping it with a funnel</figcaption></figure><p>An alternative representation of the CJ can be done via Sankey plots. These show not only the positive cases (i.e. people who continued to the next stage) but also where the people who dropped out went instead. Given that I used the same data for both diagrams, there is no big difference but if we care about where people who stopped interacting went afterwards, a Sankey plot might be the better choice. This would also be the case in special situations where not everyone has to go through all the stages but rather there are different starting points (and maybe even different ‘final’ interactions).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gllgGh3ozCItoz2uzjnGKQ.png" /><figcaption>Mapping the Customer Journey with a Sankey Plot</figcaption></figure><p>In the figure below, we can see that there are different ways in which people might have gotten to the landing page and also how they continued afterward. Very quickly we can see that a lot of the people coming from the email campaign churn between the landing page and the product page and therefore very few of them make it to the subsequent touch points. This is also a good way of comparing the CJ of different cohorts (more on cohorts later) or how the CJ improved over time.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1022/1*ucO1FohTyPoNNm5XLh0rUw.png" /></figure><p>Especially when wanting to compare the CJ across different dimensions (e.g. customer groups, demographics, or time) it can become inefficient to do so via the visualizations above. It is also hard to read precise percentages from the chart and compare them with each other.</p><p>Because of this, we can also summarize the information with some metrics and then plot that metric across dimensions (or just put the metric as-is on a dashboard as a KPI). Firstly, we can look at the <strong>overall conversion rate.</strong> This is the percentage of users who complete the desired action (e.g., making a purchase, signing up) compared to the total number of users who started the journey. This is a very high-level metric that can be plotted over time to see general trends.</p><p>We can also look at this rate for each stage to see where the bottlenecks occur. When we take one minus the conversion rate, we get the <strong>drop-off rate</strong>. This is just the percentage of customers that abandon the journey (or abandon it at a given stage). The drop-off rate at the first touch point is also called the <strong>bounce rate</strong> (in our case this would be the drop-off rate at the landing page). In our case, we can see that although much of the blue stream makes it through the landing page to the product page (low bounce rate), most of them drop-off at the product page and don’t make it to the cart. The drop-off rate at the cart is also often called the <strong>cart abandonment rate. </strong>A high rate may indicate friction in the checkout process.</p><p>If there is a more complex customer journey with lots of branches and different ways of getting to the final touchpoints, one can also consider looking at the most common or least common paths. Also, in some cases, it might be of interest to know how fast customers reach the different touch points and how much time they spend at them. Long durations might indicate difficulty in completing the action (or again, some friction), while short times might suggest efficient user flow or low engagement. In this case, the correct explanation for the behavior depends on the specific business context.</p><p>Since there is always room for improvement in the CJ and we have limited resources (e.g. limited marketing spend), one needs to have some way of deciding for which area of the funnel (i.e. touch point) or which customer group to optimize first. For this, we can look at measures such as customer acquisition cost, average transaction value and customer lifetime value (CLV) to see in which areas an improvement of the funnel has a high return on investment (ROI). As an example, if we have a high bounce rate at one of our touch points but only people with a very low CLV are bouncing off, this might now be as bad as losing customers that have a high CLV.</p><h3>Pareto Charts</h3><p>A Pareto chart plots the cumulative percentage as a function of some continuous or discrete variable. In our case, we have different marketing channels on the x-axis and the cumulative relative number of conversions on the y-axis. This enables us to quickly see that email marketing and social media make up around 70% of all conversions (also called the vital few) whereas the rest of the channels (called the useful many) make up around 30%.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/627/1*2heprHSrpoPTEnBbXTrZZw.png" /></figure><p>The advice then often is to focus on these vital few. But what about this chart tells us that focussing on the vital few is the right thing to do? There is nothing causal that establishes that investing resources in email marketing will lead to an increase in conversions. It could just be that further efforts in one of these vital few would only give us marginal gains whereas other channels still have some potential or might even be ‘low-hanging fruit’. Although the Pareto chart is a nice tool for quickly giving us a snapshot of the current situation it does not directly give us actionable insights. On the other side, it can give a quick overview and could for example show us that we have a long right tail with little to no contribution. In our case, this would mean having lots of marketing channels on the right of the spectrum that contribute very little to the conversions. In that case, one can do a deeper analysis to see if having these channels is worth the effort of maintaining them (maybe there are interaction effects between channels). In either case, the Pareto chart can create awareness around such issues.</p><h3>(Cohort) Retention Analysis</h3><p>Retention analysis is the process of tracking and analyzing customer behavior to understand how customers continue to engage with a company, product, or service after their initial interaction. It is a critical component of understanding customer behavior and ensuring long-term business success since acquiring new customers is often more expensive than retaining the current ones.</p><p><em>Since the following section is part of another notebook/project you can find the code to reproduce the figures </em><a href="https://github.com/LaurinBrechter/BayesianInference/blob/main/customer_usage_dynamics.ipynb"><em>here</em></a><em>.</em></p><p>Firstly, we can look at the chart below to get a general feeling for how customers interact with the product after signing up. We have the relative time (i.e. periods since signing up) on the x-axis and some measure for the intensity of interaction on the y-axis. Every line represents a single customer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/844/1*6ZZn31rNMYuQFAhiOrgCWw.png" /><figcaption>Showing Retention for Different Customers</figcaption></figure><p>This is an example with synthetic data but we can see that some customers have declining interaction rates (e.g. red) while others show a pattern of constant interaction followed by a sudden increase in interaction after some time (e.g. green).</p><p>An obvious drawback is that we can never plot our entire customer base since the chart gets messy at around ten customers. If we want to measure our entire customer base we will probably have to calculate some metrics that summarize the charts seen above. One such method can be seen in the chart below. For every customer, we fit a first-order polynomial (i.e. just intercept and slope). Note that this is probably not a very good description of the customer behavior. It would be especially bad at extrapolating since customer activity would be predicted to rise indefinitely as time increases. This can be seen in the green line below where the activity plateaus but from the line we would predict an increase.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/625/1*r4naioLeZGHoBdn26vJaWQ.png" /><figcaption>Same Chart with First Order Polynomials</figcaption></figure><p>Nonetheless, by looking at the slopes, we can estimate how many of our customers show an increasing interaction strength and for how many it declines.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/609/1*SpMWU7DknfanHNRNaR4olw.png" /></figure><h3>Customer Segmentation</h3><p>In many cases, we don’t know along which dimensions we want to look at our customers. That is, we don’t already have some way of grouping our customers but instead, we want to find such a grouping of customers within the data. This post won’t go into the details of the technical process of customer segmentation, I just wanted to show one chart I found interesting for visualizing the results of a clustering.</p><p>For each segment (i.e. cluster) the radar chart shows the average value of each attribute. We can then try and find a precise label for the group. After that, we have created the foundation for being able to specifically target each of these groups.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*YM7PqyYRDtcMaW0978THoQ.png" /><figcaption>Radar Chart Showing the Results of a Clustering</figcaption></figure><h3>Further Reading</h3><p>[1] <a href="https://www.kaggle.com/code/mustafacicek/detailed-marketing-cohort-pareto-rfm-forecast?scriptVersionId=78275259">Detailed Marketing: Cohort, Pareto, RFM, Forecast</a></p><p>[2] <a href="https://d1wqtxts1xzle7.cloudfront.net/34976842/Iryna_Kurylets_MT_2011-2012-libre.pdf?1412304702=&amp;response-content-disposition=inline%3B+filename%3DWEB_analytics_and_performance_evaluation.pdf&amp;Expires=1724925488&amp;Signature=Qkl7OibpxgTQ5c7CvivsIg4WQ7PDqrThEV4wkyYKFwnlSaNIw-dbPM8sy~RVvzpbRSgJdEnkO8Q9bnawPKLszc~EMWAEfhZtfC6ULeKCUF3NKmFRFT1CnL9hSqAUQj~-qPXSfL2ftME-evS9~CrVzdgcLbemTM4UU0UrqqnCM~tTjNaGv5cDn2o2ZQ3K6fbYgiB1z7gSlEDTKvRQUQRapz2Q4ZJ2SZCHY90eSlTZQgGtDPP60ZkDzZiVpB8Vl6lhLeXz6oV4fAcu6EA2pa4YV4IhVzoHUMwwUsbOTvTLq8nmsX9t3ga~gHo1ig7alWg1Je7KsUAihBANwfSchPV~Vw__&amp;Key-Pair-Id=APKAJLOHF5GGSLRBV4ZA">WEB-analytics and performance evaluation of internet marketing</a></p><p>[3] Unilever Digital Marketing</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=58980a9c6d06" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to use PostgresDB as your One-Stop RAG Solution]]></title>
            <link>https://medium.com/@brechterlaurin/how-to-use-postgresdb-as-your-one-stop-rag-solution-8536ef7d762e?source=rss-4a7fc9f4bfd4------2</link>
            <guid isPermaLink="false">https://medium.com/p/8536ef7d762e</guid>
            <category><![CDATA[postgres]]></category>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[vector-database]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[chatgpt]]></category>
            <dc:creator><![CDATA[Laurin Brechter]]></dc:creator>
            <pubDate>Fri, 12 Jan 2024 18:43:56 GMT</pubDate>
            <atom:updated>2024-01-12T18:43:56.869Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/411/1*Ki7Oh750vR_yYE8EhAZANA.jpeg" /></figure><h4>Introduction</h4><p>Many RAG setups currently need a separate dedicated vector database such as Weaviate or ChromaDB for doing vector search. In this post, I will try to show the benefits of using a relational database (in this case postgres) for that purpose. More specifically, we will use the pg vector extension of postgres to add vector db functionality to postgres. Let’s get started right away with the code to see how we can ingest documents into the DB and retrieve them.</p><h4>The Data Model</h4><p>We don’t want to write the raw SQL as text when interacting with the database. Instead, we can use an <em>Object-relational Mapper</em> (ORM) to describe the objects in our database and enable us to interact with them through Python. In thise case, we will be using <a href="https://www.sqlalchemy.org/">SQLALchemy</a>. Our first model will be that for storing documents in our db.</p><p>We keep track of the filename, filehash as well as the user id. This enables us to manage the documents and check whether a user has already uploaded a file with the same name / contents. A document will be split up into multiple chunks, hence we have the relation to the “Chunk” table.</p><pre>from sqlalchemy.orm import mapped_column, Mapped, relationship<br>from sqlalchemy import Column, Integer, String, DateTime, func, ForeignKey, Boolean, Float<br><br>class Document(Base):<br>  __tablename__ = &#39;document&#39;<br>  id = Column(Integer, primary_key=True, autoincrement=True, unique=True)<br>  filename = Column(String)<br>  filehash = Column(String)<br>  created_at = Column(DateTime, default=func.now())<br>  chunks : Mapped[List[&quot;Chunk&quot;]] = relationship(back_populates=&quot;document&quot;, cascade=&quot;all, delete-orphan&quot;)<br>  user_id = Column(Integer)</pre><p>As per usual, we are splitting the document up into chunks. This is done because firstly, giving the entire document as context may exceed the context length of the LLM and secondly, we are saving tokens (and therefore money) by not passing the entire document since most likely only a small part of it is relevant anyways.</p><p>Below, we are creating the schema for the “chunk” table. The “content” attribute contains the actual text content of the chunk and is later on passed to the LLM. We are also referencing the document id as a foreign key. And, most importantly, we are creating a “Vector” column, which holds the actual embedding of the chunk. With the number in brackets, we specify the dimensionality of the embedding. In our case, we choose 1536 which is the dimensionality for the OpenAI embeddings.</p><pre>from pgvector.sqlalchemy import Vector<br><br>class Chunk(Base):<br>    __tablename__ = &#39;chunk&#39;<br>    id = Column(Integer, primary_key=True, autoincrement=True)<br>    content = Column(String)<br>    page = Column(Integer, nullable=True)<br>    <br>    document: Mapped[Document] = relationship(back_populates=&quot;chunks&quot;)<br>    doc_id: Mapped[int] = mapped_column(ForeignKey(Document.id))<br>    embedding = Column(Vector(1536), nullable=True)</pre><h4>Inserting data</h4><p>We can now insert data by creating “Chunk” and “Document” objects in Python and passing them to the db. For this, we can define a function that, given a document, ingests that document in the database. As you can see below, we are using Llama Index to read the document and split it up into chunks. This is because the SimpleDirectoryReader allows us to get started very quickly and can process lots of different filetypes without having to specify anything.</p><p>We then split up the document into chunks of equal length. After that, we are passing the list of chunks to openai, thereby obtaining an embedding for each chunk. Lastly, we are adding the document along with the chunks to the database. By passing the list of chunks as an argument to the document, we are ensuring that they are pointing to the right document (i.e. have the right document id).</p><pre>from llama_index.schema import Document as LlamaDocument<br>from llama_index import SimpleDirectoryReader<br>from llama_index.node_parser import SentenceSplitter<br><br>def ingest_document(openai_client: OpenAI, db:Session, filepath:str, user_id:int, overwrite:bool=False):<br>    <br>    filename = filepath.split(&quot;/&quot;)[-1]<br><br>    reader = SimpleDirectoryReader(<br>            input_files=[filepath]<br>    )<br><br>    text_splitter = SentenceSplitter(chunk_size=512, chunk_overlap=10)<br><br>    docs: list[LlamaDocument] = reader.load_data()<br>    contents = &quot; &quot;.join([doc.get_content() for doc in docs])<br><br>    doc_hash = hashlib.sha256(contents.encode()).hexdigest()<br>    chunks = text_splitter.split_text(contents)<br>    embeddings = get_embedding(openai_client, chunks)<br><br>    db.add(Document(<br>      filename=filename,<br>      filehash=doc_hash,<br>      user_id=user_id,<br>      chunks=[<br>          Chunk(<br>              content = chunks[i],<br>              embedding = embeddings[i].embedding<br>          )<br>         for i in range(len(chunks))]<br>    ))<br><br>    db.commit()</pre><h4>Querying Data</h4><p>After having inserting some documents, we can start querying our new “vector database”. We will start by embedding the query string and then simply perform a SQL query against our “chunk” table.</p><pre>def query_db(db:Session, client:OpenAI, query_str:str, top_k:int=5):<br><br>    emb = get_embedding(client, query_str)[0].embedding<br>    <br>    res = db.scalars(<br>        select(Chunk)<br>        .order_by(Chunk.embedding.l2_distance(emb))<br>        .limit(top_k)<br>    ).all()<br><br>    return res</pre><p>The actual similarity search happens within the “.order_by” statement. We are instructing the database to order the chunks descending by their L2 distance to the embedding that we are passing to the function. Additionally, we are only returning the k chunks whose embedding is most similar to that of our query. In this statement, we could include additional logic such as filtering on other criteria such as user_id.</p><h3>Conclusion</h3><p>The great thing about using postgres here, is that we don’t have to familiarize ourselves with some new query language. Instead, we can use the same old SQL that we always used.</p><p>Another advantage is that we don’t have to maintain a separate database in our system since we can simply use postgres as a vector database but also for all the normal transactional stuff that we would normally use it for. This makes your system architecture much more simple.</p><p>Thirdly, we are benefiting from over <a href="https://www.youtube.com/watch?v=MDxEXKkxf2Q&amp;t=781s&amp;ab_channel=AIEngineer">30 years of development</a> that Postgres has now undergone. We get all the benefits of using a relational database such as ACID compliance, database GUIs, transaction support and full text search.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8536ef7d762e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Putting Bayesian Models into Production]]></title>
            <link>https://medium.com/@brechterlaurin/putting-bayesian-models-into-production-1efae307a138?source=rss-4a7fc9f4bfd4------2</link>
            <guid isPermaLink="false">https://medium.com/p/1efae307a138</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[bayesian-statistics]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[Laurin Brechter]]></dc:creator>
            <pubDate>Mon, 28 Aug 2023 15:18:26 GMT</pubDate>
            <atom:updated>2023-08-28T15:18:26.704Z</atom:updated>
            <content:encoded><![CDATA[<p><em>In this post, I will highlight some of the things to consider when putting Bayesian models into production and how they compare to ‘classical’ ML models. The full code can be found in </em><a href="https://github.com/LaurinBrechter/BayesianInference/blob/main/production_bayes.ipynb"><em>GitHub</em></a><em>.</em></p><h3>Introduction</h3><p>Bayesian modeling is getting a lot of traction in the data science community. This is especially the case in areas such as marketing where models for e.g. customer lifetime valutation (CLV) or marketing mix modeling (MMM) are getting more and more ‘Bayesian’.</p><p>This post will show, what we need to consider when we actually want to put these models into production. There are several questions that arise.</p><ul><li>How can we obtain predictions on unseen data from a Bayesian model?</li><li>How and when do we retrain the model?</li><li>How can we get priors into our model?</li><li>How do we handle new categorical values?</li></ul><p>All of these will be explained through an example and with the help of the Python library PyMC which we will use for doing the actual modeling.</p><h3>Bayes in Production</h3><p>In the example, we have a store which wants to predict the amount that customers spend on any given visit. Since this is not an adhoc analysis, they need to predict that figure continuously.</p><h4>Data Generating Process</h4><p>The function below simulates N visits to our store where each visit yields a transaction. First, a customer for that transaction is picked at random. We can then get the amount of that transaction as a combination of the feature x and the customer’s income with some coefficients (who are unknown to us of course).</p><pre>def generate_data(N, n_cust, prob_new=0, customer_ids=None, income=None):<br>    &quot;&quot;&quot;<br>     generates either new data or continues to generate data.<br>    &quot;&quot;&quot;<br><br>    if customer_ids is None:<br>        customer_ids = np.arange(0,n_cust)<br>    if income is None:<br>        income = np.random.normal(0,3, len(customer_ids))<br><br>    transactions = []<br><br>    for t in range(N):<br>        transaction = {}<br>        cust = np.random.choice(customer_ids)<br>        x = np.random.normal()<br>        spend = 0.5 * income[cust] + x * 0.3 + np.random.normal(0,1)<br>        transaction[&#39;cust_id&#39;] = cust<br>        transaction[&#39;spend&#39;] = spend<br>        transaction[&#39;x&#39;] = x<br>        transactions.append(transaction)<br><br>        # generate a new customer with some probability<br>        if np.random.random() &lt; prob_new:<br>            income = np.append(income, np.random.normal(3))<br>            customer_ids = np.append(customer_ids, max(customer_ids)+1)<br><br>    return pd.DataFrame(transactions), income, customer_ids</pre><p>Note that the feature x is not related to the customer. Also, we never get to see the customer’s income directly. It only manifests itself through the spend. At each transaction, there’s also a small chance that a new customer will be generated. In that case, we also generate some income for them. That customer could then appear as the customer in the subsequent transactions. Note that for simplicity, I assumed both spend an income to be possibly negative, since the focus of this article is not the modeling itself but rather how we can use the model in production.</p><p>Below is also a graph that shows the average spend of each customer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/546/1*4vDXY_fZRcxlTdt97NDhrA.png" /></figure><h4>The Model</h4><p>We define a relatively simple PyMC model. Every customer gets their unique intercept that contributes to the expected spend of the transaction. At the end, we perform inference by setting up two markov chains from which we draw 1000 times each.</p><pre>with pm.Model() as model:<br><br>    obs = pm.MutableData(&#39;obs&#39;, df.spend.values)<br>    x = pm.MutableData(&#39;x&#39;, df.x.values)<br>    cust_id = pm.MutableData(&quot;cust_id&quot;, df.cust_id.values)<br><br>    sig = pm.HalfNormal(&quot;sig&quot;, sigma=10)<br>    beta = pm.Normal(&quot;beta&quot;, mu=0, sigma=10)<br>    alpha = pm.Normal(&quot;alpha&quot;, mu=0, sigma=10)<br>    cust_exp = pm.Normal(&quot;cust_exp&quot;, mu=0, sigma=5, shape=int(max(df.cust_id.values)+1))<br>    y = pm.Normal(&quot;y&quot;, mu=cust_exp[cust_id] + beta*x, sigma=sig, observed=obs)<br><br>    trace = pm.sample(1000)</pre><p>It is also important, that we wrap our observed data in the <a href="https://www.pymc.io/projects/docs/en/stable/api/generated/pymc.Data.html">pm.MutableData</a> object. This is such that we can change the values later on and get predictions for new data points.</p><h4>Getting Predictions from a Bayesian Model</h4><p>In normal ML models, we can simply get a prediction by running the data through some equation (e.g. regression) or down a tree (e.g. a decision tree) (highly oversimplified). Predictions from a Bayesian model are obtained by drawing samples from the posterior distribution of the model’s parameters given the observed data. This process is often carried out using markov mhain monte carlo (MCMC) methods or other Bayesian sampling techniques. We can then use these samples to generate predictions for new or unobserved data points. This approach provides not only point predictions but also quantifies the uncertainty associated with the predictions, making it a powerful tool for decision-making and analysis.</p><p>In PyMC, we can do that via the code below. The trace object contains the model parameters that we obtained from ‘fitting’ the model and we will now also add our predictions to it.</p><pre>with model:<br>    pm.sample_posterior_predictive(trace, extend_inferencedata=True)</pre><p>Note that the samples from the posterior predictive are a 3-dimensional numpy array. In our case, the array has the shape (2,1000,100). This is because we ran two parallel chains with 1000 samples each and we have 100 observations.</p><pre>trace.posterior_predictive[&quot;y&quot;].values.shape<br><br>&gt;&gt;&gt; (2,1000,100)</pre><p>If we simply wanted point estimates, we could average over the first two dimensions like below.</p><pre>trace.posterior_predictive[&quot;y&quot;].values.mean(axis=0).mean(axis=0)</pre><p>That way, we get one array with 100 values that are the point predictions. But, Bayesian inference excels at quantifying uncertainty. It is also possible, to obtain uncertainty estimates for a single observation as shown below where we plot the distribution of predicted values for the first observation.</p><pre>fig, ax = plt.subplots()<br>ax.hist(trace.posterior_predictive[&quot;y&quot;].values.mean(axis=0)[:,0], bins=50)<br>ax.axvline(df.spend.values[0], color=&quot;red&quot;)<br>ax.set(title=&quot;Posterior predictive on first data point&quot;, xlabel=&quot;spend&quot;, ylabel=&quot;Frequency&quot;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/562/1*tEQL037apoiTrGxKfoW6Bg.png" /></figure><p>Above, we got predictions for observations that we already saw during inference. When we want to obtain predictions for unseen (out-of-sample) data, we need to first replace the unseen values with our old ones like below. Since we wraped our variables with pm.MutableData in our model specification, we can now <a href="https://www.pymc.io/projects/docs/en/stable/api/generated/pymc.set_data.html">set the data</a> to new values and then get predictions for those.</p><pre>with model:<br>    pm.set_data(<br>        {&quot;x&quot;: df_new.x.values, <br>        &quot;cust_id&quot;: df_new.cust_id.values, <br>        &quot;obs&quot;: df_new.spend.values<br>        })<br>    post_pred_new = pm.sample_posterior_predictive(trace)</pre><p>Importantly, we use the trace that we obtained from the ‘old’ data since it contains the parameters we can now use to predict the new data. post_pred_new now contains the predictions for out-of-sample data. This only works if the set of customers seen during training is a superset as that in the out-of-sample data (meaning that there are no new customers in the new data).</p><h4>Retraining</h4><p>In classical ML, models can be retrained with new data, but this often involves starting from scratch and re-optimizing the parameters (there are of course also many models such as neural networks where this is not the case).</p><p>Bayesian models can be easily updated with new data using the same Bayesian principles. The posterior from the previous training session becomes the prior for the new training session. This process is called <em>sequential learning</em>.</p><p>Let us for now consider, that we always have the same set of customers (we will see how to deal with new customer later). In that case, when generating the data we have to set prob_new to 0. What we now have to do, is to create a new model with priors set equal to the posterior of the old model. First we obtain point estimates for the posterior of our ‘old’ model.</p><pre>prior_beta_new = trace.posterior[&quot;beta&quot;].mean().values<br>prior_cust_exp_new = trace.posterior[&quot;cust_exp&quot;].mean(axis=0).mean(axis=0).values<br>prior_alpha_new = trace.posterior[&quot;alpha&quot;].mean().values</pre><p>We get one value for the prior of the alpha and beta parameters and one parameter per customer. After that, we build a new model in which we make some slight changes. Firstly, we use the new data (df_new). Secondly, instead of using our initial priors, we use the posterior of the old model as our priors. We can then again sample from the model using only the new data while still retaining the information from the first model run (via the priors).</p><pre>with pm.Model() as new_model:<br><br>    obs = pm.MutableData(&#39;obs&#39;, df_new.spend.values)<br>    x = pm.MutableData(&#39;x&#39;, df_new.x.values)<br>    cust_id = pm.MutableData(&quot;cust_id&quot;, df_new.cust_id.values)<br><br>    sig = pm.HalfNormal(&quot;sig&quot;, sigma=10)<br>    beta = pm.Normal(&quot;beta&quot;, mu=prior_beta_new, sigma=10)<br>    alpha = pm.Normal(&quot;alpha&quot;, mu=prior_alpha_new, sigma=10)<br><br>    cust_exp = pm.Normal(&quot;cust_exp&quot;, mu=prior_cust_exp_new, sigma=5)<br>    y = pm.Normal(&quot;y&quot;, mu=cust_exp[cust_id] + beta*x, sigma=sig, observed=obs)<br>        <br>    trace_new = pm.sample(1000)</pre><p>We could of course repeat this process now each time new data comes in. In practise, we would of course also turn the code above into a function and wrap it into an API, but the principles stay the same.</p><h4>Handling new Categories</h4><p>Oftentimes, there are categorical features, where we will constantly see new values. For example, say we want to predict the transaction value based on the customer. One option would be to use characteristics of the customer such as their age or their income. But in our case, we do not have this information. The only thing we have is a customer id that associates a purchase with a customer such as in the table below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/203/1*ISqH4ea5vdLGkI1kfkNolA.png" /></figure><p>We usually handle categorical features by means of one-hot encoding. That is, we create one new column in our feature matrix for each distinct value that we have. If the cardinality of the feature is too high (i.e. too many distinct values) this approach might be too costly, as the feature matrix becomes very sparse. Below is an example of one-hot encoding the customer id for historical data.</p><pre>oc = OneHotEncoder()<br>ex = pd.DataFrame(oc.fit_transform(df[[&#39;cust_id&#39;]]).toarray())<br>ex[&quot;cust_id&quot;] = df[&quot;cust_id&quot;]</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/549/1*p23j76_mjCm_nDbakoOomg.png" /><figcaption>Customer id one-hot encoded</figcaption></figure><p>In our data-to-day operations, customers visit the store, and we could easily transform a customer id into the one-hot representation as follows and then run a prediction.</p><pre>oc.transform(np.array([[8]])).toarray()<br><br>&gt;&gt;&gt; array([[0., 0., 0., 0., 0., 0., 0., 0., 1., 0.]])</pre><p>But the problem is then, what happens if a new, unseen customer (e.g. customer id = 10) enters the store? We only had customer id 0 through 9 in our training data so we cannot represent her with our feature matrix.</p><pre>oc.transform(np.array([[10]])).toarray()<br><br>&gt;&gt;&gt; ValueError: Found unknown categories [10] in column 0 during transform</pre><p>We would then need to refit our one-hot encoder which would yield an extra column for the additional customer. But this also means that we need to retrain our model since we have an extra feature. Another option would be to flag all customers not seen during training as ‘unknown’ but this obviously throws away a lot of information.</p><p>In the Bayesian setting, we also have to make some adjustments to our model in order to accomodate new customers. Specifically, we need to add coefficients for our new customers. For example, if we had 10 different customers in our training data and then two new appear, we need to add two new coefficients to accomodate them. Therefore, we only need to change one line when creating our new model. So, we would add coefficients to our model equal to the number of new customers.</p><pre>n_total = max(np.concatenate([df.cust_id.values, df_new.cust_id.values]))+1<br>n_old = df.cust_id.nunique()<br><br>n_new = n_total-n_old<br><br># add uninformative priors for the new customers<br>prior_cust_exp_new = np.append(prior_cust_exp_new, [0]*n_new)</pre><p>We can then run inference on the model as before. It is also important to note that in our case, we assumed uninformative priors for all customers. If we had any reason to assume that a customer might have a higher/lower spend (a priori), we could also set the prior for the coefficient differently. So instead of having e.g. [0,0,0] for three new customers, we might have [3,0,-3] if we think that the new customer 1 will spend more, the 2nd neutral and the third less than normal.</p><p>In any case, we are still being efficient in the sense that we only need to train our model on new, unseen data when we are in an online setting. We do not need the old data when retraining the model, instead, we can simply give the parameters we obtained from old runs.</p><p>This is of course only helpful if we constantly observe new categories. If we for example have a feature like marital status, we are not expecting to constantly see new distinct values and one-hot encoding will be totally fine.</p><h3>Conclusion</h3><p>In this post, I showed some examples of how we can use Bayesian models in production and what things we need to consider. One needs to always look at their specific business context to see if Bayesian modeling makes sense. Although it is usually more computationally expensive than classical ML, there can be situations in which sequential learning turns out to be very useful especially if we want to make small, incremental changes to our model.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1efae307a138" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Customer Analytics: Pattern Mining on Clickstream Data in Python]]></title>
            <link>https://medium.com/@brechterlaurin/customer-analytics-pattern-mining-on-clickstream-data-in-python-1bcd2de15a5d?source=rss-4a7fc9f4bfd4------2</link>
            <guid isPermaLink="false">https://medium.com/p/1bcd2de15a5d</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[frequent-pattern-mining]]></category>
            <category><![CDATA[data-mining]]></category>
            <category><![CDATA[marketing-analytics]]></category>
            <dc:creator><![CDATA[Laurin Brechter]]></dc:creator>
            <pubDate>Mon, 21 Aug 2023 15:36:28 GMT</pubDate>
            <atom:updated>2023-08-21T15:36:28.424Z</atom:updated>
            <content:encoded><![CDATA[<p><em>tl;dr In this post I show how we can use raw clickstream data and data mining to find patterns in the online user behavior of customers of an ecommerce site. We will cover the full workflow, going from raw API data to results.</em></p><p>The full code can be found on <a href="https://github.com/LaurinBrechter/Reinforcement-Learning/blob/main/pattern_mining_matomo.ipynb">GitHub</a>.</p><h3>Introduction</h3><p>In this post, I will look at techniques that can be used for pattern mining on clickstream data. Our general goal is to find patterns in the ways that customers interact with a website.</p><h4>Clickstream Data</h4><p>A clickstream refers to the sequence of interactions that a user has with a website, application, or digital platform. It includes every action taken by the user, such as clicking on links, buttons, or images, as well as any other interactions like submitting forms, scrolling, and navigating between pages. Essentially, a clickstream is a record of the user’s digital journey, capturing their behavior and engagement with the digital content.</p><p>Clickstream data is valuable for understanding user behavior, preferences, and patterns. By analyzing clickstreams, businesses and website owners can gain insights into how users navigate their platforms, which pages are most popular, where users tend to drop off, and what actions lead to conversions or desired outcomes. This data can be used for improving user experience, optimizing content placement, making data-driven decisions about design and functionality, and ultimately enhancing the overall performance of a digital platform.</p><h4>Pattern Mining</h4><p>Pattern mining is a data mining technique used to discover meaningful and interesting patterns, relationships, or associations within a dataset. These patterns can provide valuable insights into the underlying structure and characteristics of the data, which can be used for decision-making, prediction, and optimization in various domains. Pattern mining involves finding patterns that occur frequently, have significant co-occurrences, or follow certain sequences in the data.</p><p>There are several types of pattern mining techniques, each tailored to uncover different types of patterns within the data:</p><ol><li>Frequent Itemset Mining: This technique focuses on finding sets of items that frequently appear together in a dataset. For example, in a retail setting, frequent itemset mining can identify which products are often bought together by customers. This information can be used for strategies like product placement and cross-selling.</li><li>Association Rule Mining: Building on frequent itemset mining, association rule mining uncovers relationships between different items in the dataset. These relationships are expressed as rules such as “If a customer buys item A, they are likely to buy item B.” Association rules are often used in market basket analysis and recommendation systems.</li><li>Sequential Pattern Mining: In datasets where the order of events matters, sequential pattern mining discovers patterns that occur in a specific order. This is especially relevant for understanding user behavior and sequences of events.</li><li>Spatial Pattern Mining: Spatial data often has geographic attributes. Spatial pattern mining aims to find relationships and patterns in data with spatial coordinates, like identifying clusters of similar locations or understanding the distribution of events across a geographic area.</li><li>Graph Pattern Mining: In datasets represented as graphs (nodes connected by edges), graph pattern mining seeks to discover patterns within the relationships and connections between nodes. This is useful for understanding social networks, web structures, and other interconnected data.</li></ol><h4>Mathematics of Clickstreams</h4><p>Let us now try to define the clickstream of a user in a more formal manner. One way to do this is by using a sequence of events notation. For now, we will consider one single journey. We can write the clickstream as follows.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/262/1*5jLehF-bzQ-6dFDZmsHKGQ.png" /><figcaption>Clickstream formulation</figcaption></figure><p>Where t_i is the time at which the user had the interaction and e_i contains information about the interaction.</p><p>Besides the pure interactions we can also have some context data such as the device of the user, the browser and the user’s location. Let us simply call this context A. In our case, some of the attributes we have for one visit include the following.</p><pre>A = {&#39;serverDate&#39;: &#39;2023-08-19&#39;,<br> &#39;visitorType&#39;: &#39;returningCustomer&#39;,<br> &#39;visitCount&#39;: &#39;135&#39;,<br> &#39;deviceModel&#39;: &#39;Generic Desktop&#39;,<br> &#39;operatingSystem&#39;: &#39;Windows 7&#39;}</pre><p>We can see that all of these might be things that influence the user’s behavior. We can then make different hypothesis regarding the user behavior. We could for example say, that user behavior differs through time or that it is different depending on the device that the user uses.</p><p>If we want to get some high level overview of the data, we can look at the distribution of interaction duration.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/841/1*UiG9sXjLf9Q80nzSIhu9Dw.png" /><figcaption>Distribution of interaction lengths</figcaption></figure><p>As we can see, most interactions consisted of only 1–2 events, but some were quite lengthy.</p><p>There is also lots of metadata about each event. Two example events (with some fields omitted) can be seen below.</p><pre>{&#39;type&#39;: &#39;action&#39;,<br>    &#39;url&#39;: &#39;https://dive-shop.net/products/diving-tank/&#39;,<br>    &#39;pageTitle&#39;: &#39;Divezone Brand Diving Tank - Divezone Store&#39;,<br>    &#39;pageIdAction&#39;: &#39;19&#39;,<br>    &#39;idpageview&#39;: &#39;Hp1IIb&#39;,<br>    &#39;serverTimePretty&#39;: &#39;Aug 19, 2023 11:42:47&#39;,<br>    &#39;pageId&#39;: &#39;25678766&#39;,<br>    &#39;timeSpent&#39;: 610,<br>    &#39;pageviewPosition&#39;: &#39;1&#39;,<br>    &#39;timestamp&#39;: 1692445367,<br> {&#39;type&#39;: &#39;form&#39;,<br>    &#39;icon&#39;: &#39;plugins/FormAnalytics/images/form.png&#39;,<br>    &#39;idpageview&#39;: &#39;Hp1IIb&#39;,<br>    &#39;title&#39;: &#39;interacted with form Checkout Form&#39;,<br>    &#39;formName&#39;: &#39;Checkout Form&#39;,<br>    &#39;formId&#39;: &#39;42&#39;,<br>    &#39;formStatus&#39;: &#39;running&#39;,<br>    &#39;converted&#39;: &#39;0&#39;,<br>    &#39;submitted&#39;: 0,<br>    &#39;serverTimePretty&#39;: &#39;Aug 19, 2023 11:52:56&#39;,<br>    &#39;timeToFirstSubmission&#39;: &#39;1&#39;,<br>    &#39;timeSpent&#39;: &#39;752994&#39;,<br>    &#39;timeHesitation&#39;: &#39;0&#39;,<br>    &#39;leftBlank&#39;: 2,<br>    &#39;fields&#39;: [{&#39;fieldName&#39;: &#39;billing_address_1&#39;,<br>      &#39;timeSpent&#39;: &#39;4043&#39;,<br>      &#39;timeHesitation&#39;: &#39;1318&#39;,<br>      &#39;leftBlank&#39;: &#39;0&#39;,<br>      &#39;submitted&#39;: &#39;0&#39;},<br>...<br>     {&#39;fieldName&#39;: &#39;billing_city&#39;,<br>      &#39;timeSpent&#39;: &#39;2075&#39;,<br>      &#39;timeHesitation&#39;: &#39;990&#39;,<br>      &#39;leftBlank&#39;: &#39;0&#39;,<br>      &#39;submitted&#39;: &#39;0&#39;}]<br></pre><p>The first event shows the customer clicking on a link to an url. We additionally get the time at which she clicked the link and how long she then stayed on the page. In the second event, the user filled out a form. We can see exactly, what form it was and on which page it is shown. We can even see how long the user spent on filling out each of the form fields.</p><p>These two exemplary events already show how complex the data at hand is and that there are many ways in which to analyze it. For now, we will simplify the problem a bit and look at how we can discover some first patterns in the data.</p><h3>The Data</h3><p>The data at hand is taken from <a href="https://matomo.org/">Matomo</a>. Matomo is an open source analytics tool, similar to Google Analytics. The platform lets you track everything that a user does on your website. Luckily for us, they also have a <a href="https://demo.matomo.cloud/index.php?module=CoreHome&amp;action=index&amp;idSite=1&amp;period=day&amp;date=yesterday#?period=day&amp;date=yesterday&amp;category=Dashboard_Dashboard&amp;subcategory=1">demo</a> website with some data. By default, Matomo already provides us with many analytics capabilities. But if we want to dig a little deeper, we can also retrieve the log data from the system via an <a href="https://developer.matomo.org/api-reference/reporting-api">API</a>. In this case, we have data from an ecommerce site that sells diving gear.</p><h3>Sequential Pattern Mining</h3><p>Although we have rich metadata about the events and journeys, we will for now simply look at which events there were and in which order they happened. We will also disregard the exact timing of the events and simply look at their order.</p><p>We have to first think about what we define as an event. Here, we can think about different levels of granularity. For example, let’s say the user clicked on the link <a href="https://dive-shop.net/products/scuba-fins/"><em>https://dive-shop.net/products/scuba-fins/</em></a><em> </em>we can then say that the event is ‘user clicked on the product scuba fins’ or we can simply say that ‘the user clicked on a product’. Although the latter case is less precise, it also makes the feature space less sparse. A similar decision has to be made for ‘search’ type events. We can simply say that ‘the user searched’ or we can also include what he searched for. Again, in the latter case, we could end up with too many combinations.</p><h4>Aside; Combinatorics</h4><p>We will quickly run into the problem that, the number of possible ways in which the customer can interact with our website grows exponentially as the number of possible events increases. To show this, let K be the number of interactions a customer has on his visit. Further, let N be the number of types of interactions (e.g. ‘click’, ‘add to cart’, …). Then, the number of possible interactions X is the following.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/86/1*jf7vAAlZYCdhfLCAdB_hXg.png" /><figcaption>Number of combinations</figcaption></figure><p>We are essentially asking, how many possible ways exist, in which we can draw K elements from the set of interactions, when the order matters.</p><h4>Back to Pattern Mining: Preprocessing</h4><p>We will therefore have to try and reduce the types of events as much as possible such that they still retain most of their meaning.</p><p>As an example, we can classify the actions by their urls. That way, we just see which section the customer was headed to but not what exactly they interacted with.</p><pre>def classify_action(url):<br>    types = [<br>        &quot;jobs&quot;, &quot;products&quot;, &quot;cart&quot;, &quot;checkout&quot;, &quot;faq&quot;, &quot;diving&quot;, &quot;best-dive-sites&quot;, <br>        &quot;best-of-the-best&quot;, &quot;my-account&quot;, &quot;liveaboard&quot;, &quot;divesite&quot;, &quot;blog&quot;, &quot;resumes&quot;,<br>        &quot;forum&quot;, &quot;travel&quot;, &quot;guides&quot;, &quot;buying-guide&quot;]<br>    for type_ in types:<br>        if type_ in url:<br>            e = {&quot;type&quot;: &quot;action&quot;, &quot;details&quot;: type_, &quot;e&quot;: &quot;action__&quot;+type_}<br>            break<br>        elif url == &quot;https://dive-shop.net/&quot;:<br>            e = {&quot;type&quot;: &quot;action&quot;, &quot;details&quot;: &quot;shop-home&quot;, &quot;e&quot;: &quot;action__shop-home&quot;}<br>            break<br>        elif url == &quot;https://divezone.net/&quot;:<br>            e = {&quot;type&quot;: &quot;action&quot;, &quot;details&quot;: &quot;zone-home&quot;, &quot;e&quot;: &quot;action__zone-home&quot;}<br>            break<br>    else:<br>        print(url)<br>        e = {&quot;type&quot;: &quot;action&quot;, &quot;details&quot;: &quot;other&quot;, &quot;e&quot;: &quot;action__other&quot;}<br><br>    return e</pre><p>We do the same thing for the other events after which we have an identifier for each event that we can use as a feature. So, as an example, let’s say we have a list of interactions such as below.</p><pre>example_interactions = [&#39;https://divezone.net/diving/maldives&#39;,<br> &#39;https://divezone.net/diving/florida&#39;,<br> &#39;https://divezone.net/diving/red-sea&#39;,<br> &#39;https://dive-shop.net/products/diving-accessory-starter-kit/&#39;,<br> &#39;https://dive-shop.net/products/distance-line-reel/&#39;]</pre><p>Then, they will appear as the following events.</p><pre>example_transf = [classify_action(x) for x in example]<br><br>print(example_transf)<br><br>&gt;&gt;&gt; [{&#39;type&#39;: &#39;action&#39;, &#39;details&#39;: &#39;diving&#39;, &#39;e&#39;: &#39;action__diving&#39;},<br> {&#39;type&#39;: &#39;action&#39;, &#39;details&#39;: &#39;diving&#39;, &#39;e&#39;: &#39;action__diving&#39;},<br> {&#39;type&#39;: &#39;action&#39;, &#39;details&#39;: &#39;diving&#39;, &#39;e&#39;: &#39;action__diving&#39;},<br> {&#39;type&#39;: &#39;action&#39;, &#39;details&#39;: &#39;products&#39;, &#39;e&#39;: &#39;action__products&#39;},<br> {&#39;type&#39;: &#39;action&#39;, &#39;details&#39;: &#39;products&#39;, &#39;e&#39;: &#39;action__products&#39;}]</pre><p>A sample interaction with the website now looks like this.</p><pre>[{&#39;type&#39;: &#39;action&#39;, &#39;details&#39;: &#39;products&#39;, &#39;e&#39;: &#39;action__products&#39;},<br>  {&#39;type&#39;: &#39;event&#39;, &#39;eventAction&#39;: &#39;Cart change&#39;, &#39;e&#39;: &#39;event__Cart change&#39;},<br>  {&#39;type&#39;: &#39;action&#39;, &#39;details&#39;: &#39;cart&#39;, &#39;e&#39;: &#39;action__cart&#39;},<br>  {&#39;type&#39;: &#39;action&#39;, &#39;details&#39;: &#39;checkout&#39;, &#39;e&#39;: &#39;action__checkout&#39;},<br>  {&#39;type&#39;: &#39;ecommerceOrder&#39;, &#39;e&#39;: &#39;ecommerceOrder&#39;},<br>  {&#39;type&#39;: &#39;action&#39;, &#39;details&#39;: &#39;checkout&#39;, &#39;e&#39;: &#39;action__checkout&#39;},<br>  {&#39;type&#39;: &#39;action&#39;, &#39;details&#39;: &#39;my-account&#39;, &#39;e&#39;: &#39;action__my-account&#39;},<br>  {&#39;type&#39;: &#39;action&#39;, &#39;details&#39;: &#39;products&#39;, &#39;e&#39;: &#39;action__products&#39;},<br>  {&#39;type&#39;: &#39;action&#39;, &#39;details&#39;: &#39;my-account&#39;, &#39;e&#39;: &#39;action__my-account&#39;},<br>  {&#39;type&#39;: &#39;outlink&#39;,<br>   &#39;url&#39;: &#39;https://www.instagram.com/Divezone.net/&#39;,<br>   &#39;e&#39;: &#39;outlink&#39;},<br>  {&#39;type&#39;: &#39;action&#39;, &#39;details&#39;: &#39;shop-home&#39;, &#39;e&#39;: &#39;action__shop-home&#39;},<br>  {&#39;type&#39;: &#39;outlink&#39;,<br>   &#39;url&#39;: &#39;https://www.instagram.com/Divezone.net/&#39;,<br>   &#39;e&#39;: &#39;outlink&#39;}]</pre><p>In the above log, we can see that the customer first looked at a product, then added it to the cart, after which he looked at his cart, proceeded to checkout and then did some other stuff. Interestingly, the customer did not actually buy the product (one interesting use case might be to try and predict whether a customer will buy something based on clickstream data).</p><p>Note that there is some more preprocessing we have to do, I only explained the process for the ‘action’ type interactions. There are of course other types of interactions such as ‘ecommerceOrder’, ‘form’, etc. that need to be preprocessed similarly. I have uploaded all the code needed to reproduce the experiment in this <a href="https://github.com/LaurinBrechter/Reinforcement-Learning/blob/main/pattern_mining_matomo.ipynb">notebook</a> on GitHub.</p><p>We can now do some further preprocessing, namely converting our types to ids. I also decided to sort some sequences out that were very short (sequences with 3 interactions or less). We also get 2 mappings, one from ids to types and one from types to ids.</p><pre>sequences = []<br>type_to_id = {}<br>id_to_type = {}<br>i = 0<br><br>for visit in d:<br>    events = [j[&quot;e&quot;] for j in visit]<br><br>    for event in events:<br>        if event not in type_to_id:<br>            type_to_id[event] = i<br>            i += 1<br>    events_id = [[type_to_id[event]] for event in events]<br>    if len(events) &gt; 3:<br>        sequences.append(events_id)<br><br></pre><p>The result looks the following. Note that with some libraries one might have to pad the sequences to the maximum sequence length, in our case, this is not necessary.</p><pre>print(sequences)<br><br>&gt;&gt;&gt; [[[8], [4], [8], [4], [9], [8], [8]],<br> [[5], [8], [8], [8], [8], [8], [8], [8], [5]],<br> [[12],[3]],<br> [[12],<br>  [0],<br>  [3],<br>...<br>  [9],<br>  [10],<br>  [4]],<br> [[8], [10], [10], [10], [10], [10]],</pre><p>Each event id is in an extra bracket as in the normal setting, this can be used to signify, that two events are happening at the same time. So for example, if the first sequence were.</p><pre>first_seq = [[8, 1], [4], [8], [4], [9], [8], [8]]</pre><p>Then this would mean that in the first interaction, event id 8 and 1 are being done at the same time.</p><h4>Doing the Actual ‘Mining’</h4><p>After we have done a lot of preprocessing work, we can know use our pattern mining algorithm. In this case, we are using the SPAM algorithm from the ‘<a href="https://pypi.org/project/sequence-mining/">sequence-mining</a>’ Python library. I will not go into the details of the algorithm here, but you can check out the original <a href="https://www.cs.cornell.edu/johannes/papers/2002/kdd2002-spam.pdf">paper</a> if you are interested in learning more about it [4]. For us, it suffices to say, that it finds the most frequent sequences in our data in an efficient way. We can also define a minimal threshold that must be surpassed for the sequence to be considered as frequent, this is called the support.</p><p>In the code below, we import the algorithm, initialize it with a support of 0.1 (i.e. the sequence must appear in 10% of visits) and then do the algorithm on the data. As you can see, compared with the preprocessing this step only consists of four lines of code!</p><pre>from sequence_mining.spam import SpamAlgo<br><br>algo = SpamAlgo(0.1) # set up the algorithm<br>algo.spam(df) # do the mining<br><br>frequent_items = algo.frequent_items # get the frequent items</pre><p>We can map the ids back to their string representations to give back some semantics.</p><pre>frequent_items_types = []<br>for i in frequent_items:<br>    s = []<br>    for j in i:<br>        id_ = j if isinstance(j, int) else j[0]<br>        s.append(id_to_type[id_])<br>    frequent_items_types.append(s)</pre><h4>Looking at the Results</h4><p>The results are now all those sequences that are frequent. This is exactly where a subject matter expert (SME) would have to look at the results for anything unexpected. We could either see something that should not be there (i.e. user interacting with the site in an unexpected way) or we do not find something that should be there (i.e. behavior we would be expecting but that is simply not there).</p><pre>frequent_sequences = [[&#39;action__diving&#39;],<br> [&#39;outlink&#39;],<br> [&#39;action__jobs&#39;],<br> [&#39;action__liveaboard&#39;],<br> [&#39;action__diving&#39;, &#39;action__diving&#39;],<br> [&#39;action__diving&#39;, &#39;action__diving&#39;, &#39;action__diving&#39;],<br> [&#39;action__diving&#39;, &#39;action__diving&#39;, &#39;action__diving&#39;, &#39;action__diving&#39;],<br> [&#39;outlink&#39;, &#39;outlink&#39;],<br> [&#39;outlink&#39;, &#39;action__products&#39;],<br> [&#39;outlink&#39;, &#39;action__cart&#39;],<br> [&#39;action__jobs&#39;, &#39;action__jobs&#39;],<br> [&#39;action__jobs&#39;, &#39;action__jobs&#39;, &#39;action__jobs&#39;],<br> [&#39;action__jobs&#39;, &#39;action__jobs&#39;, &#39;action__jobs&#39;, &#39;action__jobs&#39;],<br> [&#39;action__jobs&#39;,<br>  &#39;action__jobs&#39;,<br>...<br> [&#39;action__shop-home&#39;, &#39;action__cart&#39;],<br> [&#39;action__shop-home&#39;, &#39;action__my-account&#39;],<br> [&#39;action__shop-home&#39;, &#39;action__my-account&#39;, &#39;action__my-account&#39;],<br> [&#39;action__shop-home&#39;, &#39;action__shop-home&#39;],<br> [&#39;action__liveaboard&#39;, &#39;action__liveaboard&#39;]]</pre><p>Note that there is also some redundancy here. If the sequence a-a-a is frequent, then, the sequence a-a must also be frequent. We can see this in the results: If [‘action__jobs’, ‘action__jobs’, ‘action__jobs’, ‘action__jobs’] is frequent, a sequence like [‘action__jobs’, ‘action__jobs’] must also be frequent as the former is a superset of the latter.</p><h3>Conclusion</h3><p>In this post, we have seen a full pattern mining workflow. We started with getting raw data from an API, after which came the lengthy step of preprocessing and deciding on how to best represent an event. We can easily see that once we have done the preprocessing, performing the actual pattern mining is only a few lines of code. This again shows you that very often in ML, the actual algorithm takes up only a very small amount of work, wheras everything else (e.g. preprocessing) needs much more time. Additionally, in order to gain actionable insights, a SME needs to look over the results and interpret them.</p><h3>Resources &amp; Further Reading</h3><p>[1] <a href="https://www.nature.com/articles/s41598-020-73622-y">Shopper intent prediction from clickstream e-commerce data with minimal browsing information</a></p><p>[2] <a href="https://dl.acm.org/doi/pdf/10.1145/3448139.3448169">Using Clickstream Data Mining Techniques to Understand and Support First-Generation College Students in an Online Chemistry Course</a></p><p>[3] <a href="https://sites.cs.ucsb.edu/~ravenben/publications/pdf/clickstream-usenixsec13.pdf">You are How You Click: Clickstream Analysis for Sybil Detection</a></p><p>[4] <a href="https://www.cs.cornell.edu/johannes/papers/2002/kdd2002-spam.pdf">Sequential PAttern Mining using A Bitmap Representation</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1bcd2de15a5d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A Data-driven, Bayesian Approach to the Critical Path Method]]></title>
            <link>https://medium.com/@brechterlaurin/a-data-driven-bayesian-approach-to-the-critical-path-method-26fd7905e85b?source=rss-4a7fc9f4bfd4------2</link>
            <guid isPermaLink="false">https://medium.com/p/26fd7905e85b</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[operations-research]]></category>
            <category><![CDATA[bayesian-statistics]]></category>
            <category><![CDATA[project-management]]></category>
            <dc:creator><![CDATA[Laurin Brechter]]></dc:creator>
            <pubDate>Mon, 14 Aug 2023 16:06:01 GMT</pubDate>
            <atom:updated>2023-08-14T16:06:01.281Z</atom:updated>
            <content:encoded><![CDATA[<p><em>TL;DR In this post, I provide a short review of the CPM project management technique. After this, we discuss the shortcomings and see how a Bayesian approach can make the technique more data-driven while still incorporating expert knowledge. You can find the source code at this </em><a href="https://github.com/LaurinBrechter/Reinforcement-Learning/blob/main/probCPM.ipynb"><strong><em>GitHub Link</em></strong></a></p><h3>Introduction</h3><p>The <em>Critical Path Method</em> (CPM) is used in project management to schedule activities within a project. Each activity that has to be done to complete the project has predecessors which need to be done before the task can be started. We can then construct a <em>Directed Acyclic Graph</em> (DAG) from the list of dependencies. Additionally, each task has a duration. An example of CPM data might look like this (taken from the following GitHub <a href="https://github.com/cppcoders/cpm-python">Repo</a>).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/335/1*q3BkC_-2jErkjF_bcIucSw.png" /></figure><p>In the table above, each row represents an activity that has a list of predecessors and a duration. The activity with no predecessors is the start activity. The last activity (last row) is the end activity.</p><p>The CPM method then does a form of breadth first search to calculate the statistics below. I will not go into the details of the CPM algorithm, but you can find a video that explains it pretty well <a href="https://www.youtube.com/watch?v=-TDh-5n90vk&amp;ab_channel=JoshuaEmmanuel">here</a>.</p><ul><li>Early start time (ES): the earliest time at which the task can be started.</li><li>Early finish time (EF): the earliest time at which the task can be finished (simply ES + duration).</li><li>Late start time (LS): the earliest time at which the task can be finished.</li><li>Late finish time (LF): the latest time at which the task can be finished (LS + duration).</li><li>Slack value/Total float: The time between LF and EF. If the slack is &gt;0 there is some wiggle room between the LF and EF and the task might be started later without impacting the following tasks.</li><li>Critical Activities: If an activity has no slack (slack=0), it is critical. If something goes wrong with this activity, the activities that depend on it cannot start in time and the whole project will become delayed.</li></ul><p>If the slack is 0, nothing can go wrong in the project. You can imagine that if something did go wrong, the following tasks, that depend on the delayed task cannot be started on time and the whole project will become delayed. We can then also obtain the total duration of the project by looking at the finishing time of the last activity.</p><h4>Shortcomings of the CPM Method</h4><p>The world is not deterministic. In reality, we never know the exact duration of tasks a priori. Instead, the true duration of a task is often very much influenced by chance. To partially address these shortcomings, the PERT method has been invented.</p><h3>The PERT Method</h3><p>The <a href="https://en.wikipedia.org/wiki/Program_evaluation_and_review_technique"><em>Project Evaluation and Review Technique</em></a><em> </em>(PERT) is often used in conjunction with the CPM method. The most notable difference is that, instead of assuming one fixed task duration, one can specify a <em>pessimistic (p)</em>, <em>most likely (m) </em>and <em>optimistic (o) </em>duration for each task. Similar to CPM, these three values have to be specified by the project manager for each task. These three values define a so-called <a href="https://en.wikipedia.org/wiki/PERT_distribution"><em>PERT distribution</em></a><em>. </em>This is a modified beta distribution but instead of being bounded by 0 and 1, it is bounded by the pessimistic and the optimistic values. At the same time, the mode of the distribution is the most likely value m. We can then calculate the expected value of that distribution (and thereby the exp. duration of the task) via the following formula.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/143/1*gPeLHeaUQb6DivwKNpl2tw.png" /><figcaption>Formula taken from PERT <a href="https://en.wikipedia.org/wiki/Program_evaluation_and_review_technique">wikipedia article</a></figcaption></figure><p>One can see, that the expected duration is a waited average of the times specified before. This way, we can calculate te for each task and then proceed with CPM as usual.</p><p>I came across this topic as it is part of a course on operations research that I have at my university. There are some things that bother me about this approach.</p><ul><li>why assume any special probability distribution. What if, for example, there is no real upper or lower bound? What if we want a heavy-tailed distribution?</li><li>how can we use data (e.g. duration of tasks in the past) in a structured manner to inform the task duration while still being able to incorporate expert knowledge?</li><li>After calculating the estimated duration te, PERT proceeds with CPM as if te is deterministic although it clearly is not. We then get a project duration as a single number although it should realistically be a distribution.</li></ul><p><em>Note that I am no expert on this topic and simply ‘stumbled’ across it. If there is something completely wrong about my logic, I would love to be corrected. Also, I did not have much time to look into all kinds of project management techniques. If what I present here has already been done before, I am sorry, I am mainly doing this to expand my knowledge and connect concepts. So feel free to dicsuss this in the comments or send me a message.</em></p><p>Of course, some of these things are easily explained. The PERT distribution is used, as it is simple for a <em>Subject Matter Expert</em> (SME) to specify the paramters of the distribution. Most of these people aren’t trained statisticians and it would be hard for them to choose the appropriate distribution, let alone the parameters of it. The PERT distribution on the other hand, is very intuitive.</p><h3>Probabilistic CPM (probCPM)</h3><p>To address these shortcomings, let us assume now, that for some of the tasks, we may have durations from previous projects. Further, we will assume, that the duration of a task does not depend on the project in which it was done. We might then have data such as this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/418/1*sfL6rX5bplFVOj5R7b9m7A.png" /></figure><p>Note that a task must not have already been done before. If we were to use a purely data-driven approach, we might simply use the average previous durations as our new guess. For the tasks with no previous durations, we might use the average of all other tasks with previous durations. This approach would not, however, take advantage of the knowledge of a SME.</p><h4>Bayesian Statistics to the Rescue</h4><p>We can use Bayesian inference to leverage both the knowledge of the SME as well as the previous task durations in a structured manner. Bayes theorem specifies how to update our beliefs about something given our priors beliefs and the data that we have seen. In the formula below, x represents the previous task durations that we have already seen (evidence). Pr(theta) is the prior. This represents our belief about how the task duration could be distributed without seeing the data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/179/1*pRZ_GxWNEv13pX8eTI_EWg.png" /><figcaption>Bayes theorem (image by author)</figcaption></figure><p>To see how this technique could work in reality, we will assume that the task duration for any given task is normally distributed with a mean and a standard deviation (note that this approach can be generalized to any distribution). This is also how we can generate the ‘prev_duration’ column in the previous table. Of course, the project manager never gets to see the true duration. He only knows the project durations so far and has his own experience. In order to implement this in Python, we can start by creating our data. We have 5 activities, each with a true average duration and true standard deviation. As evident from the predecessors field, we must first complete A, then B,C and D and can then proceed with E after which the project is finished. This is of course a very simplistic project but it suffices for demonstrating the idea.</p><pre>data = [<br>    {&quot;activity&quot;: &quot;A&quot;, &quot;true_duration&quot;: 3, &quot;predecessors&quot;: [], &quot;true_std&quot;: 1},<br>    {&quot;activity&quot;: &quot;B&quot;, &quot;true_duration&quot;: 3, &quot;predecessors&quot;: [&quot;A&quot;], &quot;true_std&quot;: 0.5},<br>    {&quot;activity&quot;: &quot;C&quot;, &quot;true_duration&quot;: 4, &quot;predecessors&quot;: [&quot;A&quot;], &quot;true_std&quot;: 1.5},<br>    {&quot;activity&quot;: &quot;D&quot;, &quot;true_duration&quot;: 6, &quot;predecessors&quot;: [&quot;A&quot;], &quot;true_std&quot;: 2},<br>    {&quot;activity&quot;: &quot;E&quot;, &quot;true_duration&quot;: 5, &quot;predecessors&quot;: [&quot;B&quot;, &quot;C&quot;, &quot;D&quot;], &quot;true_std&quot;: 3},<br>]</pre><p>We must first add the tasks (nodes) to our project and can then connect them with each other. We can make a small sample run, to see that everything works. I will use the <a href="https://github.com/chrisspen/criticalpath">criticalpath</a> Python library for the implemenation of the CPM algorithm.</p><pre>p = Node(&#39;project&#39;)<br><br>for node in data:<br>    p.add(Node(node[&#39;activity&#39;], duration=node[&#39;true_duration&#39;]))<br><br>for node in data:<br>    if node[&#39;predecessors&#39;] != []:<br>        for predecessor in node[&#39;predecessors&#39;]:<br>            p.link(predecessor, node[&#39;activity&#39;])<br><br>p.update_all()<br>p.duration # total duration of the project<br><br>&gt;&gt;&gt; 14<br><br>p.get_critical_path() # nodes on the critical path<br><br>&gt;&gt;&gt; [A,D,E]</pre><p>When the durations are deterministic, we will always get a duration of 14 time units. In that case, the critical path is A-D-E. This makes sense, since both B and C take less time than D and therefore have some slack. Even if for example B was started later or took longer, the project would still not be delayed, since D is the critical activity. We can now go back to the non-deterministic scenario and simulate some data.</p><p>For each task in the project, we first sample the number of times that it has been done in the past (this can be 0 for a task that has never been done before). In this case, I arbitrarily set this to a discrete uniform distribution ranging from 0 to 5. We then sample n_prev previous task durations from a normal distribution.</p><pre>def simulate_normal(data:list[dict]):<br>    <br>    for idx, task in enumerate(data):<br>        n_prev = np.random.randint(0,5)<br>        prev = np.round(np.random.normal(task[&quot;true_duration&quot;], task[&quot;true_std&quot;], n_prev),2)<br>        data[idx][&quot;prev_durations&quot;] = prev<br><br>    return data</pre><h4>Getting the Posterior</h4><p>In the next step, we have to combine the observed data with the SME opinion about the task duration. Let’s say, we have observed the following task durations in the past and we have a guess from the SME of how long the task might take for this project.</p><pre>obs = np.array([3.72, 4.58, 4.15]) # previous observed durations<br>expert_guess = 8 # SME guess<br>vagueness = 1 # how vague the expert is about his guess</pre><p>We must then find a way of combining the expert’s guess with the actual historic data. For this, we can look towards Bayesian inference and PyMC. The prior (expert opinion) gets transformed into the posterior via the likelihood. If the vagueness is low, we do not let the posterior deviate much from the expert’s opinion. If it is high, it can deviate a lot. This in turn means, that we put more emphasis on the data.</p><pre>def get_posterior_mean(obs, expert_guess, vagueness):<br>    with pm.Model() as task_model:<br><br>        mu = pm.Normal(&#39;mu&#39;, mu=expert_guess, sigma=vagueness)<br>        sigma = pm.HalfNormal(&#39;sigma&#39;, sigma=3)<br><br>        y = pm.Normal(&#39;y&#39;, mu=mu, sigma=sigma, observed=obs)<br><br>        trace = pm.sample()<br><br>    return trace.posterior[&quot;mu&quot;].values.mean(axis=1)</pre><p>We can test this hypothesis by running the above function with different guess and vagueness parameters while still working with the above data.</p><pre>expert_guess = 8<br>vagueness = 1<br>get_posterior_mean(obs, expert_guess, vagueness)<br><br>&gt;&gt;&gt; array([6.72840376, 6.65522789])<br><br>vagueness = 1.7<br>get_posterior_mean(obs, expert_guess, vagueness)<br><br>&gt;&gt;&gt; array([4.78000139, 4.98078503])</pre><p>If we look at the first case (vagueness=1) we can see that, even though the task has only taken an average of 4.15 units in the past, we estimate that the task in the new project will take around 6.7 units. This is of course because the SME was very sure (low vagueness) about his estimation of 8 time units. In the second case, we can already see that the estimate is much closer to the observed data since the SME in not that sure about his guess.</p><p>This approach can be generalized to any distribution. Note that you can also use conjugate distributions in many cases.</p><p>Incorporating Bayesian inference is especially useful in two cases.</p><ul><li>The activity has only been performed a small number of times before and we therefore don’t have lots of historic data.</li><li>The task in this project will be different from the previous ones.</li></ul><p>In the latter case, simply taking the historic mean would be erraneous since the distribution is most likely not the same.</p><p>We must now make an expert guess about the duration of each activity in the same manner. This means augmenting our data in the following way.</p><pre>data = [<br>    {&quot;activity&quot;: &quot;A&quot;, &quot;true_duration&quot;: 3, &quot;predecessors&quot;: [], <br>       &quot;true_std&quot;: 1, &quot;guess&quot;: 8, &quot;vagueness&quot;: 1.5},<br>    {&quot;activity&quot;: &quot;B&quot;, &quot;true_duration&quot;: 3, &quot;predecessors&quot;: [&quot;A&quot;], <br>       &quot;true_std&quot;: 0.5, &quot;guess&quot;: 2, &quot;vagueness&quot;: 1.3},<br>    {&quot;activity&quot;: &quot;C&quot;, &quot;true_duration&quot;: 4, &quot;predecessors&quot;: [&quot;A&quot;], <br>       &quot;true_std&quot;: 1.5, &quot;guess&quot;: 2, &quot;vagueness&quot;: 4},<br>    {&quot;activity&quot;: &quot;D&quot;, &quot;true_duration&quot;: 6, &quot;predecessors&quot;: [&quot;A&quot;], <br>       &quot;true_std&quot;: 2, &quot;guess&quot;: 4, &quot;vagueness&quot;: 2},<br>    {&quot;activity&quot;: &quot;E&quot;, &quot;true_duration&quot;: 5, &quot;predecessors&quot;: [&quot;B&quot;, &quot;C&quot;, &quot;D&quot;], <br>       &quot;true_std&quot;: 3, &quot;guess&quot;: 2, &quot;vagueness&quot;: 1.2},<br>]</pre><p>For each task, we will then combine the observed data and the expert guess into the posterior. Through this, we get 1000 variants of how the task duration could look like (1000 draws of the posterior).</p><pre>for idx, task in enumerate(d):<br>    posterior = get_posterior_mean(<br>        task[&quot;prev_durations&quot;], <br>        task[&quot;guess&quot;], <br>        task[&quot;vagueness&quot;])<br>    d[idx][&quot;posterior&quot;] = posterior</pre><p>One activity might now look like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/880/1*OMCazPHKj7e6F_Kb3ELedw.png" /><figcaption>Data for one activity</figcaption></figure><p>When we combine the different tasks, we now get 1000 different versions of how the project could turn out. Every time we simulate the project, we look at how long it took and what tasks were on the critical path.</p><pre>durations = []<br>critial_paths = []<br><br>for it in range(1000): # simulate 1000 projects<br>    p = Node(&#39;project&#39;)<br><br>    for node in d:<br>        p.add(Node(node[&#39;activity&#39;], duration=node[&#39;posterior&#39;][it]))<br><br>    for node in d:<br>        if node[&#39;predecessors&#39;] != []:<br>            for predecessor in node[&#39;predecessors&#39;]:<br>                p.link(predecessor, node[&#39;activity&#39;])<br><br>    p.update_all()<br><br>    durations.append(p.duration)<br>    critial_paths.append(p.get_critical_path())</pre><p>We now get a full posterior probability distribution of the project duration.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/841/1*o0tzBVdj2PJdeqhUqmv0nw.png" /><figcaption>Posterior of project duration.</figcaption></figure><p>We can also see, what the probability of a node being on the critical path is. As we can see, A and E are always on the critical path which is logical since they are also the start/end activities. D is much more likely to be on the critical path which makes sense since it has a high true_duration (leading to high past durations) and our guess was high. For the same reasons, we can never find B on the critical path.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/859/1*ceaHgGqzhC6eNDnTvQp3CQ.png" /><figcaption># times an activity was on the critical path.</figcaption></figure><h3>Conclusions</h3><p>In this post, I presented a Bayesian, data-driven alternative to the CPM and PERT method. We also looked at a basic implementation in Python. Note that there is still much more room for improvement here. We would, for example, need to generalize this to work for any probability distribution (not just the normal).</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=26fd7905e85b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Applying the Bayesian Gamma-gamma Model for Customer Lifetime Valuation]]></title>
            <link>https://medium.com/@brechterlaurin/applying-the-bayesian-gamma-gamma-model-for-customer-lifetime-valuation-aee88e7d2bbb?source=rss-4a7fc9f4bfd4------2</link>
            <guid isPermaLink="false">https://medium.com/p/aee88e7d2bbb</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[bayesian-statistics]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[marketing-analytics]]></category>
            <dc:creator><![CDATA[Laurin Brechter]]></dc:creator>
            <pubDate>Sat, 12 Aug 2023 15:47:33 GMT</pubDate>
            <atom:updated>2023-08-12T15:47:33.122Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://github.com/LaurinBrechter/BayesianInference/blob/main/olist_marketing_analysis.ipynb"><em>Link to Code in GitHub</em></a>;<em> This post applies the gamma-gamma model for Customer Lifetime Valuation (CLV) to a </em><a href="https://www.kaggle.com/datasets/olistbr/brazilian-ecommerce"><em>brazilian e-commerce dataset</em></a><em>. This is part two of a series on the application of CLV methods. For the first part, see </em><a href="https://medium.com/@brechterlaurin/a-bayesian-approach-to-customer-lifetime-valuation-cd0cde6ee105"><em>here</em></a><em>.</em></p><p>CLV, as the name suggests, is a method for estimating the lifetime value of a customer to the company. For this purpose, we need to estimate the following.</p><ul><li>How many transactions we can expect from a customer in a given timeframe.</li><li>The likelihood of a customer becoming inactive in any given timeframe</li><li>How much the customer will spend on an average transaction.</li></ul><p>In my <a href="https://medium.com/@brechterlaurin/a-bayesian-approach-to-customer-lifetime-valuation-cd0cde6ee105">previous post</a>, I looked at a Bayesian version of the BG/NBD model that answers the first two questions. The gamma-gamma model can help with the third one.</p><h4>The Gamma-gamma Model</h4><p>The gamma-gamma or also called “spend” model tries to estimate the (average) transaction value of a customer given the transactions that we have observed so far. It does so my modelling the individual transaction values as being gamma distributed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/226/1*a9M4bZQ1wC-Vgiuye9sUTg.png" /><figcaption>Assumed Distribution for a Single Transaction</figcaption></figure><p>Note that technically, a normal distribution would not be appropriate in this case, as transaction values cannot be negative. Besides that, we would not expect transaction values to be symmetrical.</p><p>It is worth stopping here to make ourselves more familiar with this distribution as it arises in many applications in statistics. The gamma distribution is often used to model arrival times. This is because, similarly to an exponential distribution, it models the time until arrivals. But in a way it is a superset of the exponential distribution as it models the time until the <em>p-th </em>arrival whereas the exponential distribution always models the time until the first arrival (it follows then that the gamma distribution is a special case of the exponential with p=1). The gamma distribution therefore has 2 parameters, one that tells us how many events we are looking for (shape) and the other, v, the rate at which these events happen.</p><p>The second gamma in the name arises because we assume the rate parameter v to also be gamma distributed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/230/1*r3934jsRoIDUuJyaiEqctQ.png" /><figcaption>Distribution of the v parameter (<a href="http://brucehardie.com/notes/025/gamma_gamma.pdf">source</a>)</figcaption></figure><p>Given the above model definition, we can come up with he following model in PyMC where d is the second parameter of the gamma distribution for v. Note that every customer (in our case seller) has their own parameter v.</p><p><em>Please refer to the following </em><a href="https://github.com/LaurinBrechter/BayesianInference/blob/main/olist_marketing_analysis.ipynb"><em>notebook</em></a><em> for the full code (mainly the preprocessing part), I have only included things relevant for modeling here.</em></p><pre>customer_id=LabelEncoder().fit_transform(merged.seller_id.values)<br>z_i = merged.payment_value.values<br><br>coords = {<br>    &quot;seller_id&quot;: np.unique(customer_id),<br>    &quot;transactions&quot;: range(len(customer_id))<br>    }<br><br>with pm.Model(coords=coords) as model:<br>    p = pm.HalfFlat(&quot;p&quot;)<br>    q = pm.HalfFlat(&quot;q&quot;)<br>    d = pm.HalfFlat(&quot;d&quot;)<br><br>    v = pm.Gamma(&quot;v&quot;, q, d, dims=(&quot;seller_id&quot;,))<br>    pm.Gamma(&quot;spend&quot;, p, v[customer_id], observed=z_i, dims=(&quot;transactions&quot;,))</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/468/1*FsnuJo4dUm57iZEgwiqIJg.png" /><figcaption>The basic gamma-gamma model for estimating z_i in PyMC</figcaption></figure><p>We can see that we model and predict the transaction value of every single transaction. The problem of this is that we have a model that may take rather long to fit. At the same time, we are not really interested in modelling every single transaction. What we want to model instead, is the average transaction value. For this, let x be the total number of transactions that the customer made (up until the time of analysis)<em>. </em>We can then calculate the average transaction value z_bar with the following formula.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/186/1*hZn5mtlZM6RaIoJr9Qgjtw.png" /><figcaption>Formula for the average transaction value (<a href="http://brucehardie.com/notes/025/gamma_gamma.pdf">source</a>)</figcaption></figure><p>Because of the convolution and scaling properties of the gamma distribution, the mean transaction value is therefore distributed like the following.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/254/1*RnNjHG_X7Tev27xM-Vi3Kw.png" /><figcaption>Distribution of the average transaction value (<a href="http://brucehardie.com/notes/025/gamma_gamma.pdf">source</a>)</figcaption></figure><p>Having our model specified, we can move to inference. One way of doing this, is with standard (frequentist) likelihood methods. In this post, however, we will perform Bayesian inference.</p><h3>Application to Olist Data</h3><p>As in my last post, we have data about sellers. Each seller has a four attributes.</p><ul><li>Recency: Number of days between last item sold an present.</li><li>T: # Days between first sale and present.</li><li>Frequency: # of repeat sales.</li><li>Monetary_value: Average transaction value (z_bar).</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/756/1*WzXQt8xVnBUjPfbrsyDaTA.png" /><figcaption>RFM Data from <a href="https://www.kaggle.com/datasets/olistbr/brazilian-ecommerce">olist</a></figcaption></figure><p>We can then setup our model with <a href="https://www.pymc-marketing.io/en/stable/">PyMC marketing</a>. There is also a version of the model that is the equivalent to the first version we discussed (i.e. modelling z_i) but it is rather slow to fit. We need to specify a frequency (i.e. x) and a mean_transaction_value (the observed z_bar) and can then fit the model.</p><pre>model = clv.GammaGammaModel(<br>    customer_id=data.index,<br>    frequency=data.frequency,<br>    mean_transaction_value=data.monetary_value,<br>)<br><br>trace = model.fit()</pre><p>After fitting the model we can get a summary of the model parameters (i.e. posterior). Note that in this case, we are assuming one parameter for p,q and v for the entire population. These three parameters essentially define a mapping from <em>observed</em> spent to underlying <em>expected</em> spent.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/760/1*ETbTjkiOQoLPltCPVFIanw.png" /><figcaption>Posterior model parameters</figcaption></figure><p>More precisely, the expected spent of a customer is a mixture of their observed spent and the average spent of the entire population. Note that this has kind of a regularizing effect, such that, even if we observe a customer with one huge transaction, we don’t expect every following transaction to be like that. But rather, we still expect the customer to have some similarity to the underlying population. Also, the more transactions we observe for a given customer, the more weight we are putting on their observed average transaction value. This is the same as a <a href="https://en.wikipedia.org/wiki/Shrinkage_(statistics)">shrinkage</a> effect in hierarchical modeling. In PyMC marketing (similar to lifetimes) we can simply get the expected spend by calling a method.</p><pre>expected_spend = model.expected_customer_spend(<br>    customer_id=data.index,<br>    frequency=data.frequency,<br>    mean_transaction_value=data.monetary_value<br>)<br><br>az.plot_forest(expected_spend.isel(customer_id=(range(10))), combined=True)</pre><p>We can then plot the expected spent for a random selection of customers.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/533/1*I_513snxbxpuXHOKkZtYFg.png" /><figcaption>Expected CLV for some customers.</figcaption></figure><h3>Conclusion</h3><p>We have seen some of the theory behind the gamma-gamma model for estimating the CLV. This can be a good model to start out with and also provides possibilities for extensions (may be covered in the following posts). We also saw the application of this model to the olist dataset with PyMC marketing.</p><h3>Sources</h3><p><strong>[1] </strong>Fader, P. S., &amp; Hardie, B. G. (2013). The Gamma-Gamma model of monetary value. February, 2, 1–9. <a href="http://www.brucehardie.com/notes/025/gamma_gamma.pdf">http://www.brucehardie.com/notes/025/gamma_gamma.pdf</a></p><p><strong>[2] </strong>Peter S. Fader, Bruce G. S. Hardie, and Ka Lok Lee (2005), “RFM and CLV: Using iso-value curves for customer base analysis”, Journal of Marketing Research, 42 (November), 415–430. <a href="https://journals.sagepub.com/doi/pdf/10.1509/jmkr.2005.42.4.415">https://journals.sagepub.com/doi/pdf/10.1509/jmkr.2005.42.4.415</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=aee88e7d2bbb" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>