wisemonkeys logo
FeedNotificationProfileManage Forms
FeedNotificationSearchSign in
wisemonkeys logo

Blogs

"Can Lisp do Machine Learning?"

profile
Abhinav Kumar
Jul 16, 2025
0 Likes
0 Discussions
0 Reads

Linear Regression is the simplest form of predictive modeling. It assumes a straight-line relationship between two variables. For example:

More study hours → Higher exam scores

This relationship can be modeled as

y=mx+c


Where:

m: Slope (how fast y changes when x changes)

c: Intercept (the starting point of the line)

Given some data, Linear Regression helps us find m and c automatically.


1)

First, we need to calculate the average of a list of numbers.

(defun mean (lst)

(/ (reduce #'+ lst) (length lst)))


2)

Variance measures how spread out a list of numbers is.

(defun variance (lst)

(let ((m (mean lst)))

(mean (mapcar (lambda (x) (expt (- x m) 2)) lst))))


3)

Covariance tells us how two variables change together.

(defun covariance (x y)

(let* ((mean-x (mean x))

(mean-y (mean y))

(n (length x))

(sum 0))

(dotimes (i n)

(incf sum (* (- (nth i x) mean-x)

(- (nth i y) mean-y))))

(/ sum n)))


4)

This is where we calculate our model’s intercept and slope (our c and c).

(defun linear-regression-coeffs (x y)

(let ((b1 (/ (covariance x y) (variance x))))

(let ((b0 (- (mean y) (* b1 (mean x)))))

(list b0 b1))))


5)

Finally, given x, this function predicts y using our model.

(defun predict (model x)

(+ (first model) (* (second model) x)))



6)

(defparameter *hours* '(1 2 3 4 5 6))

(defparameter *scores* '(50 55 65 70 75 85))


(defparameter *model* (linear-regression-coeffs *hours* *scores*))


(format t "Intercept: ~f, Slope: ~f~%" (first *model*) (second *model*))

(format t "Prediction for 7 hours: ~f~%" (predict *model* 7))


7) Output

Intercept: 42.666668, Slope: 6.857143

Prediction for 7 hours: 90.666668


Intercept: 42.66 → Predicted score if study hours were zero.

Slope: 6.85 → Each extra hour of study increases the score by ~6.85 points.

Prediction for 7 hours: Expected score ~90.66


Comments ()


Sign in

Read Next

Why am I never satisfied with my Life?

Blog banner

Human factor, a critical weak point in the information security of an organization’s IOT

Blog banner

Platonic Solids

Blog banner

Introduction my self

Blog banner

New Ransomware Encrypts Your Android And Then Changes PIN Lock

Blog banner

1.1 basic elements

Blog banner

Session Vulnerabilities

Blog banner

Data Science in Healthcare: Predicting Diseases

Blog banner

Why You Need 2FA (Two-Factor Authentication) On Your Email And Other Online Accounts

Blog banner

Understanding Mobile Device Forensics

Blog banner

Operating System

Blog banner

Software

Blog banner

Cloud Security: Trends and Innovations

Blog banner

Concurrency:Deadlock and Starvation

Blog banner

Process in OS

Blog banner

Deadlock

Blog banner

Top 5 Benefits of Artificial Intelligence

Blog banner

What is a geographic information system (GIS)?

Blog banner

Functions of Operating System

Blog banner

ProofHub

Blog banner

Memory Management

Blog banner

How return on investment is defined in IT services

Blog banner

A Brief Review on Cyber Forensics and its Analysis Tool

Blog banner

WORKFRONT SOFTWARE

Blog banner

Product Discount Calculator

Blog banner

National Payments Corporation of India-Rupay - Gayatri Nayak

Blog banner

ahh wait a min

Blog banner

Importance of self defence for girls

Blog banner

THE DESIRE OF MANY

Blog banner

Malware Detection Techniques for Mobile Devices

Blog banner

Operating Systems

Blog banner

MODERN OPERATING SYSTEM

Blog banner

What is metaverse?

Blog banner

E-security systems | CCTV

Blog banner

Deadlock in operating system

Blog banner

Linux

Blog banner

Threads

Blog banner

Challenges and risks in service operations

Blog banner

Privacy-Enhancing Computation Techniques

Blog banner

What if COVID does not exist!

Blog banner

Malicious softwares

Blog banner

MQTT (MQ Telemetry Transport) in Data Science

Blog banner