Basics of Fuzzy Logic
Tags: datascience, machinelearning, programming, tutorial
Published: 2023-10-16 18:30:09

In the 1960s, Lotfi Zadeh introduced the science of fuzzy logic to analyze systems where Boolean logic was not good enough. To capture the (lack of) precision, Boolean logic improvises only two options: true or false. 

There are different scenarios in the real world that can be clearly marked as true or false. For example: 
- 1+1=2
- 5>10
- ChatGPT generates correct responses

The truthfulness of the first two statements can be captured by Boolean logic because the possible answer is either true or false. However, the truthfulness of the third statement is relatively harder to capture using Boolean logic. Labeling it as a true or false statement won't capture the ground reality. The actual answer lies somewhere between true and false. Fuzzy logic takes care of such situations. Let's first understand the basics of fuzzy logic.

## Basics of Fuzzy Logic 

Fuzzy logic improvises a continuous line to represent the magnitude of the truthfulness of a given statement. For completely true or false statements, it will take the extreme points of the line: 1 or 0. However, the magnitude of truthfulness of a statement or concept can be represented using any appropriate membership value between 0 and 1, as shown in the following figure:

![Fuzzy logic](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/spss5v56ow4mekjxhckh.png)

There are certain situations where we need to capture the notion of partial truth. For example, “Sara is a tall person.” It depends on how we define the concept of someone’s height. If we define a strict threshold like anyone having a height of more than 5 feet is tall, then this point can serve as the cutoff to categorize someone as tall or not tall. If Alice has a height of less than 5 feet (say 4.9 feet), then we’ll say that Alice is not tall. We’ll output the same answer for any smaller number. If Bob has a height greater than 5 feet (say 5.1 feet), then we’ll treat Bob as a tall person. We’ll do the same for a person whose height is significantly higher than 5 feet. The absolute difference between the heights of Alice and Bob is 0.2 feet, but we’re classifying them into two mutually exclusive categories because of the strict cutoff. This is what a Boolean decision is. The same phenomenon is shown in the following figure:

![Boolean](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bb5ci3lo8hmviplnil9e.png)

Fuzzy logic is the superset of Boolean logic. It captures the notion of partial truthfulness beyond capturing absolute true or false answers. 

Each linguistic concept is modeled as a fuzzy variable. [Values of fuzzy variables](https://www.educative.io/blog/fuzzy-inference-system?utm_campaign=&utm_source=devto&utm_medium=text&utm_content=&utm_term=&eid=5082902844932096) are represented as membership functions. Fuzzy membership functions capture the notion of continuity and partial truthfulness. Moreover, they can overlap with each other. For example, the concept of tallness has been modeled as a fuzzy variable in the following figure. The variable can have three possible values: short, medium, and tall. Each fuzzy value is modeled as a membership function. The membership value *μ* for each membership function is computed based on the actual height. These fuzzy membership functions representing their respective linguistic values can overlap with each other as shown below: 

![Fuzzy logic](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e9dpuoncubv06rpk5uco.png)

Let’s compute the membership values μ for the height of Alice as shown in the following figure:

![Fuzzy logic](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sliqfi036lwl3esdbh5r.png)

The figure above shows:

![Fuzzy logic](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rv2qdslb73wkymgawu2f.png)

Let’s repeat the same experiment for the height of Bob as shown in the following figure:

![Fuzzy logic](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hr57dwwrxda3262u9lxy.png)

The figure above shows:

![Fuzzy logic](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0c8ss1d43pe4ivi4yfwr.png)

In both figures, the colored lines show membership of the heights of Alice and Bob in each of the membership functions. For instance, the height of Alice has a membership value of 1.0 in the membership function *medium* and Bob has a membership value of 0.98 in the same function. Comparing both of them reveals that they’re quite close to each other. The same is true for the other two membership functions. The membership value in a particular membership function represents the degree of truthfulness of belonging to that particular concept or fuzzy value.

## Fuzzy Sets 

In conventional [sets](https://www.educative.io/blog/learn-python-lists-sets-tuples-data-structures?utm_campaign=topic_python&utm_source=devto&utm_medium=text&utm_content=&utm_term=&eid=5082902844932096), the membership of each element to a set is either 0 or 1. We interpret it as an element belonging to a set or not. For example, the following set:

![Fuzzy](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ew35esqfynr0j7m92d1e.png)

can be represented as follows:

![Fuzzy](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/28ddij63g7qoaa74j2gf.png)

In this notation, each element of the set is notated as *x* over *y*, where *x* is an element and *y* is the membership value *μ* of that element in the set. If *μ* is 1 then the element is present in the set. If *μ* is 0 then the element is not present in the set. For conventional sets, we may omit writing the explicit value of *μ*, as shown above.

Fuzzy sets capture the partial membership of an element to a set too. For example, if we have to create a set named *medium* to notate the membership of Alice and Bob, we can write it as follows: 

![Fuzzy](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ut7x1rsz3k8s6saw4l3k.png)

The fundamental principles of set theory remain the same. Let’s take an example of two fuzzy sets A and B, as shown below, and exercise the fundamental properties of sets:

![Sets](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ny1jvb0i1wmstyo4nffs.png)

### Union of two Fuzzy Sets 

The union of the two fuzzy sets is computed by taking the maximum of the two membership values of an element in both sets. For example:

![Fuzzy](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/42jak9ofh9pgejvdju1d.png)

### Intersection of two Fuzzy Sets

The intersection of the two fuzzy sets is computed by taking the minimum of the two membership values of an element in both sets. For example:

![Fuzz](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wnght3ozfop7seb1qsl4.png)

### Complement of a fuzzy set

The complement of a fuzzy set is computed by subtracting the membership value of each element from 1. For example:

![Fuzzy set](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pwyk7ns7qdnviqun2buz.png)

```Python
A = dict()
B = dict()
Union_of_A_B = dict()
Intersection_of_A_B = dict()
Complement_of_A = dict()
Complement_of_B = dict()
A = {"red":0.8, "green":0.5, "blue":0.2}
B = {"red":0.6, "green":0.9, "blue":0.7}
for a,b in zip(A,B):
  Union_of_A_B[a] = max(A[a], B[a])
  Intersection_of_A_B[a] = max(A[a], B[a])
  Complement_of_A[a] = round(1 - A[a],1)
  Complement_of_B[b] = round(1 - B[b],1)
print "Union of A and B = ", Union_of_A_B
print "Intersection of A and B = ", Intersection_of_A_B
print "Complement of A = ", Complement_of_A
print "Complement of B = ", Complement_of_B
```

**Output:**

```
Union of A and B =  {'blue': 0.7, 'green': 0.9, 'red': 0.8}
Intersection of A and B =  {'blue': 0.7, 'green': 0.9, 'red': 0.8}
Complement of A =  {'blue': 0.8, 'green': 0.5, 'red': 0.2}
Complement of B =  {'blue': 0.3, 'green': 0.1, 'red': 0.4}
```

### De Morgan's law for fuzzy sets

Let’s apply these basic operations to verify that De Morgan’s law holds for fuzzy sets:

![Fuzzy](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n0rzopxws9qowd75u2b6.png)

Hence, 

![Fuzzy](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t3wjbxd12yrguplkyy39.png)

Similarly, 

![Fuzzy](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bgj2xkg5t9s28gkht9wr.png)

Hence, 

![Fuzzy](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y0b5t45uvz7w949f7ccx.png)

Let’s verify these results:

```Python
def intersection(A, B):
  Intersection_of_A_B = dict()
  for a,b in zip(A,B):
    Intersection_of_A_B[a] = min(A[a], B[a])
  return Intersection_of_A_B

def union(A, B):
  Union_of_A_B = dict()
  for a,b in zip(A,B):
    Union_of_A_B[a] = max(A[a], B[b])
  return Union_of_A_B

def complement(A):
  Complement_of_A = dict()
  for a in A:
    Complement_of_A[a] = round(1 - A[a],1)
  return Complement_of_A

A = dict()
B = dict()
A = {"red":0.8, "green":0.5, "blue":0.2}
B = {"red":0.6, "green":0.9, "blue":0.7}
print "Union of A and B = ", union(A, B)
print "Intersection of A and B = ", intersection(A, B)
print "Complement of A = ", complement(A)
print "Complement of B = ", complement(B)
print "Rule-1: ", complement(intersection(A, B)), " = ", union(complement(A), complement(B))
print "Rule-2: ", complement(union(A, B)), " = ", intersection(complement(A), complement(B))
```

**Output:**

```
Union of A and B =  {'blue': 0.7, 'green': 0.9, 'red': 0.8}
Intersection of A and B =  {'blue': 0.2, 'green': 0.5, 'red': 0.6}
Complement of A =  {'blue': 0.8, 'green': 0.5, 'red': 0.2}
Complement of B =  {'blue': 0.3, 'green': 0.1, 'red': 0.4}
Rule-1:  {'blue': 0.8, 'green': 0.5, 'red': 0.4}  =  {'blue': 0.8, 'green': 0.5, 'red': 0.4}
Rule-2:  {'blue': 0.3, 'green': 0.1, 'red': 0.2}  =  {'blue': 0.3, 'green': 0.1, 'red': 0.2}
```

## Application areas of fuzzy logic

Example application domains of fuzzy logic are listed below: 
- Automatic speed control system for vehicles
- Altitude control of spacecraft
- Intelligent decision support system in business intelligence
- Evaluation system to grant financial support and loan in financial organizations
- Optimization in the manufacturing industry
- Medical diagnostic support systems
- Handwriting recognition systems

For a deeper dive into the relevant subject areas and applications, you may explore: 

* [Getting Started with Image Classification with PyTorch](https://www.educative.io/courses/getting-started-with-image-classification-with-pytorch?utm_campaign=topic_machine_learning&utm_source=devto&utm_medium=text&utm_content=&utm_term=&eid=5082902844932096)
* [Performing Modern Statistical Analysis with R](https://www.educative.io/courses/performing-modern-statistical-analysis-r?utm_campaign=topic_data_science&utm_source=devto&utm_medium=text&utm_content=&utm_term=&eid=5082902844932096)
* [Game Data Science Using R](https://www.educative.io/courses/game-data-science-using-r?utm_campaign=topic_data_science&utm_source=devto&utm_medium=text&utm_content=&utm_term=&eid=5082902844932096)

*Happy learning!*