Story Models Predictions API Database
Chapter II

The Five Minds

Five specialized AI models working in harmony to understand human life

Each model is a specialist. Each brings unique insights. Together, they form an intelligence greater than the sum of its parts—an ensemble that achieves 75-85% prediction accuracy through democratic consensus.

The Ensemble

Democratic Intelligence Through Voting

The Concept

Three algorithms, three perspectives, one prediction. The Ensemble combines Random Forest, Gradient Boosting, and Linear Regression into a voting system where each algorithm contributes its unique strengths.

Random Forest

Strength: Captures non-linear relationships and complex interactions

Method: 100 decision trees voting on predictions

Weight: 40% of final prediction

Gradient Boosting

Strength: Learns from mistakes, focuses on hard cases

Method: Sequential tree building with error correction

Weight: 40% of final prediction

Linear Regression

Strength: Provides stable baseline, interpretable

Method: Linear combination of features

Weight: 20% of final prediction

The Mathematics

Ensemble Prediction Formula
ŷ = 0.4 · RF(x) + 0.4 · GB(x) + 0.2 · LR(x)

Where weights sum to 1.0 for normalized voting

Training Process

from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor
from sklearn.linear_model import LinearRegression

# Random Forest: 100 trees, max depth 10
rf = RandomForestRegressor(
    n_estimators=100,
    max_depth=10,
    random_state=42
)
rf.fit(X_train, y_train)

# Gradient Boosting: 100 estimators, learning rate 0.1
gb = GradientBoostingRegressor(
    n_estimators=100,
    learning_rate=0.1,
    max_depth=5
)
gb.fit(X_train, y_train)

# Linear Regression: Simple baseline
lr = LinearRegression()
lr.fit(X_train, y_train)

# Ensemble Prediction
def predict(X):
    rf_pred = rf.predict(X)
    gb_pred = gb.predict(X)
    lr_pred = lr.predict(X)
    return 0.4 * rf_pred + 0.4 * gb_pred + 0.2 * lr_pred

Performance Metrics

0.84
RMSE

Root Mean Square Error - predictions typically within 0.84 points

0.67
MAE

Mean Absolute Error - average deviation from actual values

0.82
R² Score

Explains 82% of variance in the data

85%
Accuracy

Predictions within ±1 point of actual value

Why Ensemble Works

  • Diversity: Each model has different strengths and weaknesses
  • Robustness: If one model fails, others compensate
  • Reduced Overfitting: Averaging reduces variance
  • Better Generalization: Works well on unseen data

The Oracle

Markov Chain State Transitions

The Concept

Your life is a sequence of states. Each day, you're in a specific state defined by your energy, mood, stress, and sleep. The Markov Chain learns the probability of transitioning from one state to another, predicting tomorrow based on today.

State Definition

Each state is a tuple of discretized values:

State = (Energy_Level, Mood_Level, Stress_Level, Sleep_Quality)

Example states:

  • ("high", "positive", "low", "good") - Great day
  • ("low", "negative", "high", "poor") - Burnout risk
  • ("medium", "neutral", "medium", "fair") - Average day

The Mathematics

State Transition Probability
P(St+1 = j | St = i) = Tij

Probability of moving from state i to state j

Transition Matrix Calculation
Tij = count(i → j) / count(i)

Frequency of transitions from i to j divided by total occurrences of i

Example Transition Matrix

# Simplified 3-state example
States: ["High Energy", "Medium Energy", "Low Energy"]

Transition Matrix:
                High    Medium   Low
High Energy     0.60    0.30     0.10
Medium Energy   0.25    0.50     0.25
Low Energy      0.15    0.35     0.50

# Interpretation:
# If you're in "High Energy" today:
# - 60% chance you'll be "High Energy" tomorrow
# - 30% chance you'll be "Medium Energy" tomorrow
# - 10% chance you'll be "Low Energy" tomorrow

Prediction Process

class MarkovChainModel:
    def __init__(self):
        self.transition_matrix = {}
        self.states = []
    
    def fit(self, health_logs):
        # Discretize continuous values into states
        states = [self._discretize(log) for log in health_logs]
        
        # Count transitions
        for i in range(len(states) - 1):
            current    = states[i]
            next_state = states[i + 1]
            
            if current not in self.transition_matrix:
                self.transition_matrix[current] = {}
            if next_state not in self.transition_matrix[current]:
                self.transition_matrix[current][next_state] = 0
            self.transition_matrix[current][next_state] += 1
        
        # Normalize to probabilities
        for current in self.transition_matrix:
            total = sum(self.transition_matrix[current].values())
            for next_state in self.transition_matrix[current]:
                self.transition_matrix[current][next_state] /= total
    
    def predict(self, current_state):
        if current_state in self.transition_matrix:
            # Return most likely next state
            return max(
                self.transition_matrix[current_state].items(),
                key=lambda x: x[1]
            )[0]
        return None

Performance

70-80%
Accuracy

Next-day state prediction accuracy

50+
States

Unique states discovered from data

Real-World Example

Current State: High energy (8/10), Good mood (7/10), Low stress (3/10), Good sleep (7.5 hours)

Prediction: Tomorrow you'll likely be in "Moderate Energy, Good Mood" state (75% probability)

Recommendation: Good day for challenging tasks, but energy may dip slightly

The Network

Bayesian Causal Discovery

The Concept

Correlation isn't causation. The Bayesian Network doesn't just see that sleep and energy are related—it discovers that sleep causes energy changes. It constructs a directed graph showing causal relationships between variables.

Causal Graph Structure

Nodes represent variables, edges represent causal influence:

Sleep → Energy
Sleep → Mood
Exercise → Energy
Exercise → Mood
Stress → Energy
Stress → Mood
Mood → Energy
Energy → Stress

Interpretation: Sleep directly influences both energy and mood. Exercise has positive effects on both. Stress negatively impacts energy and mood. There's bidirectional feedback between mood and energy.

The Mathematics

Conditional Probability
P(Energy | Sleep, Exercise, Stress) = P(E | S, X, St)

Energy depends on sleep, exercise, and stress

Bayes' Theorem
P(A | B) = P(B | A) · P(A) / P(B)

Update beliefs based on evidence

Discovery Process

class BayesianNetworkModel:
    def __init__(self):
        self.edges = []
        self.correlations = {}
    
    def fit(self, data, variables):
        # Step 1: Calculate correlations
        for var1 in variables:
            for var2 in variables:
                if var1 != var2:
                    corr = self._pearson_correlation(
                        data[var1], data[var2]
                    )
                    self.correlations[(var1, var2)] = corr
        
        # Step 2: Test for conditional independence
        for (var1, var2), corr in self.correlations.items():
            if abs(corr) > 0.3:  # Significant correlation
                if self._test_causality(data, var1, var2):
                    self.edges.append((var1, var2))
        
        return self.edges
    
    def _test_causality(self, data, cause, effect):
        # Temporal ordering + conditional independence test
        return True  # Simplified
    
    def predict_influence(self, variable, change):
        # Trace through graph to find affected variables
        influenced = []
        for (source, target) in self.edges:
            if source == variable:
                influenced.append(target)
        return influenced

Discovered Relationships

Sleep → Energy

Correlation: 0.72 (strong positive)

Effect: +1 hour sleep → +0.8 energy points

Exercise → Mood

Correlation: 0.65 (strong positive)

Effect: +30 min exercise → +1.2 mood points

Stress → Energy

Correlation: -0.58 (moderate negative)

Effect: +1 stress point → -0.6 energy points

What-If Analysis

Question: "What if I sleep 8 hours instead of 6?"

Direct Effects:

  • Energy: +1.6 points (2 hours × 0.8)
  • Mood: +0.9 points (sleep → mood pathway)

Indirect Effects:

  • Stress: -0.7 points (better energy reduces stress)
  • Exercise capacity: +15 minutes (more energy enables more exercise)

Total Impact: Cascading positive effects across all metrics

The Analyst

Statistical Models & Time Series Analysis

The Concept

The mathematician of the group. While other models predict future values, the Statistical Analyzer detects trends, identifies change points, finds anomalies, and measures correlations with 80-90% accuracy. It's the foundation for understanding what's happening in your data.

Core Capabilities

Trend Detection

Identifies upward, downward, or stable trends in any metric over time using linear regression and moving averages.

Change Point Detection

Finds moments when patterns shift dramatically—like when stress suddenly increases or sleep quality drops.

Anomaly Detection

Spots outliers and unusual patterns that deviate significantly from your normal behavior.

Correlation Analysis

Measures strength and direction of relationships between variables using Pearson correlation.

The Mathematics

Pearson Correlation Coefficient
r = Σ[(xi - x̄)(yi - ȳ)] / √[Σ(xi - x̄)² · Σ(yi - ȳ)²]

Measures linear relationship strength (-1 to +1)

Moving Average (Trend Smoothing)
MAt = (xt-n+1 + xt-n+2 + ... + xt) / n

Average of last n data points

Z-Score (Anomaly Detection)
z = (x - μ) / σ

|z| > 2 indicates potential anomaly

Implementation

class StatisticalAnalyzer:
    def __init__(self):
        self.trends = {}
        self.correlations = {}
        self.anomalies = []
    
    def analyze_trend(self, data, window=7):
        """Detect trend using moving average"""
        ma = self._moving_average(data, window)
        slope = self._linear_regression_slope(ma)
        
        if slope > 0.1:
            return "increasing"
        elif slope < -0.1:
            return "decreasing"
        else:
            return "stable"
    
    def detect_change_points(self, data, threshold=1.5):
        """Find significant shifts in data"""
        change_points = []
        for i in range(1, len(data)):
            change = abs(data[i] - data[i-1])
            std = np.std(data[:i])
            if change > threshold * std:
                change_points.append({
                    'index': i,
                    'magnitude': change,
                    'direction': 'up' if data[i] > data[i-1] else 'down'
                })
        return change_points
    
    def find_anomalies(self, data):
        """Detect outliers using z-score"""
        mean = np.mean(data)
        std  = np.std(data)
        anomalies = []
        for i, value in enumerate(data):
            z_score = (value - mean) / std
            if abs(z_score) > 2:
                anomalies.append({'index': i, 'value': value, 'z_score': z_score})
        return anomalies
    
    def calculate_correlation(self, x, y):
        """Pearson correlation coefficient"""
        n = len(x)
        sum_x  = sum(x)
        sum_y  = sum(y)
        sum_xy = sum(x[i] * y[i] for i in range(n))
        sum_x2 = sum(x[i]**2 for i in range(n))
        sum_y2 = sum(y[i]**2 for i in range(n))
        numerator   = n * sum_xy - sum_x * sum_y
        denominator = sqrt((n * sum_x2 - sum_x**2) * (n * sum_y2 - sum_y**2))
        return numerator / denominator if denominator != 0 else 0

Real-World Examples

Trend Detection

Data: Energy levels over 30 days

Analysis: Decreasing trend detected (slope: -0.15)

Insight: Your energy has been declining steadily. Consider increasing sleep or reducing stress.

Change Point Detection

Data: Stress levels over 60 days

Analysis: Significant change on day 42 (stress jumped from 4 to 8)

Insight: Major stress increase detected. What changed in your life around that time?

Anomaly Detection

Data: Sleep hours over 90 days

Analysis: 3 anomalies detected (z-score > 2)

Insight: You had 3 nights with unusually poor sleep (< 4 hours). Pattern: all on Sundays.

Performance Metrics

80-90%
Accuracy

Trend and anomaly detection accuracy

95%
Precision

True anomalies vs false positives

The Learner

Reinforcement Learning & Adaptive Optimization

The Concept

The only model that learns from your actual outcomes. While other models predict based on patterns, the Reinforcement Learning model optimizes recommendations based on what actually works for you. It uses Q-learning to discover which activities, in which situations, produce the best results.

How It Works

State: Your current condition (energy, mood, stress, time of day)

Action: An activity you could do (exercise, meditation, work, rest)

Reward: The outcome (energy change, mood improvement, stress reduction)

Learning: Update Q-values based on actual rewards received

The Mathematics

Q-Learning Update Rule
Q(s,a) ← Q(s,a) + α[r + γ·max Q(s',a') - Q(s,a)]

α = learning rate (0.1), γ = discount factor (0.9), r = reward

Reward Function
R = Δenergy + Δmood - Δstress

Positive for improvements, negative for declines

Action Selection (ε-greedy)
a = argmax Q(s,a) with probability (1-ε), random with probability ε

Balance exploitation (best known) vs exploration (try new things)

Implementation

class ReinforcementLearner:
    def __init__(self, alpha=0.1, gamma=0.9, epsilon=0.1):
        self.alpha   = alpha    # Learning rate
        self.gamma   = gamma    # Discount factor
        self.epsilon = epsilon  # Exploration rate
        self.q_table = {}       # State-action values
    
    def get_state(self, health_log):
        """Convert health log to discrete state"""
        return (
            self._discretize(health_log.energy, 'energy'),
            self._discretize(health_log.mood,   'mood'),
            self._discretize(health_log.stress, 'stress'),
            self._get_time_of_day()
        )
    
    def choose_action(self, state, available_actions):
        """ε-greedy action selection"""
        if random.random() < self.epsilon:
            return random.choice(available_actions)  # Explore
        else:
            q_values = [self.q_table.get((state, a), 0)
                        for a in available_actions]
            return available_actions[np.argmax(q_values)]  # Exploit
    
    def update(self, state, action, reward, next_state):
        """Q-learning update rule"""
        current_q  = self.q_table.get((state, action), 0)
        next_acts  = self._get_available_actions(next_state)
        max_next_q = max([self.q_table.get((next_state, a), 0)
                         for a in next_acts])
        # Bellman equation
        new_q = current_q + self.alpha * (
            reward + self.gamma * max_next_q - current_q
        )
        self.q_table[(state, action)] = new_q
    
    def calculate_reward(self, before, after):
        """Reward = sum of positive outcome deltas"""
        energy_change = after.energy - before.energy
        mood_change   = after.mood   - before.mood
        stress_change = before.stress - after.stress  # reduction = good
        return energy_change + mood_change + stress_change
# Exploit: best known action q_values = [self.q_table.get((state, a), 0) for a in available_actions] return available_actions[np.argmax(q_values)] def update(self, state, action, reward, next_state): """Q-learning update""" current_q = self.q_table.get((state, action), 0) # Find max Q-value for next state next_actions = self._get_available_actions(next_state) max_next_q = max([self.q_table.get((next_state, a), 0) for a in next_actions]) # Q-learning formula new_q = current_q + self.alpha * ( reward + self.gamma * max_next_q - current_q ) self.q_table[(state, action)] = new_q def calculate_reward(self, before, after): """Calculate reward from activity outcome""" energy_change = after.energy - before.energy mood_change = after.mood - before.mood stress_change = before.stress - after.stress # Reduction is good return energy_change + mood_change + stress_change

Learning Process

Episode 1

State: Low energy, morning

Action: Coffee (random exploration)

Reward: +2 energy, -1 stress

Q-value: 0 → 0.1

Episode 50

State: Low energy, morning

Action: Exercise (learned preference)

Reward: +3 energy, +2 mood

Q-value: 0.8 → 1.2

Episode 200

State: Low energy, morning

Action: Exercise (confident choice)

Reward: +3 energy, +2 mood

Q-value: 2.5 (stable)

Real-World Example

Scenario: You're feeling low energy at 3 PM

Options: Coffee, nap, walk, snack, power through

Q-Values Learned:

  • Coffee: Q = 0.5 (temporary boost, crash later)
  • Nap: Q = 1.8 (best long-term energy recovery)
  • Walk: Q = 1.2 (good mood boost, moderate energy)
  • Snack: Q = 0.3 (minimal effect)
  • Power through: Q = -0.5 (leads to burnout)

Recommendation: Take a 20-minute nap (highest Q-value)

Confidence: Based on 47 similar situations in your history

Continuous Improvement

  • Personalized: Learns what works specifically for you
  • Adaptive: Updates as your patterns change
  • Context-Aware: Considers time, energy, mood, stress
  • Exploration: Occasionally tries new things to discover better options

Performance

200+
Episodes

Learning experiences accumulated

75%
Success Rate

Recommendations that improve outcomes