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

Memory Management

Blog banner

Network Security Risks

Blog banner

Service stratergy principles

Blog banner

You'll get to know about Pankaj Negi

Blog banner

OPERATING SYSTEM

Blog banner

Linker

Blog banner

Full Disk Encryption

Blog banner

Importance Of Education.

Blog banner

Wrike

Blog banner

Workplace mental health: A Psychological Perspective on Employee Well-being and Organizational Growth

Blog banner

How to lose belly fat

Blog banner

From Procrastinator to Performer: How to Beat the Last-Minute Rush

Blog banner

The Role of Data Provenance and Lineage in Modern Data Science

Blog banner

Environmental Management using GIS

Blog banner

INTERNET SECURITY

Blog banner

Travelling blog

Blog banner

A Deep Dive

Blog banner

The House ??of Patola Designs: Traditional Weaves with a Modern Twist

Blog banner

10 Reasons to Date a South Indian Girl

Blog banner

Memory management

Blog banner

Dal Bafla Recipe

Blog banner

Importance of Network Security Risk

Blog banner

A buffer overflow

Blog banner

DBMS and various career options related to it.

Blog banner

Operating Systems

Blog banner

Deadlock in operating system

Blog banner

GEOLOGY AND GEO-TECTONIC FRAME WORK OF WESTERN BASTAR CRATON

Blog banner

Social Media Sentiment Analysis

Blog banner

Virtual memory

Blog banner

Wedding photographer

Blog banner

Mumbai Metro 3

Blog banner

Denial-of-Service and Distributed Denial-of-Service Attack Techniques

Blog banner

21ST CENTURY PATRIARCHY

Blog banner

Reconnaissance

Blog banner

Building a Simple Doctor Appointment System in Common Lisp

Blog banner

Apache Kafka

Blog banner

Preparing Students for Higher Education: How International Schools Shape Future Success

Blog banner

Scala - a programming tool

Blog banner

Daycare Centres Help Children Transition into Structured Learning

Blog banner

Cycling

Blog banner

The Importance of Eating on Time: How Meal Timing Affects Your Health

Blog banner

What is Amazon?

Blog banner