This directory contains a comprehensive Python implementation of active inference with teacher-student learning dynamics and scientific computing capabilities.
The Python implementation provides:
- Scientific computing with NumPy and SciPy
- Teacher-student POMDP framework
- Rich visualization with matplotlib
- Comprehensive analysis tools
- Student_Teacher.py: Main teacher-student implementation
- teacher_wrapper.py: Teacher model wrapper
- output/: Generated visualizations and results
The implementation features a sophisticated teacher-student learning model:
class TeacherModel:
def __init__(self, n_states):
self.knowledge = np.zeros(n_states)
self.growth_rate = 0.1
self.max_knowledge = 1.0
def update_knowledge(self, state):
# Logistic growth model
current = self.knowledge[state]
exp_gr = np.exp(self.growth_rate)
self.knowledge[state] = (self.max_knowledge * current * exp_gr) / \
(self.max_knowledge + current * (exp_gr - 1))
def suggest_resource(self, student_beliefs):
# Intelligent resource allocation
gaps = (self.max_knowledge - self.knowledge) * student_beliefs
return np.argmax(gaps)class StudentTeacherPOMDP:
def __init__(self, n_states, n_observations, n_actions):
self.n_states = n_states
self.A = self.initialize_matrix(n_observations, n_states) # Likelihood
self.B = self.initialize_matrix(n_states, n_actions) # Transition
self.C = self.initialize_vector(n_observations) # Preferences
self.D = self.initialize_vector(n_states) # Prior
def update_beliefs(self, observation):
# Bayesian belief updating
likelihood = self.A[observation, :]
posterior = likelihood * self.beliefs
self.beliefs = posterior / np.sum(posterior)- Python 3.7+
- NumPy: Matrix operations and numerical computing
- SciPy: Scientific computing and optimization
- Matplotlib: Visualization and plotting
- Seaborn: Statistical visualization
- Pandas: Data manipulation and analysis
pip install numpy scipy matplotlib seaborn pandas# Run the main implementation
python Student_Teacher.py
# Run with custom parameters
python Student_Teacher.py --states 6 --steps 200
# Run teacher model analysis
python teacher_wrapper.py
# Generate visualizations
python -c "from Student_Teacher import StudentTeacherPOMDP; pomdp = StudentTeacherPOMDP(4, 3, 2); pomdp.plot_matrices()"The implementation supports flexible configuration:
# Simulation parameters
config = {
'n_states': 4,
'n_observations': 3,
'n_actions': 2,
'learning_rate': 0.1,
'precision': 1.0,
'uncertainty_weight': 0.1,
'max_iterations': 100,
'convergence_threshold': 1e-6,
'visualization_enabled': True
}def update_beliefs(self, observation):
likelihood = self.A[observation, :]
posterior = likelihood * self.beliefs
# Normalize to ensure probabilities sum to 1
if np.sum(posterior) > 0:
self.beliefs = posterior / np.sum(posterior)
else:
# Handle zero posterior case
self.beliefs = np.ones(self.n_states) / self.n_statesdef calculate_free_energy(self, policy):
# Expected free energy calculation
efe = 0
for action in policy:
# Calculate expected surprise
predicted_beliefs = self.predict_beliefs(action)
expected_surprise = self.calculate_expected_surprise(predicted_beliefs)
efe += np.sum(self.beliefs * expected_surprise)
return efedef infer_policy(self, policies):
best_policy = None
min_free_energy = float('inf')
for policy in policies:
fe = self.calculate_free_energy(policy)
if fe < min_free_energy:
min_free_energy = fe
best_policy = policy
return best_policyThe implementation creates comprehensive visualizations:
- Matrix Visualizations: A, B, C, D matrices as heatmaps
- Belief Evolution: Time series of belief state changes
- Teacher Knowledge: Growth curves and knowledge distribution
- Learning Progress: Performance metrics over time
A_matrix.png: Likelihood matrix visualizationB_matrix.png: Transition matrix visualizationstudent_beliefs_final.png: Final belief stateteacher_knowledge_final.png: Final teacher knowledge
- Matrix Operations: Optimized NumPy vectorized operations
- Memory Usage: Efficient data structures with minimal overhead
- Scalability: Linear scaling with state space size
- Parallel Processing: Optional multiprocessing support
- Numerical Stability: Robust floating-point calculations
- Statistical Analysis: Built-in hypothesis testing
- Optimization: Gradient-based and heuristic optimization
- Data Analysis: Comprehensive statistical tools
- Machine Learning Integration: Scikit-learn compatibility
- Deep Learning: PyTorch/TensorFlow integration
- Reinforcement Learning: OpenAI Gym compatibility
- Real-time Processing: Streaming data analysis
- Web Applications: Flask/Django integration
- Database Systems: SQL/NoSQL database integration
- Cloud Computing: AWS/Google Cloud deployment
- Real-time Dashboards: Interactive visualization
- Friston, K. (2010): The free-energy principle: a unified brain theory?
- Da Costa, L., et al. (2020): Active inference on discrete state-spaces
- Parr, T., & Friston, K. (2019): Generalised free energy and active inference
- Python Data Science Handbook: Scientific computing guide
- Numerical Python: Advanced NumPy techniques
- Python for Scientific Computing: Best practices and optimization