  In this paper, we obtain affine analogues of Gindikin-Karpelevich formula and Casselman-Shalika formula as sums over Kashiwara-Lusztig's canonical bases. Suggested by these formulas, we define natural $q$-deformation of arithmetical functions such as (multi-)partition function and Ramanujan $\tau$-function, and prove various identities among them. In some examples, we recover classical identities by taking limits. We also consider $q$-deformation of Kostant's function and study certain $q$-polynomials whose special values are weight multiplicities. 
Quantum affine algebras, canonical bases and $q$-deformation of   arithmetical functions