# Problems calculating transition matrix A

**URL:** <https://community.deeplearning.ai/t/problems-calculating-transition-matrix-a/93576>\
**Category:** NLP with Probabilistic Models\
**Tags:** week-module-2\
**Created:** [January 31, 2022, 3:47pm UTC](https://community.deeplearning.ai/t/problems-calculating-transition-matrix-a/93576 "2022-01-31T15:47:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![drew\_Frances](https://avatars.discourse-cdn.com/v4/letter/d/ea5d25/32.png) [@drew\_Frances](https://community.deeplearning.ai/u/drew_Frances)\
**Post date:** [January 31, 2022, 3:47pm UTC](https://community.deeplearning.ai/t/problems-calculating-transition-matrix-a/93576/1 "2022-01-31T15:47:03Z")

</div>

I​ am calculating the values for transmission matrix A. My values are orders of magnitude off.

I​ get:

A at row 0, col 0: 0.021739130 , A at row 3, col 1: 5021.7609

expected output:

A at ​row 0, col 0: 0.000007040, A ​at ​row3, col1: 0.1691

m​y equation is:

A[i,j] = (count + alpha) / (count\_prev\_tag + (alpha \* num\_tags))

All the code leading up to here, works. I​ am stuck. I not sure what I am doing wrong? It must be something obvious.

P.S - despite changes, my values don’t seem to change when I re-run the example

---

<div class="post-metadata">

**Author:** ![arvyzukai](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/arvyzukai/32/165165_2.png) [@arvyzukai](https://community.deeplearning.ai/u/arvyzukai)\
**Post date:** [February 18, 2022, 1:46pm UTC](https://community.deeplearning.ai/t/problems-calculating-transition-matrix-a/93576/2 "2022-02-18T13:46:02Z")

</div>

Hi.

There is nothing wrong with your given equation. You must be calculating wrong one or more of the variables inside the equation. When you get stuck I would suggest to make use some `print()` statements (or `breakpoint()` if you know what you are doing) to track if you get the right values for them - in other words I would suggest to calculate some tags’ transitions manually and check where is the mistake.

For example, if you would use these `print()` statements:

```auto
print(f'i:{i}, j:{j}, count:{count}, alpha:{alpha}',)
print(f', count_prev_tag: {count_prev_tag}, num_tags: {num_tags}')

```

you should get:

> i:0, j:0, count:0, alpha:0.001  
> count\_prev\_tag: 142, num\_tags: 46

to check manually you would calculate:

> (0 + 0.001) / (142 + 0.001\*46) = 0.000007040

Expected Output:  
A at row 0, col 0: 0.000007040

If you are curious which tags’ value is this you could:  
`trans_df.iloc[0:2, 0:2]`

and get:

```auto
| |# |$|
|---|---|---|
|# |7.039973e-06 |7.039973e-06|
|$ |1.356476e-07 |1.356476e-07|

```

So for tag ‘#’ to transition to tag ‘#’ there is 0.000007040 chance. (P.S. This is somewhat bad example because this probability is exactly the same to most tags from ‘#’ and there is probability of 0.992643 transitioning to ‘CD’ tag but I think you get the idea).

---

<div class="post-metadata">

**Author:** ![drew\_Frances](https://avatars.discourse-cdn.com/v4/letter/d/ea5d25/32.png) [@drew\_Frances](https://community.deeplearning.ai/u/drew_Frances)\
**Post date:** [February 21, 2022, 12:07am UTC](https://community.deeplearning.ai/t/problems-calculating-transition-matrix-a/93576/3 "2022-02-21T00:07:40Z")

</div>

Arvyzukai, thanks! I found the problem. I was using the wrong key to get count\_prev\_tag! The defaultdict meant I got a zero so I didn’t think twice!

Cheers,  
Andrew

---

<div class="post-metadata">

**Author:** ![Daniel\_Hecko](https://avatars.discourse-cdn.com/v4/letter/d/e0b2c6/32.png) [@Daniel\_Hecko](https://community.deeplearning.ai/u/Daniel_Hecko)\
**Post date:** [April 21, 2022, 8:27am UTC](https://community.deeplearning.ai/t/problems-calculating-transition-matrix-a/93576/4 "2022-04-21T08:27:18Z")

</div>

Thank you for both the question and an answer, it helped a lot 🙂
