توضیح 1
The live starter code is self-contained and uses only runner-compatible functions.
Loading
درس 14 از 22
Document provenance, align calendars, define returns, and interpret quote direction before modelling.
Motivation
Return code can be mathematically correct and economically reversed when an adjustment field, currency quote, or calendar rule is misunderstood.
Why this matters
Document provenance, align calendars, define returns, and interpret quote direction before modelling.
Packages and data
Base R; xts, zoo, quantmod, tidyquant, and PerformanceAnalytics are optional.
توضیح 1
The live starter code is self-contained and uses only runner-compatible functions.
توضیح 2
Book-only outputs are labelled when the separate source dataset or advanced package was not supplied.
توضیح 3
R code, paths, function names, and formulas remain left-to-right in every locale.
اصطلاحات
Price ratio minus one; compounds multiplicatively.
Pt/P(t-1)-1
Difference in log price; adds across time.
log(Pt)-log(P(t-1))
Units of one currency per another.
USD/CAD is CAD per USD
نمادگذاری و فرمولها
Rt = Pt/P(t-1)-1; rt = log(Pt)-log(P(t-1))
Simple and log returns are close for small movements but differ in tails and compounding.
مثال حلشده
سناریو
How close are simple and log returns for a short USD/CAD teaching path?
بررسی با R
fx <- c(1.32,1.34,1.31,1.36,1.35,1.38)
simple <- 100*(fx[-1]/head(fx,-1)-1)
log_return <- 100*diff(log(fx))
print(data.frame(simple,log_return,difference=simple-log_return))
print(sum(log_return)/100)
print(log(tail(fx,1)/fx[1]))Because the quote is CAD per USD, a positive return means one U.S. dollar buys more Canadian dollars. Limitation: This tiny embedded path is not live market data and is not an investment recommendation.
Visual
The quote moves within a narrow range before return conversion.
ارتباط با R
Run the self-contained starter code in the protected STATLAB R runner. The code prints an auditable result and avoids network or unrestricted file access.
Live R Lab
Run a self-contained example, verify its output, and explain one limitation for financial data and return series.
Ready to run
Common mistake
Do not log interest-rate levels as though they were positive asset prices, and do not reverse quote interpretation.
STATLAB Tip
Record source, code, field, currency, time zone, adjustment, coverage, and retrieval date.
تمرین هدایتشده
سناریو
Invert the quote and verify that log returns change sign.
Compare your result with the definition, units, and model assumptions—not only with a target number.
Exercises
Complete these without looking at the selected solutions. More than one defensible program may exist.
توضیح 1
Verify cumulative log return identity.
توضیح 2
Detect missing months in a date sequence.
توضیح 3
Explain why yields crossing zero need another transformation.
Selected solutions
These are compact solution routes. Confirm dimensions, units, and any changed modelling choices.
توضیح 1
sum(diff(log(fx))) equals log(tail(fx,1)/fx[1]).
توضیح 2
setdiff(seq(min(date),max(date),by='month'),date)
توضیح 3
A percentage change or log is undefined/problematic at zero or negative levels.
Chapter summary
Document provenance, align calendars, define returns, and interpret quote direction before modelling.
توضیح 1
Provenance is part of the model.
توضیح 2
Simple returns compound; log returns add.
توضیح 3
Quote units determine meaning.
توضیح 4
Calendar alignment must be explicit.
اصطلاحات
log returns
Use in Financial Data and Return Series.
wealth index
Use in Financial Data and Return Series.
calendar alignment
Use in Financial Data and Return Series.
dated series
Use in Financial Data and Return Series.
References and provenance
Safavi (2026), Chapter 14; Bank of Canada Valet documentation. Student notes: Mohammad Safavi, Ph.D., STATLAB Academy, Version 1.0.
منبع
The authoritative 125-page English PDF accompanies this native lesson.
دریافتبازاندیشی
Name the assumption, evidence you would seek, and how the recommendation might change.
ارزیابی پایانی