0.4 Data Types - JulTob/Data GitHub Wiki
Types and Sample Spaces
A set can hold anything: Symbols, numbers, stories, or worlds.
And formally, probability begins with sets.
But a quieter, usually silenced, question comes first:
Why do we need sets?
This is the question of Types.
🔰 What is a Type?
Well, a type defines a structure.
It tells us what counts as a possible value, what kinds of comparisons make sense, and what operations are allowed. It Builds an Algebra. It Produces a System.
In that sense, Type is meaning.
⚜️ To choose a type is to choose what can be known, compared, and transformed.
Different types describe different kinds of data, and not all data behaves the same.
When you ask a yes-or-no question, you are working with a Boolean type:
trueorfalse- Equality makes sense
- but magnitude usually does not
When you classify without ranking, you are working with a nominal or categorical type: Species of Fish, Country of Origin, or Favourite Color.
These are labels, not measurements. Is Trout better than Salmon? Green less than Red? There is no natural answer.
In a Nominal or Category Type:
- labels matter
- equality matters
- but there is no natural notion of greater or less
When categories come with order, you have an Ordinal Type: small, medium, large; low, medium, high; expensive, neutral, cheap; first, second, third...
We may disagree on thresholds, but we can still compare.
- order matters
- but distance is not yet stable or measurable
When values support arithmetic, you enter a Numerical type.
Not all Numbers are fit for all data. Here it helps to distinguish two common cases:
-
An interval scale supports meaningful differences
(for example, temperature in Celsius: the difference between 10° and 20° is meaningful, but 20° is not “twice as hot” as 10°). -
A ratio scale has a meaningful zero, so multiplication and division also make sense
(for example, mass, length, duration).
⚜️ Change the type, and you change what is possible.
Not only what is true, but what can even be asked.
💡 Types are alphabets
When reality speaks to us, what we percieve is a signal. A type is the alphabet in which that signal is written.
A dice roll speaks in six symbols.
$$ {⚀ ⚁ ⚂ ⚃ ⚄ ⚅} $$
A weekday has an alphabet of seven symbols: Monday to Sunday.
A measured length may speak in decimals. Sometimes, a real measurement has an alphabet so masive, so dense, we treat it as an unbroken continuous.
So when we define a measurement, we are also choosing a language. And every language comes with rules.
Those rules are where probability begins to take form.
⚙️ From type to sample space
Once a type is chosen, we can describe the sample space.
A sample space is the set of all possible outcomes of an experiment.
The important point is that the sample space is not chosen in a vacuum. It depends on the type of outcome we are describing.
This matters because probability is assigned to events, not directly to “types.” But type tells us what outcomes are available and what kinds of events or measurements make sense.
☯ A closing thought
We often think data is the same as numbers. That is not always true.
Data can be logical, categorical, ordinal, symbolic, numerical, or something richer still.
Data is the basis of decisions, of information, of knowledge, of meaning. Without structure, numbers don't mean anything.
A type is an agreement about what counts as a value, what counts as a comparison, what counts as a measurement, what counts as a signal, what counts as information.
It is an agreement on the maps of meaning.
From that agreement, outcomes can be named, events can be formed, decisions can be made, and randomness can be understood.
That agreement is type.
Data has to be stored in some format inside our computers and databases. Data Types are the different ways we can store and access this data.
Types are alphabets.
$$ \Omega \neq \mathcal X $$
Ω
𝒳
Una variable aleatoria es precisamente la traducción:
$$ X:\Omega\rightarrow\mathcal X $$
El dato $(x)$ no es el mundo $(\omega)$
Type operations
Enumerations
We can be very efficient storing data if we know the values a type can use. For these cases we use ENUMERATIONS.
For these we can just enumerate, list, all the possible values. Sometimes, specially when the data is ordered, we can access some features of ordering and classification by using the codification as if it were numeric values.
| Trafic_Light |
|---|
| 🟢 Green Light |
| 🟡 Yellow Light |
| 🔴 Red Light |
Ada Example - Low Level
with Ada.Text_IO; use Ada.Text_IO;
procedure Enum_Example is
-- Define an enumeration type
type Traffic_Light is (Red, Yellow, Green);
-- Declare a variable of the enumeration type
Current_Light : Traffic_Light := Red;
begin
-- Print the current light
Put_Line("Current Light: " & Traffic_Light'Image(Current_Light));
-- Change the light and print again
Current_Light := Green;
Put_Line("Updated Light: " & Traffic_Light'Image(Current_Light));
end Enum_Example;
Current Light: RED
Updated Light: GREEN
In Ada, a low level Language, each light is stored as binary code for each value. Probably something like this:
| Trafic_Light | Code |
|---|---|
| 🟢 Green Light | 00 |
| 🟡 Yellow Light | 01 |
| 🔴 Red Light | 10 |
It is bad practice to access those values directly, as they make no sense. All operations should be determined at its abstraction level.
In python, a high level language, we can code the values as strings:
from enum import StrEnum
from pandas.api.types import CategoricalDtype
import pandas as pd
class Size(StrEnum):
XS = "XS"; S = "S"; M = "M"; L = "L"; XL = "XL" # closed set
def ToOrdinal(series: pd.Series, order: list[str]) -> pd.Series:
return series.astype(CategoricalDtype(categories=order, ordered=True))
# use
df["size"] = ToOrdinal(df["size"], [s.value for s in Size])