時間序列
如何生成具有指定範圍並與實際價格相關的隨機價格序列?
我想生成一個模擬價格系列。我希望它在一定範圍內,並與原始價格系列具有明確的相關性。
如果我選擇石油,我想要盡可能多的時間序列,它們在相關性方面具有一些相似的特徵,但並不精確。我不希望數據在與歷史路徑完全不同的路徑上徘徊。它不一定是協整的,相關就足夠了。我也希望這些堅持原來的價格範圍。系列應該是隨機的,例如每個新的實現都應該採用不同的路徑。
除了我對上面 Samik R 的回答的評論之外,這裡是我的部落格文章的連結,其中我嘗試重新創建連結到所述答案的方法。
根據 Tal Fishman 的評論進行編輯以包含更多資訊
八度 .oct 功能碼
#include <octave/oct.h> #include <octave/dColVector.h> #include <octave/CNDArray.h> #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 }
八度腳本程式碼
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' ) ;
更多資訊和圖表在我的部落格文章中。