Prediction is very difficult, especially about the future.
Common quotation, variously attributed to Yogi Berra, Niels Bohr, Mark Twain and others.
Introduction
In Isaac Asimov’s famous science fiction novel Foundation, a group of scientists in the distant future led by Hari Seldon discover a mathematical method to predict the course of future events, anticipating the collapse of the reigning Galactic Empire into a new Dark Age. Armed with the mathematical methods of Hari Seldon’s “psychohistory,” the scientists create a Foundation on the edge of the Galaxy that saves civilization from the prophesied Dark Age. The notion that mathematics can be used to predict human behavior in economics, finance, politics, and many other fields and activities has great appeal. With the spread of increasingly powerful computers, complex mathematical models of economics, finance, and other human activities have become more and more common. Often, the motives are much less admirable than the selfless super scientists of Asimov’s tale. Often, too, the accuracy and performance of the mathematical models has been much less impressive than Asimov’s fictional new science of psychohistory.
In the last few years, complex models of the value of mortgage backed securities have proven disastrously incorrect, a major contributing factor to the Great Recession, the present financial crisis. This is only the latest in a succession of such failures in quantitative finance. Similarly, many sophisticated econometric models of the economy have proven unreliable. To the extent that these models are shaping public policy, personal and corporate investment decisions, and so forth, the pitfalls of mathematical modeling and seemingly abstruse issues in the philosophy of science such as Karl Popper‘s doctrine of falsifiability are having a substantial impact on people and society.
In fact, applying mathematical models to economics, finance, and other human activities is especially treacherous. All mathematical modeling suffers from the deep problem that one can construct an infinite range of functions that approximate current observations and data arbitrarily well and yet make any and all possible predictions about possible new observations. In practice, human beings use various criterion to select mathematical models that are likely to be true, many of which criterion cannot be justified in any rigorous or rational way and some of which criterion are difficult to identify (intuition, “that just doesn’t feel right”, “God doesn’t play dice with the universe.”). Nonetheless, human judgment has a high error rate, though surely much more accurate than a blind guess.
In addition to this pervasive problem, many of the assumptions used in applying mathematical models to physical processes such as the motion of the planets or radioactive decay surely do not apply to economics, finance, or other forms of human behavior. Our expectation is that the motion of the planets is governed by the same “laws” today as last year or last decade or in 1605 when Johannes Kepler first recognized the elliptical orbits of the planets. Indeed, this regularity of many natural phenomenon is strongly born out by experience. On the other hand, the economy or financial markets or other human activities change and evolve. However imperfectly, human beings learn from mistakes, develop new technologies and processes. Human beings are herd animals and prone to fads and fashions that have no parallel in physical phenomena. One would not expect the market for gold to be the same today as in 1960 and it was not. The price of gold was fixed by the US federal government in 1960 and today it is not.
This article takes a look at the price of gold since 1970 when the United States ended the gold standard. Gold is currently rising sharply in price as it did in the late 1970’s and early 1980’s. This article will show how it is possible to construct many different purely symbolic mathematical models of the price of gold that make different predictions. Indeed, one can cook up whatever prediction one would like. The article will also discuss the serious problems with applying the mathematical methods of physics and other “hard” sciences to the price of gold as a specific example of a general problem.
Mathematics for Goldfinger
With the collapse of the Bretton-Woods foreign exchange system (1968-1971), the price of gold, previously fixed to the US dollar, was allowed to float free. Since 1970 the price of an ounce of gold in US dollars has risen substantially.
Nominal Gold Price
The United States government reports a consumer price index (CPI) that is purported to reflect the cost of living in US dollars for a typical US citizen. Although there are some reasons to be skeptical about the accuracy of the CPI in recent years, this article will use the CPI as a proxy for the overall price level in the United States.
United States Consumer Price Index
One can see that the CPI has generally risen at a higher rate since the United States ended the gold standard in 1970. This also corresponds to a period of overall rising energy prices and very limited progress in power and propulsion technologies. Many other areas have seen relatively limited scientific and technological progress during this period. Computers and electronics have, of course, continued their historical trend of rapid progress to the present.
Historically, the US inflation rate was high during the periods of World War I and World War II, but otherwise generally lower than the present period, since 1970. The official inflation rate, the rate of increase of the CPI, was especially high during the 1970s, a period of sharp increases in nominal and real energy prices. The inflation rate derived from the CPI in recent years does not seem to reflect the common experience of rising energy prices since the late 1990s. Nor does it show any evidence of the sharp rise in housing prices in the US from 2002 to 2005; by many estimates, housing prices in urban areas remain high compared to the historical trend.
Inflation Adjusted Gold Price
Investors are generally interested in the real inflation-adjusted value of an investment. The value of gold in real terms fluctuated somewhat during the period of the gold standard but has fluctuated much more since 1970. Particularly notable is the large spike in the real and nominal price of gold in the early 1980’s. In real terms, the current rise in gold price has not reached the level of the spike in the early 1980’s. If inflation has been higher than the official CPI, the current real price of gold would be even lower than the spike in the early 1980s.
One can construct a simple symbolic mathematical model of the real price of gold since 1970 (the gold standard disintegrated between 1968 and August 1971 when the US government ended any attempt to tie the dollar to gold) by using a polynomial with several terms:
Where [tex]\mathrm{p}\left( t\right)[/tex] is the gold price as a function of the time [tex]t[/tex]. This is a simple mathematical model with little real-world justification. It will, in fact, make grossly unrealistic predictions that should call it into question. A polynomial model of the time series of real gold prices is:
One can approximate any continuous function as accurately as one would like with a sum of a series of powers. The problem is that a power such as [tex] t^{12} [/tex] will grow without bound as the independent variable [tex] t [/tex] grows. This is usually quite unrealistic. This is a simple illustration of the difference between a symbolic mathematical model of reality and our common sense every day sense of reality.
Apocalypse or Gold Bubble?
Many other mathematical models are possible. In fact, there are an infinite number of functions or curves that agree with the data during the period from 1970 to 2009. In this way, one can predict anything. In mathematical models of physical phenomenon, it is common to try to construct the model from a set of building block functions, or more generally terms such as terms in a differential equation. This may be arbitrary or justified by arguing that the building blocks represent fundamental building blocks of the physical process in some way. In practice, one is trying to capture regularities in the data that may recur in new data. For example, the position of a pendulum is periodic; it repeats over and over again. Simple harmonic motion of this type was one of the first kinds of behavior understood mathematically by the ancients. One can construct models of the gold price data that agree with the data quite well by eye using a collection of peaks rather than periodic functions. One might, for example, represent the seeming peaks in the gold price data as the Gaussian or Normal function, commonly referred to as the “Bell Curve,”
This simple model with two Gaussians does not agree very well with the data. It predicts the following future performance:
Gold 2 Gaussian Prediction
It predicts that the real price of gold will peak in about 2025 and then drop. One can get much better agreement between the model and the gold price data by adding more Gaussians, loosely corresponding to the apparent gold peaks in about 1974, 1982, 1987, 1993, and today. The spike in 1982 is especially sharp and can better be approximated by combining two Gaussians.
Gold 7 Gaussian Model
Now the agreement by eye is much better. The curves appear essentially the same. This model makes a different prediction.
Gold 7 Gaussian Prediction
This model predicts a peak in real gold prices in just a few years, about 2012, followed by a decline to essentially zero. One can get significantly different predictions simply by using a different function to model the peaks in the gold price. For example, one can use the Cauchy-Lorentz distribution as a model for the peaks:
where [tex]t[/tex] is the time (year in this case), [tex] \mu [/tex] is the location of the peak, and [tex] \sigma [/tex] is a measure of the width or dispersion of the peak. Initially, one can try a model with two peaks:
This model looks very similar to the two Gaussian model. It predicts something different however.
Gold 2 Cauchy Prediction
In this case, the price of gold trails off slowly instead of dropping to essentially zero in about a decade. This is a difference between the Gaussian and the Cauchy-Lorentz functions. Of course, the agreement between the model and data is not very good. One probably would not and should not trust it. One can achieve better agreement with more peaks, just as in the Gaussian models.
Gold 7 Cauchy Model
Now the agreement is almost exact by eye. The prediction however differs from the seven Gaussian model.
Gold 7 Cauchy Prediction
Again, the price of gold trails off slowly over a period of decades due to the difference between the Gaussian and Cauchy-Lorentz peak models. In fact, one can get essentially any prediction by choosing the appropriate mathematical model. How reasonable are these predictions? There are several theories about the present rise in gold prices. One clear contender is that the gold price rise in the last decade is yet another speculative financial bubble. In this scenario, one would expect the price of gold to drop substantially within a decade. One might also argue that the sharp run-up in gold prices will encourage overproduction of gold and the development of better gold refining, recycling technologies, alternatives to gold, and even perhaps the alchemist’s dream of converting base metals into gold (using nuclear reactors for example). At the other extreme, the rise in gold prices is tied to apocalyptic scenarios in which the US and other governments go bankrupt due to deficit spending and the dollar and other paper currencies becomes as valueless as the German mark during the infamous German hyperinflation. Thus, the real inflation-adjusted price of gold spikes as investors seek an inflation proof haven. Unfortunately, civilization collapses. Gold and other luxury items become valueless in the bitter Darwinian battle for survival in the post-apocalyptic world. The most valuable possessions are a gun, ammunition, and a large horde of dried food — all purchased from advertisers on Alex Jones web site. In either scenario, gold eventually tanks.
All kidding aside, it is usually possible to find technically sophisticated, plausible justifications for mathematical models. Usually does not mean always. The behavior of the polynomial models is grossly unrealistic. How likely is it that the price of gold would go negative, let alone extremely negative — meaning that people would be paying large sums of money to get rid of gold? The evidence that the Earth is nearly spherical with a diameter of about 8000 miles is very strong. Any argument that the Earth is really flat is extremely difficult to make in the present day. Most of us are pretty confident that the Sun will set today and rise again tomorrow and continue to do so for many, many years to come. There is some knowledge that is pretty certain. There are some mathematical models that have proven extremely accurate and reliable.
Nonetheless, it is usually possible to find plausible arguments for mathematical models and conceptual theories. One can usually explain away even grossly contradictory — “falsifying” in the language of Karl Popper — observations or experiments in a technically sophisticated, plausible way. This can create the illusion that a theory or mathematical model falls into the category of almost certain knowledge, our common sense notion of a fact. Once upon a time, many people believed that it was a fact that the Earth was flat. How could it possibly be a sphere? Political power, social status, and sizable funding tend to flow to those who claim certainty, to know hard facts rather than speculative theories. In common thinking, an expert is someone who says “I know the answer.” not someone who says “I don’t know.”
Scientists and science popularizers often seek to promote reigning scientific theories to the level of “almost certain” knowledge or “facts,” such as that the Earth is roughly spherical or the historical fact of the Holocaust. These latter two are the most common analogies cited in the popular science literature. Scientific theories are said to be supported by “overwhelming evidence.” There is a “consensus” that the theory is correct. The theory is no longer a theory, but a fact. In debates about evolution and creation, one encounters the curious claim that evolution is a falsifiable theory, that creationism or “intelligent design” is not falsifiable and thus not scientific, and that evolution is also a fact beyond any reasonable dispute or falsification. Increasingly, malcontents or die-hards who question the “consensus” are “deniers” or “denialists” in analogy to Holocaust deniers, an extremely emotional analogy indeed. There are now evolution deniers, AIDS deniers, global warming deniers, and a growing list of other deniers. Yet, certain knowledge is relatively rare especially in the frontiers of science.
In the case of gold, one can make a strong argument that the models above are unlikely to be correct. There is a certain fundamental demand for gold for industrial uses, in electronics for example. There is a long history of people buying gold for jewelry. The economic and political problems, the fears and the greed, that have historically driven the fluctuations in the price of gold are likely to continue through the 21st century. Thus, one should doubt models that show the real price of gold dropping to zero or nearly zero. Yet, both symbolic mathematics and verbal reasoning enable one to make a plausible argument that this will happen, supported by fancy symbolic mathematics and technical graphs.
A Deep Problem
It is always possible to perfectly match or fit any set of [tex]N[/tex] data points using a linear combination of at least [tex]N[/tex] linearly independent functions. Under many conditions, a function or composition of building block functions with [tex]N[/tex] adjustable parameters can also match or fit any set of [tex]N[/tex] data points. In addition, with creativity and trial and error, one can often construct mathematical models with less than [tex]N[/tex] parameters or building block functions that nonetheless do a good job of matching or fitting [tex]N[/tex] data points. These models may often fail to predict future data or data that lies outside of the region used to make the model or fit.
In general, a composition of a set of building block functions has a sort of plasticity, a certain ability to match any data to some degree. If there are [tex]N[/tex] building block functions or adjustable parameters and [tex]N[/tex] data points, this plasticity can be rigorously shown to be essentially infinite, that is the model can always match any set of [tex]N[/tex] data points. The philosopher of science Karl Popper called such models or theories unfalsifiable. They can never be proven wrong. They predict everything and therefore they also predict nothing. Unlike popular presentations of his doctrine of falsifiability, Popper recognized that falsifiability was often not a black and white distinction. There were degrees of falsifiability and he tried to define some logical and quantitative criterion for the degree of falsifiability.
What this means is that a mathematical model or theory with [tex]M[/tex] adjustable parameters where [tex]M[/tex] is less than the number of data points [tex]N[/tex] may still match a broad class of possible observations or data. It can be falsified, perhaps, but only with great difficulty. There is, in the examples above, a lot of freedom to match many different sets of data with seven peaks in the model. The models have 21 adjustable parameters and there are 40 data points (40 years). Although the models cannot exactly match all sets of 40 data points, nonetheless they are likely to be good enough for most purposes. Remaining differences between the data and the models can easily be attributed to noise, measurement errors, or some similar excuse.
In physics, there are some examples of very large data sets that match very simple mathematical formulas. The motions of the planets in the Solar System conform to Kepler’s Laws and Newton’s theory of gravitation to a very high degree. This is not necessarily true for the motion of stars in the Milky Way galaxy, galaxies in galactic clusters and so forth. A deviation from Newtonian gravity is a possible explanation for the anomalies often cited as evidence for so-called “dark matter” or “dark energy.” So too, there is an enormous amount of quantitative data on the vibration of springs and strings, the motion of pendulums, simple radioactive decays, and various other physical phenomena that show precise matches to simple mathematical expressions such as Hooke’s Law for springs or exponential decay for radioactive decay. It is fair to say that the amount of data points is now in the millions or more and the models have only a few adjustable parameters such as the half-life of a radioactive isotope. Consequently, it is probably reasonable to have a high degree of confidence in these models. At the other extreme, it is clear that we should put no confidence in the predictive power of models with as many adjustable parameters or building block functions as the number of data points.
In many situations however including the frontiers of human knowledge and often areas like economics, the situation falls into a gray area. The theories, both symbolic mathematical models and conceptual models, may be complex, but not so complex as to be obviously “unfalsifiable.” The amount of data may be limited or of questionable quality, but still not obviously inadequate to draw conclusions. It is here that faulty human judgment is in practice applied, in part because human judgment, fallible though it undoubtedly is, is still the best option, more reliable than symbolic mathematics or computer programs in many cases. In constructing mathematical models in these cases, human judgment is often hidden in the definition of the symbols and the choice of building block functions or other mathematical components used in the model.
Economics, finance, politics and other human activities are especially treacherous areas for mathematical modeling. The mathematical and scientific methods used in physics and other “hard” sciences were developed to study highly repeatable phenomena that do not appear to change appreciably over time. For example, a “fair” coin will come up heads when flipped on average half the time, tails half the time. This was true in the early days of probability and statistics during the Renaissance. It is true today. Fair games of chance work the same way today as yesterday, last year, last century, and presumably thousands of years ago or in the future. One can collect vast amounts of data on these games. Similarly, physical phenomena such as the motion of the planets, radioactive decay, and so forth appear to behave the same way time after time. They do not evolve, learn from mistakes, forget lessons learned, or suddenly change for difficult to understand reasons. None of this is true of much economic or financial data.
In the example of gold, the behavior of gold changed radically from 1968 to August 1971 when the gold standard ended. Since 1970, there have been extensive political, economic, and technological changes that probably effect the price of gold. The Cold War ended. Apartheid in gold producing South Africa ended, followed by a large exodus of the white minority who dominated the mining industry. The use of electronics has increased; gold is used in electronics. Women’s fashions in clothing and jewelry have changed. The terrorist attacks of September 11, 2001 probably contributed to general unease and the rise in gold prices. Against these many changes, there is only forty years of data on gold prices. Day to day gold prices are correlated and gold seems to follow trends over several years, seemingly rising and falling in peaks. Thus, there is very limited data to analyze compared to a physical process. At the same time, one still has the freedom to construct many different mathematical models, theoretically an infinite number.
This ability to construct many different models that agree with observations or experiment but which make quite different predictions is not a problem unique to symbolic mathematical models. Rather, we encounter the same or a similar problem in everyday life, in politics, in personal relationships, where concepts, words, and pictures are the norm rather than symbolic mathematics with its illusion of certainty. It occurs when opposing attorneys are able to present radically different interpretations of the same evidence in a court case. It occurs when political activists explain the same events in radically different ways that almost always confirm their beliefs. It occurs in disputes between co-workers where each sees the same event or problem quite differently (it is all your fault). It occurs in conflicts between husbands and wives when each sees the same events differently. If verbal concepts and mental pictures are actually mathematical models maintained in the brain (but not in a symbolic way), then it may well be exactly the same problem as that encountered in mathematical modeling.
There is no doubt that human judgment is faulty and limited. In some relatively rare cases mathematics or formal logic can clearly outperform human judgment. Nonetheless, in many situations, human judgment and intuition still win out over mathematics, formal logic, or computer programs. It remains an unsolved and perhaps unsolvable problem to find a way to select the right model that, in fact, predicts new observations as well or even better than human judgment.
The nature and origin of human judgment and intuition remains an enigma. Governments have spent billions of dollars and decades on artificial intelligence in the mostly futile effort to replicate even sometimes seemingly simple aspects of human reasoning. Human beings often cannot explain either verbally or in symbolic logical or mathematical ways their successful reasoning processes. Ancient scholars and philosophers might attribute their ideas to divine inspiration or mystical insight. Indeed historical accounts of inventions and discoveries are replete with reported sudden insights or realizations such as the famous story of Archimedes in his bathtub suddenly realizing how to determine the gold content of the King’s crown without destroying the crown and then racing naked through the streets of Syracuse shouting “Heureka!” One may wonder what Archimedes feared the King would do to him if he had failed to solve the problem. The modern scientific view would probably attribute this ability of the human mind to find the right answer to an anthropic or evolutionary cause. Our brain incorporates billions of years of evolution and is thus tuned to the mysterious underlying logical or mathematical structure of the universe.
Conclusion
It is important to realize that one can construct an infinite number of mathematical models that match a set of data. In some sense, one can construct an even “larger” number of mathematical models that match a set of data “well enough,” where remaining differences can easily be attributed to noise, instrument error, or minor effects that can be ignored for practical purposes. In high school and college, one is often exposed to mathematics and geometry as a rigorous deductive system. The epitome of this is Euclid’s geometry which many people are exposed to in high school; high school math courses typically teach the first three of Euclid’s thirteen books. One starts with axioms and definitions that often seem self-evident, with the possible exception of the so-called parallel postulate. One can apply a sequence of logical steps to get a precise unambiguous answer.
Similarly, high school and college science courses frequently focus primarily on extremely well-measured phenomena such as vibrating springs or radioactive decay that precisely follow simple mathematical laws. Scientists are often described as “deriving,” “figuring out,” or “deducing” mathematical laws such as Newton’s Theory of Gravitation, Maxwell’s Equations of Electromagnetism, or Schrodinger’s Equation for quantum mechanics. The implication is that these mathematical theories can be found by the application of rigorous mathematical or logical rules, much in the way that theorems in Euclidean geometry are proven. This has a great appeal compared to messy, fallible human judgment and intuition. But the reality is that the theories were found through the application of messy, fallible human judgment and intuition, perhaps assisted by some mathematics, by model fitting methods, and so forth, but in the end it was mysterious human judgment and intuition.
If we could understand what human beings are actually doing, this would be a great advance. It would be an even greater advance to find a way to improve human judgment and intuition, which is certainly quite fallible.
In the meantime, it is particularly hazardous to try to apply mathematical modeling to economics, finance, and human behavior. This does not mean that we should not try. Nor does it mean that there may not be successes in applying mathematics to human activity. Indeed, in economics, there are some mathematical rules of thumb that are often correct. It is, for example, generally observed that inflation is lower when unemployment is higher; this relation loosely follows a mathematical curve. However, we are a very long distance from a predictive mathematical method comparable to Isaac Asimov’s fictional psychohistory if this is even possible. Most people don’t think in mathematics; can human behavior really be reduced to mathematics?
The present financial crisis illustrates that these seemingly abstruse issues of mathematical models can impact the lives of many people and organizations. This is, if anything, likely to increase with growing reliance on computers and mathematical models implemented in computer software and hardware. There remains no greater wisdom than the ancient Latin saying: Caveat Emptor (Buyer Beware).
Note: An appendix with the technical details of the analysis and plots presented above follows the Suggested Reading/References section below. This includes the raw data, the models, and the Octave scripts used to fit the models to the annual gold price data. This article is primarily for informational and educational purposes. It is not investment advice.
John F. McGowan, Ph.D. is a software developer, research scientist, and consultant. He works primarily in the area of complex algorithms that embody advanced mathematical and logical concepts, including speech recognition and video compression technologies. He has extensive experience developing software in C, C++, Visual Basic, Mathematica, MATLAB, and many other programming languages. He is probably best known for his AVI Overview, an Internet FAQ (Frequently Asked Questions) on the Microsoft AVI (Audio Video Interleave) file format. He has worked as a contractor at NASA Ames Research Center involved in the research and development of image and video processing algorithms and technology. He has published articles on the origin and evolution of life, the exploration of Mars (anticipating the discovery of methane on Mars), and cheap access to space. He has a Ph.D. in physics from the University of Illinois at Urbana-Champaign and a B.S. in physics from the California Institute of Technology (Caltech). He can be reached at jmcgowan11@earthlink.net.
Suggested Reading/References
Karl Popper, The Logic of Scientific Discovery, Routledge, London, England 2000 (First published 1959, English translation with new notes and appendices of Logik der Forschung, published Vienna, Austria, 1934)
Paul Feyerabend, Against Method (3rd Edition), Verso, 1993
The models were fitted to the data using the free Octave numerical programming environment. Octave is Matlab compatible and available under the GNU Public License. Octave is available in binary as well as source code versions for Windows, Mac OS, and Linux. The standard polyfit polynomial fitting function in Octave was used to fit the polynomial models to the annual gold price data. The leasqr least squares fitting function from the optim Octave add-on package was used to fit the Gaussian and Cauchy-Lorentz peak models to the annual gold price data. Octave Forge add-on packages are available as Unix style blatz.tar.gz files. There is no need to extract the contents of these files. Octave has a command pkg install which handles installing the packages; just type pkg install blatz.tar.gz. The GNU Octave installation includes a C/C++ compiler to compile any C or C++ files included in the package. Note that the run-time errors reported by Octave, for example due to a syntax error, can be cryptic. Be patient and don’t always trust the error message.
% plot_gold.m
% Description: Octave (Matlab compatible) script to plot annual gold price data
% and fit polynomial models to the data. Tested on Windows XP Service Pack 2
% with Octave 3.2.4 for Windows installed.
%
% Author: John F. McGowan, Ph.D.
% Copyright (C) 2010 by John F. McGowan
%
%
disp('reading gold price data...');
fflush(stdout);
data = dlmread('annual_gold_price_from_1900.txt');
disp('making plot 1');
fflush(stdout);
figure(1)
plot(data(:,1), data(:,8));
title('Inflation Adjusted Gold Price');
xlabel('Year');
ylabel('Gold Price USD (2009)');
disp('making plot 2');
fflush(stdout);
figure(2)
plot(data(:,1), data(:,2));
title('Nominal Gold Price');
xlabel('Year');
ylabel('Gold Price USD');
disp('making cpi plot');
figure(3)
plot(data(:,1), data(:,6));
title('US Consumer Price Index (CPI)');
xlabel('Year');
% annual inflation rate
disp('plotting annual inflation rate');
fflush(stdout);
figure(4)
cpi = data(:,6);
cpi_shift = shift(cpi,1);
inflation = (cpi - cpi_shift) ./ cpi_shift;
years = data(:,1);
plot(years(2:end), inflation(2:end)*100);
title('US Annual Inflation Rate');
xlabel('Year');
ylabel('Inflation (%)');
%
% fitting polynomial model to data 1970 to 2009
%
disp('making gold price prediction 12 terms');
fflush(stdout);
figure(5)
floating = years(71:end);
floating_gold_price = data(71:end, 8);
p = polyfit(floating, floating_gold_price, 12);
prediction = (1970:2020);
f = polyval(p, prediction);
plot(floating, floating_gold_price, 'o', prediction, f, '-');
title('Polynomial Model of Gold Price (12 Terms)');
xlabel('Year');
ylabel('Gold Price USD (2009)');
%
disp('making gold price prediction 24 terms');
fflush(stdout);
figure(6)
floating = years(71:end);
floating_gold_price = data(71:end, 8);
p = polyfit(floating, floating_gold_price, 24);
prediction = (1970:2020);
f = polyval(p, prediction);
plot(floating, floating_gold_price, 'o', prediction, f, '-');
title('Polynomial Model of Gold Price (24 Terms)');
xlabel('Year');
ylabel('Gold Price USD (2009)');
%
disp('making gold price prediction 32 terms');
fflush(stdout);
figure(7)
floating = years(71:end);
floating_gold_price = data(71:end, 8);
p = polyfit(floating, floating_gold_price, 32);
prediction = (1970:2020);
f = polyval(p, prediction);
plot(floating, floating_gold_price, 'o', prediction, f, '-');
title('Polynomial Model of Gold Price (32 Terms)');
xlabel('Year');
ylabel('Gold Price USD (2009)');
disp('all done');
fflush(stdout);
% THE END
The script to fit the two Gaussian peak model to the data is:
% fit_gold.m
% Description: Octave (matlab compatible) script to fit two gaussian peak model
% to annual gold price data using Octave 3.2.4 and the optim package version 1.0.15
% Tested on Windows XP Service Pack 2 with Octave 3.2.4 and optim 1.0.15 installed.
%
% Author: John F. McGowan, Ph.D.
% Copyright (C) 2010 by John F. McGowan
%
disp('fitting model to gold price data');
fflush(stdout);
% fit model to gold price data
%floating_gold_price has inflation adjusted gold price since 1970
floating_years = years(71:end);
% Define functions
% model annual gold price data as two Gaussians
leasqrfunc = @(x,p) p(1) * exp(-1.0*(x - p(2)).^2/p(3)^2) + p(4) * exp(-1.0*(x - p(5)).^2/p(6)^2);
leasqrdfdp = @(x, f, p, dp, func) [exp(-1.0*(x - p(2)).^2/p(3)^2), (2*(x - p(2))/p(3)^2) * p(1) .* exp(-1.0*(x - p(2)).^2/p(3)^2), (2 * (x - p(2)).^2/p(3)^3) * p(1) .* exp(-1.0*(x - p(2)).^2/p(3)^2), exp(-1.0*(x - p(5)).^2/p(6)^2), (2*(x - p(5))/p(6).^2) * p(4) .* exp(-1.0*(x - p(5)).^2/p(6)^2), (2 * (x - p(5)).^2/p(6)^3) * p(4) .* exp(-1.0*(x - p(5)).^2/p(6)^2) ];
wt1 = ones(size(floating_gold_price));
t = floating_years;
data = floating_gold_price;
F = leasqrfunc;
dFdp = leasqrdfdp; % exact derivative
dp = [50.0; 1.0; 1.0; 50.0; 1.0; 1.0];
pin = [500.0; 1980.; 5.0; 500.0; 2010.0; 5.0 ];
stol=0.01; niter=50;
minstep = [10.0; 0.2; 0.2; 10.0; 0.2; 0.2];
maxstep = [100.0; 5.0; 5.0; 100.0; 5.0; 5.0];
options = [minstep, maxstep];
disp(size(t));
disp(size(data));
disp(size(wt1));
fflush(stdout);
figure(1);
global verbose;
verbose = 1;
[f1, p1, kvg1, iter1, corp1, covp1, covr1, stdresid1, Z1, r21] = ...
leasqr (t, data, pin, F, stol, niter, wt1, dp, dFdp, options);
%
% make a prediction
figure(2);
pred_years = [1970:2020];
prediction = leasqrfunc(pred_years, p1);
plot(floating_years, data, 'o', pred_years, prediction, '-');
title('Inflation Adjusted Gold Price (Prediction vs Data)');
xlabel('Year');
ylabel('Gold Price USD (2009)');
%
figure(3);
pred_years = [1970:2050];
prediction = leasqrfunc(pred_years, p1);
plot(floating_years, data, 'o', pred_years, prediction, '-');
title('Inflation Adjusted Gold Price (Prediction vs Data)');
xlabel('Year');
ylabel('Gold Price USD (2009)');
%
figure(4);
pred_years = [1970:2100];
prediction = leasqrfunc(pred_years, p1);
plot(floating_years, data, 'o', pred_years, prediction, '-');
title('Inflation Adjusted Gold Price (Prediction vs Data)');
xlabel('Year');
ylabel('Gold Price USD (2009)');
data_2G = data;
model_2G = leasqrfunc(floating_years, p1);
diff_2G = model_2G - data_2G;
chisq_2G = diff_2G' * diff_2G;
disp('all done');
% THE END
The script to fit the seven (7) Gaussian peak model to the annual gold price data is:
The derivatives of the models with respect to parameters were computed with the symbolic manipulation package Maxima. Maxima was also used to generate some of the Latex mathematical formulas in this article.
Excellent post! With the advent of Quantitative Finance and the Big Data fever, it is really important to think about the limits of Mathematical Modelling.