YouTube2Text

Moran's I : Data Science Concepts — Transcript

by ritvikmath · 3,159 words · 513 segments · language en · Watch on YouTube

Full transcript

  1. 0:00[Music]
  2. 0:01hey everyone welcome back so in this
  3. 0:03video we'll be talking about moran's eye
  4. 0:05which is a measure of spatial
  5. 0:06correlation now let's just show you an
  6. 0:08example right away just to get an idea
  7. 0:10for what kind of questions can be
  8. 0:12answered using moran's eye
  9. 0:14so what i've shown here is the election
  10. 0:15map for the 2016 presidential election
  11. 0:17in the united states
  12. 0:19so you see that the red states voted for
  13. 0:20donald trump and the blue states voted
  14. 0:22for hillary clinton
  15. 0:24so as you can see there's a little bit
  16. 0:25of clustering here and there you can see
  17. 0:26some areas where
  18. 0:28red states cluster together in some
  19. 0:29areas where blue states cluster together
  20. 0:31so a natural quantity that you might
  21. 0:32want to measure is the magnitude of this
  22. 0:34clustering through the united states so
  23. 0:36that would help answer the question of
  24. 0:37do red states cluster together are red
  25. 0:39states more likely to be near other red
  26. 0:41states and blue states near other
  27. 0:43blue states or is it more random
  28. 0:44geographically
  29. 0:46so that's the type of question that can
  30. 0:47be answered with moran's eye
  31. 0:49now instead of going into that example
  32. 0:51which is going to be very complicated
  33. 0:52let's look at a smaller example and
  34. 0:54you'll be able to see how we can answer
  35. 0:56the initial question
  36. 0:57pretty easily so rather than just give
  37. 0:59you the formula for moran's eye you know
  38. 1:01on this channel i don't like to just
  39. 1:02throw formulas at you because i don't
  40. 1:04see that as an effective method of
  41. 1:05teaching
  42. 1:06instead we'll be looking at two examples
  43. 1:08and then we'll be building up the
  44. 1:09formula so that by the end you're going
  45. 1:11to have the formula
  46. 1:12but also the understanding about what
  47. 1:13each component means
  48. 1:15okay so let's pretend like we're looking
  49. 1:17at the same example except with a much
  50. 1:19smaller country so let's say that this
  51. 1:21is our country
  52. 1:22it has eight states so each state is
  53. 1:24each of these cells and a one let's say
  54. 1:26means that that state voted republican
  55. 1:28in the last election and let's say zero
  56. 1:30means that it voted democrat in the last
  57. 1:32election so we're going to look at two
  58. 1:33possible scenarios here and look at what
  59. 1:35the moran's eye
  60. 1:36is for each one so our first scenario is
  61. 1:39up here so the states are numbered with
  62. 1:41the green values so 1
  63. 1:422 3 4 5 6 7 8 and the same numbering
  64. 1:46down here for the second scenario now we
  65. 1:48see in the first scenario there is a
  66. 1:50very obvious clustering all of the
  67. 1:52states that voted republican or have
  68. 1:54ones in them are to the left and all the
  69. 1:56states that voted democrat or have a
  70. 1:58zero in them are to the right
  71. 2:00so we would expect moran's eye to be
  72. 2:02rather high just a quick note morant's
  73. 2:04eye
  74. 2:04is bounded between negative one and one
  75. 2:06so that the interpretation doesn't
  76. 2:07change
  77. 2:08based on whether your values are
  78. 2:09measured in feet or meters or
  79. 2:11whatever other units you might have so
  80. 2:13we have this case where we'd expect
  81. 2:15moran's eye to be rather high towards
  82. 2:17the one side because there is a strong
  83. 2:19positive spatial correlation and what
  84. 2:21that means in layman's terms is that
  85. 2:23there's a
  86. 2:23strong indication that states with
  87. 2:26similar voting patterns cluster together
  88. 2:28now let's look at the bottom example
  89. 2:30the bottom example is actually the exact
  90. 2:32opposite scenario we see that if we have
  91. 2:34a republican state or one that has a one
  92. 2:36in it
  93. 2:36then it's going to be bordered by
  94. 2:38democrat states that have zero in them
  95. 2:40and vice versa if we have a democrat
  96. 2:42state that has zero in it it's going to
  97. 2:44be bordered by republican states
  98. 2:46so this is a prime example of negative
  99. 2:48spatial correlation and we don't see
  100. 2:50this much in real life and it doesn't
  101. 2:51get talked about too much but
  102. 2:53i do want to give it to you so you can
  103. 2:55have the full picture
  104. 2:57negative spatial correlation means that
  105. 2:58the value at any given geography tends
  106. 3:01to be very
  107. 3:01far from the value at neighboring
  108. 3:03geographies so it's even difficult for
  109. 3:05me to come up with a natural example of
  110. 3:07this because we typically think of
  111. 3:09either having
  112. 3:10a positive spatial correlation where
  113. 3:12similar values cluster together
  114. 3:14or having no spatial correlation where
  115. 3:16there's no real pattern
  116. 3:18in the geography you're looking at but
  117. 3:19thinking about having a negative spatial
  118. 3:21correlation is a little bit more
  119. 3:23difficult but that doesn't mean that it
  120. 3:24doesn't exist it just means that we
  121. 3:25think about a little bit less but this
  122. 3:27is a graphical example of a negative
  123. 3:29spatial correlation
  124. 3:30where the value at any given geography
  125. 3:32is the opposite or very
  126. 3:34far from the value at neighboring
  127. 3:36geographies now just to reiterate again
  128. 3:38we have n states here so our n is equal
  129. 3:40to eight and that's just the number of
  130. 3:42units that you have if you're looking at
  131. 3:43the map of the united states then your n
  132. 3:45would be equal to 50 because we have 50
  133. 3:47states so the last component you'll need
  134. 3:49before we start the calculations of
  135. 3:50moran's eye is to define a weight matrix
  136. 3:53and this ends up being actually a very
  137. 3:54important part of what your moran's i
  138. 3:56values are going to be
  139. 3:58this weight matrix where each entry is
  140. 4:00given by w i
  141. 4:01j where i goes from one to n and j goes
  142. 4:04from one to n
  143. 4:05basically tells the story of what is the
  144. 4:07weight or what is the connectedness of
  145. 4:09geography i to geography j
  146. 4:11for example if i'm looking at geography
  147. 4:12one and two that would be these first
  148. 4:14two cells here
  149. 4:15then i'm going to give that a weight of
  150. 4:17one because
  151. 4:19those two states or those two
  152. 4:20geographies border each other
  153. 4:22however if i'm looking at geography one
  154. 4:25and three
  155. 4:26i'm going to give that a weight of zero
  156. 4:27because those geographies don't border
  157. 4:29each other
  158. 4:29another example would be looking at
  159. 4:31geography one and five those two do
  160. 4:33border each other spatially so i'm going
  161. 4:35to give that a weight of one
  162. 4:37so this weight matrix completely tells a
  163. 4:39story about what is the connection or
  164. 4:41strength of connection between one
  165. 4:42geography and any other geography that's
  166. 4:45in your situation so the weight matrix
  167. 4:47i've chosen for this example is that
  168. 4:48very simple one where if you see a state
  169. 4:50having a border
  170. 4:52not a diagonal border but either a
  171. 4:53horizontal or vertical border with
  172. 4:55another state we're going to give that
  173. 4:56weight
  174. 4:571 and if they don't have such a border
  175. 4:59i'm going to give that a weight of 0. so
  176. 5:01that's the story that's being told
  177. 5:02mathematically
  178. 5:03over here now that's not the only way
  179. 5:05you can define a weight matrix for
  180. 5:07example you can also do something like a
  181. 5:09k
  182. 5:09nearest neighbor weight matrix where
  183. 5:11you're going to give the
  184. 5:12five nearest neighbors or three nearest
  185. 5:14neighbors of a geography a weight of one
  186. 5:17or you might have a decaying weight
  187. 5:18matrix where the nearest neighbors get a
  188. 5:21weight of something near one
  189. 5:22but as you get further away the weight
  190. 5:24is not zero but it is going to zero so
  191. 5:26for example the weight between one and
  192. 5:28four
  193. 5:28would be low in that case so there's a
  194. 5:30lot of ways you can define this weight
  195. 5:32matrix and that's going to impact the
  196. 5:34final calculation of moran's eye so the
  197. 5:36way you define this weight matrix is an
  198. 5:37important factor
  199. 5:38in the final values okay but anyway now
  200. 5:41that we have our weight matrix defined
  201. 5:43and by the way this big w is just going
  202. 5:44to be the sum
  203. 5:45of all of the values in my weight matrix
  204. 5:47and that's going to be equal to 20.
  205. 5:49quick note why is it going to be equal
  206. 5:50to 20
  207. 5:51because if we look at the situation and
  208. 5:53you count the number of pairs of
  209. 5:54bordering states you find that there are
  210. 5:5610 pairs
  211. 5:57and each pair is symmetric so for
  212. 5:59example the weight matrix contains a 1
  213. 6:02for state 1 connected to 2 and if you
  214. 6:04look at the transpose element
  215. 6:06or the matching element on the other
  216. 6:07side of the weight matrix that's also a
  217. 6:091. so
  218. 6:09there's 10 pairs times 2 and we get
  219. 6:1320 as the total sum of all the elements
  220. 6:15in the weight matrix and we're going to
  221. 6:16call that variable
  222. 6:17big w so now that we have the whole
  223. 6:20situation set up we're ready to go ahead
  224. 6:21and just do the calculation and explain
  225. 6:23why this calculation
  226. 6:24intuitively does capture the spatial
  227. 6:27correlation
  228. 6:28so i've broken it down for you in three
  229. 6:29easy steps the first step
  230. 6:31is to compute x bar x bar is simply just
  231. 6:34the mean
  232. 6:35across the entire geography of the
  233. 6:37variable you care about
  234. 6:39here the variable we care about is easy
  235. 6:41it's binary either one if the state
  236. 6:42voted republican and zero of the state
  237. 6:44voted democrat
  238. 6:45so we see that the mean would be
  239. 6:47one-half because if i add up all these
  240. 6:49binary variables in either case i'm
  241. 6:51going to get four
  242. 6:52and there's eight states total so four
  243. 6:54over eight is one-half so we have
  244. 6:55x-bar is equal to one-half so the next
  245. 6:57step step two is to compute the total
  246. 6:59variation
  247. 7:00in our eight-state country based on how
  248. 7:03far
  249. 7:03each value is away from that mean we
  250. 7:05just computed so that is given
  251. 7:07mathematically down here
  252. 7:08so what we're doing here is going
  253. 7:10through each state from one to n
  254. 7:12and we're simply doing the x i of that
  255. 7:14state again that's either one or zero
  256. 7:16minus the mean we just calculated and
  257. 7:19squaring it
  258. 7:20so this is a sum of square differences
  259. 7:22between each state's
  260. 7:24democrat or republican value and the
  261. 7:26mean value
  262. 7:27now notice that this doesn't take
  263. 7:28anything into account just yet about the
  264. 7:30weights between these states or the fact
  265. 7:32that one state borders another
  266. 7:33this is simply just the total variation
  267. 7:36a measure of how much variation there is
  268. 7:38total in the entire geography so we see
  269. 7:40that if we do this calculation we get
  270. 7:41two so the last step and the step that's
  271. 7:43most
  272. 7:44interesting and makes this truly a
  273. 7:45spatial correlation rather than just a
  274. 7:47regular correlation
  275. 7:49is that we take that weight matrix into
  276. 7:51account so what we're doing here is
  277. 7:52summing over every i
  278. 7:53j pair that's what this two sums are
  279. 7:55doing and for each ij pair we're
  280. 7:57computing the following value we're
  281. 7:59first going to compute this value we're
  282. 8:00going to compute
  283. 8:01x i minus x bar times x j minus x bar
  284. 8:04so you might have seen something similar
  285. 8:06when you're doing a correlation or a
  286. 8:08covariance calculation
  287. 8:09and this is basically measuring whether
  288. 8:11state i and state j's difference from
  289. 8:12the mean
  290. 8:13are in similar or opposite directions
  291. 8:15for example let's say that x
  292. 8:17i was democrat and x j was republican in
  293. 8:19that case we would have
  294. 8:20a one here and a zero here this quantity
  295. 8:23would be positive one half and this
  296. 8:24quantity would be negative one-half so
  297. 8:26that we would have a negative one-fourth
  298. 8:28total
  299. 8:28and the fact that that's negative tells
  300. 8:30us that these two states are on opposite
  301. 8:32sides of the mean so they're not similar
  302. 8:34on the other hand if we had two
  303. 8:35republican states like one and one
  304. 8:37then we would have one minus of half and
  305. 8:39one minus a half and so we would have
  306. 8:41one fourth
  307. 8:42and the fact that one fourth is positive
  308. 8:43would tell us that these two states
  309. 8:45do lie on the same side of the mean so
  310. 8:47this part is basically just computing
  311. 8:49whether or not these two states we care
  312. 8:51about
  313. 8:52and again we're looping over every pair
  314. 8:53of states here and each time we're
  315. 8:55looking at two states we're seeing if
  316. 8:56these two states do match up
  317. 8:58in terms of where they are relative to
  318. 9:00the mean now here's the most important
  319. 9:02part
  320. 9:03we take that measure whether it's
  321. 9:05positive or negative or near zero
  322. 9:07and we weight it by the corresponding
  323. 9:09entry w i j
  324. 9:10in the weight matrix now why is this
  325. 9:13important because it adds that very
  326. 9:14necessary
  327. 9:15spatial layer to the story it basically
  328. 9:17says that okay
  329. 9:18you might have that two states aren't
  330. 9:20similar for example one is republican
  331. 9:22and one's democrat
  332. 9:23but i really only care about that as
  333. 9:25much as their weight
  334. 9:26for example let's say those two states
  335. 9:28don't border let's say we're looking at
  336. 9:29state one and state four
  337. 9:31so that we would have a negative value
  338. 9:33when we do this part of the computation
  339. 9:35the question is should i care about that
  340. 9:37and their weight is zero because they
  341. 9:39don't border so according to my weight
  342. 9:41matrix i created here their weight is
  343. 9:42zero
  344. 9:43so i actually don't even care about that
  345. 9:45term at all now compare that with
  346. 9:47whether i'm looking at state one and
  347. 9:48state two
  348. 9:49so that i get positive 1 4 in this part
  349. 9:52here
  350. 9:52the question is should i care about that
  351. 9:54their weight is 1
  352. 9:56because they do border so that does get
  353. 9:58included in this term so you see why
  354. 10:00this term
  355. 10:00even though it looks a little bit
  356. 10:01complex does exactly the job that we
  357. 10:03need because the second two terms
  358. 10:05compute the strength of similarity for
  359. 10:07any pair of states that we see in our
  360. 10:09geography
  361. 10:09and the first term weights that strength
  362. 10:12by how
  363. 10:13close together these states are
  364. 10:15literally like geographically
  365. 10:17so if these two states are really close
  366. 10:18geographically their weight is going to
  367. 10:20be really high
  368. 10:21and we put a big emphasis on this term
  369. 10:23here
  370. 10:24however if those two states are very far
  371. 10:25apart we would expect their weights to
  372. 10:27be very low
  373. 10:28and so we don't put a big emphasis here
  374. 10:31so this here is computing the variation
  375. 10:33between states but weighted by how close
  376. 10:36those states are
  377. 10:38geographically to each other and if we
  378. 10:39do that literal calculation
  379. 10:41for the top example here we see that it
  380. 10:43is eight times one-fourth where does
  381. 10:45that come from again there are 10
  382. 10:47pairs of neighboring states here for
  383. 10:48example here's 1 2
  384. 10:503 4 5 6 7 8
  385. 10:539 10. so there are 10 pairs of
  386. 10:56neighboring states here
  387. 10:57and each one is symmetric so really
  388. 10:59there's 20 pairs of neighboring states
  389. 11:01if you look at it from the side of
  390. 11:02one state versus a different state now
  391. 11:04eight of these pairs are connecting ones
  392. 11:06to ones
  393. 11:07that means that we have eight times
  394. 11:09one-fourth
  395. 11:10because the one-fourth is what's going
  396. 11:11to come out if you do this x i minus x
  397. 11:13bar x j minus x bar
  398. 11:15and x i and x j are both one similarly
  399. 11:19eight of these pairs are connecting
  400. 11:20zeros to zeros and we get the exact same
  401. 11:22calculation here
  402. 11:24four of these pairs are connecting ones
  403. 11:26to zeros where are those four
  404. 11:28we have one two and they're both
  405. 11:31symmetric so we have three and four
  406. 11:33so we have four where the measure of
  407. 11:35covariance is going to be negative one
  408. 11:37fourth
  409. 11:38and so if we do this whole calculation
  410. 11:40we get three for this step three for the
  411. 11:42top one
  412. 11:43and we can do a very similar calculation
  413. 11:45for the bottom one it's actually much
  414. 11:46easier
  415. 11:47because every pair you can look at is
  416. 11:49connecting a one to a zero
  417. 11:50so we simply get twenty negative one
  418. 11:53fourths so we get negative five on the
  419. 11:54bottom few so we're actually all done
  420. 11:56doing our calculations hopefully that
  421. 11:58you understood the calculations but more
  422. 12:00importantly understood the intuition
  423. 12:02behind why we are computing each of the
  424. 12:04values in these steps
  425. 12:06the last thing we have to do is just put
  426. 12:07them all together and this is the final
  427. 12:09formula for moran's i
  428. 12:11so moran's i is given by big n over big
  429. 12:14w
  430. 12:14big n is again the number of states we
  431. 12:16have eight and big w
  432. 12:18is the sum of all the entries in the
  433. 12:20weight matrix 20.
  434. 12:22these are simply just normalization
  435. 12:23factors just so that the final moran's i
  436. 12:26is bound in between negative one and one
  437. 12:27and then
  438. 12:28we do step three divided by step two now
  439. 12:30let's quickly think about why that makes
  440. 12:32sense
  441. 12:32remember step two was the total
  442. 12:34variation not taking into account weight
  443. 12:36or geographical similarities between
  444. 12:38states at all that's just how much
  445. 12:40variation is there in my country overall
  446. 12:42so that becomes our denominator step
  447. 12:44three
  448. 12:45was how much variation is there but now
  449. 12:47i do care about whether that is
  450. 12:48variation between states that are close
  451. 12:50together or very far apart
  452. 12:52so we see that it's very natural to do
  453. 12:54step three's value divided by step two's
  454. 12:56value because now we kind of get a
  455. 12:57measure of
  456. 12:58the fraction of variation that is
  457. 13:00attributed to states that are close
  458. 13:02together so we do step three over step
  459. 13:04two
  460. 13:04and we do these calculations and we get
  461. 13:06that it's three-fifths
  462. 13:08for the top example and we get negative
  463. 13:11one for the bottom example so this does
  464. 13:13totally match up to our understanding
  465. 13:15this negative one for the bottom example
  466. 13:16because this is a prime example
  467. 13:19of negative spatial correlation and
  468. 13:21three-fifths for the top example is very
  469. 13:23close to one
  470. 13:24in fact the only reason it's not exactly
  471. 13:26one is because i have such a small
  472. 13:28example if you took this example of
  473. 13:30eight states and made it huge
  474. 13:31then you would get this approaching
  475. 13:33pretty much one so the last couple
  476. 13:34things i'll say is the real
  477. 13:36applications of moran's eye again this
  478. 13:38negative spatial correlation case is not
  479. 13:40talked about as much because it's harder
  480. 13:42to find real world examples
  481. 13:43so a lot of times you'll be checking
  482. 13:45whether there's positive spatial
  483. 13:46correlation versus whether there's
  484. 13:48zero spatial correlation zero spatial
  485. 13:50correlation although i didn't do an
  486. 13:52example
  487. 13:52would be where if these ones and zeros
  488. 13:54are pretty much randomly interspersed
  489. 13:56there is no connection to geography
  490. 13:58and if that were the case then you would
  491. 13:59get spatial correlation around
  492. 14:01zero so a lot of times researchers are
  493. 14:03looking at a map of something whether it
  494. 14:05is the election or whether it's heights
  495. 14:07or whether it's population densities
  496. 14:09in given city areas and they're really
  497. 14:11asking the question about
  498. 14:12is there a statistically significant
  499. 14:14positive spatial correlation here
  500. 14:16or is it really just zero and if you
  501. 14:18find that there is a positive spatial
  502. 14:20correlation there then you can start
  503. 14:22saying that okay
  504. 14:23we do see clustering of population
  505. 14:25densities here
  506. 14:26by geography or election by geography
  507. 14:29and so on
  508. 14:30so hopefully this helped you understand
  509. 14:31moran's eye this very widely used
  510. 14:33measure of spatial correlation please
  511. 14:34like and subscribe for more videos just
  512. 14:36like this
  513. 14:37i'll see you next time

About this transcript

This page contains the full transcript of Moran's I : Data Science Concepts by ritvikmath, generated from the public captions YouTube serves with the video. The transcript has 3,159 words across 513 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.

What you can do with it

Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.

Free YouTube transcript tool

YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.