wisemonkeys logo
FeedNotificationProfileManage Forms
FeedNotificationSearchSign in
wisemonkeys logo

Blogs

"Can Lisp do Machine Learning?"

profile
Abhinav Kumar
Jul 16, 2025
0 Likes
0 Discussions
4 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

Use case of K-means clustering

Blog banner

Street foods

Blog banner

Mumbai local ......

Blog banner

The launch of UniMap by HERE

Blog banner

Place to visit in pune

Blog banner

How to Compare Schools Before an Admission Decision

Blog banner

Multiple-Processor Scheduling in Operating System

Blog banner

Are Social Media Paid Campaigns Worth It?

Blog banner

Balance

Blog banner

Your Guide to Great Flossing, by the best affordable dentist in Maroubra

Blog banner

Inventory management software system

Blog banner

Never give up

Blog banner

Data Visualization

Blog banner

A Traveller’s Guide to Offbeat Places in Arcadia, Florida

Blog banner

Life lesson

Blog banner

Veg/Non-veg/Egg Tiffin Meals That Are Nutritious and Filling

Blog banner

"Audit" In Data Science

Blog banner

Should you be using a mouthwash? Know from the experts

Blog banner

10 Types of Friends in every friend group

Blog banner

AI and Cyber Security

Blog banner

Sage business cloud accounting

Blog banner

BrainGate Technology

Blog banner

Ganpati Special: Exotic 51 Modak flavours by Mahavir Dryfruits

Blog banner

Threads

Blog banner

Paginng In OS

Blog banner

Why You Should Not Use Free VPNs

Blog banner

RAID

Blog banner

Network Footprinting in Cybersecurity

Blog banner

Deadlock in operating system

Blog banner

10 Reasons why Monica and Chandler are the best couple ever.!!!

Blog banner

Data Security must be your Priority!

Blog banner

How Schools Can Reduce Exam Stress in Students?

Blog banner

Beyond the Track: Why the Best Hotel in Arcadia, Florida, Completes Your IMSA Sebring Getaway

Blog banner

SQL Injection Techniques

Blog banner

All you need to know about “On-page SEO”

Blog banner

Vikrant’s first blog

Blog banner

SQL Injection

Blog banner

Memory Management

Blog banner

What Your Child Learns During Free Play (That You Might Not Notice)

Blog banner

MODERN OPERATING SYSTEM

Blog banner

Emerging threats in cyber Forensics

Blog banner

File Management In OS

Blog banner