Financial analysis depends on how you represent data. Prices, returns, portfolio weights and risk measures may look similar, but a programming language does not treat them the same way.
4.1 Variables
Variables have three main components:
Name (Identifier): A unique label like stock_price, daily_returns or portfolio_value;
Data Type: The kind of financial data it holds (e.g., numeric for prices, string for ticker symbols, logical for buy/sell signals);
Value: The actual financial data stored (e.g., 150.25, “AAPL”, true for buy).
Naming variables is harder than it looks. Single-letter names such as x or P save a few keystrokes and cost clarity. Descriptive names like Price or VaR99 are better.
When working with financial data, you encounter several basic data types.
4.1.1 Objects and types
An object bundles data with the functions that act on it. In finance, a portfolio object holds positions, prices and returns together with the methods that value or rebalance them. The data it holds are its attributes and the functions are its methods. It also carries state, meaning its composition and value right now, and identity, so two portfolios with identical holdings remain two distinct objects.
In R, Python and Julia, the values that variables refer to are objects, which lets a single variable hold a structure as rich as a portfolio. A variable itself is only a name bound to such a value, and the same value can have several names.
Every object has a type or class that describes what kind of object it is. The type determines how the language treats the object and what operations you can perform on it.
The main variable types covered in this chapter are integers, floating point numbers, characters/strings and logical values. Date and time variables are covered separately in Chapter 5 due to their complexity in financial applications.
4.1.2 Integer
In mathematics, an integer is a whole number like 0, -1, 10. In financial analysis, integers commonly count discrete items such as the number of shares in a portfolio (100 shares), trading days in a period (252 trading days per year) or position sizes (long 500, short 200).
Programming languages handle integers in slightly different ways. R has a dedicated integer type, written 5L or created with as.integer(), but numeric literals such as 5 default to double. Python has a dedicated int type. In Julia, integer literals are Int (Int64 on a 64-bit platform). These differences matter for accuracy, for interfacing with external systems and for memory use.
4.1.3 Floating point numbers
Floating point numbers represent real numbers with decimal places, such as stock prices ($150.25), returns (0.0523) or volatility measures (0.187). These are the most common type of numerical data in financial analysis.
All three languages store floating point numbers similarly (as 64-bit doubles) and can handle very large and very small values, making them suitable for financial calculations ranging from individual stock prices to portfolio valuations in millions or billions.
However, floating point numbers have precision limitations, discussed in Section 4.2, that affect financial calculations.
4.1.4 Characters and strings
Text data is stored as strings (called character in R, str in Python and String in Julia). You create string values by enclosing text in quotes, such as "AAPL". R and Python accept single or double quotes for strings, while Julia requires double quotes and reserves single quotes for Char literals, so 'R' is valid Julia but 'Risk' is not.
Ticker symbols ("AAPL", "MSFT"), instrument names ("Apple Inc.", "10-Year Treasury"), currency codes ("USD", "GBP") and categorical data such as sector classifications ("Technology", "Healthcare") are common examples of string data in financial analysis.
All three languages provide functions for combining strings, extracting substrings and pattern matching — all useful when cleaning and manipulating financial datasets.
4.1.5 Logical
Logical values (also called Boolean values) represent true or false. The syntax varies slightly: R uses TRUE/FALSE, Python uses True/False and Julia uses true/false. Financial analysts use logical values for decision making and filtering, such as buy/sell signals, portfolio inclusion flags, market condition indicators or risk threshold breaches.
Logical values are particularly useful for subsetting financial data. For example, you might filter stocks where the price-to-earnings ratio is below 15 or identify days when trading volume exceeded the average. These comparisons return logical vectors that can be used to select specific observations from your datasets.
4.2 Technical background
The practical view is not enough. To see why data types behave as they do, you need the machinery underneath.
4.2.1 Bit
In computer science, a bit (short for binary digit) is the smallest unit of data in a computer. It can hold only one of two possible values:
0 → Represents an “off” state or false.
1 → Represents an “on” state or true.
A bit is the building block of binary code, the language computers use to process and store data.
The letter ‘A’ in ASCII is represented as 01000001.
The number 5 in binary is 00000101.
4.2.2 Character encoding
Computers store characters as numbers. ASCII and Unicode specify which number maps to which character.
ASCII was developed in the 1960s as a standard for encoding English text. It uses 7 bits to represent each character, allowing for 128 unique characters (values 0–127), and covers basic English letters, digits (0–9), punctuation marks and control characters (e.g., newline, carriage return).
It supports only English characters and has no native support for special symbols, emojis or non-Latin scripts (e.g., Chinese, Arabic). For example, the character ‘A’ is represented by the number 65, the 01000001 seen above.
Unicode was introduced in the 1990s to address the global limitations of ASCII and aims to encode all characters from all written languages, as well as emojis and symbols. It covers characters from multiple scripts and offers several encoding formats:
UTF-8 (variable length, backwards compatible with ASCII)
UTF-16
UTF-32
4.2.3 Floating point precision
While floating point numbers work well for most financial calculations, they have precision limitations due to how computers store decimal numbers in binary format. Most decimal fractions, including 0.1, have no exact binary representation, only a truncated one, which produces small rounding errors in ordinary arithmetic.
Here is the classic arithmetic error that demonstrates the issue:
result =0.1+0.2cat(format(result, digits=17), "\n")cat(result ==0.3, "\n")cat("Difference from 0.3:", format(result -0.3, digits=16), "\n")
0.30000000000000004
FALSE
Difference from 0.3: 5.551115123125783e-17
result =0.1+0.2print(result)print(result ==0.3)print(f"Difference from 0.3: {result -0.3}")
0.30000000000000004
False
Difference from 0.3: 5.551115123125783e-17
result =0.1+0.2println(result)println(result ==0.3)println("Difference from 0.3: ", result -0.3)
0.30000000000000004
false
Difference from 0.3: 5.551115123125783e-17
In large risk systems, the danger is not the size of an individual rounding error but where it surfaces. An exact-equality test can fail silently, as above, and subtracting two nearly equal large numbers cancels most of their significant digits, a poorly conditioned operation regardless of how precisely each number was computed. The algorithm chosen to compute a quantity, not just the data type, determines how much of this risk a calculation carries.