Opened 11 years ago

Last modified 4 years ago

#1893 needs_work enhancement

[with bundle, needs additional review] Added graphical plotting to discrete random variables.

Reported by: kohel Owned by: boothby
Priority: minor Milestone: sage-6.4
Component: graphics Keywords:
Cc: Merged in:
Authors: Reviewers:
Report Upstream: N/A Work issues:
Branch: Commit:
Dependencies: Stopgaps:

Description (last modified by mabshoff)

This example explains the functionality for the new function 'plot':

sage: import urllib2
sage: S = AlphabeticStrings()
sage: ISARC = 'http://iml.univ-mrs.fr/~kohel/tch/ISARC/'
sage: en = S.encoding(urllib2.urlopen(ISARC + 'blackcat.txt').read())
sage: F_en = en.frequency_distribution()
sage: F_en.plot()
sage: fr = S.encoding(urllib2.urlopen(ISARC + 'chapitre.1.txt').read())
sage: F_fr = fr.frequency_distribution()
sage: F_fr.plot()

Unfortunately, I did not include such an example in the function since it hard-codes an external web address, and would otherwise require a reasonably sized sample text for a proper demonstration.

Attachments (1)

probabiity.hg (5.1 KB) - added by kohel 11 years ago.

Download all attachments as: .zip

Change History (12)

Changed 11 years ago by kohel

comment:1 Changed 11 years ago by mabshoff

  • Milestone set to sage-2.10.1

comment:2 Changed 11 years ago by mhansen

  • Summary changed from Added graphical plotting to discrete random variables. to [with bundle, needs review] Added graphical plotting to discrete random variables.

comment:3 Changed 11 years ago by ncalexan

  • Summary changed from [with bundle, needs review] Added graphical plotting to discrete random variables. to [with bundle, with positive review] Added graphical plotting to discrete random variables.

This patch has been submitted (twice!) in the bundle accompanying #2153.

Whether it should be applied... I say yes, but I'm worried that it will fail on infinite domains.

comment:4 Changed 11 years ago by mabshoff

  • Description modified (diff)

comment:5 Changed 11 years ago by mabshoff

  • Summary changed from [with bundle, with positive review] Added graphical plotting to discrete random variables. to [with bundle, needs additional review] Added graphical plotting to discrete random variables.

Nick raised some issue which should be addressed.

Cheers,

Michael

comment:6 Changed 9 years ago by mpatel

  • Component changed from notebook to graphics
  • Report Upstream set to N/A

According to hg in probabiity.hg -p in 4.3.1.rc0,

  • sage/probability/random_variable.py

    summary:     Added plotting function for graphical display of a discrete random variable.
    
    diff --git a/sage/probability/random_variable.py b/sage/probability/random_variable.py
    a b from sage.structure.parent_base import P 
    2121from sage.misc.functional import sqrt, log
    2222from sage.rings.all import RealField, RationalField
    2323from sage.sets.set import Set
     24import sage.plot.plot as plot
    2425
    2526################################################################################
    2627################################################################################
    class DiscreteRandomVariable(RandomVaria 
    265266               "Correlation not defined if standard deviations are not both nonzero."
    266267       return cov/(sigX*sigY)
    267268
     269    def plot(f, rgbcolor=(1,0,0)):
     270        """
     271        Create a histogram from a frequency distribution or random variable.
     272        """
     273       X = f.domain()
     274       Text = []
     275        Bars = []
     276        fnc_max = 0.0
     277        N = len(X)
     278        for n in range(N):
     279            x = X[n]
     280            y = f(x)
     281            fnc_max = max(fnc_max,y)
     282            Text.append(plot.text(str(x),(n+0.5,-0.01),rgbcolor = (0,0,0)))
     283            Bars.append(plot.polygon([ [n,0], [n,y], [n+1,y], [n+1,0] ], rgbcolor = rgbcolor))
     284        Hist = sum(Text+Bars)
     285        Hist.range(xmin=0,xmax=N,ymin=0,ymax=fnc_max)
     286        return Hist
     287

Does this belong in graphics?

comment:7 Changed 6 years ago by jdemeyer

  • Milestone changed from sage-5.11 to sage-5.12

comment:8 Changed 5 years ago by vbraun_spam

  • Milestone changed from sage-6.1 to sage-6.2

comment:9 Changed 5 years ago by vbraun_spam

  • Milestone changed from sage-6.2 to sage-6.3

comment:10 Changed 5 years ago by vbraun_spam

  • Milestone changed from sage-6.3 to sage-6.4

comment:11 Changed 4 years ago by kcrisman

I have a feeling that #9671 will address this sufficiently if one could plug it into the class in question.

Note: See TracTickets for help on using tickets.