<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Data Science on kenji.blog</title><link>http://kenji.blog/en/categories/data-science/</link><description>Recent content in Data Science on kenji.blog</description><generator>Hugo -- gohugo.io</generator><language>en</language><copyright>kenjinote</copyright><lastBuildDate>Sat, 12 Sep 2026 12:00:00 +0900</lastBuildDate><atom:link href="http://kenji.blog/en/categories/data-science/index.xml" rel="self" type="application/rss+xml"/><item><title>The Daily Life of an Engineer Managing Health with Smart Rings (Health Hacks)</title><link>http://kenji.blog/en/p/engineer-health-hacks-wearables/</link><pubDate>Sat, 12 Sep 2026 12:00:00 +0900</pubDate><guid>http://kenji.blog/en/p/engineer-health-hacks-wearables/</guid><description>&lt;img src="http://kenji.blog/p/engineer-health-hacks-wearables/img/eyecatch.jpg" alt="Featured image of post The Daily Life of an Engineer Managing Health with Smart Rings (Health Hacks)" />&lt;h2 id="1-introduction-the-intersection-of-software-engineering-and-biohacking">1. Introduction: The Intersection of Software Engineering and Biohacking
&lt;/h2>&lt;p>Modern software engineering is demanding intellectual labor that involves extreme cognitive load and a sedentary lifestyle. Constantly catching up with changing technology stacks, bug hunting in complex distributed systems, and pressure from deadlines. To overcome these, a data-driven approach to tune the hardware that is your own body, like debugging a system, is essential, rather than simply relying on &amp;ldquo;spirit&amp;rdquo; or &amp;ldquo;guts&amp;rdquo;. This is known as &amp;ldquo;Biohacking&amp;rdquo;.&lt;/p>
&lt;p>In the past, we relied on the subjective feeling (heuristics) of &amp;ldquo;I feel somewhat good/bad today,&amp;rdquo; but nowadays, with the spread of high-performance wearable devices like Oura Ring, Apple Watch, and Garmin, we can acquire biological data non-invasively, 24/7. In this article, I will explain how to acquire biological data (HRV, RHR, sleep architecture) and productivity data (coding metrics from WakaTime, etc.) via APIs, and perform correlation analysis using a data science approach with Python and Pandas. I will also unravel scientific evidence-based health hacks for engineers in extreme detail, such as mathematical models of circadian rhythms and optimal coffee intake timing based on caffeine metabolism half-life.&lt;/p>
&lt;h2 id="2-you-cant-manage-what-you-cant-measure-hardware-for-biological-data-acquisition">2. You Can&amp;rsquo;t Manage What You Can&amp;rsquo;t Measure: Hardware for Biological Data Acquisition
&lt;/h2>&lt;p>Sensors (wearable devices) for acquiring biological data each have their own areas of expertise. In data-driven health management, selecting the optimal device according to the purpose is the first step.&lt;/p>
&lt;h3 id="21-oura-ring-generation-3--4">2.1 Oura Ring (Generation 3 / 4)
&lt;/h3>&lt;p>Because it acquires data directly from the arteries in the finger, it features extremely high accuracy in measuring heart rate, heart rate variability (HRV), and changes in body surface temperature during sleep, compared to smartwatches measured on the wrist. Capillaries are dense in the fingers, allowing for low-noise data acquisition using optical heart rate sensors (PPG: Photoplethysmography). In addition, it has a comprehensive REST API and raw data in JSON format can be easily exported via OAuth 2.0, making it the most hackable device for engineers.&lt;/p>
&lt;h3 id="22-apple-watch-series--ultra">2.2 Apple Watch Series / Ultra
&lt;/h3>&lt;p>It excels in tracking during activities, and measuring blood oxygen saturation (SpO2) and electrocardiograms (ECG). It is the strongest device for daytime activity tracking and on-demand HRV measurement through mindfulness apps (breathe apps). However, exporting data requires going through HealthKit, and direct access from Python or others requires an intermediate step such as CSV export via an iOS app (like AutoSleep or HealthFit).&lt;/p>
&lt;h3 id="23-garmin-fenix--forerunner">2.3 Garmin (Fenix / Forerunner)
&lt;/h3>&lt;p>In addition to the accuracy of GPS tracking, its unique energy remainder indicator called &amp;ldquo;Body Battery&amp;rdquo; is excellent. This is calculated based on HRV and stress levels. Garmin data can be acquired through the Garmin Connect API, but due to the barrier of a corporate API, individual developers need to use open-source libraries or scraping tools created by volunteers.&lt;/p>
&lt;p>In this article, we will focus on &lt;strong>Oura Ring&lt;/strong>, which is the peak of sleep and recovery tracking and extremely easy to extract data from its API, and &lt;strong>WakaTime&lt;/strong>, which measures coding time as a plugin for IDEs (such as VS Code and IntelliJ).&lt;/p>
&lt;h2 id="3-basic-theory-of-biological-data-data-science-of-hrv-and-rhr">3. Basic Theory of Biological Data: Data Science of HRV and RHR
&lt;/h2>&lt;p>Rather than the simple metric of &amp;ldquo;longer sleep time is better,&amp;rdquo; from a data science perspective, the following two metrics are the master metrics of &amp;ldquo;Recovery&amp;rdquo;.&lt;/p>
&lt;h3 id="31-hrv-heart-rate-variability-and-autonomic-nervous-system-modeling">3.1 HRV (Heart Rate Variability) and Autonomic Nervous System Modeling
&lt;/h3>&lt;p>The heart does not beat at a constant rhythm like a metronome. For example, even if the heart rate is 60 bpm, the interval between each beat (R-R interval) is constantly fluctuating, such as &amp;ldquo;0.92 seconds&amp;rdquo;, &amp;ldquo;1.05 seconds&amp;rdquo;, and &amp;ldquo;0.98 seconds&amp;rdquo;. The quantified magnitude of this fluctuation is HRV (Heart Rate Variability).&lt;/p>
&lt;p>HRV directly reflects the autonomic nervous system, that is, the balance between the &amp;ldquo;sympathetic nervous system (accelerator)&amp;rdquo; and the &amp;ldquo;parasympathetic nervous system (brake)&amp;rdquo;. In states of stress, overwork, or after alcohol consumption, the sympathetic nervous system becomes dominant, heartbeats become more constant, and HRV decreases. Conversely, in a state of sufficient relaxation and recovery, the parasympathetic nervous system (vagus nerve) becomes dominant, and the heartbeat dynamically fluctuates with breathing, leading to high HRV.&lt;/p>
&lt;p>There are two approaches to calculating HRV: Time-domain and Frequency-domain. However, &lt;strong>RMSSD (Root Mean Square of Successive Differences)&lt;/strong> is the most commonly used in time-domain analysis and is also adopted by Oura Ring and Apple Watch. This calculates the root mean square of successive differences between normal heartbeats (RR intervals).&lt;/p>
&lt;p>Strictly expressed mathematically, it is as follows:&lt;/p>
$$ RMSSD = \sqrt{\frac{1}{N-1} \sum_{i=1}^{N-1} (RR_{i+1} - RR_i)^2} $$&lt;p>Here,&lt;/p>
&lt;ul>
&lt;li>$N$ is the total number of measured heartbeats&lt;/li>
&lt;li>$RR_i$ is the $i$-th RR interval (in milliseconds)&lt;/li>
&lt;/ul>
&lt;p>For engineers, if the HRV (RMSSD) upon waking up in the morning is significantly lower than their personal baseline (the moving average of the past few weeks), they can make a data-driven decision: &amp;ldquo;Today should be a day to avoid high cognitive load architecture design or deploying to the production environment, and instead focus on expanding test codes or writing documentation&amp;rdquo;.&lt;/p>
&lt;h3 id="32-rhr-resting-heart-rate-and-recovery-signals">3.2 RHR (Resting Heart Rate) and Recovery Signals
&lt;/h3>&lt;p>RHR is the number of heartbeats per minute when the body is in a state of complete rest (usually during sleep). When the body is allocating energy to internal metabolism or immune responses, such as after drinking alcohol, late-night overeating, or early symptoms of illness (e.g., infections), RHR rises several to over ten bpm above the baseline.&lt;/p>
&lt;p>A lower RHR means that the heart muscle can pump more blood with a single beat (higher stroke volume), indicating high aerobic capacity and the degree of recovery from fatigue. Ideally, a &amp;ldquo;hammock-shaped&amp;rdquo; curve where the RHR reaches its lowest value in the first half of sleep indicates the highest quality of recovery.&lt;/p>
&lt;h2 id="4-detailed-analysis-of-sleep-architecture">4. Detailed Analysis of Sleep Architecture
&lt;/h2>&lt;p>What determines the brain performance of an engineer is not just the &amp;ldquo;quantity&amp;rdquo; but the &amp;ldquo;quality&amp;rdquo; of sleep, namely Sleep Architecture. A night&amp;rsquo;s sleep usually repeats a 90 to 110-minute cycle 4 to 5 times.&lt;/p>
&lt;h3 id="41-nrem-sleep-stage-1-2-light-sleep">4.1 NREM Sleep Stage 1-2 (Light Sleep)
&lt;/h3>&lt;p>This is a preparatory stage where brain waves gradually slow down and the body begins to relax. It accounts for about 50% of total sleep. Although its contribution to cognitive recovery is small, it serves as an important bridge to transition to the next deep sleep stage.&lt;/p>
&lt;h3 id="42-nrem-sleep-stage-3-deep-sleep--slow-wave-sleep-sws">4.2 NREM Sleep Stage 3 (Deep Sleep / Slow Wave Sleep: SWS)
&lt;/h3>&lt;p>This is the core time for physical body recovery, where delta waves (low frequency of 0.5-2Hz) appear in brain waves. Growth hormone is secreted in large amounts, and cell repair takes place. It is also essential for strengthening the immune system, directly relating not only to the muscle fatigue recovery of athletes but also to the repair of eye strain and neck/shoulder muscles for engineers. Deep sleep typically concentrates in the first half of the sleep cycle.&lt;/p>
&lt;h3 id="43-rem-sleep-rapid-eye-movement">4.3 REM Sleep (Rapid Eye Movement)
&lt;/h3>&lt;p>The brain is as active as when awake, but the body&amp;rsquo;s muscles are in a paralyzed state. REM sleep is extremely important for engineers, as it plays the role of organizing the syntax of new programming languages or the concepts of complex algorithms learned during the day in the brain, consolidating them into long-term memory (Memory Consolidation). It enhances neuroplasticity and strengthens creative problem-solving abilities (the inspiration like &amp;ldquo;suddenly coming up with a bug fix while taking a shower&amp;rdquo;). REM sleep tends to be longer in the latter half of sleep (early morning).&lt;/p>
&lt;p>In other words, &amp;ldquo;cutting sleep time by forcing oneself to wake up early with an alarm&amp;rdquo; means locally and significantly cutting REM sleep, which is related to memory consolidation and creativity, and is an act equivalent to a critical bug that severely degrades performance as an engineer.&lt;/p>
&lt;h2 id="5-continuous-glucose-monitoring-cgm-and-spike-defense">5. Continuous Glucose Monitoring (CGM) and Spike Defense
&lt;/h2>&lt;p>Recently, the introduction of CGM (Continuous Glucose Monitor) has become essential among biohackers. Representative devices include FreeStyle Libre and Dexcom.
When consuming food (especially carbohydrates and sugar), the glucose concentration in the blood sharply rises (blood sugar spike) and then plunges due to the massive secretion of insulin (crash). At the timing of this &amp;ldquo;crash&amp;rdquo;, intense drowsiness (Brain Fog) and decreased concentration are triggered. The drowsiness of the &amp;ldquo;devil&amp;rsquo;s 2 PM&amp;rdquo; after lunch is likely caused not merely by the biological clock, but by a blood sugar spike due to excessive intake of ramen or white rice.&lt;/p>
&lt;p>The blood glucose response curve $G(t)$ can be approximately expressed as a damped oscillation model as the difference between the absorption rate of ingested carbohydrates and the clearance by insulin:&lt;/p>
$$ G(t) = G_{base} + \Delta G \cdot e^{-\alpha t} \sin(\beta t) $$&lt;p>Here,&lt;/p>
&lt;ul>
&lt;li>$G_{base}$: Fasting blood glucose level (baseline)&lt;/li>
&lt;li>$\Delta G$: Amplitude of blood glucose rise due to meals&lt;/li>
&lt;li>$\alpha$: Damping coefficient based on insulin sensitivity and metabolic rate&lt;/li>
&lt;li>$\beta$: Frequency component of the oscillation&lt;/li>
&lt;li>$t$: Elapsed time after the meal&lt;/li>
&lt;/ul>
&lt;p>To maintain engineer performance, it is crucial to minimize the amplitude $\Delta G$. Specifically, hacks like &amp;ldquo;eating vegetables (dietary fiber) first&amp;rdquo;, &amp;ldquo;avoiding refined carbohydrates&amp;rdquo;, and &amp;ldquo;taking a light 15-minute walk after a meal (activating GLUT4 transporters to take glucose into muscles independently of insulin)&amp;rdquo; are effective.&lt;/p>
&lt;h2 id="6-architecture-design-building-a-local-data-pipeline">6. Architecture Design: Building a Local Data Pipeline
&lt;/h2>&lt;p>We will build a local data pipeline to integrate and analyze biological data and productivity data.
The following Mermaid diagram (flowchart) shows the flow from acquiring data from the API to visualizing it on a dashboard.&lt;/p>
&lt;pre class="mermaid">
flowchart TD
A[&amp;#34;Oura Ring API (Biometrics)&amp;#34;] --&amp;gt;|JSON via OAuth2| C[&amp;#34;Python Data Ingestion Pipeline&amp;#34;]
B[&amp;#34;WakaTime API (Code Time)&amp;#34;] --&amp;gt;|JSON via API Key| C
E[&amp;#34;SwitchBot API (Room Temp / CO2)&amp;#34;] --&amp;gt;|JSON via API Key| C
C --&amp;gt;|Extract &amp;amp; Transform| D[&amp;#34;Pandas DataFrame (Memory)&amp;#34;]
D --&amp;gt;|Load| F[&amp;#34;TimescaleDB / PostgreSQL&amp;#34;]
F --&amp;gt; G[&amp;#34;Jupyter Notebook (Ad-hoc Analysis)&amp;#34;]
F --&amp;gt; H[&amp;#34;Streamlit Web App (Daily Monitoring)&amp;#34;]
&lt;/pre>
&lt;p>With this architecture, you can automatically monitor the correlation between your physical condition (input) and coding performance (output) every day.&lt;/p>
&lt;p>Let&amp;rsquo;s look at the sequence between systems in more detail.&lt;/p>
&lt;pre class="mermaid">
sequenceDiagram
participant U as &amp;#34;User (Engineer)&amp;#34;
participant W as &amp;#34;WakaTime Plugin (VS Code)&amp;#34;
participant O as &amp;#34;Oura Ring &amp;amp; API&amp;#34;
participant S as &amp;#34;Python ETL Batch&amp;#34;
participant DB as &amp;#34;TimescaleDB&amp;#34;
U-&amp;gt;&amp;gt;W: &amp;#34;Write code (Keyboard typing)&amp;#34;
W--&amp;gt;&amp;gt;W: &amp;#34;Log precise coding heartbeat&amp;#34;
U-&amp;gt;&amp;gt;O: &amp;#34;Sleep (Wear ring)&amp;#34;
O--&amp;gt;&amp;gt;O: &amp;#34;Record HRV, RHR, Temp &amp;amp; Sleep Stages&amp;#34;
S-&amp;gt;&amp;gt;W: &amp;#34;GET /api/v1/users/current/summaries&amp;#34;
S-&amp;gt;&amp;gt;O: &amp;#34;GET /v2/usercollection/sleep&amp;#34;
S-&amp;gt;&amp;gt;S: &amp;#34;Clean and Merge Data (Pandas)&amp;#34;
S-&amp;gt;&amp;gt;DB: &amp;#34;INSERT INTO daily_metrics&amp;#34;
Note over S,DB: &amp;#34;Scheduled via Cron / Airflow&amp;#34;
&lt;/pre>
&lt;h2 id="7-data-ingestion-with-python-and-pandas">7. Data Ingestion with Python and Pandas
&lt;/h2>&lt;p>Let&amp;rsquo;s look at the process of actually using a Python script to acquire data from the Oura Ring and WakaTime APIs and integrate it as a Pandas DataFrame. We will build robust code capable of withstanding practical operation.&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt"> 1
&lt;/span>&lt;span class="lnt"> 2
&lt;/span>&lt;span class="lnt"> 3
&lt;/span>&lt;span class="lnt"> 4
&lt;/span>&lt;span class="lnt"> 5
&lt;/span>&lt;span class="lnt"> 6
&lt;/span>&lt;span class="lnt"> 7
&lt;/span>&lt;span class="lnt"> 8
&lt;/span>&lt;span class="lnt"> 9
&lt;/span>&lt;span class="lnt">10
&lt;/span>&lt;span class="lnt">11
&lt;/span>&lt;span class="lnt">12
&lt;/span>&lt;span class="lnt">13
&lt;/span>&lt;span class="lnt">14
&lt;/span>&lt;span class="lnt">15
&lt;/span>&lt;span class="lnt">16
&lt;/span>&lt;span class="lnt">17
&lt;/span>&lt;span class="lnt">18
&lt;/span>&lt;span class="lnt">19
&lt;/span>&lt;span class="lnt">20
&lt;/span>&lt;span class="lnt">21
&lt;/span>&lt;span class="lnt">22
&lt;/span>&lt;span class="lnt">23
&lt;/span>&lt;span class="lnt">24
&lt;/span>&lt;span class="lnt">25
&lt;/span>&lt;span class="lnt">26
&lt;/span>&lt;span class="lnt">27
&lt;/span>&lt;span class="lnt">28
&lt;/span>&lt;span class="lnt">29
&lt;/span>&lt;span class="lnt">30
&lt;/span>&lt;span class="lnt">31
&lt;/span>&lt;span class="lnt">32
&lt;/span>&lt;span class="lnt">33
&lt;/span>&lt;span class="lnt">34
&lt;/span>&lt;span class="lnt">35
&lt;/span>&lt;span class="lnt">36
&lt;/span>&lt;span class="lnt">37
&lt;/span>&lt;span class="lnt">38
&lt;/span>&lt;span class="lnt">39
&lt;/span>&lt;span class="lnt">40
&lt;/span>&lt;span class="lnt">41
&lt;/span>&lt;span class="lnt">42
&lt;/span>&lt;span class="lnt">43
&lt;/span>&lt;span class="lnt">44
&lt;/span>&lt;span class="lnt">45
&lt;/span>&lt;span class="lnt">46
&lt;/span>&lt;span class="lnt">47
&lt;/span>&lt;span class="lnt">48
&lt;/span>&lt;span class="lnt">49
&lt;/span>&lt;span class="lnt">50
&lt;/span>&lt;span class="lnt">51
&lt;/span>&lt;span class="lnt">52
&lt;/span>&lt;span class="lnt">53
&lt;/span>&lt;span class="lnt">54
&lt;/span>&lt;span class="lnt">55
&lt;/span>&lt;span class="lnt">56
&lt;/span>&lt;span class="lnt">57
&lt;/span>&lt;span class="lnt">58
&lt;/span>&lt;span class="lnt">59
&lt;/span>&lt;span class="lnt">60
&lt;/span>&lt;span class="lnt">61
&lt;/span>&lt;span class="lnt">62
&lt;/span>&lt;span class="lnt">63
&lt;/span>&lt;span class="lnt">64
&lt;/span>&lt;span class="lnt">65
&lt;/span>&lt;span class="lnt">66
&lt;/span>&lt;span class="lnt">67
&lt;/span>&lt;span class="lnt">68
&lt;/span>&lt;span class="lnt">69
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-python" data-lang="python">&lt;span class="line">&lt;span class="cl">&lt;span class="kn">import&lt;/span> &lt;span class="nn">requests&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="kn">import&lt;/span> &lt;span class="nn">pandas&lt;/span> &lt;span class="k">as&lt;/span> &lt;span class="nn">pd&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="kn">from&lt;/span> &lt;span class="nn">datetime&lt;/span> &lt;span class="kn">import&lt;/span> &lt;span class="n">datetime&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">timedelta&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="kn">import&lt;/span> &lt;span class="nn">os&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># Environment Variables&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">OURA_TOKEN&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">os&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">getenv&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;OURA_ACCESS_TOKEN&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">WAKATIME_API_KEY&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">os&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">getenv&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;WAKATIME_API_KEY&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">def&lt;/span> &lt;span class="nf">fetch_oura_sleep_data&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">start_date&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="nb">str&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">end_date&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="nb">str&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="o">-&amp;gt;&lt;/span> &lt;span class="n">pd&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">DataFrame&lt;/span>&lt;span class="p">:&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="s2">&amp;#34;&amp;#34;&amp;#34;Fetch daily sleep summary from Oura Ring API v2.&amp;#34;&amp;#34;&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">url&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="s2">&amp;#34;https://api.ouraring.com/v2/usercollection/sleep&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">params&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="p">{&lt;/span>&lt;span class="s2">&amp;#34;start_date&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="n">start_date&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s2">&amp;#34;end_date&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="n">end_date&lt;/span>&lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">headers&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="p">{&lt;/span>&lt;span class="s2">&amp;#34;Authorization&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="sa">f&lt;/span>&lt;span class="s2">&amp;#34;Bearer &lt;/span>&lt;span class="si">{&lt;/span>&lt;span class="n">OURA_TOKEN&lt;/span>&lt;span class="si">}&lt;/span>&lt;span class="s2">&amp;#34;&lt;/span>&lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">response&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">requests&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">get&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">url&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">headers&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="n">headers&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">params&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="n">params&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">response&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">raise_for_status&lt;/span>&lt;span class="p">()&lt;/span> &lt;span class="c1"># Raise exception for 4xx/5xx errors&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">data&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">response&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">json&lt;/span>&lt;span class="p">()&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">get&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;data&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="p">[])&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">if&lt;/span> &lt;span class="ow">not&lt;/span> &lt;span class="n">data&lt;/span>&lt;span class="p">:&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">return&lt;/span> &lt;span class="n">pd&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">DataFrame&lt;/span>&lt;span class="p">()&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">df&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">pd&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">json_normalize&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">data&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="c1"># Extract deeply nested values or select essential columns&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">df&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">df&lt;/span>&lt;span class="p">[[&lt;/span>&lt;span class="s1">&amp;#39;day&amp;#39;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s1">&amp;#39;score&amp;#39;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s1">&amp;#39;time_in_bed&amp;#39;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s1">&amp;#39;total_sleep_duration&amp;#39;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="s1">&amp;#39;average_hrv&amp;#39;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s1">&amp;#39;lowest_heart_rate&amp;#39;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="s1">&amp;#39;deep_sleep_duration&amp;#39;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s1">&amp;#39;rem_sleep_duration&amp;#39;&lt;/span>&lt;span class="p">]]&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="c1"># Convert dates to datetime objects and set as index&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">df&lt;/span>&lt;span class="p">[&lt;/span>&lt;span class="s1">&amp;#39;day&amp;#39;&lt;/span>&lt;span class="p">]&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">pd&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">to_datetime&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">df&lt;/span>&lt;span class="p">[&lt;/span>&lt;span class="s1">&amp;#39;day&amp;#39;&lt;/span>&lt;span class="p">])&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">df&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">set_index&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s1">&amp;#39;day&amp;#39;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">inplace&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="kc">True&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">return&lt;/span> &lt;span class="n">df&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">def&lt;/span> &lt;span class="nf">fetch_wakatime_data&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">start_date&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="nb">str&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">end_date&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="nb">str&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="o">-&amp;gt;&lt;/span> &lt;span class="n">pd&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">DataFrame&lt;/span>&lt;span class="p">:&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="s2">&amp;#34;&amp;#34;&amp;#34;Fetch coding duration summaries from WakaTime API.&amp;#34;&amp;#34;&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">url&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="s2">&amp;#34;https://wakatime.com/api/v1/users/current/summaries&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">params&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="p">{&lt;/span>&lt;span class="s2">&amp;#34;start&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="n">start_date&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s2">&amp;#34;end&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="n">end_date&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s2">&amp;#34;api_key&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="n">WAKATIME_API_KEY&lt;/span>&lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">response&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">requests&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">get&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">url&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">params&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="n">params&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">response&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">raise_for_status&lt;/span>&lt;span class="p">()&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">data&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">response&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">json&lt;/span>&lt;span class="p">()&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">get&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;data&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="p">[])&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">records&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="p">[]&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">for&lt;/span> &lt;span class="n">day_data&lt;/span> &lt;span class="ow">in&lt;/span> &lt;span class="n">data&lt;/span>&lt;span class="p">:&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">date_str&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">day_data&lt;/span>&lt;span class="p">[&lt;/span>&lt;span class="s1">&amp;#39;range&amp;#39;&lt;/span>&lt;span class="p">][&lt;/span>&lt;span class="s1">&amp;#39;date&amp;#39;&lt;/span>&lt;span class="p">]&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="c1"># Extract total seconds spent coding&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">total_seconds&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">day_data&lt;/span>&lt;span class="p">[&lt;/span>&lt;span class="s1">&amp;#39;grand_total&amp;#39;&lt;/span>&lt;span class="p">][&lt;/span>&lt;span class="s1">&amp;#39;total_seconds&amp;#39;&lt;/span>&lt;span class="p">]&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">records&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">append&lt;/span>&lt;span class="p">({&lt;/span>&lt;span class="s1">&amp;#39;day&amp;#39;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="n">date_str&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s1">&amp;#39;coding_hours&amp;#39;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="n">total_seconds&lt;/span> &lt;span class="o">/&lt;/span> &lt;span class="mf">3600.0&lt;/span>&lt;span class="p">})&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">df&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">pd&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">DataFrame&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">records&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">if&lt;/span> &lt;span class="ow">not&lt;/span> &lt;span class="n">df&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">empty&lt;/span>&lt;span class="p">:&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">df&lt;/span>&lt;span class="p">[&lt;/span>&lt;span class="s1">&amp;#39;day&amp;#39;&lt;/span>&lt;span class="p">]&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">pd&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">to_datetime&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">df&lt;/span>&lt;span class="p">[&lt;/span>&lt;span class="s1">&amp;#39;day&amp;#39;&lt;/span>&lt;span class="p">])&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">df&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">set_index&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s1">&amp;#39;day&amp;#39;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">inplace&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="kc">True&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">return&lt;/span> &lt;span class="n">df&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">if&lt;/span> &lt;span class="vm">__name__&lt;/span> &lt;span class="o">==&lt;/span> &lt;span class="s2">&amp;#34;__main__&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="c1"># Fetch data for the last 60 days&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">end&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">datetime&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">now&lt;/span>&lt;span class="p">()&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">strftime&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;%Y-%m-&lt;/span>&lt;span class="si">%d&lt;/span>&lt;span class="s2">&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">start&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="p">(&lt;/span>&lt;span class="n">datetime&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">now&lt;/span>&lt;span class="p">()&lt;/span> &lt;span class="o">-&lt;/span> &lt;span class="n">timedelta&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">days&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="mi">60&lt;/span>&lt;span class="p">))&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">strftime&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;%Y-%m-&lt;/span>&lt;span class="si">%d&lt;/span>&lt;span class="s2">&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">oura_df&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">fetch_oura_sleep_data&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">start&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">end&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">waka_df&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">fetch_wakatime_data&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">start&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">end&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="c1"># Merge datasets on &amp;#39;day&amp;#39; index using inner join&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">merged_df&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">pd&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">merge&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">oura_df&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">waka_df&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">left_index&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="kc">True&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">right_index&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="kc">True&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">how&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="s1">&amp;#39;inner&amp;#39;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="c1"># Save raw data to CSV/DB&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">merged_df&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">to_csv&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;health_productivity_raw.csv&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nb">print&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="sa">f&lt;/span>&lt;span class="s2">&amp;#34;Ingested &lt;/span>&lt;span class="si">{&lt;/span>&lt;span class="nb">len&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">merged_df&lt;/span>&lt;span class="p">)&lt;/span>&lt;span class="si">}&lt;/span>&lt;span class="s2"> days of data.&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;h2 id="8-data-preprocessing-and-feature-engineering">8. Data Preprocessing and Feature Engineering
&lt;/h2>&lt;p>It is dangerous to analyze the acquired raw data as is. It is necessary to handle missing values due to forgetting to charge the device and generate meaningful new indicators (Feature Engineering).&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt"> 1
&lt;/span>&lt;span class="lnt"> 2
&lt;/span>&lt;span class="lnt"> 3
&lt;/span>&lt;span class="lnt"> 4
&lt;/span>&lt;span class="lnt"> 5
&lt;/span>&lt;span class="lnt"> 6
&lt;/span>&lt;span class="lnt"> 7
&lt;/span>&lt;span class="lnt"> 8
&lt;/span>&lt;span class="lnt"> 9
&lt;/span>&lt;span class="lnt">10
&lt;/span>&lt;span class="lnt">11
&lt;/span>&lt;span class="lnt">12
&lt;/span>&lt;span class="lnt">13
&lt;/span>&lt;span class="lnt">14
&lt;/span>&lt;span class="lnt">15
&lt;/span>&lt;span class="lnt">16
&lt;/span>&lt;span class="lnt">17
&lt;/span>&lt;span class="lnt">18
&lt;/span>&lt;span class="lnt">19
&lt;/span>&lt;span class="lnt">20
&lt;/span>&lt;span class="lnt">21
&lt;/span>&lt;span class="lnt">22
&lt;/span>&lt;span class="lnt">23
&lt;/span>&lt;span class="lnt">24
&lt;/span>&lt;span class="lnt">25
&lt;/span>&lt;span class="lnt">26
&lt;/span>&lt;span class="lnt">27
&lt;/span>&lt;span class="lnt">28
&lt;/span>&lt;span class="lnt">29
&lt;/span>&lt;span class="lnt">30
&lt;/span>&lt;span class="lnt">31
&lt;/span>&lt;span class="lnt">32
&lt;/span>&lt;span class="lnt">33
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-python" data-lang="python">&lt;span class="line">&lt;span class="cl">&lt;span class="k">def&lt;/span> &lt;span class="nf">engineer_features&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">df&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="n">pd&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">DataFrame&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="o">-&amp;gt;&lt;/span> &lt;span class="n">pd&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">DataFrame&lt;/span>&lt;span class="p">:&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="s2">&amp;#34;&amp;#34;&amp;#34;Apply feature engineering and cleaning to the merged dataframe.&amp;#34;&amp;#34;&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">df&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">df&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">copy&lt;/span>&lt;span class="p">()&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="c1"># 1. Handle missing values (e.g., forward fill)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">df&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">fillna&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">method&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="s1">&amp;#39;ffill&amp;#39;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">inplace&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="kc">True&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="c1"># 2. Calculate Sleep Efficiency&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="c1"># Formula: (Total Sleep Time / Time in Bed) * 100&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">df&lt;/span>&lt;span class="p">[&lt;/span>&lt;span class="s1">&amp;#39;sleep_efficiency_pct&amp;#39;&lt;/span>&lt;span class="p">]&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="p">(&lt;/span>&lt;span class="n">df&lt;/span>&lt;span class="p">[&lt;/span>&lt;span class="s1">&amp;#39;total_sleep_duration&amp;#39;&lt;/span>&lt;span class="p">]&lt;/span> &lt;span class="o">/&lt;/span> &lt;span class="n">df&lt;/span>&lt;span class="p">[&lt;/span>&lt;span class="s1">&amp;#39;time_in_bed&amp;#39;&lt;/span>&lt;span class="p">])&lt;/span> &lt;span class="o">*&lt;/span> &lt;span class="mi">100&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="c1"># 3. Calculate Sleep Stage Ratios&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">df&lt;/span>&lt;span class="p">[&lt;/span>&lt;span class="s1">&amp;#39;rem_ratio&amp;#39;&lt;/span>&lt;span class="p">]&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">df&lt;/span>&lt;span class="p">[&lt;/span>&lt;span class="s1">&amp;#39;rem_sleep_duration&amp;#39;&lt;/span>&lt;span class="p">]&lt;/span> &lt;span class="o">/&lt;/span> &lt;span class="n">df&lt;/span>&lt;span class="p">[&lt;/span>&lt;span class="s1">&amp;#39;total_sleep_duration&amp;#39;&lt;/span>&lt;span class="p">]&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">df&lt;/span>&lt;span class="p">[&lt;/span>&lt;span class="s1">&amp;#39;deep_ratio&amp;#39;&lt;/span>&lt;span class="p">]&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">df&lt;/span>&lt;span class="p">[&lt;/span>&lt;span class="s1">&amp;#39;deep_sleep_duration&amp;#39;&lt;/span>&lt;span class="p">]&lt;/span> &lt;span class="o">/&lt;/span> &lt;span class="n">df&lt;/span>&lt;span class="p">[&lt;/span>&lt;span class="s1">&amp;#39;total_sleep_duration&amp;#39;&lt;/span>&lt;span class="p">]&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="c1"># 4. Calculate 7-day Moving Averages (Rolling Mean) to smooth out daily noise&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">df&lt;/span>&lt;span class="p">[&lt;/span>&lt;span class="s1">&amp;#39;hrv_7d_ma&amp;#39;&lt;/span>&lt;span class="p">]&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">df&lt;/span>&lt;span class="p">[&lt;/span>&lt;span class="s1">&amp;#39;average_hrv&amp;#39;&lt;/span>&lt;span class="p">]&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">rolling&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">window&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="mi">7&lt;/span>&lt;span class="p">)&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">mean&lt;/span>&lt;span class="p">()&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">df&lt;/span>&lt;span class="p">[&lt;/span>&lt;span class="s1">&amp;#39;rhr_7d_ma&amp;#39;&lt;/span>&lt;span class="p">]&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">df&lt;/span>&lt;span class="p">[&lt;/span>&lt;span class="s1">&amp;#39;lowest_heart_rate&amp;#39;&lt;/span>&lt;span class="p">]&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">rolling&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">window&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="mi">7&lt;/span>&lt;span class="p">)&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">mean&lt;/span>&lt;span class="p">()&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="c1"># 5. Calculate daily deviation from baseline&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">df&lt;/span>&lt;span class="p">[&lt;/span>&lt;span class="s1">&amp;#39;hrv_deviation&amp;#39;&lt;/span>&lt;span class="p">]&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">df&lt;/span>&lt;span class="p">[&lt;/span>&lt;span class="s1">&amp;#39;average_hrv&amp;#39;&lt;/span>&lt;span class="p">]&lt;/span> &lt;span class="o">-&lt;/span> &lt;span class="n">df&lt;/span>&lt;span class="p">[&lt;/span>&lt;span class="s1">&amp;#39;hrv_7d_ma&amp;#39;&lt;/span>&lt;span class="p">]&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="c1"># 6. Normalize targets for Machine Learning (Optional)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="kn">from&lt;/span> &lt;span class="nn">sklearn.preprocessing&lt;/span> &lt;span class="kn">import&lt;/span> &lt;span class="n">MinMaxScaler&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">scaler&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">MinMaxScaler&lt;/span>&lt;span class="p">()&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">df&lt;/span>&lt;span class="p">[[&lt;/span>&lt;span class="s1">&amp;#39;hrv_scaled&amp;#39;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s1">&amp;#39;coding_scaled&amp;#39;&lt;/span>&lt;span class="p">]]&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">scaler&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">fit_transform&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">df&lt;/span>&lt;span class="p">[[&lt;/span>&lt;span class="s1">&amp;#39;average_hrv&amp;#39;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s1">&amp;#39;coding_hours&amp;#39;&lt;/span>&lt;span class="p">]])&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="c1"># Drop rows with NaN generated by rolling window&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">df&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">dropna&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">inplace&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="kc">True&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">return&lt;/span> &lt;span class="n">df&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">processed_df&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">engineer_features&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">merged_df&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;h2 id="9-correlation-analysis-the-intersection-of-productivity-and-health-metrics">9. Correlation Analysis: The Intersection of Productivity and Health Metrics
&lt;/h2>&lt;p>Based on the preprocessed data, we will analyze the relationship between health metrics and coding productivity. The hypothesis is that &amp;ldquo;On days with high HRV (when the autonomic nervous system is regulated and recovered), concentration is sustained, coding time is longer, or more complex tasks can be handled&amp;rdquo;.&lt;/p>
&lt;pre class="mermaid">
xychart-beta
title HRV vs Daily Coding Hours (Normalized)
x-axis [&amp;#34;Mon&amp;#34;, &amp;#34;Tue&amp;#34;, &amp;#34;Wed&amp;#34;, &amp;#34;Thu&amp;#34;, &amp;#34;Fri&amp;#34;, &amp;#34;Sat&amp;#34;, &amp;#34;Sun&amp;#34;]
y-axis &amp;#34;HRV &amp;amp; Coding Output&amp;#34; 10 --&amp;gt; 100
line [45, 52, 65, 75, 70, 58, 48]
bar [35, 42, 58, 65, 60, 20, 15]
&lt;/pre>
&lt;p>&lt;em>(Note: The line chart shows the deviation of HRV from the baseline, and the bar chart shows WakaTime coding hours. A correlation can be seen where coding output is maximized from Wednesday to Friday, when sufficient recovery is achieved)&lt;/em>&lt;/p>
&lt;p>We will calculate the correlation coefficient (Pearson&amp;rsquo;s product-moment correlation coefficient $r$) in Pandas and test for statistical significance (p-value) using SciPy.&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt"> 1
&lt;/span>&lt;span class="lnt"> 2
&lt;/span>&lt;span class="lnt"> 3
&lt;/span>&lt;span class="lnt"> 4
&lt;/span>&lt;span class="lnt"> 5
&lt;/span>&lt;span class="lnt"> 6
&lt;/span>&lt;span class="lnt"> 7
&lt;/span>&lt;span class="lnt"> 8
&lt;/span>&lt;span class="lnt"> 9
&lt;/span>&lt;span class="lnt">10
&lt;/span>&lt;span class="lnt">11
&lt;/span>&lt;span class="lnt">12
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-python" data-lang="python">&lt;span class="line">&lt;span class="cl">&lt;span class="kn">import&lt;/span> &lt;span class="nn">scipy.stats&lt;/span> &lt;span class="k">as&lt;/span> &lt;span class="nn">stats&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># Select numerical columns for correlation matrix&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">cols_of_interest&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="p">[&lt;/span>&lt;span class="s1">&amp;#39;average_hrv&amp;#39;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s1">&amp;#39;score&amp;#39;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s1">&amp;#39;deep_sleep_duration&amp;#39;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s1">&amp;#39;rem_sleep_duration&amp;#39;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s1">&amp;#39;coding_hours&amp;#39;&lt;/span>&lt;span class="p">]&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">correlation_matrix&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">processed_df&lt;/span>&lt;span class="p">[&lt;/span>&lt;span class="n">cols_of_interest&lt;/span>&lt;span class="p">]&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">corr&lt;/span>&lt;span class="p">()&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nb">print&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;Correlation with Coding Hours:&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nb">print&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">correlation_matrix&lt;/span>&lt;span class="p">[&lt;/span>&lt;span class="s1">&amp;#39;coding_hours&amp;#39;&lt;/span>&lt;span class="p">]&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">sort_values&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">ascending&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="kc">False&lt;/span>&lt;span class="p">))&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># Calculate Pearson correlation coefficient and p-value for REM sleep and Coding Hours&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">r&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">p_value&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">stats&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">pearsonr&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">processed_df&lt;/span>&lt;span class="p">[&lt;/span>&lt;span class="s1">&amp;#39;rem_sleep_duration&amp;#39;&lt;/span>&lt;span class="p">],&lt;/span> &lt;span class="n">processed_df&lt;/span>&lt;span class="p">[&lt;/span>&lt;span class="s1">&amp;#39;coding_hours&amp;#39;&lt;/span>&lt;span class="p">])&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nb">print&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="sa">f&lt;/span>&lt;span class="s2">&amp;#34;REM Sleep vs Coding Hours: r = &lt;/span>&lt;span class="si">{&lt;/span>&lt;span class="n">r&lt;/span>&lt;span class="si">:&lt;/span>&lt;span class="s2">.3f&lt;/span>&lt;span class="si">}&lt;/span>&lt;span class="s2">, p-value = &lt;/span>&lt;span class="si">{&lt;/span>&lt;span class="n">p_value&lt;/span>&lt;span class="si">:&lt;/span>&lt;span class="s2">.4f&lt;/span>&lt;span class="si">}&lt;/span>&lt;span class="s2">&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>In many cases, a significant positive correlation ($p &lt; 0.05$) is observed between &lt;code>average_hrv&lt;/code> or &lt;code>rem_sleep_duration&lt;/code> and &lt;code>coding_hours&lt;/code>. In particular, it is heavily reported in the engineer Quantified Self community that the length of REM sleep the previous night strongly affects &amp;ldquo;time taken to resolve errors (debugging)&amp;rdquo; and &amp;ldquo;productivity&amp;rdquo; on the day.&lt;/p>
&lt;h2 id="10-mathematical-model-of-circadian-rhythm-and-cognitive-peak-optimization">10. Mathematical Model of Circadian Rhythm and Cognitive Peak Optimization
&lt;/h2>&lt;p>Humans are equipped with a biological clock called the Circadian Rhythm with a cycle of about 24 hours. This rhythm fluctuates body temperature, hormone secretion (morning cortisol spike and nighttime melatonin secretion), and &amp;ldquo;cognitive ability&amp;rdquo;.&lt;/p>
&lt;p>Fluctuations in circadian rhythm are often approximated by a mathematical model using cosine curves (Cosinor model), and changes in biometric indicators can be formulated as follows:&lt;/p>
$$ y(t) = M + A \cos\left(\frac{2\pi}{24}(t - \phi)\right) + e(t) $$&lt;ul>
&lt;li>$y(t)$: Biometric indicator at time $t$ (e.g., core body temperature or alertness)&lt;/li>
&lt;li>$M$: MESOR (Midline Estimating Statistic of Rhythm) - The central value (average level) of the rhythm&lt;/li>
&lt;li>$A$: Amplitude - The magnitude of the fluctuation&lt;/li>
&lt;li>$\phi$: Acrophase - The phase (time) of the peak&lt;/li>
&lt;li>$e(t)$: Error term due to environmental factors, etc.&lt;/li>
&lt;/ul>
&lt;p>In engineering, what this equation means is that &amp;ldquo;the time of day ($\phi$) when performance (alertness) peaks is biologically determined, and the tasks with the highest cognitive load (complex bug fixes, designing new architectures) should be assigned to that time block&amp;rdquo;.&lt;/p>
&lt;p>In the case of a typical morning lark chronotype, the first cognitive peak arrives 2 to 4 hours after waking up (for example, 9 AM to 11 AM). After that, the trough of the circadian rhythm (post-lunch dip) arrives around 2 PM, and another small peak comes in the evening. Identifying your peak time ($\phi$) from the activity level or subjective concentration of wearable data, and protecting your schedule like Google Calendar with &amp;ldquo;time blocking&amp;rdquo; is the best health hack. Putting a meaningless meeting during peak time is like assigning the highest-performing core of a CPU to an idle process.&lt;/p>
&lt;h2 id="11-caffeine-pharmacokinetics-and-optimal-intake-timing">11. Caffeine Pharmacokinetics and Optimal Intake Timing
&lt;/h2>&lt;p>Engineers and coffee are inseparable, but excessive caffeine intake or taking it late in the day blocks adenosine receptors in the brain and destroys &amp;ldquo;Deep Sleep&amp;rdquo; at night. Subjectively, you may feel like you are sleeping, but looking at Oura Ring data confirms that the heart rate doesn&amp;rsquo;t drop and the percentage of deep sleep drastically decreases.&lt;/p>
&lt;p>Caffeine elimination from the body follows first-order kinetics. That is, blood concentration decays exponentially.&lt;/p>
$$ C(t) = C_0 e^{-k t} $$&lt;p>Here,&lt;/p>
&lt;ul>
&lt;li>$C(t)$: Blood caffeine concentration after time $t$&lt;/li>
&lt;li>$C_0$: Initial concentration (maximum concentration right after intake)&lt;/li>
&lt;li>$k$: Elimination rate constant&lt;/li>
&lt;li>$t$: Elapsed time since intake (hours)&lt;/li>
&lt;/ul>
&lt;p>The elimination rate constant $k$ is expressed using the half-life of caffeine ($t_{1/2}$) as follows:&lt;/p>
$$ k = \frac{\ln(2)}{t_{1/2}} $$&lt;p>In the case of a healthy adult, depending on individual genetics (CYP1A2 gene), the half-life of caffeine $t_{1/2}$ is considered to be approximately &lt;strong>5 to 6 hours&lt;/strong>.
For example, suppose you drink a cup of drip coffee (about 150 mg of caffeine) at 3 PM ($C_0 = 150$). Assuming a half-life of 5.5 hours, $k \approx 0.126$.
Calculating the residual caffeine concentration in the body at bedtime of 11 PM (8 hours later):&lt;/p>
$$ C(8) = 150 \times e^{-0.126 \times 8} = 150 \times e^{-1.008} \approx 150 \times 0.365 = 54.75 \text{ mg} $$&lt;p>In other words, even when it&amp;rsquo;s time to sleep, 54 mg (a little over one espresso shot) of caffeine still remains in the body, and this directly negatively impacts sleep architecture.
The data-driven conclusion derived from this pharmacokinetic model is that &lt;strong>&amp;ldquo;To ensure high-quality sleep, caffeine intake should start 90 minutes after waking up (after the cortisol spike settles), and should be completely cut off by 2 PM at the latest (9 to 10 hours before bedtime).&amp;rdquo;&lt;/strong>&lt;/p>
&lt;h2 id="12-hacking-environment-variables-lux-temperature-co2">12. Hacking Environment Variables (Lux, Temperature, CO2)
&lt;/h2>&lt;p>It is important to optimize not only the internal system of one&amp;rsquo;s own body but also external environment variables.&lt;/p>
&lt;h3 id="121-programming-light-environment-lux">12.1 Programming Light Environment (Lux)
&lt;/h3>&lt;p>The most powerful &amp;ldquo;Zeitgeber (time cue)&amp;rdquo; to reset the circadian rhythm is light. In the morning, about 100,000 Lux of sunlight hitting the retinal photoreceptor cells (ipRGC) stops melatonin secretion and resets the timer. Conversely, at night, it is essential to block blue light and not inhibit melatonin secretion. Rather than just lowering the display color temperature with software like f.lux, it is effective to write a script to control smart lighting (like Philips Hue) via API and automatically lower the room&amp;rsquo;s illuminance and color temperature according to sunset.&lt;/p>
&lt;h3 id="122-bedroom-temperature-control-and-sleep-latency">12.2 Bedroom Temperature Control and Sleep Latency
&lt;/h3>&lt;p>Humans enter sleep as their core body temperature drops. Keeping the bedroom at a cool 18-19 degrees Celsius (64-66°F) and getting into bed aiming for the timing when the core body temperature, temporarily raised by a warm bath 90 minutes before bedtime, plummets, can drastically shorten sleep latency (the time it takes to fall asleep after getting into bed) and maximize deep sleep.&lt;/p>
&lt;h3 id="123-co2-concentration-and-cognitive-decline">12.3 CO2 Concentration and Cognitive Decline
&lt;/h3>&lt;p>When adding the API of a SwitchBot Hub or Netatmo weather station to the data pipeline, a clear negative correlation can be seen between indoor carbon dioxide (CO2) concentration and productivity.
As shown by studies from Harvard University and others, when the CO2 concentration exceeds 1,000 ppm, cognitive function (especially strategic decision-making ability) begins to significantly decline, and exceeding 2,000 ppm causes serious performance degradation. Remote work in a closed room during winter lowers performance without you even noticing.&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt"> 1
&lt;/span>&lt;span class="lnt"> 2
&lt;/span>&lt;span class="lnt"> 3
&lt;/span>&lt;span class="lnt"> 4
&lt;/span>&lt;span class="lnt"> 5
&lt;/span>&lt;span class="lnt"> 6
&lt;/span>&lt;span class="lnt"> 7
&lt;/span>&lt;span class="lnt"> 8
&lt;/span>&lt;span class="lnt"> 9
&lt;/span>&lt;span class="lnt">10
&lt;/span>&lt;span class="lnt">11
&lt;/span>&lt;span class="lnt">12
&lt;/span>&lt;span class="lnt">13
&lt;/span>&lt;span class="lnt">14
&lt;/span>&lt;span class="lnt">15
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-python" data-lang="python">&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># Pseudo-code for intelligent room ventilation using Home Assistant / SwitchBot API&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="kn">import&lt;/span> &lt;span class="nn">requests&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">def&lt;/span> &lt;span class="nf">check_and_ventilate&lt;/span>&lt;span class="p">():&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="c1"># Get current CO2 level from Netatmo/SwitchBot API&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">co2_ppm&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">get_sensor_data&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;co2_sensor_id&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">if&lt;/span> &lt;span class="n">co2_ppm&lt;/span> &lt;span class="o">&amp;gt;&lt;/span> &lt;span class="mi">1000&lt;/span>&lt;span class="p">:&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nb">print&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="sa">f&lt;/span>&lt;span class="s2">&amp;#34;Warning: CO2 level high (&lt;/span>&lt;span class="si">{&lt;/span>&lt;span class="n">co2_ppm&lt;/span>&lt;span class="si">}&lt;/span>&lt;span class="s2"> ppm). Cognitive decline risk.&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="c1"># Trigger smart plug to turn on ventilation fan&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">turn_on_smart_plug&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;ventilation_fan_id&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="c1"># Send notification to Slack/Discord&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">send_notification&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;Activated ventilation fan. CO2 concentration is high.&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">elif&lt;/span> &lt;span class="n">co2_ppm&lt;/span> &lt;span class="o">&amp;lt;&lt;/span> &lt;span class="mi">600&lt;/span>&lt;span class="p">:&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">turn_off_smart_plug&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;ventilation_fan_id&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>By periodically executing such a script with Cron, an autonomous environment control system that always maintains optimal oxygen concentration is completed.&lt;/p>
&lt;h2 id="13-conclusion-cicd-of-the-system-called-the-human-body">13. Conclusion: CI/CD of the System Called the Human Body
&lt;/h2>&lt;p>Try viewing your own body as a complex distributed system. Wearable devices (Oura Ring) are metrics exporters for monitoring (Prometheus), Python/Pandas scripts are log analysis pipelines (Logstash/Fluentd), and daily changes in physical condition and performance are the system health displayed on the dashboard (Grafana/Streamlit).&lt;/p>
&lt;p>&amp;ldquo;Working by cutting sleep time&amp;rdquo; is the same as forcing the addition of features while ignoring technical debt. You might make it for the release in the short term, but in the long term, it will inevitably cause a system down (burnout, serious health damage, depression).&lt;/p>
&lt;p>Monitor HRV, check RHR trends, and optimize sleep architecture. Then, finely tune &amp;ldquo;hyperparameters&amp;rdquo; such as diet, exercise, sleep, and environment daily, looking at the correlation with WakaTime productivity data. This is exactly the &lt;strong>CI/CD (Continuous Integration / Continuous Delivery)&lt;/strong> process for the human body.&lt;/p>
&lt;p>Let&amp;rsquo;s engineer a health condition that can perform at its best using data science and APIs. The quality of the code you write is directly linked to the health of your own biological system.&lt;/p>
&lt;hr>
&lt;p>&lt;em>Disclaimer: This article summarizes the author&amp;rsquo;s personal experiments and data science approaches, and does not provide medical advice. If you have continuous poor health or sleep disorders, please consult a specialized medical institution.&lt;/em>&lt;/p></description></item><item><title>The Trade-off Between Privacy and Convenience: The Fate of Personal Information in the Era of Big Data</title><link>http://kenji.blog/en/p/privacy-vs-convenience-big-data/</link><pubDate>Sat, 12 Sep 2026 12:00:00 +0900</pubDate><guid>http://kenji.blog/en/p/privacy-vs-convenience-big-data/</guid><description>&lt;img src="http://kenji.blog/p/privacy-vs-convenience-big-data/img/eyecatch.jpg" alt="Featured image of post The Trade-off Between Privacy and Convenience: The Fate of Personal Information in the Era of Big Data" />&lt;h1 id="the-trade-off-between-privacy-and-convenience-the-fate-of-personal-information-in-the-era-of-big-data">The Trade-off Between Privacy and Convenience: The Fate of Personal Information in the Era of Big Data
&lt;/h1>&lt;p>In today&amp;rsquo;s digital society, we generate enormous amounts of data in our daily lives. A wide variety of &amp;ldquo;big data&amp;rdquo;—such as smartphone location information, social media posts, online shopping purchase histories, and health data recorded by wearable devices—is constantly being collected. This data is essential for the evolution of AI (Artificial Intelligence) and the provision of personalized services, making our lives more convenient and richer.&lt;/p>
&lt;p>However, on the other hand, the risk of privacy infringement associated with the collection and use of personal information has emerged as a serious social issue. The risks lurking behind convenience have reached an undeniable scale, including data breaches, the provision of data to third parties without user consent, and concerns about a surveillance society by the state. This article provides an extremely detailed technical explanation of how technology and legal regulations are approaching the modern dilemma of this &amp;ldquo;trade-off between privacy and convenience,&amp;rdquo; along with the latest trends.&lt;/p>
&lt;h2 id="1-the-paradigm-of-a-data-driven-society-and-the-evolution-of-data-architecture">1. The Paradigm of a Data-Driven Society and the Evolution of Data Architecture
&lt;/h2>&lt;p>To collect and utilize data efficiently, companies are adopting various data architectures. There is an ongoing paradigm shift from the once-mainstream &amp;ldquo;Data Warehouse&amp;rdquo; to a &amp;ldquo;Data Lake&amp;rdquo; that centrally manages all data, including unstructured data, and now to a &amp;ldquo;Data Mesh,&amp;rdquo; which is a decentralized architecture.&lt;/p>
&lt;h3 id="centralized-data-lakes-and-anonymization-pipelines">Centralized Data Lakes and Anonymization Pipelines
&lt;/h3>&lt;p>A data lake is a storage repository that stores large amounts of raw data in its native format. However, using raw data containing PII (Personally Identifiable Information) directly for analysis causes serious compliance violations. Therefore, a strict &amp;ldquo;Anonymization Pipeline&amp;rdquo; is implemented between the data lake and the analysis environment.&lt;/p>
&lt;p>The figure below shows the flow of an anonymization pipeline in a typical centralized data lake.&lt;/p>
&lt;pre class="mermaid">
flowchart TD
A[&amp;#34;Data Sources (Web, IoT, Mobile)&amp;#34;] --&amp;gt;| Ingestion | B[&amp;#34;Raw Data Zone (Untouched)&amp;#34;]
B --&amp;gt;| ETL Process | C[&amp;#34;Anonymization &amp;amp; Cleansing Pipeline&amp;#34;]
C --&amp;gt;| Pseudonymization / Tokenization | D[&amp;#34;Trusted Zone (k-anonymized)&amp;#34;]
D --&amp;gt;| Feature Engineering | E[&amp;#34;Refined Zone (Ready for ML)&amp;#34;]
E --&amp;gt;| Model Training | F[&amp;#34;BI Tools &amp;amp; ML Models&amp;#34;]
C --&amp;gt;| Audit Logs | G[&amp;#34;Security &amp;amp; Compliance Hub&amp;#34;]
&lt;/pre>
&lt;p>In such pipelines, processes like hashing, masking, and encryption are automatically applied when data flows in. However, as discussed later, simple masking or pseudonymization cannot completely eliminate the risk of &amp;ldquo;Re-identification&amp;rdquo; through matching with other data sources.&lt;/p>
&lt;h2 id="2-a-deep-understanding-of-privacy-enhancing-technologies-pets">2. A Deep Understanding of Privacy-Enhancing Technologies (PETs)
&lt;/h2>&lt;p>The key to balancing privacy and data utilization is &amp;ldquo;Privacy-Enhancing Technologies (PETs)&amp;rdquo;. Here, we provide detailed mathematical definitions and technical implementations of the major PETs that play extremely important roles in modern big data analysis and machine learning.&lt;/p>
&lt;h3 id="21-k-anonymity-and-its-extensions">2.1 K-Anonymity and Its Extensions
&lt;/h3>&lt;p>Proposed by Latanya Sweeney and Pierangela Samarati in 1998, &amp;ldquo;k-anonymity&amp;rdquo; is a foundational concept for privacy protection in data publication. It means ensuring that every record in a dataset is indistinguishable from at least $k-1$ other records.&lt;/p>
&lt;p>Attributes in a database are broadly classified into the following three categories:&lt;/p>
&lt;ol>
&lt;li>&lt;strong>Explicit Identifiers&lt;/strong>: Information that can directly identify an individual, such as names and social security numbers (these are usually deleted or encrypted).&lt;/li>
&lt;li>&lt;strong>Quasi-Identifiers (QIs)&lt;/strong>: Information that cannot identify an individual on its own, such as age, gender, and zip code, but can be used to identify them when combined.&lt;/li>
&lt;li>&lt;strong>Sensitive Attributes&lt;/strong>: Information that should be protected, such as medical conditions or annual income.&lt;/li>
&lt;/ol>
&lt;p>K-anonymity guarantees that there are always at least $k$ combinations of quasi-identifiers (Equivalence Classes). However, k-anonymity is vulnerable to &amp;ldquo;Homogeneity Attacks&amp;rdquo; and &amp;ldquo;Background Knowledge Attacks&amp;rdquo;. For example, if all $k$ people belonging to an equivalence class have the same medical condition (sensitive attribute), the condition will be identified even if k-anonymity is maintained.&lt;/p>
&lt;p>To overcome this, the following extended models have been proposed:&lt;/p>
&lt;ul>
&lt;li>&lt;strong>l-diversity&lt;/strong>: Guarantees that sensitive attributes have at least $l$ different values in each equivalence class.&lt;/li>
&lt;li>&lt;strong>t-closeness&lt;/strong>: Ensures that the distance (such as Earth Mover&amp;rsquo;s Distance) between the distribution of sensitive attributes in each equivalence class and the distribution of sensitive attributes in the entire dataset is less than or equal to a threshold $t$.&lt;/li>
&lt;/ul>
&lt;h3 id="22-differential-privacy-dp">2.2 Differential Privacy (DP)
&lt;/h3>&lt;p>Overcoming the limitations of the k-anonymity model, &amp;ldquo;Differential Privacy&amp;rdquo; proposed by Cynthia Dwork et al. in 2006 is now widely adopted as the strongest and most mathematically rigorous privacy standard. Tech giants such as Apple, Google, and Microsoft apply this $\epsilon$-differential privacy when collecting telemetry and statistical data from users.&lt;/p>
&lt;h4 id="mathematical-definition-of-differential-privacy">Mathematical Definition of Differential Privacy
&lt;/h4>&lt;p>A randomized algorithm $\mathcal{M}$ satisfies $\epsilon$-differential privacy if, for any two adjacent datasets $D$ and $D'$ differing by only one record (i.e., $\|D - D'\|_1 = 1$), and for any subset of outputs $S \subseteq \text{Range}(\mathcal{M})$, the following inequality holds:&lt;/p>
$$ \Pr[\mathcal{M}(D) \in S] \le e^\epsilon \Pr[\mathcal{M}(D') \in S] $$&lt;p>Here, $\epsilon$ (privacy budget) is a non-negative parameter that controls the level of privacy protection. The smaller the $\epsilon$, the stronger the privacy protection, but the utility of the data decreases.&lt;/p>
&lt;p>Furthermore, $(\epsilon, \delta)$-differential privacy, a relaxed model that allows the privacy guarantee to be broken with a very small probability $\delta$, is also widely used.&lt;/p>
$$ \Pr[\mathcal{M}(D) \in S] \le e^\epsilon \Pr[\mathcal{M}(D') \in S] + \delta $$&lt;h4 id="laplace-mechanism">Laplace Mechanism
&lt;/h4>&lt;p>A typical method for achieving differential privacy is the &amp;ldquo;Laplace Mechanism,&amp;rdquo; which intentionally adds noise (random numbers) following a specific distribution to the true output result of a query. How much noise should be added depends on the &amp;ldquo;Global Sensitivity&amp;rdquo; $\Delta f$ of the function $f$.&lt;/p>
&lt;p>The global sensitivity $\Delta f$ is defined as the maximum change in the output of the function $f$ for any adjacent datasets $D, D'$.&lt;/p>
$$ \Delta f = \max_{D, D'} \| f(D) - f(D') \|_1 $$&lt;p>The Laplace mechanism adds noise $Y$ sampled from the Laplace distribution $\text{Lap}(b)$ with scale parameter $b = \frac{\Delta f}{\epsilon}$ to the result of the function $f(D)$.&lt;/p>
$$ \mathcal{M}(D) = f(D) + Y, \quad Y \sim \text{Lap}\left(\frac{\Delta f}{\epsilon}\right) $$&lt;p>The probability density function of the Laplace distribution is as follows:&lt;/p>
$$ p(x \mid b) = \frac{1}{2b} \exp\left( - \frac{|x|}{b} \right) $$&lt;p>By this noise injection, it becomes impossible to infer whether a specific individual is included in the dataset from the output result. Companies leverage DP as a technology that masks individual data itself while maintaining the utility of statistical trends (mean, variance, counts, etc.) of the entire data.&lt;/p>
&lt;h3 id="23-federated-learning-fl">2.3 Federated Learning (FL)
&lt;/h3>&lt;p>Traditional machine learning took a centralized approach, where massive amounts of data were aggregated on a central server to train models, like the aforementioned data lakes. However, sending sensitive data such as medical images or smartphone input histories to a central server involves significant privacy risks.&lt;/p>
&lt;p>Therefore, &amp;ldquo;Federated Learning&amp;rdquo; was proposed by Google in 2016. In federated learning, rather than moving the data itself, the &amp;ldquo;computational processing of the model&amp;rdquo; is moved to the edge devices (smartphones, hospital servers, etc.) where the data resides.&lt;/p>
&lt;pre class="mermaid">
flowchart TD
Server[&amp;#34;Central Aggregation Server&amp;#34;]
Device1[&amp;#34;Edge Device 1 (Smartphone)&amp;#34;]
Device2[&amp;#34;Edge Device 2 (Smartphone)&amp;#34;]
Device3[&amp;#34;Edge Device 3 (Smartphone)&amp;#34;]
Server --&amp;gt;| 1. Broadcast Global Model Weights | Device1
Server --&amp;gt;| 1. Broadcast Global Model Weights | Device2
Server --&amp;gt;| 1. Broadcast Global Model Weights | Device3
Device1 --&amp;gt;| 2. Local Training on Private Data | Device1
Device2 --&amp;gt;| 2. Local Training on Private Data | Device2
Device3 --&amp;gt;| 2. Local Training on Private Data | Device3
Device1 --&amp;gt;| 3. Transmit Model Gradients/Updates | Server
Device2 --&amp;gt;| 3. Transmit Model Gradients/Updates | Server
Device3 --&amp;gt;| 3. Transmit Model Gradients/Updates | Server
Server --&amp;gt;| 4. Aggregation (FedAvg) | Server
Server --&amp;gt;| 5. Update Global Model | Server
&lt;/pre>
&lt;h4 id="federated-averaging-fedavg-algorithm">Federated Averaging (FedAvg) Algorithm
&lt;/h4>&lt;p>A representative aggregation algorithm in federated learning is FedAvg. Each client $k$ performs several epochs of local training using Stochastic Gradient Descent (SGD) on their own dataset $D_k$ (of size $n_k$) to calculate the updated weights $w_{t+1}^k$.&lt;/p>
&lt;p>The central server receives the weights from the $K$ participating clients and updates the global model weights $w_{t+1}$ by taking a weighted average of these weights according to the data size. Letting the total number of data samples be $n = \sum_{k=1}^K n_k$, the update equation is as follows:&lt;/p>
$$ w_{t+1} = \sum_{k=1}^K \frac{n_k}{n} w_{t+1}^k $$&lt;p>This makes it possible to build intelligent AI models without an individual&amp;rsquo;s raw data (message history, photos, etc.) ever leaving the device. Typical applications include improving the next-word prediction feature of Google Keyboard (Gboard) and Apple&amp;rsquo;s FaceID and Hey Siri voice recognition models.&lt;/p>
&lt;h3 id="24-homomorphic-encryption-he">2.4 Homomorphic Encryption (HE)
&lt;/h3>&lt;p>&amp;ldquo;Homomorphic Encryption&amp;rdquo; is a &amp;ldquo;magical&amp;rdquo; cryptographic technology that allows computations (such as addition and multiplication) to be performed on data while it remains in an encrypted state. With normal encryption methods, when performing computations on data, it must first be decrypted (returned to plaintext), but decrypting on a cloud server creates a security vulnerability.&lt;/p>
&lt;p>By using homomorphic encryption, the following properties are achieved. Letting the encryption function be $E(\cdot)$, the addition and multiplication of plaintexts $m_1$ and $m_2$ become possible with operations ($\oplus$ and $\otimes$) directly on the ciphertext.&lt;/p>
$$ E(m_1 + m_2) = E(m_1) \oplus E(m_2) $$$$ E(m_1 \times m_2) = E(m_1) \otimes E(m_2) $$&lt;p>Homomorphic encryption is divided into &amp;ldquo;Partially Homomorphic Encryption (PHE),&amp;rdquo; which allows either addition or multiplication, and &amp;ldquo;Fully Homomorphic Encryption (FHE),&amp;rdquo; which allows both addition and multiplication an unlimited number of times. Since Craig Gentry constructed the first FHE scheme using lattice-based cryptography in 2009, it has been a major breakthrough in cryptography.&lt;/p>
&lt;p>Currently, challenges such as computational cost and increased ciphertext size (overhead) remain, but it is expected to be applied to the secure analysis of medical data on the cloud and secure multi-party computation among financial institutions.&lt;/p>
&lt;h2 id="3-trends-in-legal-regulations-and-compliance-gdpr-vs-ccpa">3. Trends in Legal Regulations and Compliance: GDPR vs. CCPA
&lt;/h2>&lt;p>In parallel with technological evolution, the development of legal frameworks is advancing rapidly worldwide. When companies utilize big data, complying with these legal regulations is a strict requirement. Let&amp;rsquo;s compare the two most influential regulatory frameworks.&lt;/p>
&lt;h3 id="eu-general-data-protection-regulation-gdpr">EU General Data Protection Regulation (GDPR)
&lt;/h3>&lt;p>Enforced in May 2018, the EU&amp;rsquo;s GDPR (General Data Protection Regulation) is recognized as the global &amp;ldquo;gold standard&amp;rdquo; for personal data protection. GDPR applies to all organizations that handle the data of individuals within the EU, and violations result in massive fines of either up to 4% of global annual turnover or 20 million euros, whichever is higher.&lt;/p>
&lt;p>&lt;strong>Key Features of GDPR:&lt;/strong>&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Opt-in Principle&lt;/strong>: Explicit and freely given prior consent from users is required for data collection and processing.&lt;/li>
&lt;li>&lt;strong>Right to be Forgotten / Right to Erasure&lt;/strong>: Users have the right to request companies to completely erase their personal data. Data must also be deleted from data lake backups, which is an extremely difficult technical requirement.&lt;/li>
&lt;li>&lt;strong>Data Controllers and Data Processors&lt;/strong>: It strictly defines the responsibilities of those who determine the purposes of using the data (controllers) and those who process the data according to those instructions (processors).&lt;/li>
&lt;/ul>
&lt;h3 id="california-consumer-privacy-act-ccpacpra">California Consumer Privacy Act (CCPA/CPRA)
&lt;/h3>&lt;p>While there is no comprehensive privacy law at the federal level in the US, the CCPA (California Consumer Privacy Act) enacted in California in 2020 serves as the de facto national standard. It was later further strengthened by the CPRA (California Privacy Rights Act).&lt;/p>
&lt;p>&lt;strong>Key Features of CCPA:&lt;/strong>&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Opt-out Principle&lt;/strong>: Unlike GDPR&amp;rsquo;s &amp;ldquo;prior consent,&amp;rdquo; data collection is possible without prior consent, but it is mandatory to provide users with a clear opt-out link stating &amp;ldquo;Do Not Sell My Personal Information.&amp;rdquo;&lt;/li>
&lt;li>&lt;strong>Right to Access Data&lt;/strong>: Consumers can request the disclosure of specific pieces of information collected by the company, their categories, sources, and whether they have been sold to third parties.&lt;/li>
&lt;/ul>
&lt;p>These regulations strongly demand that companies adopt &amp;ldquo;Privacy by Design&amp;rdquo;—embedding privacy protection from the design stage of systems and processes.&lt;/p>
&lt;h2 id="4-implementation-challenges-in-the-data-ecosystem">4. Implementation Challenges in the Data Ecosystem
&lt;/h2>&lt;p>Let&amp;rsquo;s look at the implementation perspective when applying privacy-enhancing technologies and legal regulations to actual big data environments. For example, assume a case of implementing k-anonymization or differential privacy in a data lake using Python and Pandas, or PySpark.&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt"> 1
&lt;/span>&lt;span class="lnt"> 2
&lt;/span>&lt;span class="lnt"> 3
&lt;/span>&lt;span class="lnt"> 4
&lt;/span>&lt;span class="lnt"> 5
&lt;/span>&lt;span class="lnt"> 6
&lt;/span>&lt;span class="lnt"> 7
&lt;/span>&lt;span class="lnt"> 8
&lt;/span>&lt;span class="lnt"> 9
&lt;/span>&lt;span class="lnt">10
&lt;/span>&lt;span class="lnt">11
&lt;/span>&lt;span class="lnt">12
&lt;/span>&lt;span class="lnt">13
&lt;/span>&lt;span class="lnt">14
&lt;/span>&lt;span class="lnt">15
&lt;/span>&lt;span class="lnt">16
&lt;/span>&lt;span class="lnt">17
&lt;/span>&lt;span class="lnt">18
&lt;/span>&lt;span class="lnt">19
&lt;/span>&lt;span class="lnt">20
&lt;/span>&lt;span class="lnt">21
&lt;/span>&lt;span class="lnt">22
&lt;/span>&lt;span class="lnt">23
&lt;/span>&lt;span class="lnt">24
&lt;/span>&lt;span class="lnt">25
&lt;/span>&lt;span class="lnt">26
&lt;/span>&lt;span class="lnt">27
&lt;/span>&lt;span class="lnt">28
&lt;/span>&lt;span class="lnt">29
&lt;/span>&lt;span class="lnt">30
&lt;/span>&lt;span class="lnt">31
&lt;/span>&lt;span class="lnt">32
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-python" data-lang="python">&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># Conceptual implementation of data aggregation applying differential privacy (Python)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="kn">import&lt;/span> &lt;span class="nn">numpy&lt;/span> &lt;span class="k">as&lt;/span> &lt;span class="nn">np&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="kn">import&lt;/span> &lt;span class="nn">pandas&lt;/span> &lt;span class="k">as&lt;/span> &lt;span class="nn">pd&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">def&lt;/span> &lt;span class="nf">laplace_mechanism&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">true_value&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">sensitivity&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">epsilon&lt;/span>&lt;span class="p">):&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="s2">&amp;#34;&amp;#34;&amp;#34;
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="s2"> Function to add Laplace noise to the true value
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="s2"> &amp;#34;&amp;#34;&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">scale&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">sensitivity&lt;/span> &lt;span class="o">/&lt;/span> &lt;span class="n">epsilon&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">noise&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">np&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">random&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">laplace&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">loc&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="mi">0&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">scale&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="n">scale&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">return&lt;/span> &lt;span class="n">true_value&lt;/span> &lt;span class="o">+&lt;/span> &lt;span class="n">noise&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">def&lt;/span> &lt;span class="nf">get_dp_average_salary&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">dataframe&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">epsilon&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="mf">1.0&lt;/span>&lt;span class="p">):&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="s2">&amp;#34;&amp;#34;&amp;#34;
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="s2"> Calculate the average salary with guaranteed differential privacy
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="s2"> &amp;#34;&amp;#34;&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="c1"># Actual calculation&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">true_sum&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">dataframe&lt;/span>&lt;span class="p">[&lt;/span>&lt;span class="s1">&amp;#39;salary&amp;#39;&lt;/span>&lt;span class="p">]&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">sum&lt;/span>&lt;span class="p">()&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">true_count&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="nb">len&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">dataframe&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="c1"># Application of differential privacy (based on sensitivity assumption)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="c1"># Assume the fluctuation of the maximum salary as sensitivity (more strictly, clipping is necessary)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">max_salary_diff&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="mi">100000&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="c1"># Adding noise (DP can be applied to both the sum and the count)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">noisy_sum&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">laplace_mechanism&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">true_sum&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">max_salary_diff&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">epsilon&lt;/span> &lt;span class="o">/&lt;/span> &lt;span class="mi">2&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">noisy_count&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">laplace_mechanism&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">true_count&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="mi">1&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">epsilon&lt;/span> &lt;span class="o">/&lt;/span> &lt;span class="mi">2&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">return&lt;/span> &lt;span class="n">noisy_sum&lt;/span> &lt;span class="o">/&lt;/span> &lt;span class="n">noisy_count&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># Execution within the data pipeline&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># dp_avg_salary = get_dp_average_salary(raw_df, epsilon=0.5)&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>As seen in this code snippet, the implementation of differential privacy itself is as simple as adding noise; however, in actual operations, managing the &amp;ldquo;privacy budget ($\epsilon$)&amp;rdquo; becomes extremely difficult. Issuing multiple queries to the same dataset consumes the privacy budget (based on the composition theorem), and ultimately it is necessary to build a mechanism (Privacy Budget Management) that locks the entire dataset or rejects queries.&lt;/p>
&lt;h2 id="5-future-outlook-and-ethical-issues">5. Future Outlook and Ethical Issues
&lt;/h2>&lt;p>The trade-off between big data and privacy is not a zero-sum game. With the evolution of PETs such as differential privacy, federated learning, and homomorphic encryption, a new data utilization paradigm of &amp;ldquo;sharing insights without sharing data&amp;rdquo; is becoming a reality.&lt;/p>
&lt;p>Furthermore, in recent years, connected with the concepts of &amp;ldquo;Data Mesh&amp;rdquo; and &amp;ldquo;Web3&amp;rdquo; (decentralized web), the movement to take back &amp;ldquo;Data Sovereignty&amp;rdquo; from giant platformers to individuals is accelerating. A future is being discussed where individual data is stored in Personal Data Stores (PDS) or data wallets, and users themselves control the licensing and monetization of their data.&lt;/p>
&lt;p>However, technological solutions are not perfect. In federated learning, there is a threat of &amp;ldquo;Poisoning Attacks,&amp;rdquo; where malicious clients send fraudulent model updates to corrupt the global model. In differential privacy, an ethical issue has also been pointed out where data of minorities is drowned out by noise, creating bias in AI models.&lt;/p>
&lt;h2 id="conclusion">Conclusion
&lt;/h2>&lt;p>The fate of personal information in the era of big data poses a fundamental question that goes beyond a mere technical issue: what kind of society do we desire? How can we protect individual dignity and privacy while enjoying convenience? We can only arrive at a sustainable solution through a trinity: the establishment of legal regulations, the continuous innovation of privacy-enhancing technologies, and the high literacy of each of us who provide the data. Privacy and convenience are no longer a trade-off, but will evolve into &amp;ldquo;essential requirements&amp;rdquo; that can be achieved simultaneously through the latest technologies.&lt;/p></description></item></channel></rss>