
Linear regression for non-overlapping segments of a wave

Eduard Kas
What I would like to do is calculate the regression line first of all for a segment with the first 32 points, then for the subsequent 32 points, next for the third segment of 32 points, and so on, up to the 30th segment.
Per segment I would also like to calculate the Fluctuation magnitude (see variable Fluctuationmagn) and write the outcomes (a single real number for each of the 30 segments) to a wave, so that another curve fit can be performed. This time with the Fluctuation magnitude as the dependent factor and the segment number as the independent factor.
Any help is highly welcome. Many thanks in advance.
Eduard Kas
<code>#pragma rtGlobals=1 // Use modern global access method. Function PriceTrend(Startlevel) Variable Startlevel // Price of share at time = 0 Variable Simulation = CreatePrices(Startlevel) // Fuction to simulate random walk of share // prices Variable Returncalc = CalcReturn(input) // Function to calculate natural logs of return // per period Variable Demeanedcalc = Demeaned(periodreturn) // Function to calculate demeaned returns Variable FFactor = Fluctuationmagnitude(squareddiff) // Function to calculate local trend // and fluctuation magnitude End Function CreatePrices(Startlevel) // Function to simulate random walk of share prices Variable Startlevel //Share price at time = 0 in euros Make/O/N=961 input // Create wave for share prices Input[0] = Startlevel // Share price at time = 0 Input[1,960] =Input(p-1) + gnoise(0.15) // Share prices in subsequent period, where price is // price in preceding period plus period return drawn // from a Gaussian (noise) distribution End Function CalcReturn(input) // Function to calculate natural logs of return per period Wave Input // Read share prices generated by preceding function String OutputName = NameOfWave(input)+"_logarithm" // Store the name of wave plus // "logartihm" into the new wave // OuputName Duplicate/O input $OutputName // Duplicate the wave with generated share prices as the // wave named input_logarithm Wave output = $OutputName // Reference input_logarithm so that you can use it output = Ln(input) // Calculate logarithmic values of wave Make/O/N=960 Periodreturn Periodreturn[0]= 0 // Assume return in first period to be zero Periodreturn[1,959] = output(p) - output(p-1) // In next period logartihmic return equals logarithmic // share price of this period minus logarithmic // share price in previous period End Function Demeaned(periodreturn) // Function to calculate demeaned returns Wave periodreturn // Read return values generated by previous function WaveStats/Q periodreturn // Determine statistis of return wave Variable avg = V_avg // Determine average return Make/O/N=960 Cumulative // Create wave that will include each period's return above or // below the average for all periods Cumulative[0] = Periodreturn[0]-V_avg // Determine return below or above the average for // the first period Cumulative[1,959] = Cumulative(p-1) + Periodreturn(p) - V_avg // Determine return below or // above the average for all // subsequent periods End Function Fluctuationmagnitude(cumulative) // Function to calculate local trend and fluctuation magnitude Wave cumulative // Read cumulative demeaned returns from previous function Make/O/N=960 Number // Create a new wave to show the period numbers Variable i = 0 // Initialise i do Number[i] = i +1 // Add the number 1 to the level in the preceding period, so that the first // period carries the number 1 i += 1 while(i+1<959) CurveFit line cumulative[0,959] /x=NUMBER /D // Generate a linear regression function in which // the y values (demeaned cumulative // returns are regressed against the x values) // (the period numbers) Wave Fitted = fit_Cumulative // Read fitted regression line into Wave named 'Fitted' Wave Squareddiff // Create wave squared differences Squareddiff = (Cumulative - Fitted)^2 // Calculate squared differences between // cumulative demeaned returns and fitted // regression line WaveStats Squareddiff // Dtermine statistics of wave squared differences Variable Sumofsquareddiff // Create parameter sum of squared differences Sumofsquareddiff = V_Sum //Determine sum Variable Fluctuationmagn // Create wave Fluctuation magnitude Fluctuationmagn = Sqrt(V_Sum/960) // Calculate fluctuation magnitude End<pre><code class="language-igor"><code>
You can limit a curve fit to a subset of a wave using square brackets like this:
I recommend changing your Fluctuationmagnitude to take startPoint, endPoint parameters passed in from a new, higher-level function. The new function would create the wave now created in Fluctuationmagnitude. It would then go into a loop calling Fluctuationmagnitude 30 times, once for each segment, passing startPoint and endPoint as parameters.
In the higher level function you can create a 30-point wave to store the outcome from each invocation of Fluctuationmagnitude. Fluctuationmagnitude would return this outcome result.
BTW, this:
can be written like this:
For details, execute:
Also, this:
does not read anything. It creates a reference to fit_Cumulative.
Likewise, this does not create any wave. Rather it creates a reference to an existing wave:
A wave reference is a symbol local to a function the references a wave. For details:
June 11, 2012 at 02:23 pm - Permalink
John Weeks
WaveMetrics, Inc.
support@wavemetrics.com
June 11, 2012 at 02:26 pm - Permalink
June 17, 2012 at 09:47 pm - Permalink
set the first 16 points of the Xwave equal to 1 up to and including 16 and to repeat these values 60 times for an entire series length of 960 in terms of
return values (and 961 in terms of price values). (So far I created the Function SeriesCalc separately in order to test it. Later on, I would like to integrate
it with the other functions and determine the number of segments, depending on the length of a segment.)
The Function SeriesCalc does not behave like expected. If I call it by SeriesCalc(961, 16) for each of the 960 points of Xwave the value of -928 is created.
My question: what I am doing wrong.
Any help is highly welcome. Many thanks in advance.
Eduard
June 17, 2012 at 10:30 pm - Permalink
This sets every point of Xwave every time through the loop. Perhaps you mean something like:
This is equivalent to a single statement:
For details on this kind of statement:
You will probably have to read this section multiple times and do some experimentation for it to sink in.
June 17, 2012 at 11:28 pm - Permalink
Eduard
June 20, 2012 at 12:24 am - Permalink