I want to generate a mock price series. I want it to be within a certain range and have a defined correlation with the original price series.
If I choose, say, oil, I want as many time series which have some similar characteristics in terms of correlation but not exact. I do not want the data to wander off in a completely different path from the historic path taken. It is not necessarily co-integrated, correlation will suffice. I would also like these to adhere to the original price range. The series should be random, e.g. each new realization should take a different path.
Answer
Further to my comments to Samik R's answer above, here is the link to my blog post where my attempts to recreate the method linked to in said answer are given.
Edit to include more information per Tal Fishman's comment
Octave .oct function code
#include
#include
#include
#include "MersenneTwister.h"
DEFUN_DLD (permute_vector, args, , "Input is a vector that is to be permuted once")
{
octave_value_list retval;
int nargin = args.length () ;
int vec_length = args(0).length () ;
// check the input arguments
if ( nargin > 1 )
{
error ("Invalid arguments. Input is a vector that is to be permuted once") ;
return retval ;
}
if (vec_length < 2 )
{
error ("Invalid arguments. Input is a vector that is to be permuted once") ;
return retval ;
}
if (error_state)
{
error ("Invalid arguments. Input is a vector that is to be permuted once") ;
return retval ;
}
// end of input checking
ComplexNDArray input_vector = args(0).complex_array_value () ;
ComplexNDArray output_vector = args(0).complex_array_value () ;
int k1;
int k2;
MTRand mtrand1; // Declare the Mersenne Twister Class - will seed from system time
k1 = vec_length - 1; // initialise prior to shuffling the vector
while (k1 > 0) // While at least 2 left to shuffle
{
k2 = mtrand1.randInt( k1 ); // Pick an int from 0 through k1
if (k2 > k1) // check if random vector index no. k2 is > than max vector index - should never happen
{
k2 = k1 - 1; // But this is cheap insurance against disaster if it does happen
}
output_vector(k1) = input_vector(k2) ; // allocate random pick k2 from input_vector to the k1 vector index of output_vector
input_vector(k2) = input_vector(k1) ; // replace random pick k2 content of input_vector with content k1 of input_vector
k1 = k1 - 1; // count down
} // Shuffling is complete when this while loop exits
retval(0) = output_vector ;
return retval; // Return the output to Octave
}
Octave script code
clear all
contract = input( "Enter contract symbol e.g. sp: ","s") ;
data = load("-ascii",contract) ;
n = rows(data)
index_begin = input( "Enter index_begin: ") ;
index_end = input( "Enter index_end, value not greater than n: ") ;
close = data(index_begin:index_end,7) ;
% detrend the close vector prior to applying the fft
slope = ( close(end) - close(1) ) / ( length(close) - 1 ) ;
v1 = (0:1:length(close)-1)' ;
detrended_close = close .- ( v1 .* slope ) ;
close_index_begin = close(1)
detrended_close_index_begin = detrended_close(1)
detrended_close_index_end = detrended_close(end)
% create zero padded vector for fft
L2 = 2^nextpow2( length(close) ) ; half_L2 = L2/2 ;
y2 = zeros( 1,L2 ) ; y2( 1:length(close) ) = detrended_close ;
% apply the fft
transform = fft( y2 ) ;
% permute the first half of the transform vector in "chunks" of 10
max_ii_value = floor( half_L2 / 10 ) ;
for ii = 1:max_ii_value
transform( (ii*10):(ii*10)+9 ) = permute_vector( transform( (ii*10):(ii*10)+9 ) ) ;
endfor
% take the inverse fft
ifft_vec = real( ifft( transform ) ) ;
% retrend the ifft_permuted_vec
retrended_ifft_vec = ifft_vec( 1:length(close) )' .+ ( v1 .* slope ) ;
% statistics
correl = corrcoef (close, retrended_ifft_vec)
spear = spearman (close, retrended_ifft_vec)
tau = kendall (close, retrended_ifft_vec)
plot( close,'b',retrended_ifft_vec,'r' ) ; legend( 'close','permute' ) ;
More information and charts in my blog post.
No comments:
Post a Comment