IMPOSECORRELATIONBYREORDERING-Re-pairsvectorsXandYtohaveaspecifiedrankcorrelation.[Xhat,Yhat]=vlt.stats.power.imposeCorrelationByReordering(X,Y,c)Re-ordersthepairingsoftheelementsinXandYtoproducetwonewvectors,XhatandYhat,thathaveaSpearman's(rank)correlationapproximatelyequaltothetargetvalue'c'.ThismethodisparticularlyusefulforgeneratingsurrogatedataforpoweranalysisbecauseitexactlypreservesthemarginaldistributionsofXandY.Thatis,thesetofvaluesinXhatisidenticaltothesetofvaluesinX,andthesameforY;onlythe(x_i,y_i)pairingsaremodified.Thealgorithmworksby:1.Generatingtwotemporary,normally-distributedvectors('A'and'B')thathaveaPearsoncorrelationof'c'.ThispairisknownasaGaussiancopulaandservesasatemplatefortherankstructure.2.SortingtheoriginaldataXandY.3.Re-orderingthesortedXandYdataaccordingtotherankorderofthetemporaryvectorsAandB,respectively.Inputs:X-Anumericalvector(Nx1or1xN).Y-AnumericalvectorwiththesamenumberofelementsasX.c-Ascalarvaluebetween-1and1representingthedesiredSpearman'srankcorrelationcoefficient.Outputs:Xhat-There-pairedXdata.ItcontainstheexactsamevaluesasX.Yhat-There-pairedYdata.ItcontainstheexactsamevaluesasY.Example:% 1. Create two independent, non-normal datasetsn_samples=500;X_orig=rand(n_samples,1).^3;% Skewed rightY_orig=gamrnd(2,1,n_samples,1);% Gamma distributed% 2. Check their initial correlation (should be near 0)fprintf('Initial Spearman corr: %.4f\n',corr(X_orig,Y_orig,'Type','Spearman'));% 3. Impose a new correlation of -0.8target_corr=-0.8;[X_new,Y_new]=vlt.stats.power.imposeCorrelationByReordering(X_orig,Y_orig,target_corr);% 4. Verify the resultfinal_corr_s=corr(X_new,Y_new,'Type','Spearman');fprintf('Target Spearman corr: %.4f\n',target_corr);fprintf('Final Spearman corr: %.4f\n',final_corr_s);% 5. Verify that the marginals are preserveddisp(['Values of X preserved: 'num2str(all(sort(X_orig)==sort(X_new)))]);disp(['Values of Y preserved: 'num2str(all(sort(Y_orig)==sort(Y_new)))]);% 6. Plot to visualizefigure;subplot(1,2,1);scatter(X_orig,Y_orig,'filled');title('Original (Independent)');xlabel('X');ylabel('Y');subplot(1,2,2);scatter(X_new,Y_new,'r','filled');title(['New (Correlated \rho_s \approx 'num2str(target_corr)')']);xlabel('Xhat');ylabel('Yhat');Seealso:CORR,SORT,RANDN,COPULARND