<?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 Haytham Cheikhrouhou on Medium]]></title>
        <description><![CDATA[Stories by Haytham Cheikhrouhou on Medium]]></description>
        <link>https://medium.com/@haytham99cheikhrouhou?source=rss-dc4cf0a6d913------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*uxIbPt_sDC9wQOn3</url>
            <title>Stories by Haytham Cheikhrouhou on Medium</title>
            <link>https://medium.com/@haytham99cheikhrouhou?source=rss-dc4cf0a6d913------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 14:06:41 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@haytham99cheikhrouhou/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[How big tech uses ontologies and what industry 4.0 can learn from them]]></title>
            <link>https://medium.com/@haytham99cheikhrouhou/how-big-tech-uses-ontologies-and-what-industry-4-0-can-learn-from-them-9643b9cc72cd?source=rss-dc4cf0a6d913------2</link>
            <guid isPermaLink="false">https://medium.com/p/9643b9cc72cd</guid>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[industry-4-0]]></category>
            <category><![CDATA[knowledge-graph]]></category>
            <category><![CDATA[ontology]]></category>
            <dc:creator><![CDATA[Haytham Cheikhrouhou]]></dc:creator>
            <pubDate>Sun, 01 Nov 2026 22:07:32 GMT</pubDate>
            <atom:updated>2026-11-01T22:07:32.585Z</atom:updated>
            <content:encoded><![CDATA[<p>In a previous article, I explored <a href="https://medium.com/@haytham99cheikhrouhou/how-amazon-facebook-netflix-and-spotify-use-causal-inference-to-make-data-driven-decisions-6290cd6d6dec">how Amazon, Facebook, Netflix and Spotify use causal inference to make data-driven decisions</a>, but as data continues to grow in volume and complexity, giving a structure to the information becomes important to understand relationships and provide models with the right context.</p><p>During an academic research project, I had to study ontologies and how they can be used to formally represent knowledge across complex industrial systems. Little did I know that similar principles were also being used by some of the biggest technology companies. A particularly interesting example came from Netflix.</p><p>The Netflix Technology published an article Blog titled <a href="https://netflixtechblog.com/ontology-driven-observability-building-an-end-to-end-knowledge-graph-at-netflix-scale-421392cf5adf"><em>Ontology-Driven Observability: Building an End-to-End Knowledge Graph at Netflix Scale</em></a>. They explain how Netflix uses an ontology and a knowledge graph to connect information scattered across its engineering systems to investigate production issues and provide AI agents with a common understanding of its infrastructure.</p><p>A streaming platform needs to understand how its applications, services, deployments and incidents interact, and how to handle an outage or a regression. A manufacturing plant, the same way, needs to understand how its machines, components, sensors, production processes and failure modes interact.</p><p>Netflix uses what they call the MELT layer for carrying their investigations. MELT stands for Metrics, Events, logs and Traces. Every pillar of this layer provides important raw information on what is happening across users, devices &amp; applications, and services.</p><p>To give structures to this information, ontologies defines a common vocabulary that humans and machines can use to understand information consistently.</p><p>For an industrial environment, an ontology could describe:</p><ul><li><strong>Assets:</strong> factories, production lines, machines, equipment and components.</li><li><strong>Processes:</strong> manufacturing operations, assembly, machining and inspection.</li><li><strong>Measurements:</strong> vibration, temperature, pressure, speed and electrical signals.</li><li><strong>States:</strong> normal state, degraded state, abnormal state and failure state.</li><li><strong>Failures:</strong> defects, faults, failure modes, degradation mechanisms and failure events.</li><li><strong>Maintenance:</strong> inspections, interventions, work orders, spare parts and maintenance strategies.</li><li><strong>Operational context:</strong> operating conditions, production requirements, machine utilization and environmental conditions.</li></ul><p>For Netflix, an ontology would describe:</p><ul><li><strong>Users and devices</strong>: subscribers, user sessions, and streaming devices.</li><li><strong>Applications and services</strong>: client applications, APIs, microservices, backend services, GraphQL endpoints and application components.</li><li><strong>Infrastructure:</strong> servers, containers, cloud resources, databases, networks, and service dependencies.</li><li><strong>Measurements:</strong> response times, latency, throughput, error rates, CPU and memory utilization, streaming quality and playback performance.</li><li><strong>Events: </strong>application deployments, configuration changes, A/B test rollouts, service restarts, scaling events, alerts and user interactions.</li><li><strong>Incidents and failures:</strong> service disruptions, application errors, failed requests, performance regressions, outages and their associated root-cause hypotheses.</li><li><strong>Operational activities:</strong> incident investigations, engineering interventions, rollbacks, remediation actions, on-call assignments and team responsibilities.</li></ul><p>An ontology however is more than just a taxonomy of concepts, it also describes their properties, relationships, logical axioms and constraints. More specifically, an ontology defines:</p><ul><li>What types of entities exist.</li><li>What properties those entities have.</li><li>How entities relate to each other.</li><li>What rules and constraints apply.</li></ul><p>This provides a shared interpretation of the information. For instance, in the case of industry, we need to distinguish between degradation and failure. A bearing can be degraded while still performing its required function. A failure state, on the other hand, describes a condition where the equipment can no longer perform that function. This distinction matters in predictive because a maintenance system should be able to identify deterioration before it becomes an actual failure.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lA3d8gTs36vlR48200PYAQ.png" /></figure><h3><strong>The W3C Semantic Web stack: RDF, OWL, SHACL and SPARQL</strong></h3><p>To implement such a model, Netflix relies on technologies from the W3C (World Wide Web Consortium) Semantic Web ecosystem, mainly RDF, OWL, SHACL and SPARQL, each solving a different problem.</p><p><strong>RDF : Resource Description Framework</strong></p><p>RDF provides a standard way of representing facts through triples:</p><p>(Subject, Predicate, Object)</p><p>In our case for example, this can be:</p><pre>:CNC_07 rdf:type :Machine .<br>:CNC_07 :hasComponent :SpindleBearing_07 .<br>:SpindleBearing_07 :monitoredBy :Sensor_07 .<br>:Sensor_07 :measures :Vibration .</pre><p>Each triple represents a fact. Nodes represent entities or values, while edges represent relationships.</p><p><strong>OWL: Web Ontology Language</strong></p><p>OWL allows us to formally define the meaning of concepts, classes, properties and logical relationships.</p><p>For example, we can define that a SpindleBearing is a Component and that a Component is part of an Asset structure.</p><p>A reasoner can also infer implicit information from explicitly defined facts.</p><p><strong>SHACL: Shapes Constraint Language</strong></p><p>SHACL validates that data in the graph respects predefined constraints.</p><p>For instance, a vibration measurement might be required to have a timestamp, a numerical value, a unit of measurement and a reference to the sensor that generated it.</p><p><strong>SPARQL: SPARQL Protocol and RDF Query Language</strong></p><p>SPARQL is the query language used to retrieve information from RDF graphs.</p><p>The graph allows us to retrieve relationships that would otherwise require manually joining several databases.</p><h3>From data sources to a knowledge graph</h3><p>Defining an ontology is only the beginning. We also need to populate it with information from the different industrial systems. This is where enrichment pipelines and specialized AI agents become useful.</p><p>Netflix refers to these components as <em>enrichers</em>. They collect raw information, analyze and interpret it, extract relevant facts and add them to the knowledge graph as typed RDF triples.</p><p>Once data is collected from the different sensors, systems and reports specialized enrichers, a combination of data-processing pipelines, rules and agents, transform it into meaningful, structured information. They identify relevant entities, extract facts, interpret relationships, and represent them as RDF triples that are added to the knowledge graph. The graph connects the different entities, their measurements, their historical states, their production processes and their maintenance records.</p><h4>The intelligence and reasoning layer</h4><p>Once the graph is populated, different applications and AI agents can query it for anomaly detection or failure diagnosis, tracing potential causes step by step, connecting evidence from different sources, and feeding validated findings back into the graph. Over time, this creates a continuously enriched knowledge base that supports failure prevention, maintenance planning, and operational decision-making.</p><p>Providing AI with this structured context can also help reduce the risk of hallucinations. A multi-agent system built on top of the knowledge graph becomes able to investigate the different sources of information, but still communicate through shared a shared ontology and reason over explicitly defined entities, relationships, and constraints.</p><p>In a more direct example of combining ontologies and LLMs, Apple published <em>ODKE+: Ontology-Guided Open-Domain Knowledge Extraction with LLMs</em>. The research describes a pipeline combining rule-based extraction, ontology-guided LLM prompting, evidence retrieval, fact validation and corroboration.</p><p>Amazon from their side released <em>AutoKnow</em>, a system designed to automatically enrich product knowledge graphs using existing taxonomies, product catalogues, customer interactions and unstructured text.</p><p>Whether it is Industry 4.0 or big tech companies that heavily rely on AI, a structured knowledge and semantic context are becoming essential in building large-scale AI systems. Netflix handles billions of requests every day, while factories generate massive amounts of information through sensors, machines, production systems and maintenance operations. Despite the differences between these environments, the challenge is however the same, turning fragmented information into a coherent representation of the world they operate in.</p><h3>References and further reading</h3><ol><li><a href="https://netflixtechblog.com/ontology-driven-observability-building-an-end-to-end-knowledge-graph-at-netflix-scale-421392cf5adf">Netflix Technology Blog — Ontology-Driven Observability (2026)</a></li><li><a href="https://www.amazon.science/blog/building-product-graphs-automatically">https://www.amazon.science/blog/building-product-graphs-automatically</a></li><li><a href="https://www.w3.org/RDF/">W3C — RDF</a></li><li><a href="https://www.w3.org/TR/owl2-overview/">W3C — OWL 2</a></li><li><a href="https://www.w3.org/TR/shacl/">W3C — SHACL</a></li><li><a href="https://www.w3.org/TR/sparql11-query/">W3C — SPARQL 1.1</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9643b9cc72cd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How AI Earned the 2024 Nobel Prize: A Brief Introduction to Hopfield and Hinton’s Work]]></title>
            <link>https://medium.com/@haytham99cheikhrouhou/how-ai-earned-the-2024-nobel-prize-a-brief-introduction-to-hopfield-and-hintons-work-7e78452d2442?source=rss-dc4cf0a6d913------2</link>
            <guid isPermaLink="false">https://medium.com/p/7e78452d2442</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[nobel-prize]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[deep-learning]]></category>
            <dc:creator><![CDATA[Haytham Cheikhrouhou]]></dc:creator>
            <pubDate>Fri, 18 Oct 2024 14:28:38 GMT</pubDate>
            <atom:updated>2024-10-18T14:39:40.959Z</atom:updated>
            <content:encoded><![CDATA[<p>The Nobel Prize Committee has just announced the 2024 winners of the Nobel Prize in Physics, and this time, it goes to AI. After the tremendous boom of AI over the past few years, it is not surprising that John Hopfield and Geoffrey Hinton — two pioneers who have worked on neural networks since the 1980s — have been awarded this prestigious prize <strong>“for foundational discoveries and inventions that enable machine learning with artificial neural networks.”</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/299/1*aatWcqU9HsnEJVh171AeTA.jpeg" /></figure><p>As a reminder, <strong>Artificial Neural Networks (ANNs)</strong> are a technology developed since the 1940s, inspired by the way the human brain works through neurons and synapses. These artificial neurons pass signals between each other, mimicking biological processes. As we gained access to larger datasets, more computational power, and more sophisticated optimization algorithms, neural networks — once an idea primarily of theoretical interest — have become the foundation for solving a wide variety of tasks, from image classification to generative AI.</p><p>he <strong>feedforward neural network</strong> is one common type of ANN, where information moves in one direction — from input to output — without looping back. This is explained in detail in my article <a href="https://medium.com/@haytham99cheikhrouhou/dont-worry-about-it-part-2-vector-calculus-and-optimization-3f2c504a98de?source=your_stories_page-------------------------------------">Don’t Worry About It (Part 2) : Vector Calculus and Optimization</a> where I break down the backpropagation algorithm, showing how it trains feedforward networks by minimizing error.</p><p>One variation of neural networks is <strong>Recurrent Neural Networks (RNNs)</strong>. As the name suggests, <strong>RNNs allow information to loop back</strong> through the network, enabling them to “remember” previous inputs. This feature makes RNNs especially useful for tasks where the current input depends on past data, such as time-series forecasting, natural language processing, and speech recognition.</p><h3>Why did Hopfield and Hinton win the Nobel Prize if neural networks existed since the 1940s?</h3><h4><strong>Hopfield and the associative memory</strong></h4><p>In 1982, <strong>J.J. Hopfield</strong> introduced a revolutionary model of associative memory, often called a <strong>Hopfield Network</strong>. This network is a type of recurrent neural network that allows for <strong>content-addressable memory</strong>. In Hopfield’s own words :</p><blockquote>Suppose that an item stored in memory is “H. A. Kramers &amp;G. H. Wannier Phys. Rev. 60, 252 (1941).” A general content-addressable memory would be capable of retrieving this entire memory item on the basis of sufficient partial information.</blockquote><p>This type of memory is <strong>associative</strong>, meaning the network can recall stored information based on partial or noisy inputs.</p><p>Hopfield represented stored items as points in continuous or discrete N-space and defined by its coordinates X<em>a</em>, the goal is to go from a noisy input X = X<em>a+</em>Δ and reach X<em>a</em>.</p><p>The algorithm proposed by Hopfield relies on binary neurons, ie neurons that would only take the value 0 or 1 depending on the following equation</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/101/1*6lfZ5fJxeLD-tLqFUStOOw.png" /></figure><p>where T<em>ij </em>is the weight applied to each neuron and V<em>i </em>takes 1 if the quantity above is superior to a given threshold, otherwise it would take 0.</p><p>To each global state of the network Hopfield assigned an “energy” of that state defined by :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/200/1*dZAt4UVgm_880j8PpHDU8g.png" /></figure><p>The concept of energy was a significant early step in bridging physics and neural networks, making it possible to understand networks from a theoretical perspective.</p><p>When the network minimizes its energy, it settles into a <strong>local minimum</strong>. Each local minimum in the energy landscape corresponds to a <strong>stable state</strong> of the network. These stable states act as <strong>memories</strong> stored in the network.</p><p>Once this energy landscape is formed (during the training phase), if you input a partial or noisy version of a stored pattern, the network will lead the input to a nearby <strong>local minimum</strong>, retrieving the stored memory. This process of minimizing energy allows the network to perform <strong>pattern completion</strong>.</p><h4><strong>Hinton and the Boltzmann Machine</strong></h4><p>Hinton, expanded on Hopfield’s ideas but introded stochasticity in the network’s activation and used a probabilistic framework using what he called the Boltzmann Machine and generalizing on Hopfield’s energy by adding inputs from outside the network.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/359/1*6HVlhElskqEj24L04fQCuQ.png" /></figure><p>where <strong>η</strong>i is the external input to the i<em>th</em> unit, w<em>ij</em> is the weigth from the j<em>th</em> to the i<em>th</em> unit, s<em>i</em> is the boolean value (0 or 1) and θi is a threshold.</p><p>The Boltzmann Machine, like the system proposed by Hopfield, is based on binary units connceted by bidirectional links. While in a <strong>Hopfield network</strong>, local minima represent <strong>stored “memory states”</strong> or configurations that the network can converge to, for tasks like <strong>constraint satisfaction</strong> or <strong>optimization</strong>, we typically need the system to find the <strong>global minimum</strong> of an energy function, not just any local minimum. This is made possible in the Boltzmann Machine by allowing occasional jumps where the energy is higher.</p><p>During training, the Boltzmann Machine adjusts its weights using a method that aims to <strong>match the probability distribution</strong> of the input data. Once trained, when the network is shown a new example, it can interpret the data by <strong>finding the values of the hidden units</strong> (internal variables) that would generate that example. This method inspired further developments in <strong>deep generative models</strong>.</p><p>In recent years, Hinton has worked on a wide range of transformative ideas in artificial intelligence (AI) and deep learning, far beyond his early work on the <strong>Restricted Boltzmann Machines (RBMs)</strong> and the introduction of <strong>contrastive divergence,</strong> working also on<strong> </strong>work on <strong>autoencoders</strong> and <strong>variational autoencoders (VAEs)</strong>, which are used for tasks like dimensionality reduction, data generation, and anomaly detection, and contributing to works on AI ethics. Hinton even quit his job at Google in 2023 to speak freely about the dangers of AI</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/772/1*80thB8VbW0bca6zN8OUg1Q.png" /></figure><h4>Why This Nobel Prize Matters</h4><p>Hopfield and Hinton laid the foundation for the development of Deep Learning, their methods revolutionized the way deep layers of artificial neural networks can be trained and memorize memory. Hopfield’s early models significantly influenced later developments, and together with Hinton’s extensive contributions, they have shaped the landscape of AI and machine learning and laid the foundation for technologies that now permeate industries like healthcare, finance, and even astrophysics.</p><p>These methods relied on concepts from mechanics, thermodynamics, and even quantum physics and spin theory. This makes this year’s Nobel Prize in Physics particularly notable because it recognizes how principles f<strong>principles from physics and neuroscience</strong> have significantly impacted <strong>AI and machine learning</strong>, reflecting a broader shift in how scientific fields influence one another. It also recognizes the long-term, visionary work that has now reached its potential, driving the AI revolution we see today.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7e78452d2442" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ML in production: Introduction and Challenges]]></title>
            <link>https://medium.com/@haytham99cheikhrouhou/ml-in-production-part-1-introduction-and-challenges-9ba060e231a6?source=rss-dc4cf0a6d913------2</link>
            <guid isPermaLink="false">https://medium.com/p/9ba060e231a6</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[mlops]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[engineering]]></category>
            <dc:creator><![CDATA[Haytham Cheikhrouhou]]></dc:creator>
            <pubDate>Sat, 13 Apr 2024 12:14:19 GMT</pubDate>
            <atom:updated>2026-08-24T11:05:00.879Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vbrjBLTL1C8zb4yKW4WRAw.jpeg" /></figure><p>When I landed my first internship as a data scientist in an insurtech company, I thought my work would be similar to my university projects. Available data that requires little cleaning, jupyter notebooks to carry experimentations, a little finetuning of hyperparameters and voilà. But the reality was very different from my expectations. During my first week, I had to get a hands-on project on mock data to get acquainted with their solution before working on real clients’ data, and honestly, that was quite challenging. I had to see the entire data pipeline, from getting the raw datasets and storing them, to mapping its content and running it on our ML system… I started to realise that ML coding would only consist of 5% to 10% of the actual job, and as someone with a background in applied mathematics rather than in software development, I quickly understood that a lot of hard work and learning had to be done from my side .</p><p>While of course I couldn’t grasp everything within the internship timeframe, I tried to make my learning journey as rich as possible, diving into the tutorials and documents available on their wikis and trying to understand the logic behind the different parts the code, this learning journy was very helpful to me and inspired me to write this article on machine learning in production and the challenges associated with it. In the next chapters, I will try to break little by little the components of an ML project (or broadly speaking, a data science project) lifecycle, from data management , to running the model and ensuring continuous integration/continuous delivery. In order for me to better understand these concepts, I also took the Machine Learning in Production specialization which is available on Coursera, so I will rely on the courses of the specialization as a source for what I will explain.</p><p>To get a grasp of the complexities within an End-to-end MLOps project, let’s have a look at the figure below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/688/1*iGB_MdoHLQNTkSDvOeM_5A.png" /><figcaption>End-to-end MLOps architecture and workflow with functional components and roles</figcaption></figure><p>As you can see, carrying and end-to-end ML project is a complex projet where software engineers, data engineers, data scientists and ML engineers collaborate to deliver the final result. This is probably one of the main reasons why a conducted study in 2019 shows that for one in four companies, half of AI projets fail</p><h3>Machine Learning and Production</h3><p>Machine Learning has evolved from an explanatory field, confined in notebooks, to actually being deployed in business processes. As shown by the figure below, ML code represents now only a small fraction of the work that should be done to ensure the whole deployment. This comes with numerous challenges for example when collecting and labelling data, when tracking the model performance and business metrics, as well as when ensuring continuous integration.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/461/1*m-95HMT9YEDp3Rz_0abfnw.png" /></figure><ol><li>Scoping : This first step consists mainly in articulating the problem that the machine learning project aims to solve. This is a crucial step where clear and measurable objectives, requirements and metrics should be established and clarified for the project. This also includes evaluating the resources needed for the project, such as the software tools and budget, and defining the boundaries of the project in terms of what will be included and what will be excluded. A feasability study is often carried too in order to evaluate the potential constraints and limitations that may affect the implementation of the solution.</li><li>Data: Data is undoubdetdly at the heart of any machine learning application. The data management phase consists mainly in preparing data for the modelling phase. This includes data collection where data is gathered, pre-processing, labelling, analysis and eventually augmentation to add further data samples. A lot of challenges can therefore be associated to this phase, especially when dealing with large datasets. We dive deeper into these challenges and how to tackle them in the next chapter.</li><li>Modelling: This phase is probably considered the most “sexy” among data scientist, and where most of the magic happens. It can be itself divided into to substephases. The first one is model learning, where model selection, training, and hyper-parameter selection are carried, and where challenges such as model complexity, the risk of overfitting fitting and underfitting, as well as computational cost need to be tackled. The second moddling subphase is model verification which aims at ensuring that the model complies with the performance and business driven metrics. Many considerations and challenges should be taken into account during this phase too in order to avoid biases and ensure the quality of the product in accordance with the previously set metrics and KPIs.. Another aspect that should be also taken into account when working on machine learning projects is interpretability. This is not only essential for understanding and trust, but can also be essential for regulatory compliance and decision-making, especially in highly regulated fields such as medicine and finance, where interpretability is paramount to the success of the projet. For example, while neural networks can offer a better precision in some cases, they are often seen as “black box” models that aren’t very interpretable by humans. This phase also includes model verification and iteration</li><li>Deployment: Maintaining machine learning ML systems in production presents developers with multiple challenges related to integration, monitoring and updating. While some of them are common to software development challenges, MLOps introduce additional complexities. This phase includes setting up and maintaining the infrastructure to host and serve ML models at scale, Continuous Integration/Continuous Deployment (CI/CD) which includes integration with version control systems, automated testing, and deployment to production environments and updating the model without interrupting the system’s operation.</li></ol><p>With all these technical challenges in mind, other considerations related to ethics, human biases should be taken into account in each step of the projet to ensure the safety of your users.</p><p>To conclude, as companies start getting interested in deploying AI and machine learning in their businesses, it’s important to understand that implementing these projects goes beyond Jupyter notebooks and academia to something of a much larger scale where software engineers, data scientists and data engineers work closely together to make something that their clients could benefit from without bugs.</p><p><strong>Sources</strong></p><p><a href="https://arxiv.org/abs/2205.02302">Machine Learning Operations (MLOps): Overview, Definition, and Architecture</a></p><p>Challenges in Deploying Machine Learning: a Survey of Case Studies : <a href="https://arxiv.org/abs/2011.09926">https://arxiv.org/abs/2011.09926</a></p><p><a href="https://proceedings.neurips.cc/paper_files/paper/2015/file/86df7dcfd896fcaf2674f757a2463eba-Paper.pdf">Hidden Technical Debt in Machine Learning Systems</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9ba060e231a6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Linear regression From Highschool to College]]></title>
            <link>https://medium.com/@haytham99cheikhrouhou/linear-regression-from-highschool-to-college-ce2df363876d?source=rss-dc4cf0a6d913------2</link>
            <guid isPermaLink="false">https://medium.com/p/ce2df363876d</guid>
            <category><![CDATA[statistical-analysis]]></category>
            <category><![CDATA[regression]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[data-mining]]></category>
            <category><![CDATA[mathematics]]></category>
            <dc:creator><![CDATA[Haytham Cheikhrouhou]]></dc:creator>
            <pubDate>Sat, 11 Nov 2023 18:19:27 GMT</pubDate>
            <atom:updated>2023-11-11T18:19:27.083Z</atom:updated>
            <content:encoded><![CDATA[<p>When I was a junior in high school, I thought linear regression was simple: calculate the slope and intercept, and that’s all. In my senior year, I learned about least squares and the R² coefficient in statistics class. And in college, during my statistical learning course, I was introduced to p-values, t-statistics, F-statistics, and the null hypothesis (H0). What once seemed simple became more complex. Today, I will break down regression from a junior high school level to an advanced statistical learning level. In my previous article, <a href="https://medium.com/@haytham99cheikhrouhou/the-statistical-concepts-you-need-to-learn-as-a-data-scientist-and-how-to-use-them-in-r-1f937be2d750?source=your_stories_page-------------------------------------">The Statistical Concepts you need to learn as a Data Scientist and how to use them in R</a>, I briefly discussed regression. However, I realized this model, though seemingly straightforward, requires a more detailed explanation due to its underlying complexities and widespread use in data science, often a topic in job interviews.</p><h3><strong>The motive behind linear regression</strong></h3><p>Linear regression is useful when we suspect a linear relationship between variables (known as explanatory variables, predictors, or covariates) and a response variable. While it may seem straightforward, linear regression is a powerful and widely used statistical learning method.</p><h3><strong>The basics: Simple linear regression</strong></h3><p>Simple linear regression is when we have a single predictor <strong>X</strong>, to explain a response variable <strong>Y. </strong>This is as simple as saying that <strong>Y = aX + b. a</strong> and <strong>b</strong> are called the coefficients of the regression. <strong>a </strong>is called the slope, and <strong>b</strong> is called the intercept. We can write <strong>Y=f(X) </strong>where<strong> f(x) = ax+b</strong></p><p>When we have a linear line, and we have 2 points defined by their coordinates (x1, y1) (x2,y2) on that line, it’s easy to calculate a and b.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/203/1*Ob4arHrRj8WyEEyKSR6HUw.png" /></figure><p>Once we calculate <strong>a</strong>, it’s easy to calculate <strong>b </strong>by taking one of the points and calculating <strong>b</strong> as</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/136/1*4d0ib5E8zU_4aTbASGgQ_w.png" /></figure><p><strong>Least squares</strong></p><p>In practice, we deal with real data, which exhibits noise and doesn’t fit exactly on a line. Let’s say we have n points</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/307/1*ytWwogLILAcEmjOUiqPGvA.png" /></figure><p>Our goal is to find the coefficients that best fit the line. These <em>estimated</em> coefficients are denoted with a hat symbol. The least squares method assesses model fit by calculating the distance between the estimated coefficient and observed data points.</p><p>Let’s create generate data points such as <strong>Y = 2X + ε</strong>, where<strong> ε </strong>is a random noise.</p><pre>X &lt;- seq(0, 10, length.out = 10)<br>Y &lt;- 2 * X + rnorm(10, mean = 0, sd = 1)<br><br>Y_pred &lt;- 2 * X<br><br># Plot<br>plot(X, Y, main = &quot;Linear Regression and Residuals&quot;, xlab = &quot;X&quot;, ylab = &quot;Y&quot;, pch = 19, col = &quot;blue&quot;)<br>abline(a = 0, b = 2, col = &quot;red&quot;)<br>segments(X, Y, X, Y_pred, col = &quot;green&quot;, lty = &quot;dashed&quot;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/533/1*dy7EBJiMb6DaD0rVv-7CFg.png" /></figure><p>One way to assess how well a model fits on the data would be to use the distance between the estimated coefficient, this is called the <em>least squares. </em>The<em> </em>errors between the prediced value and the real value is called the <em>residual</em> and we define the <em>residual sum of squares</em> as</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/506/1*ahHOFmXCN4zIE7E1nnU5YQ.png" /></figure><p>Our goal would be therefore to minimizing the sum of the squares of these residuals using our observations, and the means of x and y (noted with a bar).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/293/1*EfQ0VZJJ0LN-_Y1A25eDFA.png" /></figure><p>The given line by our estimated coefficients of a and b is called <em>the least squares lines.</em></p><p>Let’s illustrate this with R using the mtcars dataset, The data was extracted from the 1974 <em>Motor Trend</em> US magazine, and comprises fuel consumption and 10 aspects of automobile design and performance for 32 automobiles (1973–74 models). Let’s try a simple model where Y is the Miles/(US) gallon and X is Weight in (1000 lbs)</p><pre>data(mtcars)<br># Simple linear regression of mpg ~ wt<br>lm_simple &lt;- lm(mpg ~ wt, data = mtcars)<br>summary(lm_simple)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*XMeF6l9j2_n-p-5dD9XS0w.png" /></figure><p>Here, we can interpret the estimate of wt as that for each 1000 lbs increase in weight , the miles per gallon (mpg) is expected to decrease by approximately 5.35281. We will later explain how the p-value here indicates a highly statistically significant relationship between wt and mpg .</p><h3><strong>Multiple Regression</strong></h3><p>As you probably suspect, simple linear regression cannot effectively model complex relationships where many variables are involved. We therefore use a multiple regression model.<strong> </strong>Mathematically, the linear model with <em>p </em>predictors can be written as :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/614/0*dn7tDuTEcR52DQoO.png" /></figure><p>Where the betas (<strong>β1…βp) </strong>represent the coefficients, X1 …Xp represent the predictor variables and ε is the error term.</p><p><strong>Assumptions</strong></p><p>Many assumptions should be respected in multiple regression in order for it to perform well.</p><ol><li>Linearity: This is simply saying that there is a linear relationship between the predictors and the response variable. You can use scatterplots to visually verify this. For example here we plot(mtcars$wt, mtcars$mpg).</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/522/1*b6r2j_v4UOig6Ui-P7tOqg.png" /></figure><p>2. Independence of Errors: This means that the value of one error does not predict the value of another error. This is crucial for the reliability of standard errors, confidence intervals, and hypothesis tests.</p><p>3. Homoscedasticity (Constant Variance): This means that the residuals have the same variance for every value of the fitted values and of the predictors. One way to detect it would be using tests and plotting residuals. You can for example plot(lm_model$fitted.values, lm_model$residuals) to check if there is no pattern.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/523/1*fD46F02Trf_V-1tbRBYPkw.png" /></figure><p>4. Normality of residuals: Residuals should be normally distributed. To check this assumption, you can test in R using a Q-Q plot.</p><pre>qqnorm(lm_model$residuals)<br>qqline(lm_model$residuals)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/533/1*3EJFsaUiPh0VzhXfEerblQ.png" /></figure><p>Here we can see that almost all residuals fall approximately along the qq-line. Residuals that lie far from the reference line, especially at the tails of the distribution, can be indicative of outliers.</p><p>5. No or Little Multicollinearity: This is an important assumption, colinear features generally lead to huge coefficients with opposite signs, one way to detect it is to use the variance inflation factor VIF, which quantifies multicollinearity in regression analysis. We can also plot a correlation matrix to check correlations between variables.</p><p>Here, we create a second variable that is correlated to wt, we add a random noise because otherwise the VIF function would produce an error</p><pre>library(regclass)<br>mtcars$wtn &lt;- -3*mtcars$wt +  rnorm(n = nrow(mtcars), mean = 0, sd = 0.1)<br>lm_model &lt;- lm(mpg ~ wt + hp + qsec + wtn, data = mtcars)<br>VIF(lm_model)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/360/1*UV6X1mKLZcCd4pGzQz1FlA.png" /></figure><p>Notice how the VIF coeffients for wt and wtn are huge. This is an indicator of multicolinearity</p><p><strong>Quality of the model</strong></p><p>One way to evaluate the quality of the model would be by calculating error related metrics, like the mean squared error, as well as the mean absolute error.</p><p><strong>Test of significance</strong></p><p>In multiple regression analysis, it’s critical to assess the significance of the regression coefficients using statistical tests. These tests help determine if regression coefficients <em>βi</em> significantly differ from zero. We distinguish two statistics : t-statistic and F-statstic.</p><p>The F-test in the regression tests whether the model provides a better fit to the data than a model with no predictors (except for the intercept). As for the t-test, it evaluates for a given coefficient <em>βi </em>whether this coefficient is significantly different from zero. The test statistic is calculated as</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/188/1*WRBUCzCReUJSU3N2l1fkbQ.png" /></figure><p>where <em>SE</em>(<em>β</em>^​<em>i</em>​) is the standard error of <em>β</em>^​<em>i</em>​, the estimated coefficient. In order to understand the signifiance of such test, we calculate the p-value, which is the probability of observing a t-statistic at least as extreme as the one computed, assuming that the null hypothesis is true. The null hypothesis here is the hypothesis that the coefficient <em>βi </em>is equal to zero. A small p-value (generally &lt;0.05) leads to the rejection of the null hypothesis.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/508/1*Qj2-KOvD13eyO0QeoWB8aQ.png" /></figure><p>Here, we can see that the coefficient wt is extremely significant, showing a strong relationship between wt and mpg , on the other hand hp and qsec are not significant, suggesting these variables might not have a significant effect on mpg in this model.</p><p><strong>Subset selection</strong></p><p>Sometimes we find ourselves with many variables that we don’t know which ones to use. If we have p variables, trying all of them would require 2^p regressions. This is highly inefficients. Luckily, some methods help us choose models that maximize a certain criterion. These methods not only help with prediction accuracy but also with interpretation. These methods are the Forward, Backward and Stepwise model selection.</p><ol><li>Forward selection: We start with 1, and then step by step adds the predictor that most improves the fit to the model. We generally use the AIC or BIC criterion for choosing.</li><li>Backward: Here we start with the complete model and and remove predictors iteratively.</li><li>Stepwise selection combines elements of both forward and backward selection.</li></ol><pre># Forward selection using AIC as the criterion<br>forward_model &lt;- lm(mpg ~ 1, data = mtcars)<br>forward_selection &lt;- stepAIC(forward_model, <br>                                  scope = list(lower = formula(forward_model), <br>                                  upper = ~ wt + hp + qsec + cyl + disp),<br>                                  direction = &quot;forward&quot;)<br><br><br># Backward elimination <br>backward_model &lt;- lm(mpg ~ wt + hp + qsec + cyl + disp, data = mtcars)<br>backward_elimination &lt;- stepAIC(backward_model, <br>                                direction = &quot;backward&quot;)<br># Stepwise selection <br>library(MASS)<br>stepwise_model &lt;- stepAIC(lm_multiple, direction=&quot;both&quot;)<br>summary(stepwise_model)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/279/1*5JZbMXOhpKwnNpF17EUT1w.png" /></figure><p>Here, after calling backward selection, we are only left with hp, cyl and wt. As we can see, this model minimizes the AIC criterion.</p><p><strong>Qualitative predictors</strong></p><p>Sometimes, qualitative predictors can be useful to account for a qualitative variables that is involved in changing the predicted value. We distinguish different types of qualitative variables and how to deal with them. If the variable only two levels, ,we create a <em>dummy variable</em> that takes either 1 or 0. When we have more than 2 levels, many approaches are possible :</p><p>1- One-hot encoding: here, we create a separate binary feature for each category.</p><p>2- Ordinal encoding: When dealing with qualitative variables with more than one factor , one-hot encoding can lead to a high number of binary columns, which may not be suitable for all models and can introduce multicollinearity. In ordinal encoding, we maintain the ordinal relationship or order between the categories while representing them with numerical values. This is typically used when the categorical variable has a natural order or ranking, such as low, medium, high, or small, medium, large. The numerical values assigned indicate the relative positions of the categories within the order.</p><p>3- Imputation: Instead of using binary values, each category is replaced with the mean or the median of the target variable for that category.</p><p>Let’s create a dummy variable for transmission by setting 0 to automatic and 1 to manual</p><pre>mtcars$am_factor &lt;- factor(mtcars$am, labels = c(&quot;Automatic&quot;, &quot;Manual&quot;))<br>lm_qualitative &lt;- lm(mpg ~ wt + am_factor, data = mtcars)</pre><h3><strong>Regularized regression</strong></h3><p>We have also discussed regularized regression in the article I mentioned. Regularized regression such as Lasso, Ridge and Elastic-Net are useful for dimensionality reduction and reducing the number of parameters.</p><p>For Ridge regression, we use an L2 regularization, meaning that we add to the objective function a penalizing term to the squared values of the coefficients :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/406/0*YIi3WSlgRQuyUgOn.png" /></figure><p>Lasso (Least Absolute Shrinkage and Selection Operator) adds the sum of the absolute values of the coefficients to the mean squared error (MSE) term.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/406/0*PN6xUjFFpODmvLGc.png" /></figure><p>Elastic-Net provides a trade-off between Ridge and Lasso using a coefficient, alpha (<em>α</em>).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/634/0*ZfTL1GWby3peXjYh.png" /></figure><p>The parameter <em>λ</em> controls the amount of regularization; a higher <em>λ</em> results in more shrinkage applied to the coefficients.</p><p>You can implement Ridge, Lasso and Elastic Net using the glmnet package. Here’s an example on how to use a lasso regression in R using glmnet:</p><pre>library(glmnet)<br>x &lt;- model.matrix(mpg ~ wt + hp + qsec, data = mtcars)[,-1]<br>y &lt;- mtcars$mpg<br>lasso_model &lt;- glmnet(x, y, alpha = 1)</pre><p>As you can see, an alpha set to 1 produces a LASSO model, while an alpha set to 0 produces a Ridge regression.</p><h3>The Bible of Statistical Learning</h3><p>The Elements of Statistical Learning by Jerome H. Friedman, Robert Tibshirani, and Trevor Hastie is considered to be the bible of statistical learning as it encomapsses the different methods of supervised and unsupervised learning from regression to neural networks, trees and boosting. An introduction to statistical learning (with applications in R) is also useful to get hands-on experience with R and apply the methods described above.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ce2df363876d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How Financial Institutions Conduct a Machine Learning Project and Leverage Alternative Datasets]]></title>
            <link>https://medium.com/@haytham99cheikhrouhou/how-to-conduct-a-machine-learning-project-in-finance-and-leverage-alternative-datasets-62e1cb4103c4?source=rss-dc4cf0a6d913------2</link>
            <guid isPermaLink="false">https://medium.com/p/62e1cb4103c4</guid>
            <dc:creator><![CDATA[Haytham Cheikhrouhou]]></dc:creator>
            <pubDate>Tue, 26 Sep 2023 21:05:02 GMT</pubDate>
            <atom:updated>2023-09-26T22:04:16.009Z</atom:updated>
            <content:encoded><![CDATA[<p>For a long time, the finance industry has solely related on fundamental data, ie time series and financial statements they collect from financial services websites like Bloomberg and Reuters. This data served as the foundation for pricing derivatives, evaluating market trends, and making data driven decisions.</p><p>However, with the advances in machine learning and data science, and the variability of the available data, financial institutions started also relying alternative data and sophisticated techniques like Natural Language Processing (NLP) and computer vision to analyze market trends, gauge sentiment, and make more insightful decisions. Notably, JP Morgan estimates the expenditure on Big Data to range between $2 billion to $3 billion, listing over 500 data providers. This surge in investment can be attributed to several factors:</p><ol><li><strong>The Growing Research and Adoption of Machine Learning:</strong> Financial entities are increasingly recognizing the potential of machine learning techniques in handling vast datasets more efficiently.</li><li><strong>Exponential Growth and Variety of Available Datasets:</strong> The data landscape has expanded exponentially, offering a plethora of datasets with diverse characteristics.</li></ol><p>Furthermore, alternative data sources have become diverse and numerous, originating from individuals, businesses, or sensors:</p><ul><li><strong>Individuals</strong>: Individuals participate in creating huge datasets through client reviews and opinions, search engine activity, and traffic information. The availability of open APIs, like Twitter’s, offers a wide range of use cases for investors to carry sentiment analysis (for example through tweets) and identify trends.</li><li><strong>Businesses</strong>: Enterprises play a pivotal role in amassing various data types, ranging from transaction records to geolocation data. Additionally, market professionals and public companies often share market insights and information on individual stocks. Some data vendors also offer comprehensive app download data, revenue statistics, demographic details, and usage estimates for major mobile applications.</li><li><strong>Sensors:</strong> With remarkable advancements in computer vision and cloud computing, geospatial data can be better ingested, processed, classifyed and analyzed at a massive scale. This can provide insights into investing in commodities like wheat, rice, sugar, metals, and energy, or cover maritime, weather and aviation data which provides an understanding of global trade</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/558/1*npCO-CCW4TSG_jI0OXyMQw.png" /><figcaption>Classification of Big/Alternative Data sources (Source: J.P.Morgan Macro QDS.)</figcaption></figure><p>It’s crucial to understand that not all financial data serves the same purpose for every participant in the sector. Retail portfolio managers may prioritize long-term returns and benefit from sales data, whereas quants and high-frequency traders may find real-time signals like tweets and news more valuable. Additionally, all strategies must be rigorously evaluated for regulatory compliance, such as GDPR regulations in Europe, and backtested to estimate Alpha.</p><p>An example of using Tweets to generate signals has been adopted by many research scientists to classify Tweets to buy, hold or sell. <a href="https://pdf.sciencedirectassets.com/271506/1-s2.0-S0957417415X00169/1-s2.0-S0957417415005473/main.pdf?X-Amz-Security-Token=IQoJb3JpZ2luX2VjEHsaCXVzLWVhc3QtMSJGMEQCIGfQPoRyLGByXN8%2FTadn1tpDAyJ21vMgrpLihX1OcWDhAiBUOuq8A9g%2FRBJKYxFLMzqcTeS4NhEnVpxGToiQcRy4YSqyBQh0EAUaDDA1OTAwMzU0Njg2NSIMDZcKdXNK1aSaGvLcKo8FvuEpmhl8quzd9bBFk5EtEA5x4CHIt%2BeycYkNnrxuqfZ2LIUIZ3Uc6OrgYE50I9Aw0l9EOE1Mm6eMiDjkFWfwOnXVuKU9Gom8atmxzTyZXziSavW45PKwhBWW2eHPNmE0GdcXrOLW08AmV%2FvWGGNB35N5nKNppEB4UdUG1Qu%2F%2FuHgf%2BGDb1ACNwB3P%2FuzxmIzfOh37RaO82yFMc6wGUiIzb4cEh3yJ4eWgT1nJEFBVxofVxJMeUF46I1OX6Y90AMOE%2BrLGRdGpgNO8JaMVqDcFGPEiWyckqpAQOiigyHy7vlEUpdGybPyzn9575fj4%2B5rwacvrX8xxafmZyqmxFfGrPgDeWKJpowfj%2B2vAh%2Blx6KJAWqaqeXUoGstPuIvocDs6KmDfZdnShJO6UwTlM5lZc2OHTFMD9iPVREbQ6yqnDsK2rxjnvITIFSJv01govXZbHcXyJQuVq7cWRb3fJPOYMNLnep9MtripnI%2Fh%2BqOsGjF8V4ibDRUC4oZZbUYHf1swRjHy7Ubns2PXKYhUiAD3EWNVqPbCJpcO5MdiM2udxniei3HFY9q3rvpvMxDFMTfzgJbP0g5eXeHFpigHAUypX1QpY8fnUpYc4JAZWbkzBydkEyV3k0mR2zD56JrLudCcr4qkTZ7QlIq7Rovf9dSy0PxtSBpoGXGbVNSXfv4lkZXIiyCNyrTqKZDI5dNWVl9baBOgshlxvNFQg325SFxVhIxq4WEYErTsnymvz8RtqGehYJWjBeiM%2BU3QTfIapBG42hiNVA3kWQ3USkmZUBZYIAD5sifiZqUoB5WIQcX44EKxH9oLvTwm7PicFfIk5Co3PgLD4fwJnzoeLlAkrxHzWA4HQOE8IFJs7Lia%2BxzrDDG7sqoBjqyAfJCg7eX7rvs%2F3jhr%2FclZClop4ITZ%2B50ZdUoUTNBpI07gbE3zqMYi4YBvAQz26Gh7lFloh7cKlckKgDMFiU%2FuRe1Jk8xZsVD2Czqo6GAXvOOD6LYb2IiUYr%2Bogq%2BWF87lU%2BXzkITQkVt7RQ9Y41ZhrzsZkQ%2Bsxx0CpbWdOJ5kEGAB7ItoiwvUQuNzbhpbvcZWa%2FXvpdtZHe%2FY5g9yR85opBzMo1jDElWuXsvxjMkEyGVOxw%3D&amp;X-Amz-Algorithm=AWS4-HMAC-SHA256&amp;X-Amz-Date=20230926T115411Z&amp;X-Amz-SignedHeaders=host&amp;X-Amz-Expires=300&amp;X-Amz-Credential=ASIAQ3PHCVTY5QHCAAOB%2F20230926%2Fus-east-1%2Fs3%2Faws4_request&amp;X-Amz-Signature=8a499de0dbd50f6b9eddfeb5eae9da7a6d1caea43ed7994e225fb9203f1e4315&amp;hash=691bdb6ad771958ebdba4d872be627d9e9aeac65d8f08bfda5d55c0cffc44203&amp;host=68042c943591013ac2b2430a89b270f6af2c76d8dfd086a07176afe7c76c2c61&amp;pii=S0957417415005473&amp;tid=spdf-14429f21-08c7-4f8d-9566-96a2e78a23a6&amp;sid=963fa0685c63a148a34a1628e2b1f219797cgxrqb&amp;type=client&amp;tsoh=d3d3LnNjaWVuY2VkaXJlY3QuY29t&amp;ua=1c145853070c53085d0153&amp;rr=80cb4f8f18f93dae&amp;cc=fr">Here, A. Al Nasseri, A.Tucker , and S. de Cesarea use decision tree algorithms to Quantify StockTwits semantic terms’ trading behavior in financial markets.</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/830/1*Pj6krDlHsM7pave6AFT_sA.png" /><figcaption>Steps to carry an ML Finance Project (Source: J.P.Morgan Macro QDS.)</figcaption></figure><ol><li><strong>Defining the strategy and Acquiring the data</strong></li></ol><ul><li>Like every other Machine Learning project, the first step involves defining the use case and the data we we will need, this depends on the frequency of the executed trades, as we previously explained.</li><li>The next step would be to acquire the data, some of the data we need can be easily acquired, for example through simple web scraping and HTML manipulation or using APIs. Other datasets like are more difficult to obtain and therfore need to be bought from data providers.</li></ul><p>We distinguish 3 types of data providers:</p><ul><li>Providers of raw data</li><li>Providers of semi-processed data</li><li>Providers of signals and reports</li></ul><p>Consultants can provide valuable guidance throughout the onboarding process, addressing logistical, legal, and competitive aspects.</p><p><strong>2. Storing and pre-processing the data</strong></p><p>Once the data is acquired, the collaboration of several key roles to ensure the data is in optimal condition for analysis:</p><ul><li>Data Engineers: Once the data is obtained, data engineers decide where and how to store the data efficiently and securely, using databases, data lakes, or cloud storage solutions like AWS , Google Cloud Storage, or Azure Data Lake Storage. Data engineers also create data pipelines that automate the process of ingesting, transforming, and loading data into storage to be made available for analysis, using tools like Apache Kafka, or cloud-based services like Azure Data Factory are commonly used for this purpose. They are also responsible for cleaning and preprocessing the data to make it suitable for analysis.</li><li>Software Engineers: Software engineers are responsible for setting up the overall software infrastructure and applications. They ensure that data pipelines are running smoothly and that data is accessible to other parts of the system and might containerize data-related components using Docker or Kubernetes for deployment to ensure scalability and manageability.</li></ul><p><strong>3. Analyzing the data, designing signals and backtesting:</strong></p><p>Once the data is stored, and ready to be utilized, quantitative researchers and data scientists collaborate to craft their strategies.</p><ul><li>Quantitative Researchers: Quantitative researchers, often referred to as quants, are responsible for developing trading strategies based on the available data using statistical and mathematical models to identify patterns, signals, and opportunities in the financial markets. They design trading signals which act like triggers for buying or selling assets. They should also assess the risk associated with their strategies and develop risk management protocols to mitigate potential losses.</li><li>Data Scientists: Data scientists collaborate closely with quants to apply statistical techniques and design the relevant machine learning algorithms. Data Scientists extract the most relevant features from the datasets and use them in their models. This can involve time-series analysis, sentiment analysis of news data which is generally done using coding Python, R and Spark ML.</li></ul><p>Once the strategy is set, backtesting frameworks need to be set to by simulating trades based on historical data to evaluate strategy profitability and risk. As the cost of acquiring alternative data can be huge, this is a crucial step to estimate the return on investment before implementing a new strategy.</p><p>Other players participate in this step such as risk analysts to evaluate and quantify the potential risks, Quality Assurance (QA) who conduct thorough testing of trading algorithms to ensure accuracy and reliability and compliance teams who ensure that trading strategies adhere to regulatory requirements and internal policies.</p><p><strong>4. Executing Trades</strong></p><p>This step involves the actual execution of trades in the financial markets. Execution can be manual, ie traders and portfolio managers, execute trades manually based on the signals generated by the trading strategies or automated, which is common in algorithmic trading for faster execution and reduced human error.</p><p><strong>Conclusion</strong></p><p>To conclude, carrying a Machine Learning project in finance, especially when dealing with alternative datasets can be challenging. The remarkable technological capabilities showcased by leading financial institutions in data acquisition, storage, analysis, coupled with their recruitment of top-tier engineering talent and deployment of high-performance hardware, places them at the forefront of innovation in this field.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=62e1cb4103c4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Statistical Concepts you need to learn as a Data Scientist and how to use them in R]]></title>
            <link>https://medium.com/@haytham99cheikhrouhou/the-statistical-concepts-you-need-to-learn-as-a-data-scientist-and-how-to-use-them-in-r-1f937be2d750?source=rss-dc4cf0a6d913------2</link>
            <guid isPermaLink="false">https://medium.com/p/1f937be2d750</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[r]]></category>
            <category><![CDATA[mathematics]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[statistical-analysis]]></category>
            <dc:creator><![CDATA[Haytham Cheikhrouhou]]></dc:creator>
            <pubDate>Wed, 20 Sep 2023 10:02:22 GMT</pubDate>
            <atom:updated>2023-09-20T10:02:22.667Z</atom:updated>
            <content:encoded><![CDATA[<p>In my previous article, <a href="https://medium.com/@haytham99cheikhrouhou/youre-underestimating-r-how-to-go-grom-zero-to-hero-in-r-282915a3fe34?source=your_stories_page-------------------------------------">You’re underestimating R : How to go from Zero to Hero in R</a>, I explained the various capabilities of R in carrying statistical learning and leveraging your data science skills. However, mastering a programming language isn’t enough if you truly want to pursue a career in Data Science. You also need to understand the mathematical concepts that underpin the way Data Scientists write their algorithms, conduct their tests, and make informed decisions. Here are some fundamental statistical concepts you should familiarize yourself with, especially if you’re new to data science.</p><p><strong>Prerequisite:</strong> <strong>Probabilities and Distributions</strong></p><p>In <a href="https://medium.com/@haytham99cheikhrouhou/dont-worry-about-it-part-3-probability-and-distributions-ed38d9b28f00?source=your_stories_page-------------------------------------">Don’t Worry About It (Part 3) : Probability and Distributions</a>, I explain the numerous probability concepts and why they are useful in data science. This includes Probability theory, Bayes’ theorem, and descriptive statistics.</p><p>Here’s how to generate samples following a normal distribution and plot them in R</p><pre># Generating random samples from a normal distribution<br>normal_samples &lt;- rnorm(1000000, mean = 0, sd = 1)<br><br># Approximating the density of the sampled data<br>normal_density &lt;- density(normal_samples)<br><br># Plotting the results <br>plot(normal_density, main = &quot;PDF of Normal Distribution&quot;, xlab = &quot;Value&quot;, ylab = &quot;Density&quot;)</pre><p>Mean, Variance and other descriptive statstics are straightforward to code in R. Let’s load the built-in “mtcars” dataset and calculate the mean and variance</p><pre># Load the mtcars dataset and calculate the mean and variance<br>data(mtcars)<br>mean_mpg &lt;- mean(mtcars$mpg)<br>var_mpg &lt;- var(mtcars$mpg)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/148/1*V3tKudgZwuZdkKdYhMZwZw.png" /></figure><p>We can also use the summary() function</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/817/1*Z6W6OdLlWt4qwQViIjvbIw.png" /><figcaption>descriptive statistics of “mtcars” dataset using summary()</figcaption></figure><p><strong>Regression</strong></p><p>Regression is probably the simplest model yet it’s probably the most used given the numerous tasks it helps us solve. Mathematically, the linear model with <em>p </em>predictors can be written as :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/614/1*LCx0VM4so_3HLLJJRDLSCA.png" /></figure><p>Where the betas (<strong>β1…βp) </strong>represent the coefficients, X1 …Xp represent the predictor variables and ε is the error term. Using the standard linear regression, we estimate the coefficients by minimizing the residual sum of squares :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/905/1*qViEnSvLcVWKCQlgrx1ipg.png" /></figure><p>where <em>n</em> is the number of observations and <em>yi</em>​ is the actual observed value for the <em>i</em>th observation.</p><p>You can also use regularized regression such as Lasso, Ridge and Elastic-Net which are useful for dimensionality reduction and reducing the number of parameters.</p><p>For Ridge regression, we use an L2 regularization, meaning that we add to the objective function a penalizing term to the squared values of the coefficients :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/406/1*Ye3YtXeWEULKh1MFTvvT9A.png" /></figure><p>Lasso (Least Absolute Shrinkage and Selection Operator) adds the sum of the absolute values of the coefficients to the mean squared error (MSE) term.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/406/1*BNvLsxFD4pEXBj6adxesGA.png" /></figure><p>Elastic-Net provides a trade-off between Ridge and Lasso using a coefficient, alpha (<em>α</em>).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/634/1*T1hC13aLBu4Zb23iXAe_qw.png" /></figure><p>The parameter <em>λ</em> controls the amount of regularization; a higher <em>λ</em> results in more shrinkage applied to the coefficients.</p><p>Let’s use the “Boston” dataset from the “MASS” package in R to demonstrate linear regression, Lasso, Ridge, and Elastic Net. The Boston data frame has 506 rows and 14 columns, the last column represents the median value of owner-occupied homes in $1000s and corresponds to the target value in our regression analysis. You can read more about the dataset <a href="https://cran.r-project.org/web/packages/MASS/MASS.pdf">here</a>. <br>We will also use<a href="https://glmnet.stanford.edu/articles/glmnet.html">glmnet</a>, an R package that provides tools for fitting and regularizing generalized linear models.</p><pre># Load the necessary packages and the Boston dataset<br>library(MASS)<br>library(glmnet)<br>data(Boston)<br>boston_data &lt;- Boston<br><br># Split the data into features (X) and target (y)<br>X &lt;- as.matrix(boston_data[, -14]) <br>y &lt;- boston_data$medv</pre><p>Now, let’s start by implement linear regression</p><pre>&gt; lm_model &lt;- lm(medv ~ ., data = boston_data)<br>&gt; summary(lm_model)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/525/1*P3l6vJ6VObUSBuMdZrnG0g.png" /><figcaption>the output of summary(lm_model)</figcaption></figure><p>Here, Estimate corresponds to the estimated value of the coefficient for each predictor, Std. Error is the standard error associated with each coefficient estimate (the reliability of estimates). t-value is the t-statistic, it measures how many standard deviations the coefficient estimate is away from zero, and Pr(&gt;|t|) is the p-value associated with each coefficient. It tells you the probability of observing a t-statistic as extreme as the one calculated, assuming the null hypothesis (that the coefficient is zero) is true, we will explain this more in detail later in this article.</p><p>It’s turn for Lasso, Ridge and Elastic Net</p><pre># Lasso <br>lasso_model &lt;- glmnet(X, y, alpha = 1) # alpha = 1 for Lasso<br>lasso_coef &lt;- coef(lasso_model, s = 0.01) <br>print(lasso_coef)<br><br># Ridge <br>ridge_model &lt;- glmnet(X, y, alpha = 0) # alpha = 0 for Ridge<br>ridge_coef &lt;- coef(ridge_model, s = 0.01) <br>print(ridge_coef)<br><br># Elastic Net (Here with a balanced combinatior of ridge and lasso)<br>enet_model &lt;- glmnet(X, y, alpha = 0.5) # alpha = 0.5 <br>enet_coef &lt;- coef(enet_model, s = 0.01) <br>print(enet_coef)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/338/1*Z7Q-2tKsNNwJ4dZ_XFUvRg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/340/1*9Xf1We3dkx1h556EmfDsJA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/350/1*RZP_CBkGqFC0iIyWvaOAnw.png" /><figcaption>Comparing the outputs of Lasso, Ridge and Elastic-Net</figcaption></figure><p>As you can see, unlike Ridge, Lasso and Elastic-Net can set coefficients to 0 (for example age here), this is particularly useful when we have a large number of features and we suspect that many of them may be irrelevant or redundant. While Ridge only shrinks the coefficients without setting them exactly to zero, it offers stability and reduced variance, and is useful when all features are potentially relevant. You may encounter other challenges in regression, such as handling outliers, missing data, and collinearity.</p><p><strong>Hypothesis Testing</strong></p><p>When data scientists need to test a hypothesis, or understand the significance of their findings, they use the p-value. In order to calculate the p-value, we need first to formulate <em>the null hypothesis</em>. Statistically speaking, we consider a partition of a parameter Θ into Θ0​ and Θ1​. Given an observation <em>X</em>∼<em>Pθ</em>​ (probability distribution parameterized by <em>θ)</em> our objective is to determine whether <em>θ</em> belongs to Θ0​ or not. We establish the null hypothesis :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/95/1*6oo3O-NYOUkOMGLx-SVuHg.png" /></figure><p>and its complementary hypothesis. After that we carry a test (t-test, z-test, chi-square test). then calculate the <em>p-value </em>which corresponds to the maximum value under which we will reject the null-hypothesis.</p><p>In the case of linear regression, we use t-statistic to test if the null hypothesis of a coefficient being equal to 0.</p><p><strong>Classification Methods</strong></p><p>Classification is useful when the target value is <em>qualitative, </em>or <em>categorical</em>. The goal from classification tasks is to assign a label to each input data point based on its features.</p><p>K-<em>Nearest Neighbors: </em>Understanding KNN is straightforward. When given a new data point, KNN identifies the k training points from the dataset that are closest to this new point, considering their feature values. These ‘nearest’ data points are determined based on a distance metric, commonly using Euclidean distance. To make a classification prediction for the new data point, KNN employs a majority vote among these k nearest neighbors. <a href="https://medium.com/@haytham99cheikhrouhou/youre-underestimating-r-how-to-go-grom-zero-to-hero-in-r-282915a3fe34">Here</a> we implement a KNN on the <em>iris</em> dataset in R</p><p>Support vector machines: I explain <a href="https://medium.com/@haytham99cheikhrouhou/dont-worry-about-it-part-2-vector-calculus-and-optimization-3f2c504a98de">here</a> how SVMs work and the concepts related to it. <a href="https://cran.r-project.org/web/packages/e1071/e1071.pdf">e1071</a> is a useful package for carrying many classification tasks, including support vector machines, naive bayes and generalized k-Nearest Neighbors.</p><p>Other classification methods include Logistic regression (you can fit a logistic regression model using the glm()and the predict function to make predictions) and tree based methods like decision trees and random forsts.</p><p><strong>Evaluation</strong></p><p>There are many ways to evaluate a model performance in data science. In classification, we typically usie metrics like accuracy, precision, recall, F1-score, or we can visualize the confusion matrix, the ROC (Receiver Operating Characteristic) curve, and calculate AUC-ROC (Area Under the ROC Curve).</p><p><strong>Confidence Intervals</strong></p><p>Confidence intervals tell us “how sure” we are of having the result, providing a measure of uncertainty. This is especially useful when we want to estimate the value of a certain parameter on a sample data while taking into account the variablity in the sample.</p><p><strong>The Bias-Variance Tradeoff</strong></p><p>The Bias-Variance dilemma is a problem that we encounter in many data science applications. This comes from the fact that a model that fits <em>too </em>well on the training data will exhibit high variance but will fail to generalize on test data (High variance), this is often referred to as <em>overfitting</em>. On the other hand, a model with high bias and low variance is overly simplified and tend to <em>underfit </em>the data. Let’s explore this using synthetic data.</p><pre># Load required libraries<br>library(ggplot2)<br>library(dplyr)<br><br><br># Generate synthetic data<br>n &lt;- 150<br>x &lt;- seq(-3, 3, length.out = n)<br>y_true &lt;- 4 * x^2 - 3 * x + 5<br>y_noise &lt;- y_true + rnorm(n, mean = 0, sd = 4)<br>plot(x, y_noise)<br><br># Create a data frame<br>data &lt;- data.frame(x, y = y_noise)</pre><p>Let’s fit the data using 3 models, one is underfitting, the second is the optimal second degree polynom, and the third is overfitting</p><pre><br># Underfitting: Simple Linear Regression<br>underfit_model &lt;- lm(y ~ x, data = data)<br>p_underfit &lt;- create_plot(data, underfit_model, &quot;Underfitting: Simple Linear Regression&quot;)<br><br># Optimal Fitting: Second-Degree Polynomial Regression<br>optimal_model &lt;- lm(y ~ poly(x, 2), data = data)<br>p_optimal &lt;- create_plot(data, optimal_model, &quot;Optimal Fitting: Second-Degree Polynomial Regression&quot;)<br><br># Overfitting: High-Degree Polynomial Regression<br>overfit_model &lt;- lm(y ~ poly(x, 20), data = data)<br>p_overfit &lt;- create_plot(data, overfit_model, &quot;Overfitting: High-Degree Polynomial Regression&quot;)</pre><p>Now let’s plot the results</p><pre>create_plot &lt;- function(data, model, title) {<br>  p &lt;- ggplot(data, aes(x = x, y = y)) +<br>    geom_point() +<br>    geom_line(aes(y = predict(model)), color = &quot;red&quot;) +<br>    ggtitle(title) +<br>    labs(x = &quot;x&quot;, y = &quot;y&quot;)<br>  return(p)<br>}<br><br># Display the plots<br>print(p_underfit)<br>print(p_optimal)<br>print(p_overfit)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/499/1*t66m7YydXDXNJi9BwAE8Iw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/499/1*daPukn6du_ZrvFN3EgNr-Q.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*4_qyopl39BppvYyzrZrfoA.png" /></figure><p><strong>Correlation vs Causation</strong></p><p>In a previous article on <a href="https://medium.com/p/6290cd6d6dec/edit?source=your_stories_page-------------------------------------">How Amazon, Facebook, Netflix, and Spotify use Causal Inference to make data-driven decisions</a>, I explained how <em>correlation is not causation</em>. This is an important distinction to make if you don’t want to make spurious conclusions.</p><p><strong>Dimensionality Reduction</strong></p><p>In <a href="https://medium.com/@haytham99cheikhrouhou/dont-worry-about-it-part-1-linear-algebra-d95e9dc59042?source=your_stories_page-------------------------------------">Don’t Worry About it (Part 1) : Linear Algebra</a>, I explain how using techniques like Principal Component Analysis (PCA) help use a lower-dimensional representation while maximizing the variance captured by each eigenvector.</p><p>Let’s load the built-in USArrests dataset. We should first center the variables to have mean zero. We also divide by the standard deviation to have a variance of 1. Scaling is particularly useful when our variables are measured in different scales</p><pre># Load the libraries and USArrests dataset <br>library(FactoMineR)<br>library(factoextra)<br>data(&quot;USArrests&quot;)<br><br># Standardize the data (mean = 0, variance = 1) and perform PCA<br>scaled_data &lt;- scale(USArrests)<br>pca_result &lt;- PCA(scaled_data, graph = FALSE)<br>summary(pca_result)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/790/1*d88z11A0KHqFYI1DF4qFpA.png" /><figcaption>Summary of pca_result</figcaption></figure><p>Here, cos2 represents the quality of representation for variables on the factor map and ctr represents the contribution of each individual to the principal component.</p><p>We can also create a screeplot to visualize the variance explained by each principal component ordered from largest to the smallest.</p><pre>fviz_eig(pca_result)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/525/1*GcqwNXskUgXH0-9XdUclLQ.png" /><figcaption>Scree Plot of Explained variance by each eigenvector</figcaption></figure><p>We see that using only the first two eigenvectors, we can capture about 89% of the variance.</p><p>We can also plot the PCA graph of individuals using plot(pca_result) or a biplot, which displays both the individuals and variables on the same plot, making it easier to see relationships between them.</p><pre>fviz_pca_biplot(pca_result)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/525/1*SG3Iwvo5nmxY3z6ZnTM8vA.png" /></figure><p>To visualize how much our variables are correlated with the first two principal components, we generally use a correlation circle.</p><pre>fviz_pca_var(pca_result, col.var = &quot;contrib&quot;, gradient.cols = c(&quot;#00AFBB&quot;, &quot;#E7B800&quot;, &quot;#FC4E07&quot;), repel = TRUE)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/426/1*TvxoZdTJtFiZnRNPuExkww.png" /></figure><p>A small angle implies between the arrow and the principal component implies a positive correlation between them. Variables with longer arrows are better represented on the factor map. You can read more in-detail about how these plots are generated <a href="http://www.sthda.com/english/articles/31-principal-component-methods-in-r-practical-guide/112-pca-principal-component-analysis-essentials/">here</a>.</p><p>The concepts provided above only provide an overivew of what you should familiarize yourself with if you want to get into data science. Proficiency in a programming language, while valuable, is only one piece of the puzzle. To excel in data science, you must also grasp the mathematical foundations underpinning the algorithms, tests, and decisions made by data scientists. You can also read the “Don’t Worry about it” series, where I try to break down the mathematical concepts in <a href="https://medium.com/@haytham99cheikhrouhou/dont-worry-about-it-part-1-linear-algebra-d95e9dc59042?source=your_stories_page-------------------------------------">Linear Algebra</a> , <a href="https://medium.com/@haytham99cheikhrouhou/dont-worry-about-it-part-2-vector-calculus-and-optimization-3f2c504a98de?source=your_stories_page-------------------------------------">Vector Calculus and Optimization</a>, and <a href="https://medium.com/@haytham99cheikhrouhou/dont-worry-about-it-part-3-probability-and-distributions-ed38d9b28f00?source=your_stories_page-------------------------------------">Probability and Distributions</a> that are useful in machine learning and data science.</p><p>If you’re into Statistical Learning, and want to get a more in-depth understanding of these methods, I recommend reading <a href="https://www.statlearning.com/"><strong>An Introduction to Statistical Learning with Applications in R</strong></a><strong> </strong>by Gareth James, Daniela Witten, Trevor Hastie and Robert Tibshirani, this book covers the different supervised and unsupervised learning algorithms.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1f937be2d750" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Clustering Arxiv ML articles using KMeans BERTopic to generate mind maps]]></title>
            <link>https://medium.com/@haytham99cheikhrouhou/clustering-arxiv-ml-articles-using-kmeans-bertopic-to-generate-mind-maps-74beb27fc6a8?source=rss-dc4cf0a6d913------2</link>
            <guid isPermaLink="false">https://medium.com/p/74beb27fc6a8</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[bert]]></category>
            <category><![CDATA[research]]></category>
            <category><![CDATA[clustering]]></category>
            <dc:creator><![CDATA[Haytham Cheikhrouhou]]></dc:creator>
            <pubDate>Fri, 15 Sep 2023 10:02:00 GMT</pubDate>
            <atom:updated>2023-09-15T10:02:00.909Z</atom:updated>
            <content:encoded><![CDATA[<p>When I start learning a new topic, It’s generally easy for me to get distracted by all the research papers, all the related topics etc. One method I read about was to create a learning map to stay focused on the big picture rather than diving into the other topics.</p><p>So I decided to try and do a little something that could help me extract summaries from <a href="https://arxiv.org/">arxiv.org</a>, cluster them, and plot the most relevant words in a sort of a graph.</p><h4>1. Importing the data</h4><p>To import the data from Arxiv, I had to use the Arxiv API, and then sparse the XML output and store the content into a Pandas Dataframe. The function I created it helps</p><pre>import requests<br>import xml.etree.ElementTree as ET<br>import pandas as pd<br><br>def get_arxiv(topic):<br>  url = &quot;http://export.arxiv.org/api/query?search_query=all:{0}<br>&amp;max_results=1000&quot;.format(topic)<br>  response = requests.get(url)<br><br>  if response.status_code == 200:<br>      content = response.content<br>      # Parse the XML content<br>      root = ET.fromstring(content)<br>      # Lists to store extracted data<br>      titles = []<br>      summaries = []<br>      published_dates = []<br>      authors_list = []<br>      categories_list = []<br><br>      # Looping to extract information<br>      for entry in root.findall(&#39;{http://www.w3.org/2005/Atom}entry&#39;):<br>          title = entry.find(&#39;{http://www.w3.org/2005/Atom}title&#39;).text<br>          summary = entry.find(&#39;{http://www.w3.org/2005/Atom}summary&#39;).text<br>          <br>          published = entry.find(&#39;{http://www.w3.org/2005/Atom}published&#39;).text<br>          authors = [author.find(&#39;{http://www.w3.org/2005/Atom}name&#39;).text for author in entry.findall(&#39;{http://www.w3.org/2005/Atom}author&#39;)]<br>          categories = [category.attrib[&#39;term&#39;] for category in entry.findall(&#39;{http://www.w3.org/2005/Atom}category&#39;)]<br><br>          titles.append(title)<br>          summaries.append(summary)<br>          published_dates.append(published)<br>          authors_list.append(authors)<br>          categories_list.append(categories)<br><br>      data = { &#39;Title&#39;: titles, &#39;Summary&#39;: summaries, &#39;Published Date&#39;: published_dates, &#39;Categories&#39;: categories_list, &#39;Authors&#39;: authors_list    }<br>      df = pd.DataFrame(data)<br>      return df<br>  else:<br>      print(&quot;Error: Unable to fetch content from the URL.&quot;)</pre><h4>2. Using NLTK ans Scikit-Learn</h4><p><a href="http://nltk.org/">NLTK</a>, the Natural Language Toolkit, is an open source library that helps perform <a href="https://www.nltk.org/book/">Natural Language Processing with Python</a>, from processing and tokenizing text, to performing advanced operations like classification and extracting information. Here we use it to download stop words and perform tokenization. These are the high-frequency words like <em>the</em>, <em>to</em> and <em>also </em>that don’t help us understand the text context. As for <strong>tokenization</strong>, it is the process of breaking the text into separted words and punctuations, ie. into smaller <em>tokens.</em></p><pre>import nltk<br>from nltk.corpus import stopwords<br>from nltk.tokenize import word_tokenize<br><br>nltk.download(&#39;punkt&#39;)<br>nltk.download(&#39;stopwords&#39;)</pre><p>We will also use <a href="https://scikit-learn.org/">scikit-learn</a>, a python library that is particularly useful in machine learning projects. Here we will use the <a href="https://scikit-learn.org/stable/modules/clustering.html#k-means">KMeans</a> clustering algorithm, in order to cluster the texts, and get the most relevant words from each cluster. We will also use the <a href="https://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.text.TfidfVectorizer.html">TfidfVectorizer</a>.</p><p>The KMeans algorithm is an iterative clustering technique used to partition a given dataset of <strong>N</strong> samples <strong>X</strong> into a specified number <strong>K </strong>of clusters <strong>C</strong>, where each data point belongs to the cluster <strong>j</strong> with the nearest mean <strong>μj, </strong>also called <em>centroid</em>. The algorithm aims at minimizing the sum of squared distances between data points and their assigned cluster centroids. This distance metric is known as the <strong>“inertia”</strong> or <strong>“within-cluster sum of squares.”</strong></p><pre>from sklearn.feature_extraction.text import TfidfVectorizer<br>from sklearn.cluster import KMeans<br>from sklearn.metrics import pairwise_distances_argmin_min<br>from collections import Counter</pre><p>Now that we have imported the necessary libraries, we will proceed to clustering. We will keep both 1-gram and 2-gram words, as many topics are bigrams, for example “data science” “machine learning” “quantum computing” etc…</p><pre># Initialize a list to store clusters<br>clusters = []<br>abstract_tokens = []<br><br># Tokenize and remove stopwords for each abstract<br>for abstract in df[&#39;Summary&#39;]:<br>    tokens = word_tokenize(abstract)<br>    tokens = [word.lower() for word in tokens if word.isalpha()]<br>    stop_words = set(stopwords.words(&#39;english&#39;))<br>    filtered_tokens = [word for word in tokens if word not in stop_words]<br>    abstract_tokens.append(&#39; &#39;.join(filtered_tokens))<br><br># Calculate TF-IDF for both 1-grams and 2-grams<br>tfidf_vectorizer = TfidfVectorizer(max_features=100, ngram_range=(1, 2))<br>tfidf_matrix = tfidf_vectorizer.fit_transform(abstract_tokens)<br>feature_names = tfidf_vectorizer.get_feature_names_out()<br><br># Perform K-means clustering<br>num_clusters = 10  # Adjust the number of clusters as needed<br>kmeans = KMeans(n_clusters=num_clusters)<br>kmeans.fit(tfidf_matrix)<br><br># Get cluster centers<br>cluster_centers = kmeans.cluster_centers_<br>cluster_center_terms = [feature_names[i] for i in cluster_centers.argsort(axis=1)[:, -20:]]<br>cleaned_cluster_center_terms =  clean_cluster_terms(cluster_center_terms) <br># Find the nearest abstracts to cluster centers<br>closest_docs = pairwise_distances_argmin_min(tfidf_matrix, cluster_centers)<br><br><br># Initialize a list to store clusters<br>clusters = []<br><br># Store the cluster information<br>for idx, cluster_terms in enumerate(cleaned_cluster_center_terms):<br>    clusters.append({<br>        &#39;Cluster_terms&#39;: cluster_terms,<br>    })</pre><p>In order to not have repeated words (for example “machine” “learning” and “machine learning”), I decided to code a function that removes 1-grams that are part of 2-grams from cluster terms</p><pre>def clean_cluster_terms(cluster_center_terms):<br>    cleaned_cluster_center_terms = []<br>    for cluster_terms in cluster_center_terms:<br>        cleaned_terms = []<br>        for term in cluster_terms:<br>            if &#39; &#39; in term:<br>                # If the term is a 2-gram, add it directly<br>                cleaned_terms.append(term)<br>            else:<br>                # If the term is a 1-gram, check if it&#39;s part of any 2-gram in the same cluster<br>                is_part_of_2gram = any(term in two_gram.split() for two_gram in cluster_terms if &#39; &#39; in two_gram)<br>                if not is_part_of_2gram:<br>                    cleaned_terms.append(term)<br>        cleaned_cluster_center_terms.append(cleaned_terms)<br>    return cleaned_cluster_center_terms</pre><p>Also, to remove other unnecessary words, I decided to use <a href="https://spacy.io/">spacy</a>, to only keep nouns in the cluster terms. Spacy is useful for processing, embedding, and added a new feature for Integrating LLMs.</p><pre>import spacy<br><br># Load the English NLP model<br>nlp = spacy.load(&quot;en_core_web_sm&quot;)<br><br>def filter_nouns(cluster_terms):<br>    filtered_cluster_terms = []<br>    for terms in cluster_terms:<br>        filtered_terms = []<br>        for term in terms:<br>            doc = nlp(term)<br>            # Check if the term is a noun<br>            if any(token.pos_ == &quot;NOUN&quot; for token in doc):<br>                filtered_terms.append(term)<br>        filtered_cluster_terms.append(filtered_terms)<br>    return filtered_cluster_terms<br><br># Apply the function to your cluster terms list<br>filtered_cluster_terms = filter_nouns(cleaned_cluster_center_terms)</pre><p>Now that we have clustered the texts and filtered their terms, we will plot them using Networkx and matplotlib</p><pre>import networkx as nx<br><br>def create_graph(filtered_cluster_terms) : <br>  G = nx.Graph()<br>  # Add nodes (terms) to the graph<br>  for cluster_terms in filtered_cluster_terms:<br>      G.add_nodes_from(cluster_terms)<br><br>  # Add edges (connections) between related terms<br>  for i, cluster_terms in enumerate(filtered_cluster_terms):<br>      for term1 in cluster_terms:<br>          for j, other_cluster_terms in enumerate(filtered_cluster_terms):<br>              if i != j:  # Avoid adding connections within the same cluster<br>                  for term2 in other_cluster_terms:<br>                      G.add_edge(term1, term2)<br>  return G<br><br></pre><pre>import matplotlib.pyplot as plt<br><br># Assuming you have created the graph &#39;G&#39; as shown in the previous response<br>G= create_graph(filtered_cluster_terms)<br>def plot_graph_G(G) : <br>  # Create a layout for the graph<br>  layout = nx.spring_layout(G)<br>  plt.figure(figsize=(20,16))  # Adjust the width and height as needed<br><br>  # Draw nodes and edges<br>  nx.draw(G, layout, with_labels=True, node_size=10000, font_size=10, font_color=&quot;white&quot;)<br><br>  # Customize node labels if needed<br>  # labels = {node: node for node in G.nodes}<br>  # nx.draw_networkx_labels(G, layout, labels, font_size=10)<br><br>  # Display the graph<br>  plt.show()<br>plot_graph_G(G)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*akueLAQzlKszmuMWvNXsCw.png" /><figcaption>The graph generated by Networkx and Matplotlib</figcaption></figure><p>While we seem to capture a lot of the related topics, the results omit a lot of subtopics or genres. Let’s try using BERTopic and see if we have similar results.</p><h4>3. BERTopic</h4><p>BERTopic uses hugging-face transformers and c-TF-IDF to perform topic modeling.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/366/1*xmJWtZ1mkP7caeH7SaSDRw.png" /><figcaption>c-TF-IDF</figcaption></figure><p><a href="https://maartengr.github.io/BERTopic/api/ctfidf.html#bertopic.vectorizers._ctfidf.ClassTfidfTransformer">Here</a>’s how it’s best explained :</p><blockquote>c-TF-IDF can best be explained as a TF-IDF formula adopted for multiple classes by joining all documents per class. Thus, each class is converted to a single document instead of set of documents. The frequency of each word <strong>x</strong> is extracted for each class <strong>c</strong> and is <strong>l1</strong> normalized. This constitutes the term frequency. Then, the term frequency is multiplied with IDF which is the logarithm of 1 plus the average number of words per class <strong>A</strong> divided by the frequency of word <strong>x</strong> across all classes.</blockquote><p>Let’s import the necessary libraries that we will use for the BERTopic model, and remove the stopwords just like we did with NLTK.</p><pre>from bertopic import BERTopic<br>from sentence_transformers import SentenceTransformer, util<br>from umap import UMAP<br>from hdbscan import HDBSCAN<br>from sklearn.feature_extraction.text import CountVectorizer<br>from bertopic.vectorizers import ClassTfidfTransformer<br>import numpy as np<br>stop_words = set(stopwords.words(&#39;english&#39;))<br><br>def remove_stopwords(text):<br>    # Assuming text is a string<br>    tokens = text.split()  # Split text into tokens<br>    clean_tokens = [token for token in tokens if token.lower() not in stop_words]<br>    return &#39; &#39;.join(clean_tokens)<br><br># We also remove stopwords from the initial document <br># in order to find the documents later<br><br>df[&#39;Summary&#39;]= df[&#39;Summary&#39;].apply(remove_stopwords)<br>summaries = df[&#39;Summary&#39;].to_list()<br>summaries</pre><p>Now that the textual corpus is ready, let’s make our model step by step. Eric Kleppen wrote a <a href="https://python.plainenglish.io/topic-modeling-for-beginners-using-bertopic-and-python-aaf1b421afeb">detailed tutorial to Topic Modeling using BERT</a>. Here we will use change his initial model to take into accounts bigrams.</p><pre>embedding_model = SentenceTransformer(&quot;all-MiniLM-L6-v2&quot;)<br><br>hdbscan_model = HDBSCAN(min_cluster_size=10, metric=&#39;euclidean&#39;, cluster_selection_method=&#39;eom&#39;, prediction_data=True)<br><br>vectorizer_model = CountVectorizer(stop_words=&quot;english&quot;, ngram_range=(1, 2))<br><br>ctfidf_model = ClassTfidfTransformer()<br><br># All steps together<br>topic_model = BERTopic(<br>  n_gram_range = (1,2),<br>  embedding_model=embedding_model,    # Extract embeddings<br>  hdbscan_model=hdbscan_model,        # Cluster reduced embeddings<br>  vectorizer_model=vectorizer_model,  # Tokenize topics<br>  ctfidf_model=ctfidf_model,          # Extract topic words<br>  calculate_probabilities=True,        <br>  verbose=True<br>)<br><br>topics, probs = topic_model.fit_transform(summaries)<br>topic_model_info = topic_model.get_topic_info()</pre><p>Now we will plot the graph using a Plotly rather than matplotlib. you’ll see why</p><pre>import plotly.graph_objects as go<br><br># Create a layout for the graph<br>layout = nx.spring_layout(G)<br><br># Extract node positions from the layout<br>node_positions = layout<br><br># Create a list of node names and coordinates<br>node_names = list(G.nodes())<br>node_x = [pos[0] for pos in node_positions.values()]<br>node_y = [pos[1] for pos in node_positions.values()]<br><br># Create a list of edge positions for plotting<br>edge_x = []<br>edge_y = []<br>for edge in G.edges():<br>    x0, y0 = node_positions[edge[0]]<br>    x1, y1 = node_positions[edge[1]]<br>    edge_x.append(x0)<br>    edge_x.append(x1)<br>    edge_x.append(None)<br>    edge_y.append(y0)<br>    edge_y.append(y1)<br>    edge_y.append(None)<br><br># Create a Plotly figure<br>fig = go.Figure()<br><br># Add nodes<br>fig.add_trace(go.Scatter(<br>    x=node_x,<br>    y=node_y,<br>    mode=&quot;markers&quot;,<br>    marker=dict(size=20, color=&quot;skyblue&quot;),<br>    text=node_names,<br>    hoverinfo=&quot;text&quot;<br>))<br><br># Add edges<br>fig.add_trace(go.Scatter(<br>    x=edge_x,<br>    y=edge_y,<br>    mode=&quot;lines&quot;,<br>    line=dict(width=0.01, color=&quot;#888&quot;),<br>    hoverinfo=&quot;none&quot;<br>))<br><br># Update layout for better appearance<br>fig.update_layout(<br>    showlegend=False,<br>    hovermode=&quot;closest&quot;,<br>    margin=dict(b=0, l=0, r=0, t=0),<br>)<br><br># Show the Plotly figure<br>fig.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YVOebm1oVWAhD_qQnJiS8w.gif" /><figcaption>Zooming using plotly</figcaption></figure><p>We can also visualize a barchart of topics</p><pre>topic_model.visualize_barchart(top_n_topics=22)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/932/1*hGVKOCNJ9Cj7AzM3XGp_Vw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/934/1*qdJvp4f8Mqzqevjb8knYOA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/901/1*Hoj27vzxnPZZLoCNufCLcg.png" /><figcaption>Topics generated with BERTopic</figcaption></figure><p>We can see that we have a more diverse catalog of topics and words using BERTopic. This is still the case even if we try to increase or decrease the number of clusters in our KMeans.</p><p>We can also find the articles that best represent the cluster. Say for example I am interested in causal machine learning(Topic 20) to write my article about <a href="https://medium.com/p/6290cd6d6dec">Causal Inference</a>. We can either see the 3 representative summaries of the cluster :</p><pre># getting representative docs of the 20th cluster<br>representative_docs = topic_model.get_representative_docs(20)<br># Create a DataFrame containing only the rows with summaries in the<br># representative_docs_series<br><br>causal_ml_df = df[df[&#39;Summary&#39;].isin(representative_docs)]<br>causal_ml_df</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gjmTgBQ228UcoCYfc-s5Cw.png" /></figure><p>If you’re interested in the whole list of documents in the cluster :</p><pre>df_docs = pd.DataFrame({&quot;Summary&quot;: summaries, &quot;Topic&quot;: topic_model.topics_})<br>df_docs = df_docs[df_docs[&quot;Topic&quot;]==20]<br>causal_ml_df = df[df[&quot;Summary&quot;].isin(df_docs[&quot;Summary&quot;])]<br>causal_ml_df</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8wTLRgf5f1fshXq_IKKsbA.png" /><figcaption>Generated output</figcaption></figure><p>The generated results seem to be coherent.</p><p><strong>Conclusion</strong></p><p>The presented BERTopic model, along with KMeans, help get accurate topic modeling results. By using graphs and visualization, we can also explore subtopics effectively, like for example going from machine learning to causal machine learning, or to reinforcement learning. With better finetuning and by leveraging more powerful tools, this approach could prove beneficial for learners who seek a structured understanding of complex subjects.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=74beb27fc6a8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[RLHF : Unlocking Helpfulness, Honesty, and Harmlessness with Reinforcement learning from human…]]></title>
            <link>https://medium.com/@haytham99cheikhrouhou/rlhf-unlocking-helpfulness-honesty-and-harmlessness-with-reinforcement-learning-from-human-e8ec533a6704?source=rss-dc4cf0a6d913------2</link>
            <guid isPermaLink="false">https://medium.com/p/e8ec533a6704</guid>
            <category><![CDATA[reinforcement-learning]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[ai-ethics]]></category>
            <dc:creator><![CDATA[Haytham Cheikhrouhou]]></dc:creator>
            <pubDate>Mon, 11 Sep 2023 12:01:37 GMT</pubDate>
            <atom:updated>2023-09-11T12:01:37.295Z</atom:updated>
            <content:encoded><![CDATA[<h3>RLHF : Unlocking Helpfulness, Honesty, and Harmlessness with Reinforcement learning from human feedback in Generative AI</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0UR5OQeKWdPsDMJ8mJVatA.png" /><figcaption>DALL.E 2 Generated Image “Imagine an AI world of Helpfulness, Honesty, and Harmlessness“</figcaption></figure><p>Generative AI has been dominating the tech landscape in recent months, captivating the world with its incredible capabilities. From the lifelike image generation prowess of Midjourney and Dall-E using diffusion models to the rising popularity of large language models (LLMs), exemplified by OpenAI’s ChatGPT, the field is experiencing unprecedented growth. To deepen my understanding of this exciting technology, I decided to take the <a href="https://www.coursera.org/learn/generative-ai-with-llms?">Generative AI with Large Language Models</a> course to gain a comprehensive overview of the subject.</p><p>Large language models (LLMs) have made remarkable strides in natural language understanding and generation, but they have also faced criticism for their problematic behaviors, including the use of toxic language and dissemination of inaccurate information. These issues arise due to the vast amount of diverse data from the internet used to train these models, where harmful language and misinformation are prevalent.</p><p>To address these concerns and promote responsible AI usage, developers are adopting the principles of HHH (Helpfulness, Honesty, and Harmlessness). Fine-tuning LLMs with reinforcement learning from human feedback (RLHF) is one effective approach to aligning these models with human values.</p><p>The RLHF process leverages reinforcement learning (RL) to optimize the LLM based on human feedback data and minimize the potential for harm. In reinforcement learning, an agent learns to perform tasks by interacting with an environment and receiving feedback in the form of rewards or penalties. The goal is to maximize the usefulness and relevance of model outputs while minimizing potential harm.</p><p>Here&#39;s how RLHF works:</p><h3>1. Select a Suitable LLM</h3><p>Choose an LLM with some cabilities relevant to your task, such as text summarization or question-answering. Starting with a pre-trained model with general capabilities can expedite the process.</p><h3>2. Generate Prompt-Completion Pairs</h3><p>Use a prompt dataset to generate multiple completions for each prompt using the LLM. These completions will be ranked by human labelers later.</p><h3>3. Collect Human Feedback</h3><p>Human labelers assess the completions based on predefined criteria, such as helpfulness or toxicity. This ranking data is then transformed into pairwise comparisons of completions with corresponding reward values (1 for preferred responses, 0 for less preferred ones). The same prompt completion sets are usually assigned to multiple human labelers to establish consensus and minimize the impact of poor labelers in the group.</p><h3>4. Train the Reward Model</h3><p>This reward model itself is often constructed as a language model. For instance, a BERT model can be trained through supervised learning using the pairwise comparison data you prepared from the human labelers’ assessments of prompts. By training on the human-ranked prompt-completion pairs, the reward model learns to distinguish between different responses’ alignment with human preferences.</p><p>Upon training completion, the reward model serves as a binary classifier, generating logits for both positive and negative classes. The reward value used in the RLHF process is derived from the highest positive class logits. When applying a Softmax function to the logits, probabilities are generated.</p><h3>5. Reinforcement Learning</h3><p>The obtained reward value for the prompt completion pair is fed into the reinforcement learning algorithm. This step drives the adjustment of the LLM’s weights, pushing it to produce responses that are better aligned and hold higher rewards. As a result of this process, the completion generated by the LLM that underwent RL updates achieves a superior reward score, highlighting that the weight adjustments have led to a more aligned output.</p><p>When implementing the RLHF process, there’s a range of algorithms at your disposal for this phase. A widely favored choice is proximal policy optimization (PPO).</p><p>PPO (Proximal Policy Optimization) enhances the alignment between the LLM, and human preferences. The algorithm iteratively adjusts the LLM over multiple steps, applying controlled updates within a constrained region. This bounded modification fosters more consistent and steady learning.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/577/1*T0uCN4Y5U4_JeGtkavVYLA.png" /></figure><p>The advantage term <em>A</em> estimates how much better or worse the current action is compared to all possible actions at a given state and the ration, sometimes called <em>r</em>(<em>θ</em>) is the ratio of probabilities between the updated and original policies.</p><p>However, directly maximizing the first part of the equation lead to problems because the calculations are reliable under the assumption the old and new policies are close to each other. These extra terms are therefore guardrails and simply define a region in proximity to the LLM, where our estimates have small errors.</p><h3>6. Fine-Tuning</h3><p>The RLHF process continues for a set number of epochs or until the LLM aligns with human preferences, producing an RL fine-tuned LLM (human-aligned LLM).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/575/1*FTCxx5PTu1dfHB6X0XnA6A.png" /><figcaption>Using RL in RLHF for fine-tuning</figcaption></figure><p>Remember that as technology continues to evolve, the need for responsible AI becomes even more pronounced. Approaches like RLHF, combined with fundamental measures such as curation of training data, the filtration of undesirable content, and the inclusion of a diverse array of annotators, collectively contribute to the realization of Helpfulness, Honesty, and Harmlessness.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e8ec533a6704" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How big tech companies use ML and Causal Inference to make data-driven decisions]]></title>
            <link>https://medium.com/@haytham99cheikhrouhou/how-amazon-facebook-netflix-and-spotify-use-causal-inference-to-make-data-driven-decisions-6290cd6d6dec?source=rss-dc4cf0a6d913------2</link>
            <guid isPermaLink="false">https://medium.com/p/6290cd6d6dec</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[statistics]]></category>
            <category><![CDATA[big-tech]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[causal-inference]]></category>
            <dc:creator><![CDATA[Haytham Cheikhrouhou]]></dc:creator>
            <pubDate>Wed, 06 Sep 2023 10:02:27 GMT</pubDate>
            <atom:updated>2023-09-15T05:50:00.961Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KL0sOIk4fxqKfP2X31aULg.png" /></figure><h3>How big tech companies use Machine Learning and Causal Inference to make data-driven decisions</h3><p>Big tech companies continuously strive to provide an enhaced user experience and improve <em>everything </em>they do, to provide customers with what they <em>really </em>want. But how do they know what customers truly wants ? It looks like the answer is nothing but testing.</p><p>I first came across the concept of testing while reading <a href="https://www.amazon.com/Lean-Startup-Entrepreneurs-Continuous-Innovation/dp/0307887898">The Lean Startup</a> by Eric Ries. In the chapter titled ‘Measure’ in his book, Ries introduces <strong>A/B testing</strong>. A/B testing, also known as split testing, is widely used in marketing and product development to derive data driven results.</p><p>To understand how A/B testing works, let’s consider the example of introducing a new feature. <br>We start from a hypothesis that a given feature, for instance, would improve a certain Key Performance Indicator (KPI). The customers are then split in two groups, and only one group has access to that new feature. After that, the relevant data from both groups is collected to measure the impact of the new feature on the KPI. This data is finally used to make a decision on whether to keep the feature or discard it.<br>However, as features multiply and use cases become more complicated, the standard A/B testing method becomes time consuming and requires sclaing. For example Netflix uses Causal Machine Learning to select their artworks, while Spotify uses sophisticated experimentation methods for personalized, context-aware recommendation systems…</p><p>To bette runderstand the complexity of carrying tests, here’s what Netflix, Spotify and Amazon say:</p><p>From Netflix’ <a href="https://netflixtechblog.medium.com/causal-machine-learning-for-creative-insights-4b0ce22a8a96?source=user_profile---------17----------------------------">Causal Machine Learning for Creative Insights</a> :</p><blockquote>Given Netflix’s vast and increasingly diverse catalog, it is a challenge to design experiments that both work within an A/B test framework and are representative of all genres, plots, artists, and more.</blockquote><p>Spotify introducing <a href="https://engineering.atspotify.com/2023/08/coming-soon-confidence-an-experimentation-platform-from-spotify/">Confidence — An Experimentation Platform from Spotify</a> :</p><blockquote>Our teams were getting bogged down by restarting experiments, manually calculating statistical analyses in notebooks, and coordinating test groups in spreadsheets.</blockquote><p>Amazon’s <a href="https://www.amazon.science/blog/machine-learning-tools-increase-power-of-hypothesis-testing">Machine learning tools increase power of hypothesis testing</a></p><blockquote>Ideally, when testing background colors, you’d like to control for all the other elements on the page — page content, size and placement of banner ads, desktop versus mobile layouts, and so on. But that would mean testing all possible permutations of all those elements for each background color — and acquiring enough data about each to yield a statistically significant result.</blockquote><h3>Causal Inference and Causal ML</h3><p>When split testing isn’t feasible or is expensive, researchers collect <em>observational data</em>. Once the data is collected, data analysts and engineers want to make sense from it, they want to understand what <em>caused </em>the metrics to change, and a set of hypotheses are formulated, and finally, specific algorithms are ran to test them. Causal inference aims at predicting what would happen if we apply a <em>manipulation/intervention </em>to a <em>unit</em><strong>.</strong></p><p>The problem is that, while machine learning models perform well at finding patterns, using correlations, and making predictions, they struggle to explain <em>the cause</em> of a given outcome. This is an important point to understand, <em>correlation is not causation, </em>for example, while high prices are generally associated with high sales, this doesn’t mean that increasing the price would cause sales to rise.</p><p>This is when Causal ML becomes particularly useful, it combines causal inference with machine learning techniques to allow us to identify the actual cause-and-effect relationships. Many Causal ML algorithms exist, using methods like <a href="https://arxiv.org/pdf/1510.04342.pdf">random forests</a>, <a href="https://arxiv.org/pdf/1906.02120.pdf">Neural Networks</a>, and <a href="https://arxiv.org/pdf/2308.00904.pdf">variational learning</a>.</p><h3>Causal Inference Preliminaries</h3><p>To understand how causal ML works, it’s necessary to understand the causal inference concepts that underpin it.</p><p>At the core of causal inference is the concept of potential outcomes. <br>Let Y_i(1) represent the potential outcome for individual <em>i </em>when exposed to a treatment or intervention <em>X = 1</em>, and Y_i(0) represent the potential outcome when not exposed to the treatment <em>X = 0</em>. The observed outcome <em>Y_i </em>is one of these <em>potential outcomes</em>, depending on whether the individual received the treatment or not:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/245/1*tHxzWQxTVz7oc0YK2xoT-A.png" /></figure><p>However, we can only observe one of these potential outcomes for each individual, depending on the treatment they actually received. This is where the concept of <em>counterfactuals</em> comes into play. The counterfactual outcome for an individual represents what would have happened had they received the opposite treatment. In other words, it’s a ”what-if” scenario.</p><p>Using these two outcomes, we can define the treatment effect as the difference between the observed potential outcome and the counterfactual outcome:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/227/1*t5TqrUkNdQUY0b-sqO4X4g.png" /></figure><p>Using the difference between the observed potential outcomes, we can define the “Average Treatment Effect” (ATE)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/350/1*mUvr5Rfq9AIsXjI4eRCqWw.png" /></figure><p>This metric is useful as it helps answering the question : ”On average, how much does the treatment change the outcome ?”</p><p>When we want to focus on a subgroup of the population, we can also define the “Conditional Average Treatment Effect” (CATE) to understand how the treatment effect varies across different subgroups or conditions. For a specific set of values for the covariates, x, defined by a set of covariates X, we define CATE as:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/439/1*aB4vp5dhtg-rSZsSReqt6A.png" /></figure><h4><strong>The</strong> <strong>assumptions</strong></h4><p>Causal Machine Learning involves several assumptions to ensure that the estimated causal effects are valid and reliable.</p><p><strong>Consistency</strong>: This assumption means that there is no discrepancy between the intended treatment and the actual treatment administered.</p><p><strong>SUTVA (Stable Unit Treatment Value Assumption)</strong>: SUTVA assumes that the treatment assignment and the potential outcomes of one unit do not depend on the treatment assignment or outcomes of other units. This is particularly relevant in situations where there might be interactions or spillover effects between units.</p><p><strong>Unconfoundedness</strong>: There are no hidden or unobserved variables that affect both the treatment and the outcome.</p><p><strong>Positivity</strong>: The positivity assumption ensures that unit has some chance of receiving the treatment. In mathematical terms, this means that the probability of receiving treatment (<em>T</em>=1) given the covariates (<em>X</em>) should be greater than zero and less than one:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/312/1*OGzFxksFRzdz9oIBCgf7bg.png" /></figure><p><strong>Interventions</strong></p><p>We call an intervention changing the value of a variable, this is different from conditioning, as we fix the value rather than taking the subset of units where the variable takes the value we’re interested in. We use the “do” operator to represent interventions or treatments in causal models. The notation <strong>do(X = x) </strong>signifies that we are intervening to set the value of variable X to a specific value x, regardless of its original state. This notation helps distinguish between observational associations and causal relationships and define two different probabilities.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eX12RMklp4ufV_dcFeribg.png" /><figcaption>Condition versus intervention (From <a href="https://arxiv.org/pdf/2308.09248.pdf">Active &amp; Passive Causal Inference Learning</a>)</figcaption></figure><h4><strong>The challenge of confounders</strong></h4><p>Its possible that some variables, called confounders, or confounding variables, affect both the treatment and the outcome. This can introduce bias into the analysis and lead to an inconrrect causality. Addressing them might be challenging; it requires gathering data on additional variables, the covariates, and including them in the analysis. Other steps are taken like sensitivity analysis, to assess how sensitive the results are to different assumptions about the confounding variables.</p><p>Machine Learning methods that we mentioned above, as well as the good old linear regression also help <em>isolate </em>the causal effect. Machine Learning methods are particularly useful when capturing non linear relationships, and in some cases, such as with <a href="https://arxiv.org/pdf/2308.00904.pdf">variational learning</a>, they estimate or approximate the distribution of confounding variables that are not directly observed in the study.</p><h4>How Netflix Uses Causal ML</h4><p>In the case of Netflix Artworks, Netflix uses <a href="https://arxiv.org/abs/1608.00060">Double ML</a>, this is what its Causal ML framework looks like.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UYp2Rd4_CWx6oolo3cjGeA.png" /><figcaption>The Causal ML Framework of Netflix (From <a href="https://netflixtechblog.medium.com/causal-machine-learning-for-creative-insights-4b0ce22a8a96">Causal Machine Learning for Creative Insights</a>)</figcaption></figure><h3>More about how companies use Causal Inference</h3><p>Spotify specify <a href="https://engineering.atspotify.com/2023/08/experimentation-at-spotify-three-lessons-for-maximizing-impact-in-innovation/">here</a> their strategies to make more informed decisions</p><blockquote>1. Start with the decision that needs to be made.</blockquote><blockquote>2. Utilize localization to innovate for homogeneous populations.</blockquote><blockquote>3. Break the feature apart into its most critical pieces.</blockquote><h4><strong>Localization</strong></h4><p>Because of the different likings of different countries and cultures, tech companies find it usefeul to build a targeted product or artwork rather than making it global.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/638/0*k7wInNyEXXDlbRaI" /><figcaption>From Netflix : <a href="https://about.netflix.com/en/news/the-power-of-a-picture">The Power of a Picture</a></figcaption></figure><h4>The effect of Networks</h4><p>We have previously mentioned the stable unit of treatment value (SUTVA) assumption, meaning that the treatment assignment and the potential outcomes of one unit do not depend on the treatment assignment or outcomes of other units. However, in many cases, the SUTVA assumption can be impractical, especially in social media, where one user can influence the other. This is called network interference. Many methods were designed in order to account for important network interference conditions, where spillover effects can significantly impact the validity of causal inferences.</p><p>It’s also possible that companies want to assess a new social feature that will work only if a user and a certain number of their friends see it. These are very common among Fcebook applications and can be extended to streaming platforms. Think, for example of a Netflix feature where friends can watch movies or shows together in real-time, or a Spotify feature where users can create collaborative playlists with their friends. To address the issue, Facebook explains in <a href="https://scontent.ftun15-1.fna.fbcdn.net/v/t39.8562-6/240835824_598035454529822_8456685395786161120_n.pdf?_nc_cat=109&amp;ccb=1-7&amp;_nc_sid=ad8a9d&amp;_nc_ohc=kMNxHveYKQIAX8NI6OS&amp;_nc_ht=scontent.ftun15-1.fna&amp;oh=00_AfC2fhQeF1Zdl2fhNnuDzKm4hHkkgRoYcVC1b9jp5RB16Q&amp;oe=64EFBBB9">Network Bucket Testing</a> how it uses “a collection of multiple independent subgraphs” to ensure randomization around the population while maintaining social networks.</p><p><strong>Conclusion</strong></p><p>By testing and analyzing real-world scenarios from the data they collect, big tech companies like Amzon, Facebook, Spotify, and Netflix unlock insights into what their users truly want. This article prove how these companies rely heavily on research in machine learning and causal inference, to address the problem of confounders, social networks, speed and scalability, helping them meet the diverse user preferences.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6290cd6d6dec" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Generative AI LLM Project Lifecycle: From Inception to Application]]></title>
            <link>https://medium.com/@haytham99cheikhrouhou/the-generative-ai-llm-project-lifecycle-from-inception-to-application-1e974fba5a7b?source=rss-dc4cf0a6d913------2</link>
            <guid isPermaLink="false">https://medium.com/p/1e974fba5a7b</guid>
            <category><![CDATA[nlp]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[large-language-models]]></category>
            <category><![CDATA[application-development]]></category>
            <category><![CDATA[generative-ai]]></category>
            <dc:creator><![CDATA[Haytham Cheikhrouhou]]></dc:creator>
            <pubDate>Fri, 01 Sep 2023 12:02:33 GMT</pubDate>
            <atom:updated>2023-09-01T12:02:33.784Z</atom:updated>
            <content:encoded><![CDATA[<p>Generative AI has risen as a transformative force, giving birth to Large Language Models (LLMs) that can understand, generate, and manipulate human-like text. To harness the power of LLMs effectively, it’s crucial to understand the project lifecycle, from conceptualization to integration. In this comprehensive guide, we will navigate through the stages that define a successful Generative AI LLM project. As we will see, there’s more to the lifecycle of the project than import OpenAI and prompt engineering.</p><h3><strong>1. Define the Use Cases</strong></h3><p>This is called the <em>scope </em>phase, where you define the specific use cases that your LLM will address. Whether it’s natural language generation, text summarization, or even creative writing, you should clarify the project’s goals and desired outcomes. This will be helpful as you will have to choose the model you will work on.</p><h3><strong>2. Choose an Existing Model or Pretrain Your Own</strong></h3><p>Once the scope is set, you need to select the appropriate LLM. You can either pretrain your own model on domain-specific data, which entails costs of time, computation, and data or choose from existing models. <br>There are many available open-source and pretrained models, that you can use without starting from scratch. that you can use and adapt to your specific project needs, such as the huggingface models like <a href="https://huggingface.co/docs/transformers/model_doc/flan-t5">FLAN-T5</a>. <br>This is an enhanced version of the Text-to-Text Transfer Transformer (T5) that benefits from FLAN, a useful technique for instruction fine-tuning.</p><h3><strong>3. Adapt and Align Model</strong></h3><p>Now this is where the magic happens. We refine the LLM to align with the project requirements. There are several key steps to achieve this:</p><p><strong>a. Prompt Engineering : </strong>Designing effective prompts is an art. Crafting well-structured prompts that guide the LLM towards desired responses is essential for accurate outputs. But as you can see, this is only a step of the whole lifecycle</p><p><strong>b. Fine-Tuning:</strong> Fine-tuning bridges the gap between a generic model and one tailored to your needs. By exposing the model to domain-specific data and feedback, you enhance its ability to generate relevant content.</p><p><strong>c. Align with Human Feedback</strong>: Ensuring your LLM generates outputs that align with human preferences is crucial. Reinforcement Learning from Human Feedback (RLHF) helps refine the model by incorporating feedback to minimize errors and improve alignment.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/575/1*FTCxx5PTu1dfHB6X0XnA6A.png" /><figcaption>Using RL in RLHF for fine-tuning</figcaption></figure><p><strong>d. Evaluate</strong>: Rigorous evaluation of your adapted model is paramount. Metrics like BLEU, ROUGE, and human evaluations provide insights into the quality and relevance of generated content.</p><p><strong>BLEU (Bilingual Evaluation Understudy):</strong></p><p>Here’s how BLEU works:</p><p>1. It calculates the precision of n-grams in the generated text by counting how many n-grams appear in both the generated and reference text.<br>2. It then computes the geometric mean of these precisions.<br>3. A brevity penalty is applied to account for cases where the generated text is much shorter than the reference text.</p><p>The resulting BLEU score is a value between 0 and 1. A higher BLEU score indicates a higher level of similarity between the generated and reference text. However, BLEU has its limitations, as it doesn’t always capture semantic nuances accurately.</p><p><strong>ROUGE (Recall-Oriented Understudy for Gisting Evaluation)</strong>:</p><p>ROUGE evaluates the quality of generated text by comparing it to reference text, similar to BLEU. However, ROUGE focuses more on recall, which means it assesses how well the generated text captures the information present in the reference text.</p><p>ROUGE uses various metrics, like ROUGE-1 (unigrams), ROUGE-2 (bigrams), and ROUGE-L (longest common subsequence) to measure the overlap between n-grams in the generated and reference text.</p><p>While both BLEU and ROUGE provide valuable insights into the quality and relevance of machine-generated text, these metrics are far from perfect and should be used in conjunction with other evaluation methods and benchmarks, even including human evaluations in order to to get an understanding of the model’s performance.</p><p><strong>Other LLM evaluation benchmarks</strong></p><p>There are other evaluation metrics and benchmarks used in natural language processing to assess the performance of language models such as GLUE (General Language Understanding Evaluation) and HELM (Holistic Evaluation of Language Models). These benchmarks provide standardized ways to measure and compare the performance of different language models, helping researchers and developers identify strengths and weaknesses and drive improvements in model design and training.</p><h3><strong>4. Application Integration</strong></h3><p>With an optimized LLM in hand, it’s time to integrate it into real-world applications.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*QQW30VgO4TaQeFVvDScORg.jpeg" /></figure><p><strong>a. Optimize and Deploy Model for Inference</strong>: This can be done through optimization techniques like Distillation, where you teach a smaller model (Student model) from a bigger one (Teacher model), post-training Quantization, which involves reducing the precision of model weights and Pruning where you remove model weights with negligible impact, often those close to or equal to zero.</p><p><strong>b. Augment Model and Build LLM-Powered Applications</strong>:</p><p>While the training, tuning, and alignment techniques are valuable for creating a strong model for your application, there are broader challenges with large language models that go beyond just training. One challenge is that the knowledge inside a model is limited to what it learned during pretraining, which could be outdated. For instance, ChatGPT’s knowledge is based on information available up to 2021. You can overcome the problem using <strong>Retrieval Augmented Generation (RAG). </strong>RAG methods help us make the large language model smarter by giving it access to a current knowledge base. This is cheaper and simpler compared to training a new model or refining one, and it’s easier to put into action.</p><p>Another problem is LLM’s tendency to generate text even when they don’t know the answer to a problem. This is often called <em>hallucination</em>, you can use to help your LLM overcome these issues by using <strong>Chain-of-Thought Prompting </strong>which gives rewards to models for sticking to a human-approved chain of thoughts. For example, OpenAI explain in their research article <a href="https://openai.com/research/improving-mathematical-reasoning-with-process-supervision"><em>Improving mathematical reasoning with process supervision</em></a><em> </em>how they reward steps of reasoning <em>process supervision </em>instead of rewarding the final answer <em>outcome supervision</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/653/1*x1gSePm4x7CTVLbPileq_g.png" /><figcaption>Math Test Performance (Outcome-Supervised RM vs. Process Supervised RM) From <a href="https://openai.com/research/improving-mathematical-reasoning-with-process-supervision"><em>Improving mathematical reasoning with process supervision</em></a></figcaption></figure><p>Models can also struggle with complex math. If you prompt a model to behave like a calculator, it may get the answer wrong. <strong>Program-aided Language Models (PAL) </strong>help solve the problem. PAL involves using the LLM to understand problems in everyday language and then generating steps like instructions, but leaves the final solution part to a tool like a Python interpreter.</p><p>To conclude, the Generative AI LLM project lifecycle seamlessly combines creativity, technology, and innovation. Choosing the appropriate model is only part of a more complex lifecycle that includes defining the use cases anddeploying applications that interact with users in natural language. It’s necessary to understand that each phase is essential for the success of your project.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1e974fba5a7b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>