clear all
close all
clc
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Before beginning this assignment, you should movie this file into a
% directory (folder) where you don't mind saving additional Matlab files.
% I would recommend creating a main directory called 'PSY457-Matlab'
% and separate subdirectories for each assignment. This results in a
% structure like:
%
% > PSY456-Matlab
% > Assignment1
% > Assignment2
% > Assignment3
% > Matlab3.m
% > Assignment4
% > Assignment5
% > Assignment6
%
% In this assignment you will begin to conduct basic network analyses
% using existing datasets. To do this, you need to install the brain
% connectivity toolbox.
%
% Step 1. Visit https://www.sites.google.com/site/bctnet/
% Step 2. Click "Download the Toolbox" and unpack/unzip the
% downloaded file. It should generate a folder named "BCT."
% Step 3. Move the folder to a location of your choosing.
% Step 4. In an open Matlab session, use the "path bar" to navigate
% to the location of "BCT".
% Step 5. Type 'pwd' in the command line.
% Step 6. Copy the text (including the single quotes) and paste it on
% line 37, replacing only the purple text.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
bct_directory = '/Users/rbetzel/Downloads/BCT'; % location of BCT
addpath(genpath(bct_directory)); % ``installs'' the contents of BCT
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Section 1. In this assignment we will focus on calculating properties
% of connectivity matrices, performing statistical analysis, generating
% summary figures, and saving both the statistics as well as the figures.
%
% As in the previous Matlab assignment, we'll focus on the connectivity
% data in the matrix labeled "Coactivation_matrix.mat''. Start by loading
% in those data.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
load('Coactivation_matrix.mat');%loading the coactivation matrix
% YOUR ANSWER GOES HERE
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Section 2. The BCT includes functions for calculating different network
% measures. The function 'charpath' calculates characteristic path
% length and efficiency -- global measures that we think tell us
% something about how readily information can flow over a network's
% shorteset paths from node to node.
%
% If you use the 'help' or 'doc' functions to read about the function
% 'charpath', you'll notice that as input you need to pass 'charpath' a
% distance matrix. We have a connectivity matrix -- 'Coactivation_matrix'
% -- but not a distance matrix.
%
% We need to use another BCT function to generate a distance matrix. In
% network science, distance refers to the number of steps between pairs
% of nodes or the total weight of a path between nodes. To calculate the
% shortest distance between pairs of nodes, we need to use the
% 'distance_bin' function. Note that the '_bin' indicates that this
% function should *only* be used for binary networks whose edges have no
% weight.
%
% If 'CIJ' represents your connectivity matrix, then you can generate a
% binary version by typing:
%
% CIJ_bin = CIJ ~= 0;
%
% Essentially, this line of code tells Matlab to take all non-zero values
% in CIJ and set them equal to 1.
%
% Generate a binary version of 'Coactivation_matrix', use 'distance_bin'
% to generate a distance matrix, and then use 'charpath' to calculate the
% characteristic path length and efficiency. Assign them variable names
% 'L' and 'E'.
%
% Write your commented code in the space below.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% YOUR ANSWER GOES HERE
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Section 2. In class, we mentioned that an important part of statistical
% analysis of networks involves comparing measurements made on our real
% network (e.g. characteristic path length) the same measures made on
% surrogate or randomized networks that have some low-level properties in
% common with our real network (e.g. same number of nodes and edges).
%
% To do this, we need to generate those randomized networks. The most
% popular method for doing so is to use "rewiring" models that shuffle
% connections in our real network while preserving the total number of
% connections that each node makes.
%
% In the BCT, we can use the functions 'randmio_und' and 'randmio_dir' to
% generate rewired versions of our network. Each function needs two
% inputs (again, you can check this yourself by typing 'help randmio_und'
% or 'doc randmio_und'). The first input is your connectivity matrix. The
% second is the number of times that each edge gets rewired. In general,
% this second input should be a large number -- if it's too small, then
% the rewired network will be very similar to the original network. In
% practice, this second parameter should be > 20.
%
% In the lines below, use the function 'randmio_und' to generate a
% rewired version of 'Coactivation_matrix'. Assign the output of
% 'randmio_und' to a variable named 'Coactivation_matrix_rand'.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% YOUR ANSWER GOES HERE
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Section 3. Just like you did with the real network, calculate the
% characteristic path length and efficiency of the rewired network.
% Assign them variable names 'L_rand' and 'E_rand', respectively.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% YOUR ANSWER GOES HERE
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Section 4. Is path length greater in the random network or the real
% network? What about efficiency? Are we able to conclude from this
% comparison that our real network is ``different'' from a random
% network? Why or why not?
%
% Write your answer in the space below. Your answer should not include
% any Matlab code.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% YOUR ANSWER GOES HERE
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Section 5. To perform a rigorous statistical analysis, we need to
% compare our network against more than one rewired network -- in fact,
% we need to compare against an ``ensemble'' of many rewired networks,
% each slightly different from another. Then we can estimate the
% probability of observing a characteristic path length or efficiency as
% small (or large) simply by chance.
%
% To generate many rewired networks means that we have to run the
% 'randmio_und' function many times. Suppose we want to generate 100
% networks -- this would mean repeating the code you write in Section 2
% 100 times. This is inefficient and takes up lots of space in a Matlab
% script.
%
% In Matlab (and other computer languages) if you want to repeat a
% command many times, you use what's called a 'loop'. Below, I wrote a
% loop that repeats 10 times. Each time through the loop, the variable
% 'i', which starts at a value of 1, increases by 1 until it reaches a
% value of 10.
for i = 1:1:10 % create a loop where i increases from 1 to 10 in increments of 1
disp(i);
end
% Note that the loop begins with a 'for' statement and terminates with
% and 'end' statement. All loops must have this structure.
%
% My loop also includes a variable 'i' that goes from 1 to 10 in
% increments of 1. All loops must include a similar variable. It can be
% any variable you want, but the loop must have a variable that changes.
%
% Note, also, that in this loop I put a statement between the 'for' and
% 'end' lines. Here, I put a 'disp' command, which will display the value
% 'i' on the command line. In general, you could replace 'disp' with a
% different function or a list of commands that you want executed each
% time you pass through the loop.
%
% Loops are very important for programmers. So let's practice with a few:
%
% a) In the space below, write a loop in which the variable 'j' goes
% from 1 to 10 in increments of 1.
%
% b) In the space below, write a loop in which the variable 'i' goes
% from 2 to 50 in increments of 3.
%
% c) In the space below, write a loop in which the variable 'idx'
% goes from 1 to 10 in incremements of 1. Within the loop, create
% a new variable named 'x' that is equal to 'idx' squared.
%
% d) Modify the loop from part c so that 'x' is not overwritten each
% time through the loop. Instead, make 'x' an array that stores
% the value of 'i' squared at every iteration through the loop.
% You may want to look back to the first Matlab assignment and
% brush up on how to perform indexing in Matlab.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% YOUR ANSWERS GO HERE
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Section 6. Write a loop in which you generate 100 different rewired
% networks. Within the loop and for each network, generate its distance
% matrix, calculate its characteristic path length and efficiency, and
% store those variables in arrays named 'L_rand' and 'E_rand'. At the end
% of the loop, 'L_rand' and 'E_rand' should have dimensions of 100 x 1
% (or 1 x 100). You can check your workspace to confirm this.
%
% Note: the randomization code is stochastic, so if you are working in
% groups the exact values in 'L_rand' and 'E_rand' will likely be
% slightly different from one person to another.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% YOUR ANSWER GOES HERE
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Section 7. Once you have computed 'L_rand' and 'E_rand' you can start
% asking questions about probabilities. For instance, is it probable that
% a random network with the same number of nodes/edges will have a
% characteristic path length that is shorter than that of the real
% network?
%
% To answer this question, we compare the value of 'L' with the values in
% 'L_rand'. How many of the values in 'L_rand' are smaller than 'L'? This
% value (divided by the total number of rewired networks) is a
% probability, sometimes called a ``p-value''. If we wanted to calculate
% this number, we could do the following:
% a vector whose elements = 1 if the corresponding rewired network has
% shorter or equal path length than real network, and 0 if greater.
Lvec = L_rand
% calculate probability
Lpval = mean(Lvec);
% If this probability is small (usually
% our network is ``significantly different'' than rewired networks in terms
% of its characteristic path length. Note: statistical comparisons are
% actually more complicated than this, but this is the basic premise.
%
% In the space below, calculate the probability that the randomized
% networks are more efficient than the observed network. What can we
% conclude from these probabilities?
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% YOUR ANSWER GOES HERE (IT SHOULD INCLUDE BOTH MATLAB CODE AND WRITTEN
% TEXT).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Section 8. In the previous sections, you performed a statistical
% analysis in which we calcualted the characteristic path length and
% efficiency for a real network, compared those values against randomized
% networks, and calculated p-values.
%
% In this section, do the same, but for the mean clustering (a measure we
% talked about in the Watts & Strogatz paper).
%
% If you have a binary connectivity matrix, 'CIJ', it's mean clustering
% is calculated as:
%
% C = mean(clustering_coef_bu(CIJ));
%
% In the section below, write out the code for performing this analysis.
% You will need to choose a variable name for the mean clustering
% coefficient (in the example above I called it 'C'). You can choose
% whatever value you'd like.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% YOUR ANSWER GOES HERE (IT SHOULD INCLUDE BOTH MATLAB CODE AND WRITTEN
% TEXT).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% A final reminder: Your code should be able to run with no errors. If
% you want to test whether this is the case, you can do two things:
% 1) In the script window (the window into which you're adding text),
% there should be no red lines to the right of this document
% (there may be orange or grey).
% 2) If you click on the 'EDITOR' tab (top of screen) and the click
% the 'RUN' button, your script should run from start to finish
% without producing an error. If you script stops for any reason,
% it's like that there is an error in your code. Most errors will
% produce red text in the Command Window with some message about
% a possible cause.
%